vuestic-ui
Version:
Vue 3 UI Framework
1 lines • 1.12 kB
Source Map (JSON)
{"version":3,"file":"watch-setter.mjs","sources":["../../../../src/utils/watch-setter.ts"],"sourcesContent":["import { ComputedRef, Ref } from 'vue'\n\nconst isComputedRef = <T>(value: Ref<T>): value is ComputedRef<any> & { _setter: (v: T) => void} => {\n return typeof value === 'object' && '_setter' in value\n}\n\n// TODO: Maybe it is better to tweak useStateful\n/**\n * Do not watches for effect, but looking for computed ref setter triggered.\n * Used to track when component tries to update computed ref.\n *\n * @notice you likely want to watch when value is changed, not setter is called.\n */\nexport const watchSetter = <T>(ref: Ref<T>, cb: (newValue: T) => void) => {\n if (!isComputedRef(ref)) { return }\n const originalSetter = ref._setter\n\n ref._setter = (newValue: T) => {\n cb(newValue)\n originalSetter(newValue)\n }\n}\n"],"names":[],"mappings":"AAEA,MAAM,gBAAgB,CAAI,UAA0E;AAC3F,SAAA,OAAO,UAAU,YAAY,aAAa;AACnD;AASa,MAAA,cAAc,CAAI,KAAa,OAA8B;AACpE,MAAA,CAAC,cAAc,GAAG,GAAG;AAAE;AAAA,EAAO;AAClC,QAAM,iBAAiB,IAAI;AAEvB,MAAA,UAAU,CAAC,aAAgB;AAC7B,OAAG,QAAQ;AACX,mBAAe,QAAQ;AAAA,EAAA;AAE3B;"}