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
How should hooks share data? #601
Comments
Option 4: readonly behaviour:We can mark // pre invoc hook 1
preInvocContext.hookData.hello = 'world';
// pre invoc hook 2
preInvocContext.hookData = { foo: 'bar' };
// post invoc hook 1
console.log(postInvocContext.hookData); This would throw an error during pre invoc hook 2 like Option 5: nice helper methodsRather than have |
My personal preference is 4 and that's the one Alexey also wanted during our discussion offline. @hossam-nasr I didn't think of it during our conversation in the PR otherwise I would've mentioned it earlier. I think the main benefit of this is that it prevents accidental overwriting in a much more obvious way - TypeScript users should get a build error and JavaScript users (or TypeScript users who ignore the build error) will get a runtime error. At that point, I assume users will know to switch to |
@ejizba I agree option 4 is the least ambiguous and most straightforward of the options. I'm happy to go with that option but would still rather keep it as a separate PR |
Right now, we have the
hookData
property in all hook contexts, which allows different hooks of the same cope to share data and communicate with each other. There is also theappHookData
, which allows communication across scopes. The behavior of thehookData
property is currently inconsistent in terms of allowing overriding it with a new object. Currently, we allow overwriting thehookData
property within the same hook type, but not across hook types. We should have consistent behavior betweenhookData
andappHookData
, and also consistent behavior forhookData
for different hook types/scopes. This issue is to discuss the different options for this behavior. Specifically, the question is should we allow hooks to overwritehookData
with a new object?Option 1 : No Overwriting
This would print:
Effectively ignoring the changes in pre invoc hook 2, perhaps with also a warning along the lines of "WARNING: Ignoring changes to hook data. You may modify the hook data object, but cannot replace it."
Option 2: Allow Overwriting
This would print:
Effectively erasing the state set by pre invoc hook 1, perhaps also with a warning message.
Option 3:
Object.assign()
behaviour:This would print:
The text was updated successfully, but these errors were encountered: