From 4b84d877e7aaa1306b455479f5263bcdd4f901fe Mon Sep 17 00:00:00 2001 From: Himanshu Satija Date: Thu, 11 Feb 2021 21:45:06 +0530 Subject: [PATCH] Fix critical issue with triggering of observe and intercept --- example/Todo.tsx | 2 +- example/index.tsx | 46 +++++++++++++++++++++++++++++--------------- example/package.json | 2 +- example/yarn.lock | 8 ++++---- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/example/Todo.tsx b/example/Todo.tsx index 50981b2..97020b2 100644 --- a/example/Todo.tsx +++ b/example/Todo.tsx @@ -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); diff --git a/example/index.tsx b/example/index.tsx index ba0dae6..08314c5 100644 --- a/example/index.tsx +++ b/example/index.tsx @@ -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, }); @@ -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 => { @@ -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 => { @@ -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 }, }, }; } diff --git a/example/package.json b/example/package.json index ae6cae6..4f50328 100644 --- a/example/package.json +++ b/example/package.json @@ -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" }, diff --git a/example/yarn.lock b/example/yarn.lock index bf009f3..b70fd4d 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -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"