Skip to content

How should hooks share data? #601

@hossam-nasr

Description

@hossam-nasr

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 the appHookData, which allows communication across scopes. The behavior of the hookData property is currently inconsistent in terms of allowing overriding it with a new object. Currently, we allow overwriting the hookData property within the same hook type, but not across hook types. We should have consistent behavior between hookData and appHookData, and also consistent behavior for hookData 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 overwrite hookData with a new object?

Option 1 : No Overwriting

// 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 print:

{
    hello: 'world'
}

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

// 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 print:

{
    foo: 'bar'
}

Effectively erasing the state set by pre invoc hook 1, perhaps also with a warning message.

Option 3: Object.assign() behaviour:

// 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 print:

{
    hello: 'world',
    foo: 'bar'
}

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions