diff --git a/docs/source/about/changelog.rst b/docs/source/about/changelog.rst index d0f46a155..f5b06fbe1 100644 --- a/docs/source/about/changelog.rst +++ b/docs/source/about/changelog.rst @@ -23,7 +23,9 @@ more info, see the :ref:`Contributor Guide `. Unreleased ---------- -No changes. +**Changed** + +- :pull:`809` - Avoid the use of JSON patch for diffing models. v0.40.1 @@ -32,7 +34,7 @@ v0.40.1 **Fixed** -- :issue:`806` - child models after a component fail to render +- :issue:`806` - Child models after a component fail to render v0.40.0 (yanked) diff --git a/src/idom/core/_fixed_jsonpatch.py b/src/idom/core/_fixed_jsonpatch.py deleted file mode 100644 index 702bc4dc6..000000000 --- a/src/idom/core/_fixed_jsonpatch.py +++ /dev/null @@ -1,56 +0,0 @@ -# type: ignore - -"""A patched version of jsonpatch - -We need this because of: https://github.com/stefankoegl/python-json-patch/issues/138 - -The core of this patch is in `DiffBuilder._item_removed`. The rest is just boilerplate -that's been copied over with little to no changes. -""" - -from jsonpatch import _ST_REMOVE -from jsonpatch import DiffBuilder as _DiffBuilder -from jsonpatch import JsonPatch as _JsonPatch -from jsonpatch import RemoveOperation, _path_join, basestring -from jsonpointer import JsonPointer - - -def apply_patch(doc, patch, in_place=False, pointer_cls=JsonPointer): - if isinstance(patch, basestring): - patch = JsonPatch.from_string(patch, pointer_cls=pointer_cls) - else: - patch = JsonPatch(patch, pointer_cls=pointer_cls) - return patch.apply(doc, in_place) - - -def make_patch(src, dst, pointer_cls=JsonPointer): - return JsonPatch.from_diff(src, dst, pointer_cls=pointer_cls) - - -class JsonPatch(_JsonPatch): - @classmethod - def from_diff( - cls, - src, - dst, - optimization=True, - dumps=None, - pointer_cls=JsonPointer, - ): - json_dumper = dumps or cls.json_dumper - builder = DiffBuilder(src, dst, json_dumper, pointer_cls=pointer_cls) - builder._compare_values("", None, src, dst) - ops = list(builder.execute()) - return cls(ops, pointer_cls=pointer_cls) - - -class DiffBuilder(_DiffBuilder): - def _item_removed(self, path, key, item): - new_op = RemoveOperation( - { - "op": "remove", - "path": _path_join(path, key), - } - ) - new_index = self.insert(new_op) - self.store_index(item, new_index, _ST_REMOVE) diff --git a/src/idom/core/serve.py b/src/idom/core/serve.py index fd21c55bf..69071555f 100644 --- a/src/idom/core/serve.py +++ b/src/idom/core/serve.py @@ -6,8 +6,8 @@ from typing import Any, Awaitable, Callable, Dict, List, NamedTuple, cast from anyio import create_task_group +from jsonpatch import apply_patch -from ._fixed_jsonpatch import apply_patch, make_patch # type: ignore from .layout import LayoutEvent, LayoutUpdate from .types import LayoutType, VdomJson @@ -74,7 +74,7 @@ def apply_to(self, model: VdomJson) -> VdomJson: @classmethod def create_from(cls, update: LayoutUpdate) -> VdomJsonPatch: """Return a patch given an layout update""" - return cls(update.path, make_patch(update.old or {}, update.new).patch) + return cls(update.path, [{"op": "replace", "path": "", "value": update.new}]) async def _single_outgoing_loop(