@politie/sherlock-proxy
Version:
A proxy extension to Sherlock.
376 lines (366 loc) • 16.7 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@politie/sherlock')) :
typeof define === 'function' && define.amd ? define(['exports', '@politie/sherlock'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SherlockProxy = {}, global.Sherlock));
})(this, (function (exports, sherlock) { 'use strict';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
var IS_DERIVABLE_PROXY = Symbol('isDerivableProxy');
/**
* Returns whether obj is a DerivableProxy.
*
* @param obj the object to test
*/
function isDerivableProxy(obj) {
return obj[IS_DERIVABLE_PROXY] === true;
}
/**
* A ProxyDescriptor must be used to create DerivableProxies. It can be used in two ways, either create a new descriptor and
* change any implementation details (if needed) or create a subclass to extend the behavior. Use the {@link #$create} method
* to create a DerivableProxy.
*
* Note that `this` in methods points to the created proxy, so only methods and properties that start with a $-sign can be accessed
* without trouble.
*
* Note also that properties that start with two $-signs are cleared on $create.
*/
var ProxyDescriptor = /** @class */ (function () {
function ProxyDescriptor() {
this.$$derivable = undefined;
}
Object.defineProperty(ProxyDescriptor.prototype, "$derivable", {
/**
* The derivable that is the input to all default methods on the Proxy and the {@link #$value} property.
*/
get: function () {
var pd = this.$proxyDescriptor;
return pd.$$derivable || (pd.$$derivable = createDerivable(pd.$target, pd.$lens && pd.$lens()));
},
enumerable: false,
configurable: true
});
Object.defineProperty(ProxyDescriptor.prototype, "$value", {
/**
* The current value of the DerivableProxy. Can be expensive to calculate. When the target is settable (is an Atom) then $value
* is writable.
*/
get: function () {
var pd = this.$proxyDescriptor;
try {
return pd.$derivable.get();
}
catch (e) {
// istanbul ignore next: for debug purposes
throw Object.assign(new Error("error while getting ".concat(pd.$expression || '$value', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
}
},
set: function (newValue) {
var pd = this.$proxyDescriptor;
var atom = pd.$derivable;
var expression = pd.$expression;
if (!sherlock.isSettableDerivable(atom)) {
throw new Error("".concat(expression || '$value', " is readonly"));
}
try {
atom.set(newValue);
}
catch (e) {
throw Object.assign(new Error("error while setting ".concat(expression || '$value', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(ProxyDescriptor.prototype, "$targetValue", {
/**
* The current value of the target Derivable that was used to create the DerivableProxy.
*/
get: function () {
var pd = this.$proxyDescriptor;
try {
return pd.$target.get();
}
catch (e) {
// istanbul ignore next: for debug purposes
throw Object.assign(new Error("error while getting ".concat(pd.$expression || '$targetValue', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
}
},
set: function (newValue) {
var pd = this.$proxyDescriptor;
var atom = pd.$target;
var expression = pd.$expression;
if (!sherlock.isSettableDerivable(atom)) {
throw new Error("".concat(expression || '$targetValue', " is readonly"));
}
try {
atom.set(newValue);
}
catch (e) {
throw Object.assign(new Error("error while setting ".concat(expression || '$targetValue', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(ProxyDescriptor.prototype, "$proxyDescriptor", {
/**
* In methods of a ProxyDescriptor, `this` is bound to the Proxy Object. Therefore, only $-properties and $-methods can be
* accessed safely. Use $proxyDescriptor to get access to the ProxyDescriptor Object to prevent the ProxyHandler from messing
* with your logic.
*/
get: function () { return this; },
enumerable: false,
configurable: true
});
/**
* Wrap a Derivable as DerivableProxy using this ProxyDescriptor.
*
* @param obj the object to wrap
* @param expression the new expression to the created DerivableProxy
* @param path the new path to the created DerivableProxy
*/
ProxyDescriptor.prototype.$create = function (obj, expression, path) {
var descriptor = sherlock.utils.clone(this.$proxyDescriptor);
descriptor.$target = obj;
Object.getOwnPropertyNames(descriptor)
.filter(function (prop) { return prop.startsWith('$$'); })
.forEach(function (prop) { return descriptor[prop] = undefined; });
descriptor.$expression = expression;
descriptor.$path = path;
return new Proxy(descriptor, proxyHandler);
};
/**
* The $pluck method is the implementation of the pluck mechanism of DerivableProxy. Replace this method to change the
* pluck behavior. It should return a DerivableProxy.
*
* @param prop the property to pluck of the wrapped derivable
*/
ProxyDescriptor.prototype.$pluck = function (prop) {
var pd = this.$proxyDescriptor;
return pd.$create(pd.$derivable.pluck(prop), extendExpression(pd.$expression, prop), extendPath(pd.$path, prop));
};
/**
* The $pluckableKeys returns a list of properties that can be plucked from this object. Returned keys are guaranteed to
* result in a usable DerivableProxy when used with $pluck. Is used for `for ... in` and `Object.keys(...)` logic.
*/
ProxyDescriptor.prototype.$pluckableKeys = function () {
var value = this.$proxyDescriptor.$value;
return typeof value === 'object' ? Reflect.ownKeys(value) : [];
};
/**
* Method that determines whether the current object is iterable and if so, how many elements it contains. During iteration
* {@link #pluck} is called with indices up to but not including the result of `$length()`.
*/
ProxyDescriptor.prototype.$length = function () {
var maybeArray = this.$proxyDescriptor.$targetValue;
return Array.isArray(maybeArray) ? maybeArray.length : undefined;
};
ProxyDescriptor.prototype.$and = function (other) {
return this.$proxyDescriptor.$derivable.and(unwrapProxy(other));
};
ProxyDescriptor.prototype.$or = function (other) {
return this.$proxyDescriptor.$derivable.or(unwrapProxy(other));
};
ProxyDescriptor.prototype.$not = function () {
return this.$proxyDescriptor.$derivable.not();
};
ProxyDescriptor.prototype.$is = function (other) {
return this.$proxyDescriptor.$derivable.is(unwrapProxy(other));
};
ProxyDescriptor.prototype.$derive = function () {
var target = this.$proxyDescriptor.$derivable;
return target.derive.apply(target, arguments);
};
ProxyDescriptor.prototype.$react = function (reaction, options) {
return this.$proxyDescriptor.$derivable.react(reaction, options);
};
ProxyDescriptor.prototype.toJSON = function () {
return this.$proxyDescriptor.$value;
};
Object.defineProperty(ProxyDescriptor.prototype, Symbol.toStringTag, {
get: function () {
return 'DerivableProxy';
},
enumerable: false,
configurable: true
});
ProxyDescriptor.prototype[Symbol.iterator] = function () {
var pd, length, expression, i;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
pd = this.$proxyDescriptor;
length = pd.$length();
if (length === undefined) {
expression = pd.$expression;
throw Object.assign(new Error("".concat(expression || 'object', " is not iterable")), { value: pd.$value, expression: expression });
}
i = 0;
_a.label = 1;
case 1:
if (!(i < length)) return [3 /*break*/, 4];
return [4 /*yield*/, pd.$pluck(i)];
case 2:
_a.sent();
_a.label = 3;
case 3:
i++;
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
};
Object.defineProperty(ProxyDescriptor.prototype, "length", {
get: function () {
return this.$proxyDescriptor.$length();
},
enumerable: false,
configurable: true
});
return ProxyDescriptor;
}());
ProxyDescriptor.prototype[IS_DERIVABLE_PROXY] = true;
function createDerivable(target, proxyLens) {
if (!proxyLens) {
return target;
}
var get = proxyLens.get, set = proxyLens.set;
if (!set || !sherlock.isSettableDerivable(target)) {
return target.derive(get).autoCache();
}
return sherlock.lens({
get: get,
set: function (newValue, targetValue) {
target.set(set.call(this, newValue, targetValue));
}
}, target).autoCache();
}
function unwrapProxy(obj) {
if (isDerivableProxy(obj)) {
return obj.$derivable;
}
return obj;
}
var proxyHandler = {
get: function (target, prop, receiver) {
if (prop === '$proxyDescriptor') {
return target;
}
if (isPluckableProperty(target, prop)) {
return target.$pluck.call(receiver, prop);
}
return Reflect.get(target, prop, receiver);
},
set: function (target, prop, newValue, receiver) {
if (isPluckableProperty(target, prop)) {
var plucked = target.$pluck.call(receiver, prop);
if (newValue && isDerivableProxy(newValue)) {
plucked.$targetValue = newValue.$targetValue;
}
else {
plucked.$value = newValue && sherlock.isDerivable(newValue) ? newValue.get() : newValue;
}
return true;
}
return Reflect.set(target, prop, newValue, receiver);
},
has: function (target, prop) {
if (prop === Symbol.iterator) {
return target.$length() !== undefined;
}
return isPluckableProperty(target, prop);
},
getOwnPropertyDescriptor: function (target, prop) {
if (isPluckableProperty(target, prop)) {
return {
get: function () { return this[prop]; },
set: function (newValue) { this[prop] = newValue; },
configurable: true,
enumerable: true,
};
}
return undefined;
},
ownKeys: function (target) {
return target.$pluckableKeys();
},
};
function isPluckableProperty(target, prop) {
return typeof prop === 'number' || typeof prop === 'string' && prop[0] !== '$' && !Reflect.has(target, prop);
}
/**
* Extends an expression with a property access. Automatically uses bracket notation where appropriate and escapes
* strings in brackets to give a realistic combined expression.
*
* @param expression the (optional) expression to extend
* @param property the property that should be appended to the expression
*/
function extendExpression(expression, property) {
if (expression === void 0) { expression = ''; }
if (typeof property === 'string' && /^[a-z_][a-z_0-9]*$/i.test(property)) {
return expression + '.' + property;
}
if (typeof property === 'string') {
return expression + '["' + property.replace(/\\/g, '\\\\').replace(/\"/g, '\\"') + '"]';
}
return expression + '[' + property + ']';
}
/**
* Extends a path with a property access.
*
* @param path the (optional) path to extend
* @param property the property that should be appended to the path
*/
function extendPath(path, property) {
if (path === void 0) { path = []; }
return path.concat(property);
}
exports.ProxyDescriptor = ProxyDescriptor;
exports.extendExpression = extendExpression;
exports.extendPath = extendPath;
exports.isDerivableProxy = isDerivableProxy;
exports.unwrapProxy = unwrapProxy;
Object.defineProperty(exports, '__esModule', { value: true });
}));
//# sourceMappingURL=sherlock-proxy.umd.js.map