UNPKG

fluidstate

Version:

Library for fine-grained reactivity state management

58 lines (55 loc) 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.simplePrototypeList = exports.removeSimplePrototype = exports.isSimpleObject = exports.getChildOptions = exports.getChild = exports.addSimplePrototype = void 0; var _reactiveUtils = require("./reactive-utils"); const simplePrototypeList = new WeakSet(); /** * Registers a prototype object to be considered "simple". * * Objects whose prototype is in this list will be treated by `isSimpleObject` * as candidates for deep reactivity by default. This allows custom classes or * objects with specific prototypes to be automatically wrapped in reactive proxies * when encountered within a larger reactive structure, assuming `deep` reactivity * is enabled. * * @param simplePrototype The prototype object to register. */ exports.simplePrototypeList = simplePrototypeList; const addSimplePrototype = simplePrototype => { simplePrototypeList.add(simplePrototype); }; /** * Unregisters a prototype object, so it's no longer considered "simple". * * This is the counterpart to `addSimplePrototype`. Objects whose prototype was * previously registered will no longer be identified as "simple" by default * by `isSimpleObject` after being removed. * * @param simplePrototype The prototype object to unregister. */ exports.addSimplePrototype = addSimplePrototype; const removeSimplePrototype = simplePrototype => { simplePrototypeList.delete(simplePrototype); }; exports.removeSimplePrototype = removeSimplePrototype; const isSimpleObject = object => { return Array.isArray(object) || (0, _reactiveUtils.isObject)(object) && (object instanceof Set || object instanceof Map || object instanceof Promise || Object.getPrototypeOf(object) === Object.prototype || Object.getPrototypeOf(object) === null || simplePrototypeList.has(Object.getPrototypeOf(object))); }; exports.isSimpleObject = isSimpleObject; const getChildOptions = parentContext => { return { plugins: parentContext.reactiveOptions?.plugins }; }; exports.getChildOptions = getChildOptions; const getChild = (context, object, createProxy) => { if (context.reactiveOptions?.deep === false || !isSimpleObject(object)) { return object; } else { return createProxy(); } }; exports.getChild = getChild; //# sourceMappingURL=reactive-children.js.map