Skip to content

Commit

Permalink
Fix observer and interceptor behaviour with escaped slashes in path
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-satija committed Oct 13, 2020
1 parent 78aa573 commit f285226
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
21 changes: 18 additions & 3 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,24 @@ store.intercept(
);

const [doc, setDoc] = store.useSyncState('doc');
setDoc(doc => (doc.test = 'paihwdih'));
const [test, setTest] = store.useDoc('/test');
setTest('kkkkkk');
setDoc(doc => {
doc.test = {};
doc.test['/test3/test4/test5'] = 'paihwdih';
});
// const [test, setTest] = store.useDoc('/test');
// setTest('kkkkkk');
store.observe(
'doc',
'/test',
val => {
console.log('rerererererer &*(&(&&*(');
},
1
);

setTimeout(() => {
setDoc(doc => (doc.test['/test3/test4/test5'] = 'KKKkkkkkk'));
}, 2000);
// undoable(() => true);

const disposeCompute = store.computeDoc((getValue, change) => {
Expand Down
13 changes: 8 additions & 5 deletions src/interceptMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { produce } from 'immer';
import get from 'lodash.get';
import immerPathToJsonPatchPath from './utils/immerPathToJsonPatchPath';
import jsonPatchPathToImmerPath from './utils/jsonPatchPathToImmerPath';

export type Interceptor = {
subtree: string;
Expand Down Expand Up @@ -36,15 +38,16 @@ export const createInterceptMiddleware = (

// If depth x, call for x levels extra below interceptor path
else if (interceptor.depth > 0 && interceptor.depth !== Infinity) {
const matchingLengthPayloadPathArray = payloadPath
.split('/')
.slice(0, interceptor.path.split('/').length);
const matchingLengthPayloadPathArray = jsonPatchPathToImmerPath(
payloadPath
).slice(0, jsonPatchPathToImmerPath(interceptor.path).length);
const remainingPayloadPathLength =
payloadPath.split('/').length -
jsonPatchPathToImmerPath(payloadPath).length -
matchingLengthPayloadPathArray.length;

if (
matchingLengthPayloadPathArray.join('/') === interceptor.path &&
immerPathToJsonPatchPath(matchingLengthPayloadPathArray) ===
interceptor.path &&
remainingPayloadPathLength <= interceptor.depth
) {
discardAction = callInterceptor(interceptor, store, action);
Expand Down
13 changes: 8 additions & 5 deletions src/observeMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import get from 'lodash.get';
import immerPathToJsonPatchPath from './utils/immerPathToJsonPatchPath';
import jsonPatchPathToImmerPath from './utils/jsonPatchPathToImmerPath';

export type Observer = {
subtree: string;
Expand Down Expand Up @@ -27,15 +29,16 @@ export const createObserveMiddleware = (observers: Map<number, Observer>) => {

// If depth x, call for x levels extra below observer path
else if (observer.depth > 0 && observer.depth !== Infinity) {
const matchingLengthPayloadPathArray = payloadPath
.split('/')
.slice(0, observer.path.split('/').length);
const matchingLengthPayloadPathArray = jsonPatchPathToImmerPath(
payloadPath
).slice(0, jsonPatchPathToImmerPath(observer.path).length);
const remainingPayloadPathLength =
payloadPath.split('/').length -
jsonPatchPathToImmerPath(payloadPath).length -
matchingLengthPayloadPathArray.length;

if (
matchingLengthPayloadPathArray.join('/') === observer.path &&
immerPathToJsonPatchPath(matchingLengthPayloadPathArray) ===
observer.path &&
remainingPayloadPathLength <= observer.depth
) {
callObserver(observer, store, action);
Expand Down

0 comments on commit f285226

Please sign in to comment.