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

Preview hooks: trigger effects after story render #7791

Merged
merged 4 commits into from
Aug 19, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support slightly async decorators (#7407#issuecomment-514103397)
Hypnosphi committed Aug 17, 2019
commit d8da326655054f8c9fd957276d83c9c95729c313
12 changes: 8 additions & 4 deletions lib/client-api/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ type AbstractFunction = (...args: any[]) => any;

const hookListsMap = new WeakMap<AbstractFunction, Hook[]>();
let mountedDecorators = new Set<AbstractFunction>();
let prevMountedDecorators = mountedDecorators;

let currentHooks: Hook[] = [];
let nextHookIndex = 0;
@@ -72,7 +73,7 @@ const hookify = (fn: AbstractFunction) => (...args: any[]) => {
const prevDecoratorName = currentDecoratorName;

currentDecoratorName = fn.name;
if (mountedDecorators.has(fn)) {
if (prevMountedDecorators.has(fn)) {
currentPhase = 'UPDATE';
currentHooks = hookListsMap.get(fn) || [];
} else {
@@ -105,10 +106,11 @@ export const applyHooks = (
) => (getStory: StoryGetter, decorators: Decorator[]) => {
const decorated = applyDecorators(hookify(getStory), decorators.map(hookify));
return (context: StoryContext) => {
prevMountedDecorators = mountedDecorators;
mountedDecorators = new Set([getStory, ...decorators]);
currentContext = context;
hasUpdates = false;
let result = decorated(context);
mountedDecorators = new Set([getStory, ...decorators]);
numberOfRenders = 1;
while (hasUpdates) {
hasUpdates = false;
@@ -121,8 +123,10 @@ export const applyHooks = (
);
}
}
addons.getChannel().once(STORY_RENDERED, triggerEffects);
currentContext = null;
addons.getChannel().once(STORY_RENDERED, () => {
triggerEffects();
currentContext = null;
});
return result;
};
};