From 53e7a06a18a5cbe4138b415ece4b2b1522c95719 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Mon, 13 Nov 2023 20:21:37 +0100 Subject: [PATCH] Add Vue watch mixin to update a variable on changes to another. To be used where adding .sync to props is not convenient for one reason or another. --- lib/www/client/source/src/lib/watcher-mixin.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 lib/www/client/source/src/lib/watcher-mixin.js diff --git a/lib/www/client/source/src/lib/watcher-mixin.js b/lib/www/client/source/src/lib/watcher-mixin.js new file mode 100644 index 0000000..f0ab525 --- /dev/null +++ b/lib/www/client/source/src/lib/watcher-mixin.js @@ -0,0 +1,14 @@ +import { deepCompare } from './utils'; + +function setIfDifferent(propsLocals) { + return Object.fromEntries(Object.entries(propsLocals).map( ([prop, local]) => [ + local, + () => { + if (!deepCompare(this[prop], this[local])){ + this[local] = structuredClone(this[prop]); + } + } + ])); +} + +export default setIfDifferent;