Skip to content

Commit

Permalink
feat: check plugin in registry.resolve()
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 31, 2023
1 parent c2625db commit 3425c9c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cordis",
"description": "AOP Framework for Modern JavaScript Applications",
"version": "2.8.6",
"version": "2.8.8",
"sideEffects": false,
"main": "lib/index.cjs",
"module": "lib/index.mjs",
Expand Down
10 changes: 6 additions & 4 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export class Registry<C extends Context = Context> extends Map<Plugin<C>, MainSc
}

resolve(plugin: Plugin) {
return plugin && (typeof plugin === 'function' ? plugin : plugin.apply)
// Allow `null` as a special case.
if (plugin === null) return plugin
if (typeof plugin === 'function') return plugin
if (isApplicable(plugin)) return plugin.apply
throw new Error('invalid plugin, expect function or object with an "apply" method')
}

get(plugin: Plugin<C>) {
Expand Down Expand Up @@ -92,9 +96,7 @@ export class Registry<C extends Context = Context> extends Map<Plugin<C>, MainSc

plugin(plugin: Plugin<C>, config?: any) {
// check if it's a valid plugin
if (typeof plugin !== 'function' && !isApplicable(plugin)) {
throw new Error('invalid plugin, expect function or object with an "apply" method')
}
this.resolve(plugin)

// resolve plugin config
config = resolveConfig(plugin, config)
Expand Down
4 changes: 2 additions & 2 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module './context' {
export interface Context {
scope: EffectScope<this>
runtime: MainScope<this>
collect(label: string, callback: () => boolean): () => boolean
collect(label: string, callback: () => void): () => void
accept(callback?: (config: this['config']) => void | boolean, options?: AcceptOptions): () => boolean
accept(keys: (keyof this['config'])[], callback?: (config: this['config']) => void | boolean, options?: AcceptOptions): () => boolean
decline(keys: (keyof this['config'])[]): () => boolean
Expand Down Expand Up @@ -58,7 +58,7 @@ export abstract class EffectScope<C extends Context = Context> {
return this.runtime.isReactive ? this.proxy : this.config
}

collect(label: string, callback: () => boolean) {
collect(label: string, callback: () => any) {
const dispose = defineProperty(() => {
remove(this.disposables, dispose)
return callback()
Expand Down

0 comments on commit 3425c9c

Please sign in to comment.