Comparing version 3.1.3 to 3.2.0
@@ -7,2 +7,6 @@ import { Awaitable, Promisify, Dict } from 'cosmokit'; | ||
export function isApplicable(object: Plugin): boolean; | ||
export interface Inject { | ||
readonly required?: string[]; | ||
readonly optional?: string[]; | ||
} | ||
export type Plugin<C extends Context = Context, T = any> = Plugin.Function<C, T> | Plugin.Constructor<C, T> | Plugin.Object<C, T>; | ||
@@ -15,5 +19,5 @@ export namespace Plugin { | ||
Config?: (config?: any) => any; | ||
inject?: string[] | Partial<Inject>; | ||
inject?: string[] | Inject; | ||
/** @deprecated use `inject` instead */ | ||
using?: string[] | Partial<Inject>; | ||
using?: string[] | Inject; | ||
} | ||
@@ -31,3 +35,5 @@ interface Function<C extends Context = Context, T = any> extends Base { | ||
export interface Context { | ||
using(deps: string[] | Partial<Inject>, callback: Plugin.Function<Context.Parameterized<this, void>, void>): ForkScope<Context.Parameterized<this, void>>; | ||
/** @deprecated use `ctx.inject()` instead */ | ||
using(deps: string[] | Inject, callback: Plugin.Function<Context.Parameterized<this, void>, void>): ForkScope<Context.Parameterized<this, void>>; | ||
inject(deps: string[] | Inject, callback: Plugin.Function<Context.Parameterized<this, void>, void>): ForkScope<Context.Parameterized<this, void>>; | ||
plugin<T, S = T>(plugin: Plugin.Function<Context.Parameterized<this, T>, T> & { | ||
@@ -71,3 +77,4 @@ schema?: true; | ||
forEach(callback: (value: MainScope<C>, key: Plugin<C>, map: Map<Plugin<C>, MainScope<C>>) => void): void; | ||
using(inject: string[] | Partial<Inject>, callback: Plugin.Function<C, void>): ForkScope<C> | undefined; | ||
using(inject: string[] | Inject, callback: Plugin.Function<C, void>): ForkScope<C> | undefined; | ||
inject(inject: string[] | Inject, callback: Plugin.Function<C, void>): ForkScope<C> | undefined; | ||
plugin(plugin: Plugin<C>, config?: any): ForkScope<C> | undefined; | ||
@@ -84,6 +91,2 @@ dispose(plugin: Plugin<C>): boolean; | ||
} | ||
export interface Inject { | ||
readonly required: string[]; | ||
readonly optional: string[]; | ||
} | ||
export type Disposable = () => void; | ||
@@ -203,5 +206,5 @@ export interface AcceptOptions { | ||
unregister(hooks: [Context, any][], listener: any): true | undefined; | ||
on(name: keyof any, listener: (...args: any) => any, prepend?: boolean): any; | ||
once(name: keyof any, listener: (...args: any) => any, prepend?: boolean): any; | ||
off(name: keyof any, listener: (...args: any) => any): true | undefined; | ||
on(name: string, listener: (...args: any) => any, prepend?: boolean): any; | ||
once(name: string, listener: (...args: any) => any, prepend?: boolean): any; | ||
off(name: string, listener: (...args: any) => any): true | undefined; | ||
start(): Promise<void>; | ||
@@ -222,4 +225,4 @@ stop(): Promise<void>; | ||
'internal/update'(fork: ForkScope<Context.Parameterized<C>>, oldConfig: any): void; | ||
'internal/hook'(this: Lifecycle, name: string, listener: Function, prepend: boolean): () => boolean; | ||
'internal/event'(type: 'emit' | 'parallel' | 'serial' | 'bail', name: string, args: any[], thisArg: any): void; | ||
[K: `event/${string}`]: (ctx: C, listener: Function, prepend: boolean) => undefined | (() => boolean); | ||
} | ||
@@ -226,0 +229,0 @@ export namespace Context { |
{ | ||
"name": "cordis", | ||
"description": "AOP Framework for Modern JavaScript Applications", | ||
"version": "3.1.3", | ||
"version": "3.2.0", | ||
"sideEffects": false, | ||
@@ -24,4 +24,4 @@ "main": "lib/index.cjs", | ||
"scripts": { | ||
"compile:cjs": "esbuild src/index.ts --outfile=lib/index.cjs --bundle --sourcemap --sources-content=false --platform=node --external:cosmokit --target=es2019", | ||
"compile:esm": "esbuild src/index.ts --outfile=lib/index.mjs --bundle --sourcemap --sources-content=false --platform=neutral --external:cosmokit --target=esnext", | ||
"compile:cjs": "esbuild src/index.ts --outfile=lib/index.cjs --bundle --sourcemap --sources-content=false --platform=node --external:cosmokit --target=es2022", | ||
"compile:esm": "esbuild src/index.ts --outfile=lib/index.mjs --bundle --sourcemap --sources-content=false --platform=neutral --external:cosmokit --target=es2022", | ||
"build": "yarn compile:cjs && yarn compile:esm && yarn dtsc", | ||
@@ -28,0 +28,0 @@ "test": "mocha -r esbuild-register tests/*.spec.ts", |
@@ -382,3 +382,3 @@ # Cordis | ||
However, If a plugin completely depends on the service, we cannot just check the service in the plugin callback, because when the plugin is loaded, the service may not be available yet. To make sure that the plugin is loaded only when the service is available, we can use a special property called `using`: | ||
However, If a plugin completely depends on the service, we cannot just check the service in the plugin callback, because when the plugin is loaded, the service may not be available yet. To make sure that the plugin is loaded only when the service is available, we can use a special property called `inject`: | ||
@@ -406,3 +406,3 @@ ```ts | ||
`using` is a list of service dependencies. If a service is a dependency of a plugin, it means: | ||
`inject` is a list of service dependencies. If a service is a dependency of a plugin, it means: | ||
@@ -413,6 +413,6 @@ - the plugin will not be loaded until the service becomes truthy | ||
For plugins whose functions depend on a service, we also provide a syntactic sugar `ctx.using()`: | ||
For plugins whose functions depend on a service, we also provide a syntactic sugar `ctx.inject()`: | ||
```ts | ||
ctx.using(['database'], (ctx) => { | ||
ctx.inject(['database'], (ctx) => { | ||
ctx.database.get(table, id) | ||
@@ -423,3 +423,3 @@ }) | ||
ctx.plugin({ | ||
using: ['database'], | ||
inject: ['database'], | ||
apply: (ctx) => { | ||
@@ -679,5 +679,5 @@ ctx.database.get(table, id) | ||
#### ctx.using(names, callback) | ||
#### ctx.inject(deps, callback) | ||
- names: `string[]` service names | ||
- deps: `string[] | Inject` dependencies | ||
- callback: `Function` plugin function | ||
@@ -689,3 +689,3 @@ | ||
ctx.plugin({ | ||
using: names, | ||
inject: deps, | ||
plugin: callback, | ||
@@ -692,0 +692,0 @@ }) |
@@ -174,3 +174,3 @@ import { defineProperty, Dict, isNullable } from 'cosmokit' | ||
self.mixin('scope', ['config', 'runtime', 'collect', 'accept', 'decline']) | ||
self.mixin('registry', ['using', 'plugin', 'dispose']) | ||
self.mixin('registry', ['using', 'inject', 'plugin', 'dispose']) | ||
self.mixin('lifecycle', ['on', 'once', 'off', 'after', 'parallel', 'emit', 'serial', 'bail', 'start', 'stop']) | ||
@@ -186,4 +186,3 @@ self.provide('registry', new Registry(self, config!), true) | ||
if (!constructor) continue | ||
const name = constructor[Context.expose] | ||
self[key] = new constructor(self, name ? config?.[name] : config) | ||
self[key] = new constructor(self, config) | ||
} | ||
@@ -190,0 +189,0 @@ } |
@@ -49,17 +49,18 @@ import { Awaitable, defineProperty, Promisify, remove } from 'cosmokit' | ||
defineProperty(this, Context.current, root) | ||
defineProperty(this.on('internal/hook', function (name, listener, prepend) { | ||
defineProperty(this.on('event/ready', (ctx, listener) => { | ||
if (!this.isActive) return | ||
ctx.scope.ensure(async () => listener()) | ||
return () => false | ||
}), Context.static, root.scope) | ||
defineProperty(this.on('event/dispose', (ctx, listener, prepend) => { | ||
const method = prepend ? 'unshift' : 'push' | ||
const { scope } = this[Context.current] | ||
const { runtime, disposables } = scope | ||
if (name === 'ready' && this.isActive) { | ||
scope.ensure(async () => listener()) | ||
} else if (name === 'dispose') { | ||
disposables[method](listener as any) | ||
defineProperty(listener, 'name', 'event <dispose>') | ||
return () => remove(disposables, listener) | ||
} else if (name === 'fork') { | ||
runtime.forkables[method](listener as any) | ||
return scope.collect('event <fork>', () => remove(runtime.forkables, listener)) | ||
} | ||
ctx.scope.disposables[method](listener as any) | ||
defineProperty(listener, 'name', 'event <dispose>') | ||
return () => remove(ctx.scope.disposables, listener) | ||
}), Context.static, root.scope) | ||
defineProperty(this.on('event/fork', (ctx, listener, prepend) => { | ||
const method = prepend ? 'unshift' : 'push' | ||
ctx.scope.runtime.forkables[method](listener as any) | ||
return ctx.scope.collect('event <fork>', () => remove(ctx.scope.runtime.forkables, listener)) | ||
}), Context.static, root.scope) | ||
} | ||
@@ -140,5 +141,5 @@ | ||
on(name: keyof any, listener: (...args: any) => any, prepend = false) { | ||
on(name: string, listener: (...args: any) => any, prepend = false) { | ||
// handle special events | ||
const result = this.bail(this, 'internal/hook', name, listener, prepend) | ||
const result = this.bail(`event/${name}`, this[Context.current], listener, prepend) | ||
if (result) return result | ||
@@ -151,3 +152,3 @@ | ||
once(name: keyof any, listener: (...args: any) => any, prepend = false) { | ||
once(name: string, listener: (...args: any) => any, prepend = false) { | ||
const dispose = this.on(name, function (...args: any[]) { | ||
@@ -160,3 +161,3 @@ dispose() | ||
off(name: keyof any, listener: (...args: any) => any) { | ||
off(name: string, listener: (...args: any) => any) { | ||
return this.unregister(this._hooks[name] || [], listener) | ||
@@ -194,4 +195,4 @@ } | ||
'internal/update'(fork: ForkScope<Context.Parameterized<C>>, oldConfig: any): void | ||
'internal/hook'(this: Lifecycle, name: string, listener: Function, prepend: boolean): () => boolean | ||
'internal/event'(type: 'emit' | 'parallel' | 'serial' | 'bail', name: string, args: any[], thisArg: any): void | ||
[K: `event/${string}`]: (ctx: C, listener: Function, prepend: boolean) => undefined | (() => boolean) | ||
} |
import { defineProperty } from 'cosmokit' | ||
import { Context } from './context' | ||
import { ForkScope, Inject, MainScope } from './scope' | ||
import { ForkScope, MainScope } from './scope' | ||
import { resolveConfig } from './utils' | ||
@@ -10,2 +10,7 @@ | ||
export interface Inject { | ||
readonly required?: string[] | ||
readonly optional?: string[] | ||
} | ||
export type Plugin<C extends Context = Context, T = any> = | ||
@@ -22,5 +27,5 @@ | Plugin.Function<C, T> | ||
Config?: (config?: any) => any | ||
inject?: string[] | Partial<Inject> | ||
inject?: string[] | Inject | ||
/** @deprecated use `inject` instead */ | ||
using?: string[] | Partial<Inject> | ||
using?: string[] | Inject | ||
} | ||
@@ -44,3 +49,5 @@ | ||
/* eslint-disable max-len */ | ||
using(deps: string[] | Partial<Inject>, callback: Plugin.Function<Context.Parameterized<this, void>, void>): ForkScope<Context.Parameterized<this, void>> | ||
/** @deprecated use `ctx.inject()` instead */ | ||
using(deps: string[] | Inject, callback: Plugin.Function<Context.Parameterized<this, void>, void>): ForkScope<Context.Parameterized<this, void>> | ||
inject(deps: string[] | Inject, callback: Plugin.Function<Context.Parameterized<this, void>, void>): ForkScope<Context.Parameterized<this, void>> | ||
plugin<T, S = T>(plugin: Plugin.Function<Context.Parameterized<this, T>, T> & { schema?: true; Config: (config?: S) => T }, config?: S): ForkScope<Context.Parameterized<this, T>> | ||
@@ -124,3 +131,7 @@ plugin<T, S = T>(plugin: Plugin.Constructor<Context.Parameterized<this, T>, T> & { schema?: true; Config: (config?: S) => T }, config?: S): ForkScope<Context.Parameterized<this, T>> | ||
using(inject: string[] | Partial<Inject>, callback: Plugin.Function<C, void>) { | ||
using(inject: string[] | Inject, callback: Plugin.Function<C, void>) { | ||
return this.inject(inject, callback) | ||
} | ||
inject(inject: string[] | Inject, callback: Plugin.Function<C, void>) { | ||
return this.plugin({ inject, apply: callback, name: callback.name }) | ||
@@ -127,0 +138,0 @@ } |
@@ -17,7 +17,2 @@ import { deepEqual, defineProperty, isNullable, remove } from 'cosmokit' | ||
export interface Inject { | ||
readonly required: string[] | ||
readonly optional: string[] | ||
} | ||
export type Disposable = () => void | ||
@@ -24,0 +19,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
177136
2939