Skip to content

Commit

Permalink
feat: basic usage for ctx.effect()
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 23, 2023
1 parent 837f8e9 commit 3c5e258
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 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": "3.4.0",
"version": "3.4.1",
"sideEffects": false,
"main": "lib/index.cjs",
"module": "lib/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class Context {
self[Context.shadow] = Object.create(null)
self.root = self
self.realms = Object.create(null)
self.mixin('scope', ['config', 'runtime', 'collect', 'accept', 'decline'])
self.mixin('scope', ['config', 'runtime', 'effect', 'collect', 'accept', 'decline'])
self.mixin('registry', ['using', 'inject', 'plugin', 'dispose'])
self.mixin('lifecycle', ['on', 'once', 'off', 'after', 'parallel', 'emit', 'serial', 'bail', 'start', 'stop'])
self.provide('registry', new Registry(self, config!), true)
Expand Down
20 changes: 17 additions & 3 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare module './context' {
export interface Context {
scope: EffectScope<this>
runtime: MainScope<this>
effect(callback: () => () => void): () => void
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
Expand Down Expand Up @@ -65,6 +66,17 @@ export abstract class EffectScope<C extends Context = Context> {
return this.runtime.isReactive ? this.proxy : this.config
}

effect(callback: () => () => void) {
if (this.uid === null) return () => {}
const disposeRaw = callback()
const dispose = () => {
remove(this.disposables, dispose)
disposeRaw()
}
this.disposables.push(dispose)
return dispose
}

collect(label: string, callback: () => any) {
const dispose = defineProperty(() => {
remove(this.disposables, dispose)
Expand Down Expand Up @@ -165,9 +177,11 @@ export abstract class EffectScope<C extends Context = Context> {
accept(...args: any[]) {
const keys = Array.isArray(args[0]) ? args.shift() : null
const acceptor: Acceptor = { keys, callback: args[0], ...args[1] }
this.acceptors.push(acceptor)
if (acceptor.immediate) acceptor.callback?.(this.config)
return this.collect(`accept <${keys?.join(', ') || '*'}>`, () => remove(this.acceptors, acceptor))
return this.effect(() => {
this.acceptors.push(acceptor)
if (acceptor.immediate) acceptor.callback?.(this.config)
return () => remove(this.acceptors, acceptor)
})
}

decline(keys: string[]) {
Expand Down

0 comments on commit 3c5e258

Please sign in to comment.