hookable icon indicating copy to clipboard operation
hookable copied to clipboard

Allow differed hook calling

Open pi0 opened this issue 2 years ago • 0 comments

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

pi0 avatar Jan 25 '22 16:01 pi0