Skip to content

Commit 6b9d843

Browse files
committed
Revert "fix bad JSON patch usage"
Accidentally pushed to main. Reverting and merging via PR instead This reverts commit 0bc5d6c.
1 parent 0bc5d6c commit 6b9d843

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

idom/client/app/core_modules/layout.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,29 @@ export function mountLayout(mountElement, saveUpdateHook, sendEvent) {
5353
}
5454

5555
export default function Layout({ saveUpdateHook, sendEvent }) {
56-
const [model, patchModel] = useInplaceJsonPatch({});
57-
58-
react.useEffect(() => saveUpdateHook(patchModel), [patchModel]);
56+
const [modelRef, setModel] = useStateRef({});
57+
58+
react.useEffect(() => {
59+
saveUpdateHook((pathPrefix, patch) => {
60+
setModel(
61+
jsonpatch.applyPatch(
62+
modelRef.current,
63+
patch.map((op) => {
64+
op.path = pathPrefix + op.path;
65+
return op;
66+
}),
67+
undefined,
68+
false
69+
).newDocument
70+
);
71+
});
72+
}, [modelRef]);
5973

60-
if (model.tagName) {
61-
return html`<${Element} sendEvent=${sendEvent} model=${model} />`;
74+
if (modelRef.current.tagName) {
75+
return html`<${Element}
76+
sendEvent=${sendEvent}
77+
model=${modelRef.current}
78+
/>`;
6279
} else {
6380
return html`<div />`;
6481
}
@@ -187,24 +204,9 @@ function getPathProperty(obj, prop) {
187204
return value;
188205
}
189206

190-
function useInplaceJsonPatch(doc) {
191-
const ref = react.useRef(doc);
192-
const forceUpdate = useForceUpdate();
193-
194-
const applyPatch = react.useCallback(
195-
(path, patch) => {
196-
jsonpatch.applyPatch(
197-
jsonpatch.getValueByPointer(ref.current, path),
198-
patch
199-
);
200-
forceUpdate();
201-
},
202-
[ref, forceUpdate]
203-
);
204-
return [ref.current, applyPatch];
205-
}
206-
207-
function useForceUpdate() {
208-
const [, updateState] = react.useState();
209-
return react.useCallback(() => updateState({}), []);
207+
function useStateRef(initialValue) {
208+
const ref = react.useRef(initialValue);
209+
const [state, setState] = react.useState(initialValue);
210+
ref.current = state;
211+
return [ref, setState];
210212
}

0 commit comments

Comments
 (0)