Skip to content

Commit

Permalink
Fix critical issue where firs intercept/observe doesn't start trigger…
Browse files Browse the repository at this point in the history
…ing unless a Redux action is dispatched
  • Loading branch information
himanshu-satija committed Feb 11, 2021
1 parent 7942fe8 commit 0fdc056
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/DocStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,25 @@ by passing name in plugin configuration to createPlugin.
depth: number = 1
) => {
const observerId = getNextId();

// If observe is called inside observe callback or intercept callback, it causes an infinite loop
// Adding this new observer in postObserveMiddleware fixes that but postObserveMiddleware is
// not triggered when observe is called directly because it is not a Redux action.
// Hence, the following dummy dispatch
this.postObserve(() => {
const newLength = this.observers.set(observerId, {
subtree,
path,
callback,
depth,
});
});

// console.log(
// '$$$$added observer with id ',
// {
// subtree,
// path,
// callback,
// depth,
// },
// observerId
// );
this.dispatch({
type:
'dummy action to trigger postObserve Middleware when observe is not called inside an observe callback',
});
// The above

return () => {
this.observers.delete(observerId);
Expand All @@ -200,6 +200,11 @@ by passing name in plugin configuration to createPlugin.
depth: number = 1
) => {
const interceptorId = getNextId();

// If intercept is called inside intercept callback or intercept callback, it causes an infinite loop
// Adding this new interceptor in postInterceptMiddleware fixes that but postInterceptMiddleware is
// not triggered when intercept is called directly because it is not a Redux action.
// Hence, the following dummy dispatch
this.postIntercept(() => {
const newLength = this.interceptors.set(interceptorId, {
subtree,
Expand All @@ -208,6 +213,10 @@ by passing name in plugin configuration to createPlugin.
depth,
});
});
this.dispatch({
type:
'dummy action to trigger postIntercept Middleware when intercept is not called inside an intercept callback',
});

return () => {
this.interceptors.delete(interceptorId);
Expand Down

0 comments on commit 0fdc056

Please sign in to comment.