mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +08:00
upgrade to cordis v3 (#733)
Co-authored-by: panda <panda_dtdyy@outlook.com>
This commit is contained in:
1 parent
498cd57016
commit
a56b1650be
33 files changed
+301
-262
No files matched your search
@@ -1,47 +1,38 @@
|
||||
import * as cordis from 'cordis';
|
||||
import type { EventMap } from './api';
|
||||
|
||||
export interface Events<C extends Context = Context> extends cordis.Events<C>, EventMap {
|
||||
}
|
||||
export interface Events<C extends Context = Context> extends cordis.Events<C>, EventMap { }
|
||||
|
||||
export type { Disposable, ScopeStatus, Plugin } from 'cordis';
|
||||
|
||||
export interface Context {
|
||||
[Context.events]: Events<Context>;
|
||||
setTimeout: typeof setTimeout;
|
||||
setInterval: typeof setInterval;
|
||||
api: ApiMixin;
|
||||
[Context.events]: Events<this>;
|
||||
broadcast: Context['emit'];
|
||||
}
|
||||
|
||||
export class Context extends cordis.Context {
|
||||
static readonly session = Symbol('session');
|
||||
/** @deprecated use `ctx.root` instead */
|
||||
get app() {
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/** @deprecated use `root.config` instead */
|
||||
get options() {
|
||||
return this.root.config;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Context {
|
||||
export interface Config extends cordis.Context.Config { }
|
||||
export type Associate<P extends string, C extends Context = Context> = cordis.Context.Associate<P, C>;
|
||||
}
|
||||
|
||||
export type MainScope = cordis.MainScope<Context>;
|
||||
export type EffectScope = cordis.EffectScope<Context>;
|
||||
export type ForkScope = cordis.ForkScope<Context>;
|
||||
export type Plugin = cordis.Plugin<Context>;
|
||||
export const Service = cordis.Service<Context>;
|
||||
export namespace Plugin {
|
||||
export type Function<T = any> = cordis.Plugin.Function<T, Context>;
|
||||
export type Constructor<T = any> = cordis.Plugin.Constructor<T, Context>;
|
||||
export type Object<S = any, T = any> = cordis.Plugin.Object<S, T, Context>;
|
||||
}
|
||||
|
||||
const T = <F extends (...args: any[]) => any>(origFunc: F, disposeFunc?) =>
|
||||
function method(this: cordis.Service, ...args: Parameters<F>) {
|
||||
const res = origFunc(...args);
|
||||
this.caller?.on('dispose', () => (disposeFunc ? disposeFunc(res) : res()));
|
||||
};
|
||||
export class ApiMixin extends Service {
|
||||
static readonly methods = ['setInterval', 'setTimeout'];
|
||||
setInterval = T(setInterval, clearInterval);
|
||||
setTimeout = T(setTimeout, clearTimeout);
|
||||
constructor(ctx) {
|
||||
super(ctx, 'api', true);
|
||||
export abstract class Service<T = any, C extends Context = Context> extends cordis.Service<T, C> {
|
||||
[cordis.Service.setup]() {
|
||||
this.ctx = new Context() as C;
|
||||
}
|
||||
}
|
||||
Context.service('api', ApiMixin);
|
||||
export const ctx = new Context();
|
||||
Reference in new issue
Block a user