Skip to content

Commit

Permalink
Fix critical issue with triggering of observe and intercept
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-satija committed Feb 11, 2021
1 parent 0fdc056 commit 4b84d87
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion example/Todo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useState, useEffect } from 'react';

export function TodoApp() {
const todoPath = '/todos';
const filterPath = '/filter';
const filterPath = '/options/filter';

const [todos, setTodos, dispatch] = useDoc(todoPath);
const [todoFilter, setTodoFilter] = useDoc(filterPath);
Expand Down
46 changes: 30 additions & 16 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import * as remote from '@syncstate/remote-client';
// @ts-ignore
import socketIOClient from 'socket.io-client';

const store = createDocStore({ todos: [], filter: 'all' }, [
const store = createDocStore({ todos: [], options: { filter: 'all' } }, [
history.createInitializer(),
remote.createInitializer(),
]);

store.dispatch(remote.enableRemote('/todos'));
store.dispatch(remote.enableRemote('/options'));

const socket = socketIOClient('http://localhost:3001', {
const socket = socketIOClient('http://localhost:3010', {
timeout: 100000,
});

Expand All @@ -30,7 +31,9 @@ remote.observeStatus(store, '/todos', loading => {
});

socket.emit('fetchDoc', '/todos');
socket.emit('fetchDoc', '/options');
store.dispatch(remote.setLoading('/todos', true));
store.dispatch(remote.setLoading('/options', true));
console.log('Wewdwd', remote.getLoading(store, '/todos'));

socket.on('loaded', path => {
Expand All @@ -39,22 +42,33 @@ socket.on('loaded', path => {
});

socket.on('change', (path, patch) => {
console.log('Applying remote patch', patch);
store.dispatch(remote.applyRemote(path, patch));
});

store.intercept(
'doc',
'/todos',
(todos, change) => {
if (!change.origin) {
// Don't emit for patches received from server
socket.emit('change', '/todos', change);
}
return change;
// return null;
},
Infinity
);
// store.intercept(
// 'doc',
// '/todos',
// (todos, change) => {
// if (!change.origin && !remote.getLoading(store, '/todos')) {
// // Don't emit for patches received from server
// socket.emit('change', '/todos', change);
// }
// return change;
// // return null;
// },
// Infinity
// );

remote.onChange(store, '/todos', change => {
console.log(change, 'remote change ready todos');
socket.emit('change', '/todos', change);
});

remote.onChange(store, '/options', change => {
console.log(change, 'remote change ready filter');
socket.emit('change', '/options', change);
});

const [doc, setDoc] = store.useSyncState('doc');
setDoc(doc => {
Expand Down Expand Up @@ -111,7 +125,7 @@ const disposeInt = store.intercept(
...change,
patch: {
...change.patch,
value: { caption: 'Hello', completed: false },
value: { caption: 'Hello', completed: change.patch.value.completed },
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"@syncstate/history": "^0.6.0",
"@syncstate/react": "^0.7.0-rc.3",
"@syncstate/remote-client": "^0.6.0",
"@syncstate/remote-client": "^0.7.0-rc.0",
"react-app-polyfill": "^1.0.0",
"socket.io-client": "^2.3.0"
},
Expand Down
8 changes: 4 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -950,10 +950,10 @@
dependencies:
immer "^7.0.7"

"@syncstate/remote-client@^0.6.0":
version "0.6.0"
resolved "https://registry.npmjs.org/@syncstate/remote-client/-/remote-client-0.6.0.tgz#0aae7c7ba560928c463a50729ad9d3a98d5c73df"
integrity sha512-eIVR48e0WhkLBmW2NDLSogFp1xeR8iqM8JCp4rSmQnhfa1ntSn7fWlW6Xc/bMqgvdR9PmW3qCNYQopfBbEF/9Q==
"@syncstate/remote-client@^0.7.0-rc.0":
version "0.7.0-rc.0"
resolved "https://registry.npmjs.org/@syncstate/remote-client/-/remote-client-0.7.0-rc.0.tgz#4481a7ef9d45c7f8d2235852f1d5b0835815a2dd"
integrity sha512-sp3dI/z/AI14SQFaY+wgqIF1lZn4bn55iMNqg5LVX14IgfXUZTFBsFr2jc5HwSsoWzHxWGVxH5bnyxH6lPEUaA==

"@types/prop-types@*":
version "15.7.3"
Expand Down

0 comments on commit 4b84d87

Please sign in to comment.