UNPKG

relu-core

Version:
95 lines (78 loc) 2.14 kB
var base = require("./base"); var variable = require("./variable"); module.exports = function rpRestricted(origin, allowUpdate, allowSplice, allowNested) { var p = base(); p.constructor = rpRestricted; p._get = function() { p.depend(); return origin._getUndependend(); }; p._getUndependend = function() { return origin._getUndependend(); }; p._actionsList = function() { var originAl = origin._actionsList(); if(!originAl) return null; var al = originAl.atPath([]); if(!allowUpdate) al.update = function() { throw new Error("Update is not allowed"); }; if(!allowSplice) al.splice = function() { throw new Error("Array splicing is not allowed"); }; return al; }; p._actionsTarget = function() { return origin._actionsTarget(); }; p._element = function(idx) { if(!allowNested) return rpRestricted(origin._element(idx), allowNested, allowNested, allowNested); return origin._element(idx); }; p._property = function(idx) { if(!allowNested) return rpRestricted(origin._property(idx), allowNested, allowNested, allowNested); return origin._property(idx); }; p.isConst = function() { return origin.isConst(); }; if(allowUpdate && allowSplice) { p.writable = function() { return origin.writable(); }; } else { p._writable = false; } origin.onUpdated(onUpdate); origin.onAdded(onAdded); origin.onRemoved(onRemoved); origin.onChanged(onChanged); origin.onNested(onNested); origin.ref(p); p.onceDisposed(function() { origin.removeUpdatedListener(onUpdate); origin.removeAddedListener(onAdded); origin.removeRemovedListener(onRemoved); origin.removeChangedListener(onChanged); origin.removeNestedListener(onNested); origin.unref(p); }); function onChanged() { p._changed(); } function onNested(a, b, c, d) { p._nested(a, b, c, d); } function onUpdate(newValue, oldValue) { p._updated(newValue, oldValue); } function onAdded(idx, item) { p._added(idx, item); } function onRemoved(idx, item) { p._removed(idx, item); } return p; }