UNPKG

vue-composition-api-proxied

Version:

A proxy-based alternative to reactive in Vue 2 / Vue Composition API

61 lines 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.noProxy = exports.proxied = exports.skips = exports.isProxied = void 0; const proxyKey = '__is_proxy'; const noProxyKey = '__no_proxy'; const peerDependencies = { Vue: typeof Vue === 'undefined' ? { set: (a, b, c) => a[b] = c } : Vue, VueCompositionAPI: typeof VueCompositionAPI === 'undefined' ? { reactive: (a) => a } : VueCompositionAPI, }; const proxyConfig = { get: function (target, prop) { if (prop === proxyKey) return true; return target[prop]; }, set: function (target, prop, value) { var _a, _b, _c; target[prop] = exports.proxied(value); (_c = (_b = (_a = target === null || target === void 0 ? void 0 : target.__ob__) === null || _a === void 0 ? void 0 : _a.dep) === null || _b === void 0 ? void 0 : _b.notify) === null || _c === void 0 ? void 0 : _c.call(_b); return true; } }; const isProxied = (obj) => { return obj && obj[proxyKey] === true; }; exports.isProxied = isProxied; exports.skips = [ { name: 'Function', test: obj => obj instanceof Function, example: () => null }, { name: 'Date', test: obj => obj instanceof Date, example: new Date() }, { name: 'RegExp //', test: obj => obj instanceof RegExp, example: /foo/ }, { name: 'RegExp Class', test: obj => obj instanceof RegExp, example: new RegExp('foo') }, { name: 'ArrayBuffer', test: obj => obj instanceof ArrayBuffer, example: new ArrayBuffer(0) }, { name: 'Map', test: obj => obj instanceof Map, example: new Map() }, { name: 'Set', test: obj => obj instanceof Set, example: new Set() }, { name: 'Proxy', test: obj => exports.isProxied(obj), example: new Proxy({}, {}) }, ]; const proxied = (obj) => { if (!obj) return obj; if (typeof obj !== 'object') return obj; if (obj[noProxyKey] === true) return obj; const proto = Object.prototype.toString.call(obj); if (proto == '[object Promise]') return obj; const skip = exports.skips.find(s => s.test(obj)); if (skip) return obj; for (const key in obj) { obj[key] = exports.proxied(obj[key]); } return peerDependencies.VueCompositionAPI.reactive(new Proxy(obj, proxyConfig)); }; exports.proxied = proxied; const noProxy = (obj) => { Object.defineProperty(obj, noProxyKey, { value: true, enumerable: false }); return obj; }; exports.noProxy = noProxy; //# sourceMappingURL=index.js.map