Comparing version 1.4.1 to 1.4.3
import { Awaitable, Promisify } from 'cosmokit'; | ||
export type Plugin = Plugin.Function | Plugin.Object; | ||
export namespace Plugin { | ||
type Function<T = any> = (ctx: Context, options: T) => void; | ||
type Constructor<T = any> = new (ctx: Context, options: T) => void; | ||
interface Object<S = any, T = any> { | ||
name?: string; | ||
apply: Function<T>; | ||
reusable?: boolean; | ||
Config?: (config?: S) => T; | ||
schema?: (config?: S) => T; | ||
using?: readonly string[]; | ||
} | ||
type Config<T extends Plugin> = T extends Constructor<infer U> ? U : T extends Function<infer U> ? U : T extends Object<infer U> ? U : never; | ||
} | ||
export namespace Registry { | ||
@@ -6,8 +20,8 @@ interface Config { | ||
interface Delegates { | ||
using(using: readonly string[], callback: Plugin.Function<void>): Plugin.Fork; | ||
plugin<T extends Plugin>(plugin: T, config?: boolean | Plugin.Config<T>): Plugin.Fork; | ||
dispose(plugin?: Plugin): Plugin.Runtime; | ||
using(using: readonly string[], callback: Plugin.Function<void>): Fork; | ||
plugin<T extends Plugin>(plugin: T, config?: boolean | Plugin.Config<T>): Fork; | ||
dispose(plugin?: Plugin): Runtime; | ||
} | ||
} | ||
export class Registry extends Map<Plugin, Plugin.Runtime> { | ||
export class Registry extends Map<Plugin, Runtime> { | ||
app: App; | ||
@@ -18,64 +32,52 @@ private config; | ||
private resolve; | ||
get(plugin: Plugin): Plugin.Runtime; | ||
get(plugin: Plugin): Runtime; | ||
has(plugin: Plugin): boolean; | ||
set(plugin: Plugin, state: Plugin.Runtime): this; | ||
set(plugin: Plugin, state: Runtime): this; | ||
delete(plugin: Plugin): boolean; | ||
using(using: readonly string[], callback: Plugin.Function<void>): Plugin.Fork; | ||
using(using: readonly string[], callback: Plugin.Function<void>): Fork; | ||
static validate(plugin: any, config: any): any; | ||
plugin(plugin: Plugin, config?: any): Plugin.Fork; | ||
dispose(plugin: Plugin): Plugin.Runtime; | ||
plugin(plugin: Plugin, config?: any): Fork; | ||
dispose(plugin: Plugin): Runtime; | ||
} | ||
export type Disposable = () => void; | ||
export type Plugin = Plugin.Function | Plugin.Object; | ||
export namespace Plugin { | ||
type Function<T = any> = (ctx: Context, options: T) => void; | ||
type Constructor<T = any> = new (ctx: Context, options: T) => void; | ||
interface Object<S = any, T = any> { | ||
name?: string; | ||
apply: Function<T>; | ||
reusable?: boolean; | ||
Config?: (config?: S) => T; | ||
schema?: (config?: S) => T; | ||
using?: readonly string[]; | ||
} | ||
type Config<T extends Plugin> = T extends Constructor<infer U> ? U : T extends Function<infer U> ? U : T extends Object<infer U> ? U : never; | ||
abstract class State { | ||
parent: Context; | ||
config: any; | ||
uid: number; | ||
runtime: Runtime; | ||
context: Context; | ||
disposables: Disposable[]; | ||
abstract dispose(): boolean; | ||
abstract restart(): void; | ||
abstract update(config: any, manual: boolean): void; | ||
constructor(parent: Context, config: any); | ||
protected clear(preserve?: boolean): void; | ||
} | ||
const kPreserve: unique symbol; | ||
class Fork extends State { | ||
constructor(parent: Context, config: any, runtime: Runtime); | ||
restart(): void; | ||
update(config: any, manual?: boolean): void; | ||
dispose(): boolean; | ||
} | ||
class Runtime extends State { | ||
private registry; | ||
plugin: Plugin; | ||
runtime: this; | ||
schema: any; | ||
using: readonly string[]; | ||
forkables: Function[]; | ||
children: Fork[]; | ||
isActive: boolean; | ||
constructor(registry: Registry, plugin: Plugin, config: any); | ||
get isForkable(): boolean; | ||
fork(parent: Context, config: any): Fork; | ||
dispose(): boolean; | ||
init(): void; | ||
private apply; | ||
restart(): void; | ||
update(config: any, manual?: boolean): void; | ||
} | ||
export const kPreserve: unique symbol; | ||
export abstract class State { | ||
parent: Context; | ||
config: any; | ||
uid: number; | ||
runtime: Runtime; | ||
context: Context; | ||
disposables: Disposable[]; | ||
abstract dispose(): boolean; | ||
abstract restart(): void; | ||
abstract update(config: any, manual?: boolean): void; | ||
constructor(parent: Context, config: any); | ||
protected init(): void; | ||
protected check(): boolean; | ||
protected clear(preserve?: boolean): void; | ||
} | ||
export class Fork extends State { | ||
constructor(parent: Context, config: any, runtime: Runtime); | ||
restart(): void; | ||
update(config: any, manual?: boolean): void; | ||
dispose(): boolean; | ||
} | ||
export class Runtime extends State { | ||
private registry; | ||
plugin: Plugin; | ||
runtime: this; | ||
schema: any; | ||
using: readonly string[]; | ||
forkables: Function[]; | ||
children: Fork[]; | ||
isReusable: boolean; | ||
constructor(registry: Registry, plugin: Plugin, config: any); | ||
get isForkable(): boolean; | ||
fork(parent: Context, config: any): Fork; | ||
dispose(): boolean; | ||
init(): void; | ||
private apply; | ||
restart(): void; | ||
update(config: any, manual?: boolean): void; | ||
} | ||
export namespace Lifecycle { | ||
@@ -140,12 +142,10 @@ interface Session { | ||
export interface Events { | ||
'logger/error'(name: string, format: string, ...param: any[]): void; | ||
'logger/warn'(name: string, format: string, ...param: any[]): void; | ||
'logger/debug'(name: string, format: string, ...param: any[]): void; | ||
'plugin-added'(state: Plugin.Runtime): void; | ||
'plugin-removed'(state: Plugin.Runtime): void; | ||
'plugin-added'(state: Runtime): void; | ||
'plugin-removed'(state: Runtime): void; | ||
'ready'(): Awaitable<void>; | ||
'fork': Plugin.Function; | ||
'dispose'(): Awaitable<void>; | ||
'internal/service'(name: string, oldValue: any): void; | ||
'internal/update'(state: Plugin.Fork, config: any): void; | ||
'internal/warn'(format: any, ...param: any[]): void; | ||
'internal/service'(this: Context, name: string, oldValue: any): void; | ||
'internal/update'(state: Fork, config: any): void; | ||
'internal/hook'(name: string, listener: Function, prepend: boolean): () => boolean; | ||
@@ -156,3 +156,9 @@ } | ||
} | ||
declare global { | ||
interface Object { | ||
[Context.filter]?(context: Context): boolean; | ||
} | ||
} | ||
export class Context { | ||
static readonly filter: unique symbol; | ||
static readonly current: unique symbol; | ||
@@ -162,8 +168,9 @@ static readonly immediate: unique symbol; | ||
get source(): string; | ||
fork(meta: Partial<Context.Meta>): Context; | ||
any(): Context; | ||
never(): Context; | ||
union(arg: Filter | Context): Context; | ||
intersect(arg: Filter | Context): Context; | ||
exclude(arg: Filter | Context): Context; | ||
extend(meta: Partial<Context.Meta>): this; | ||
isolate(names: string[]): this; | ||
any(): this; | ||
never(): this; | ||
union(arg: Filter | Context): this; | ||
intersect(arg: Filter | Context): this; | ||
exclude(arg: Filter | Context): this; | ||
match(session?: Lifecycle.Session): boolean; | ||
@@ -185,6 +192,7 @@ } | ||
app: App; | ||
state: Plugin.State; | ||
state: State; | ||
filter: Filter; | ||
mapping: {}; | ||
} | ||
const internal: {}; | ||
const internal: any; | ||
function service(name: keyof any, options?: ServiceOptions): void; | ||
@@ -194,2 +202,3 @@ } | ||
options: App.Config; | ||
counter: number; | ||
constructor(config?: App.Config); | ||
@@ -196,0 +205,0 @@ start(): Promise<void>; |
{ | ||
"name": "cordis", | ||
"description": "AOP Framework for Modern JavaScript Applications", | ||
"version": "1.4.1", | ||
"version": "1.4.3", | ||
"main": "lib/index.cjs", | ||
@@ -39,4 +39,3 @@ "module": "lib/index.mjs", | ||
"chai-as-promised": "^7.1.1", | ||
"chai-shape": "^1.0.0", | ||
"dtsc": "^1.1.0", | ||
"dtsc": "^1.1.1", | ||
"esbuild": "^0.14.39", | ||
@@ -43,0 +42,0 @@ "esbuild-register": "^3.3.2", |
137
README.md
@@ -38,1 +38,138 @@ # Cordis | ||
### Events | ||
## API | ||
### Context | ||
#### ctx.extend(meta) | ||
- meta: `Partial<Context.Meta>` | ||
- returns: `Context` | ||
Creates a new context. All properties of the new context are inherited from the current context, except for properties specified in meta which are overridden. | ||
### Lifecycle | ||
`ctx.lifecycle` is a built-in service which provides event-related functionality. Most of its methods are also directly accessible in the context. | ||
#### ctx.emit(event, ...param) | ||
- event: `string` event name | ||
- param: `any[]` event parameters | ||
- returns: `void` | ||
Trigger the event called `event`, calling all associated listeners **synchronously** at the same time, passing the supplied arguments to each. | ||
<!-- An [`internal/warn`](#internalwarn) event is triggered if a listener throws an error or returns a rejected promise. --> | ||
#### ctx.parallel(event, ...param) | ||
- event: `string` event name | ||
- param: `any[]` event parameters | ||
- returns: `Promise<void>` | ||
Trigger the event called `event`, calling all associated listeners **asynchronously** at the same time, passing the supplied arguments to each. | ||
<!-- An [`internal/warn`](#internalwarn) event is triggered if a listener throws an error or returns a rejected promise. --> | ||
#### ctx.bail(event, ...param) | ||
- event: `string` event name | ||
- param: `any[]` event parameters | ||
- returns: `any` | ||
Trigger the event called `event`, calling all associated listeners **synchronously** in the order they were registered, passing the supplied arguments to each. | ||
If any listener returns a value other than `false`, `null` or `undefined`, that value is returned. If all listeners return `false`, `null` or `undefined`, an `undefined` is returned. In either case, subsequent listeners will not be called. | ||
#### ctx.serial(event, ...param) | ||
- event: `string` event name | ||
- param: `any[]` event parameters | ||
- returns: `Promise<any>` | ||
Trigger the event called `event`, calling all associated listeners **asynchronously** in the order they were registered, passing the supplied arguments to each. | ||
If any listener is fulfilled with a value other than `false`, `null` or `undefined`, the returned promise is fulfilled with that value. If all listeners are fulfilled with `false`, `null` or `undefined`, the returned promise is fulfilled with `undefined`. In either case, subsequent listeners will not be called. | ||
#### ctx.on() | ||
#### ctx.once() | ||
#### ctx.off() | ||
#### ctx.lifecycle.start() | ||
#### ctx.lifecycle.stop() | ||
#### ctx.lifecycle.mark() | ||
#### ctx.lifecycle.register() | ||
#### ctx.lifecycle.unregister() | ||
### Registry | ||
`ctx.registry` is a built-in service which provides plugin-related functionality. It is actually a subclass of `Map<Plugin, Runtime>`, so you can access plugin runtime via methods like `ctx.registry.get()`. | ||
#### ctx.plugin() | ||
#### ctx.using() | ||
#### ctx.dispose() | ||
### State | ||
State can be accessed via `ctx.state` or passed in in some events. | ||
#### state.uid | ||
- type: `number` | ||
An auto-incrementing unique identifier for the state. | ||
#### state.runtime | ||
- type: `Runtime` | ||
The plugin runtime associated with the state. If the state is a runtime, then this property refers to itself. | ||
#### state.parent | ||
#### state.context | ||
#### state.config | ||
#### state.restart() | ||
#### state.update() | ||
#### state.dispose() | ||
### Fork | ||
### Runtime | ||
#### runtime.plugin | ||
#### runtime.isForkable | ||
### Events | ||
#### ready | ||
#### dispose | ||
#### fork | ||
#### plugin-added | ||
#### plugin-removed | ||
#### internal/warn | ||
#### internal/hook | ||
#### internal/service | ||
#### internal/update |
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
129211
15
1397
175