Commit b2222766 authored by nanahira's avatar nanahira

add internal service

parent 76173704
...@@ -24,3 +24,14 @@ export function Inject(addUsing?: boolean): PropertyDecorator; ...@@ -24,3 +24,14 @@ export function Inject(addUsing?: boolean): PropertyDecorator;
export function Inject(...args: [(string | boolean)?, boolean?]) { export function Inject(...args: [(string | boolean)?, boolean?]) {
return pluginDecorators.Inject(...args); return pluginDecorators.Inject(...args);
} }
export function Internal(): MethodDecorator & PropertyDecorator {
return (obj, key, des?) => {
const cls = obj.constructor as Context.MixinOptions;
const field = des ? 'methods' : 'properties';
if (!cls[field]) {
cls[field] = [];
}
cls[field].push(key);
};
}
...@@ -85,8 +85,9 @@ export namespace Registrar { ...@@ -85,8 +85,9 @@ export namespace Registrar {
ctx: Ctx; ctx: Ctx;
}; };
export interface ProvideOptions extends Context.ServiceOptions { export interface ProvideOptions {
immediate?: boolean; immediate?: boolean;
internal?: boolean;
} }
export interface ProvideDefinition<C extends Context> extends ProvideOptions { export interface ProvideDefinition<C extends Context> extends ProvideOptions {
serviceName: string; serviceName: string;
...@@ -517,7 +518,12 @@ export class Registrar<Ctx extends Context> { ...@@ -517,7 +518,12 @@ export class Registrar<Ctx extends Context> {
name: string, name: string,
options?: Registrar.ProvideOptions, options?: Registrar.ProvideOptions,
): ClassDecorator => { ): ClassDecorator => {
Context.service(name, options); if (options?.internal) {
return (cls) => {
Context.service(name, cls);
};
}
Context.service(name);
return this.metadata.append('CordisPluginProvide', { return this.metadata.append('CordisPluginProvide', {
...options, ...options,
serviceName: name, serviceName: name,
......
import { Inject, Internal, Provide } from '../src/decorators';
import { DefinePlugin, StarterPlugin } from './utility/decorators';
import { LifecycleEvents } from '../src/plugin-def';
import { Context } from 'cordis';
declare module 'cordis' {
// eslint-disable-next-line @typescript-eslint/no-namespace
interface Context {
foo: string;
bar(): number;
baz: MyInternalService;
}
}
@Provide('baz', { internal: true })
@DefinePlugin()
class MyInternalService extends StarterPlugin() {
@Internal()
foo: string;
@Internal()
bar() {
return 5;
}
}
describe('Apply and Connect in koishi-thirdeye', () => {
let app: Context;
beforeEach(() => {
app = new Context();
});
it('should load internal service', () => {
app.foo = 'msg';
expect(app.foo).toBe('msg');
expect(app.bar()).toBe(5);
expect(app.baz.foo).toBe('msg');
expect(app.baz.bar()).toBe(5);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment