Allow differed hook calling
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 960
- Forks
- 43
- Avg merge
- 19m
- Merged PRs (30d)
- 1
Description
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.
differHookscan later accept a prefix as well for multi phased recording of specific hooks rather than all.
- While I find it simpler, it is also less explicit and encouraging to handle within a scope.
- 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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by examining the .hook(), callHook(), and proposed differ() entry points described in the issue. Compare the differ, record/stop, and setup alternatives, then define the recording lifecycle, replay behavior, cleanup, and error handling. Done requires an agreed API and corresponding behavior for hooks registered after a call.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100