Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow differed hook calling #36

Open
pi0 opened this issue Jan 25, 2022 · 0 comments
Open

Allow differed hook calling #36

pi0 opened this issue Jan 25, 2022 · 0 comments

Comments

@pi0
Copy link
Member

pi0 commented Jan 25, 2022

In framework usages, when a hook is registered (.hook()) after being called .callHook with no handlers, callers will lose the opportunity to catch it:

// A: It will be called when reaching B
hooks.hook('myplugin', () => { /* is called */ })

// B: Calls any hooks for `myplugin` are already registed
await hooks.callHook('myplugin', 'foo', 'bar')

// C: Will only by called by subsequent myplugin calls. Missing (B)
hooks.hook('myplugin', () => { /* never called */ })

To solve this issue, we need to kinda record the hook calls and replay them when new hook is registered but this means we need to keep them in memory. Simply solution would be defining an initialization phase to set bookable into a recording state and make it replay immediately (C).

// A: Gets called on line (B)
hooks.hook('myplugin', () => { /* gets called */ })

// Record calls while initializing framework
const callDifferedHooks = hooks.differ()

// B: Calls hook from (A) also internally stores a call to ['myplugin, 'foo', 'bar']
await hooks.callHook('myplugin', 'foo', 'bar')

// C: normally won't be called for call in (B)
hooks.hook('myplugin', () => { /* gets called */ })

// Call hook registered in (C) from recordings
// Stop recording and cleanup memory
await callDifferedHooks()

Alternatives:

  • We could instead have hooks.record() / hooks.stop()
    • While I find it simpler, it is also less explicit and encouraging to handle within a scope. differHooks can later accept a prefix as well for multi phased recording of specific hooks rather than all.
  • We immediately call hook in line (C) opon registration
    • It makes hooks called faster but also because it is in background, error handling harder for consumer (internally, best we could throw a console error)
  • We could have hooks.setup(() => { }) that runs callback for auto record any reply afterwards.
    • This could be based on the implementation above. Might work for simple cases, might make code uglier. Have to try.

Alternative syntax with setup:

// Calling to setup, starts recording calls
const promise = hooks.setup(async () => {
  // A: Gets called on line (B)
  hooks.hook('myplugin', () => { /* gets called */ })

  // B: Calls hook from (A) also internally stores a call to ['myplugin, 'foo', 'bar']
  await hooks.callHook('myplugin', 'foo', 'bar')

  // C: normally won't be called for call in (B)
  hooks.hook('myplugin', () => { /* gets called */ })
})

// Call hook registered in (C) from recordings
// Stop recording and cleanup memory
await promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant