import { Context, Service } from 'cordis';
declare module 'cordis' {
interface Context {
foo: FooService;
}
}
const app = new Context();
class FooService extends Service {
registry = { };
constructor(ctx) {
super(ctx, 'foo', true);
}
register(name, value) {
if (this.registry[name] && this.registry[name] !== value) throw new Error();
this.registry[name] = value;
}
}
app.plugin(FooService);
app.plugin({
inject: ['foo'],
apply(ctx) {
function bar() { }
ctx.foo.register('example', bar); // ok
ctx.foo.register('example', bar); // throws
},
});