Skip to content

Commit

Permalink
prevent infinite loops with nested observe and intercept
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-satija committed Oct 13, 2020
1 parent 928e70c commit 07a93c9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"build": "NODE_ENV=production tsdx build",
"test": "tsdx test --passWithNoTests",
"lint": "tsdx lint",
"prepare": "tsdx build"
Expand All @@ -42,6 +42,7 @@
"trailingComma": "es5"
},
"devDependencies": {
"@rollup/plugin-strip": "^2.0.0",
"@types/lodash.get": "^4.4.6",
"@types/react": "^16.9.46",
"@types/react-dom": "^16.9.8",
Expand Down
46 changes: 28 additions & 18 deletions src/DocStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import jsonPatchPathToImmerPath from './utils/jsonPatchPathToImmerPath';
import getNextId from './utils/getNextId';
import { Watch, ComputeCallback } from 'types';
import { createPostObserveMiddleware } from './postObserveMiddleware';
import { createPostInterceptMiddleware } from './postInterceptMiddleware';

type ReduxStore = Store<
CombinedState<{
Expand All @@ -34,6 +35,7 @@ export default class DocStore {
private observers = new Map<number, Observer>();
private interceptors = new Map<number, Interceptor>();
private postObserveCallbacks: Array<() => void> = [];
private postInterceptCallbacks: Array<() => void> = [];

constructor(
initialDoc: {},
Expand Down Expand Up @@ -100,6 +102,7 @@ by passing name in plugin configuration to createPlugin.
applyMiddleware(
createInterceptMiddleware(this.interceptors),
createObserveMiddleware(this.observers),
createPostInterceptMiddleware(this.postInterceptCallbacks),
createPostObserveMiddleware(this.postObserveCallbacks),
...this.plugins.map(p => p.middleware)
)
Expand Down Expand Up @@ -164,23 +167,25 @@ by passing name in plugin configuration to createPlugin.
depth: number = 1
) => {
const observerId = getNextId();
const newLength = this.observers.set(observerId, {
subtree,
path,
callback,
depth,
});

console.log(
'$$$$added observer with id ',
{
this.postObserve(() => {
const newLength = this.observers.set(observerId, {
subtree,
path,
callback,
depth,
},
observerId
);
});

console.log(
'$$$$added observer with id ',
{
subtree,
path,
callback,
depth,
},
observerId
);
});

return () => {
this.observers.delete(observerId);
Expand All @@ -195,11 +200,13 @@ by passing name in plugin configuration to createPlugin.
depth: number = 1
) => {
const interceptorId = getNextId();
const newLength = this.interceptors.set(interceptorId, {
subtree,
path,
callback,
depth,
this.postIntercept(() => {
const newLength = this.interceptors.set(interceptorId, {
subtree,
path,
callback,
depth,
});
});

return () => {
Expand All @@ -210,6 +217,9 @@ by passing name in plugin configuration to createPlugin.
postObserve = (callback: () => void) => {
this.postObserveCallbacks.push(callback);
};
postIntercept = (callback: () => void) => {
this.postInterceptCallbacks.push(callback);
};

useSyncState = (subtree: string, path: string = '') =>
useSyncState(this, subtree, path);
Expand Down
13 changes: 13 additions & 0 deletions src/postInterceptMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const createPostInterceptMiddleware = (callbacks: Array<() => void>) => {
return (store: any) => (next: any) => (action: any) => {
const result = next(action);

callbacks.forEach((cb, index) => {
cb();
});

callbacks.length = 0;

return result;
};
};
9 changes: 0 additions & 9 deletions src/postObserveMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import get from 'lodash.get';

export type Observer = {
subtree: string;
path: string;
callback: any;
depth: number;
};

export const createPostObserveMiddleware = (callbacks: Array<() => void>) => {
return (store: any) => (next: any) => (action: any) => {
const result = next(action);
Expand Down
14 changes: 14 additions & 0 deletions tsdx.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const strip = require('@rollup/plugin-strip');

module.exports = {
rollup(config, options) {
config.plugins.push(
strip({
include: ['**/*.js', '**/*.ts'],
functions: options.env === 'production' ? ['console.log'] : [],
debugger: options.env === 'production' ? true : false,
})
);
return config;
},
};
11 changes: 10 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,16 @@
"@rollup/pluginutils" "^3.0.8"
magic-string "^0.25.5"

"@rollup/pluginutils@^3.0.8":
"@rollup/plugin-strip@^2.0.0":
version "2.0.0"
resolved "https://registry.npmjs.org/@rollup/plugin-strip/-/plugin-strip-2.0.0.tgz#46014ca2bc30bfff4ac289c52d7174671418173d"
integrity sha512-1xtE2FDk7Wwr2dEPugkQp3M2FGtSVYeRQaJnHJIkHzcXpBlxT/z+jJoVUvm8LGXso+bLWgO65WrmQ6bC6e/x+A==
dependencies:
"@rollup/pluginutils" "^3.0.4"
estree-walker "^1.0.1"
magic-string "^0.25.5"

"@rollup/pluginutils@^3.0.4", "@rollup/pluginutils@^3.0.8":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
integrity sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==
Expand Down

0 comments on commit 07a93c9

Please sign in to comment.