mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +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
@@ -6,8 +6,7 @@ import { Loader } from './loader';
|
||||
import { EventMap } from './service/bus';
|
||||
import type { CheckService } from './service/check';
|
||||
|
||||
export interface Events<C extends Context = Context> extends cordis.Events<C>, EventMap {
|
||||
}
|
||||
export interface Events<C extends Context = Context> extends cordis.Events<C>, EventMap { }
|
||||
|
||||
function addScript<K>(name: string, description: string, validate: Schema<K>, run: (args: K, report: any) => boolean | Promise<boolean>) {
|
||||
if (global.Hydro.script[name]) throw new Error(`duplicate script ${name} registered.`);
|
||||
@@ -21,6 +20,12 @@ function provideModule<T extends keyof ModuleInterfaces>(type: T, id: string, mo
|
||||
return () => delete global.Hydro.module[type][id];
|
||||
}
|
||||
|
||||
export type EffectScope = cordis.EffectScope<Context>;
|
||||
export type ForkScope = cordis.ForkScope<Context>;
|
||||
export type MainScope = cordis.MainScope<Context>;
|
||||
|
||||
export type { Disposable, ScopeStatus, Plugin } from 'cordis';
|
||||
|
||||
export interface Context {
|
||||
[Context.events]: Events<Context>;
|
||||
loader: Loader;
|
||||
@@ -28,55 +33,62 @@ export interface Context {
|
||||
Connection: typeof import('./service/server').Connection;
|
||||
withHandlerClass: import('./service/server').RouteService['withHandlerClass'];
|
||||
check: CheckService;
|
||||
setTimeout: typeof setTimeout;
|
||||
setInterval: typeof setInterval;
|
||||
setImmediate: typeof setImmediate;
|
||||
addScript: typeof addScript;
|
||||
provideModule: typeof provideModule;
|
||||
/** @deprecated use injectUI instead */
|
||||
inject: typeof inject;
|
||||
injectUI: typeof inject;
|
||||
api: ApiMixin;
|
||||
broadcast: Context['emit'];
|
||||
geoip?: GeoIP;
|
||||
}
|
||||
|
||||
export class Context extends cordis.Context {
|
||||
domain?: DomainDoc;
|
||||
}
|
||||
|
||||
export namespace Context {
|
||||
export interface Config extends cordis.Context.Config { }
|
||||
}
|
||||
|
||||
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>;
|
||||
export abstract class Service<T = any, C extends Context = Context> extends cordis.Service<T, C> {
|
||||
[cordis.Service.setup]() {
|
||||
this.ctx = new Context() as C;
|
||||
}
|
||||
}
|
||||
|
||||
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()));
|
||||
this[Context.current]?.on('dispose', () => (disposeFunc ? disposeFunc(res) : res()));
|
||||
};
|
||||
|
||||
export class ApiMixin extends Service {
|
||||
static readonly methods = ['addScript', 'setInterval', 'setTimeout', 'setImmediate', 'provideModule', 'inject', 'injectUI', 'broadcast'];
|
||||
addScript = T(addScript);
|
||||
setInterval = T(setInterval, clearInterval);
|
||||
setTimeout = T(setTimeout, clearTimeout);
|
||||
setImmediate = T(setImmediate, clearImmediate);
|
||||
provideModule = T(provideModule);
|
||||
inject = T(inject);
|
||||
injectUI = T(inject);
|
||||
broadcast = (event: keyof EventMap, ...payload) => this.ctx.emit('bus/broadcast', event, payload);
|
||||
constructor(ctx) {
|
||||
super(ctx, 'api', true);
|
||||
super(ctx, '$api', true);
|
||||
ctx.mixin('$api', ['addScript', 'setImmediate', 'provideModule', 'injectUI', 'broadcast']);
|
||||
}
|
||||
}
|
||||
Context.service('api', ApiMixin);
|
||||
|
||||
export class Context extends cordis.Context {
|
||||
domain?: DomainDoc;
|
||||
|
||||
constructor(config: {} = {}) {
|
||||
super(config);
|
||||
this.plugin(ApiMixin);
|
||||
}
|
||||
|
||||
/** @deprecated use `ctx.root` instead */
|
||||
get app() {
|
||||
return this.root;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Context {
|
||||
export type Associate<P extends string, C extends Context = Context> = cordis.Context.Associate<P, C>;
|
||||
}
|
||||
|
||||
const old = cordis.Registry.prototype.inject;
|
||||
// cordis.Registry.prototype.using = old;
|
||||
cordis.Registry.prototype.inject = function wrapper(...args) {
|
||||
if (typeof args[0] === 'string') {
|
||||
console.warn('old functionality of ctx.inject is deprecated. please use ctx.injectUI instead.');
|
||||
return T(inject).call(this, ...args as any) as any;
|
||||
}
|
||||
return old.call(this, ...args);
|
||||
};
|
||||
Reference in new issue
Block a user