Skip to content

Commit

Permalink
fix(react): context subscribers weren't being called
Browse files Browse the repository at this point in the history
React wouldn't update its context correctly because the callback it was waiting for was not being
called.
  • Loading branch information
tdreyno committed Feb 20, 2020
1 parent 9c11df2 commit dd848be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
16 changes: 6 additions & 10 deletions src/__tests__/runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,11 @@ describe("onContextChange", () => {

runtime.onContextChange(onChange);

expect.hasAssertions();

runtime.run(enter()).fork(jest.fn(), () => {
expect(onChange).toHaveBeenCalledTimes(1);
});
runtime.run(enter()).fork(jest.fn(), jest.fn());

jest.runAllTimers();

expect(onChange).toHaveBeenCalledTimes(1);
});

test("should run callback once on update", () => {
Expand All @@ -551,13 +549,11 @@ describe("onContextChange", () => {

runtime.onContextChange(onChange);

expect.hasAssertions();

runtime.run({ type: "Trigger" }).fork(jest.fn(), () => {
expect(onChange).toHaveBeenCalledTimes(1);
});
runtime.run({ type: "Trigger" }).fork(jest.fn(), jest.fn());

jest.runAllTimers();

expect(onChange).toHaveBeenCalledTimes(1);
});
});

Expand Down
46 changes: 25 additions & 21 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,32 @@ export class Runtime {
}
}

private chainResults_([effects, tasks]: ExecuteResult): Task<any, Effect[]> {
runEffects(this.context, effects);

effects.forEach(this.handleSubscriptionEffect_);
private chainResults_(result: ExecuteResult): Task<any, Effect[]> {
return Task.sequence(result[1])
.chain(results => {
const joinedResults = results.reduce(
(sum, item) => {
if (isAction(item)) {
sum[1].push(this.run(item));
return sum;
} else {
return processStateReturn(this.context, sum, item);
}
},
[result[0], []] as ExecuteResult,
);

return Task.sequence(tasks).chain(results => {
const joinedResults = results.reduce(
(sum, item) => {
if (isAction(item)) {
sum[1].push(this.run(item));
return sum;
} else {
return processStateReturn(this.context, sum, item);
}
},
[effects, []] as ExecuteResult,
);
if (joinedResults[1].length > 0) {
return this.chainResults_(joinedResults);
}

if (joinedResults[1].length > 0) {
return this.chainResults_(joinedResults);
}
return Task.of(joinedResults[0]);
})
.tap(effects => {
runEffects(this.context, effects);

return Task.of(joinedResults[0]);
});
effects.forEach(this.handleSubscriptionEffect_);
});
}

private flushPendingActions_() {
Expand All @@ -172,6 +174,8 @@ export class Runtime {
results => {
task.resolve(results);

this.contextChangeSubscribers_.forEach(sub => sub(this.context));

this.flushPendingActions_();
},
);
Expand Down

0 comments on commit dd848be

Please sign in to comment.