From 3425c9c14ff6f8db681f9412546a4fcd13684be8 Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 31 Aug 2023 23:32:36 +0800 Subject: [PATCH] feat: check plugin in registry.resolve() --- package.json | 2 +- src/registry.ts | 10 ++++++---- src/scope.ts | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index c7bc980..ea9d795 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/registry.ts b/src/registry.ts index 990c918..826dfee 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -63,7 +63,11 @@ export class Registry extends Map, 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) { @@ -92,9 +96,7 @@ export class Registry extends Map, MainSc plugin(plugin: Plugin, 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) diff --git a/src/scope.ts b/src/scope.ts index 4684528..ac6633d 100644 --- a/src/scope.ts +++ b/src/scope.ts @@ -7,7 +7,7 @@ declare module './context' { export interface Context { scope: EffectScope runtime: MainScope - 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 @@ -58,7 +58,7 @@ export abstract class EffectScope { 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()