UNPKG

vue-use-sync

Version:
2 lines (1 loc) 1.09 kB
(function(f,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],u):(f=typeof globalThis<"u"?globalThis:f||self,u(f.useSync={},f.Vue))})(this,function(f,u){"use strict";function h(n){return typeof n=="function"}function p(n,i){if(!(n instanceof Array)||!(i instanceof Array)||n.length<i.length)return!1;for(var s=0,o=i.length;s<o;s++)if(!n.includes(i[s]))return!1;return!0}function y(){const n=u.reactive({}),i=u.ref([]),s=new Map,o=new Set;u.watch(i.value,e=>{l()});function r(e){i.value.includes(e)?(v(e),l()):i.value.push(e)}function l(){s.forEach((e,t)=>{a(t)})}function a(e){if(p(i.value,e)){let t=u.reactive({});e.forEach(d=>{t[d]=n[d]});let c=s.get(e);if(o.has(e))return;c(t),o.add(e)}}function v(e){o.forEach(t=>{t.includes(e)&&o.delete(t)})}function S(e,t){h(t)?new Promise(t).then(c=>{n[e]=c,r(e)}).catch(c=>{n[e]=c,r(e)}):(n[e]=t,r(e))}function g(e,t){s.set(e,t),a(e)}return{track:S,trigger:g}}f.useSync=y,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});