@politie/sherlock-proxy 3.4.15 → 3.4.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,370 +1,375 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@politie/sherlock')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@politie/sherlock'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SherlockProxy = {}, global.Sherlock));
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@politie/sherlock')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@politie/sherlock'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SherlockProxy = {}, global.Sherlock));
5
5
  })(this, (function (exports, sherlock) { 'use strict';
6
6
 
7
- /*! *****************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
-
22
- function __generator(thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- 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;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- }
7
+ /******************************************************************************
8
+ Copyright (c) Microsoft Corporation.
49
9
 
50
- var IS_DERIVABLE_PROXY = Symbol('isDerivableProxy');
51
- /**
52
- * Returns whether obj is a DerivableProxy.
53
- *
54
- * @param obj the object to test
55
- */
56
- function isDerivableProxy(obj) {
57
- return obj[IS_DERIVABLE_PROXY] === true;
58
- }
59
- /**
60
- * A ProxyDescriptor must be used to create DerivableProxies. It can be used in two ways, either create a new descriptor and
61
- * change any implementation details (if needed) or create a subclass to extend the behavior. Use the {@link #$create} method
62
- * to create a DerivableProxy.
63
- *
64
- * Note that `this` in methods points to the created proxy, so only methods and properties that start with a $-sign can be accessed
65
- * without trouble.
66
- *
67
- * Note also that properties that start with two $-signs are cleared on $create.
68
- */
69
- var ProxyDescriptor = /** @class */ (function () {
70
- function ProxyDescriptor() {
71
- this.$$derivable = undefined;
72
- }
73
- Object.defineProperty(ProxyDescriptor.prototype, "$derivable", {
74
- /**
75
- * The derivable that is the input to all default methods on the Proxy and the {@link #$value} property.
76
- */
77
- get: function () {
78
- var pd = this.$proxyDescriptor;
79
- return pd.$$derivable || (pd.$$derivable = createDerivable(pd.$target, pd.$lens && pd.$lens()));
80
- },
81
- enumerable: false,
82
- configurable: true
83
- });
84
- Object.defineProperty(ProxyDescriptor.prototype, "$value", {
85
- /**
86
- * The current value of the DerivableProxy. Can be expensive to calculate. When the target is settable (is an Atom) then $value
87
- * is writable.
88
- */
89
- get: function () {
90
- var pd = this.$proxyDescriptor;
91
- try {
92
- return pd.$derivable.get();
93
- }
94
- catch (e) {
95
- // istanbul ignore next: for debug purposes
96
- throw Object.assign(new Error("error while getting " + (pd.$expression || '$value') + ": " + (sherlock._internal.isError(e) && e.message)), { jse_cause: e });
97
- }
98
- },
99
- set: function (newValue) {
100
- var pd = this.$proxyDescriptor;
101
- var atom = pd.$derivable;
102
- var expression = pd.$expression;
103
- if (!sherlock.isSettableDerivable(atom)) {
104
- throw new Error((expression || '$value') + " is readonly");
105
- }
106
- try {
107
- atom.set(newValue);
108
- }
109
- catch (e) {
110
- throw Object.assign(new Error("error while setting " + (expression || '$value') + ": " + (sherlock._internal.isError(e) && e.message)), { jse_cause: e });
111
- }
112
- },
113
- enumerable: false,
114
- configurable: true
115
- });
116
- Object.defineProperty(ProxyDescriptor.prototype, "$targetValue", {
117
- /**
118
- * The current value of the target Derivable that was used to create the DerivableProxy.
119
- */
120
- get: function () {
121
- var pd = this.$proxyDescriptor;
122
- try {
123
- return pd.$target.get();
124
- }
125
- catch (e) {
126
- // istanbul ignore next: for debug purposes
127
- throw Object.assign(new Error("error while getting " + (pd.$expression || '$targetValue') + ": " + (sherlock._internal.isError(e) && e.message)), { jse_cause: e });
128
- }
129
- },
130
- set: function (newValue) {
131
- var pd = this.$proxyDescriptor;
132
- var atom = pd.$target;
133
- var expression = pd.$expression;
134
- if (!sherlock.isSettableDerivable(atom)) {
135
- throw new Error((expression || '$targetValue') + " is readonly");
136
- }
137
- try {
138
- atom.set(newValue);
139
- }
140
- catch (e) {
141
- throw Object.assign(new Error("error while setting " + (expression || '$targetValue') + ": " + (sherlock._internal.isError(e) && e.message)), { jse_cause: e });
142
- }
143
- },
144
- enumerable: false,
145
- configurable: true
146
- });
147
- Object.defineProperty(ProxyDescriptor.prototype, "$proxyDescriptor", {
148
- /**
149
- * In methods of a ProxyDescriptor, `this` is bound to the Proxy Object. Therefore, only $-properties and $-methods can be
150
- * accessed safely. Use $proxyDescriptor to get access to the ProxyDescriptor Object to prevent the ProxyHandler from messing
151
- * with your logic.
152
- */
153
- get: function () { return this; },
154
- enumerable: false,
155
- configurable: true
156
- });
157
- /**
158
- * Wrap a Derivable as DerivableProxy using this ProxyDescriptor.
159
- *
160
- * @param obj the object to wrap
161
- * @param expression the new expression to the created DerivableProxy
162
- * @param path the new path to the created DerivableProxy
163
- */
164
- ProxyDescriptor.prototype.$create = function (obj, expression, path) {
165
- var descriptor = sherlock.utils.clone(this.$proxyDescriptor);
166
- descriptor.$target = obj;
167
- Object.getOwnPropertyNames(descriptor)
168
- .filter(function (prop) { return prop.startsWith('$$'); })
169
- .forEach(function (prop) { return descriptor[prop] = undefined; });
170
- descriptor.$expression = expression;
171
- descriptor.$path = path;
172
- return new Proxy(descriptor, proxyHandler);
173
- };
174
- /**
175
- * The $pluck method is the implementation of the pluck mechanism of DerivableProxy. Replace this method to change the
176
- * pluck behavior. It should return a DerivableProxy.
177
- *
178
- * @param prop the property to pluck of the wrapped derivable
179
- */
180
- ProxyDescriptor.prototype.$pluck = function (prop) {
181
- var pd = this.$proxyDescriptor;
182
- return pd.$create(pd.$derivable.pluck(prop), extendExpression(pd.$expression, prop), extendPath(pd.$path, prop));
183
- };
184
- /**
185
- * The $pluckableKeys returns a list of properties that can be plucked from this object. Returned keys are guaranteed to
186
- * result in a usable DerivableProxy when used with $pluck. Is used for `for ... in` and `Object.keys(...)` logic.
187
- */
188
- ProxyDescriptor.prototype.$pluckableKeys = function () {
189
- var value = this.$proxyDescriptor.$value;
190
- return typeof value === 'object' ? Reflect.ownKeys(value) : [];
191
- };
192
- /**
193
- * Method that determines whether the current object is iterable and if so, how many elements it contains. During iteration
194
- * {@link #pluck} is called with indices up to but not including the result of `$length()`.
195
- */
196
- ProxyDescriptor.prototype.$length = function () {
197
- var maybeArray = this.$proxyDescriptor.$targetValue;
198
- return Array.isArray(maybeArray) ? maybeArray.length : undefined;
199
- };
200
- ProxyDescriptor.prototype.$and = function (other) {
201
- return this.$proxyDescriptor.$derivable.and(unwrapProxy(other));
202
- };
203
- ProxyDescriptor.prototype.$or = function (other) {
204
- return this.$proxyDescriptor.$derivable.or(unwrapProxy(other));
205
- };
206
- ProxyDescriptor.prototype.$not = function () {
207
- return this.$proxyDescriptor.$derivable.not();
208
- };
209
- ProxyDescriptor.prototype.$is = function (other) {
210
- return this.$proxyDescriptor.$derivable.is(unwrapProxy(other));
211
- };
212
- ProxyDescriptor.prototype.$derive = function () {
213
- var target = this.$proxyDescriptor.$derivable;
214
- return target.derive.apply(target, arguments);
215
- };
216
- ProxyDescriptor.prototype.$react = function (reaction, options) {
217
- return this.$proxyDescriptor.$derivable.react(reaction, options);
218
- };
219
- ProxyDescriptor.prototype.toJSON = function () {
220
- return this.$proxyDescriptor.$value;
221
- };
222
- Object.defineProperty(ProxyDescriptor.prototype, Symbol.toStringTag, {
223
- get: function () {
224
- return 'DerivableProxy';
225
- },
226
- enumerable: false,
227
- configurable: true
228
- });
229
- ProxyDescriptor.prototype[Symbol.iterator] = function () {
230
- var pd, length, expression, i;
231
- return __generator(this, function (_a) {
232
- switch (_a.label) {
233
- case 0:
234
- pd = this.$proxyDescriptor;
235
- length = pd.$length();
236
- if (length === undefined) {
237
- expression = pd.$expression;
238
- throw Object.assign(new Error((expression || 'object') + " is not iterable"), { value: pd.$value, expression: expression });
239
- }
240
- i = 0;
241
- _a.label = 1;
242
- case 1:
243
- if (!(i < length)) return [3 /*break*/, 4];
244
- return [4 /*yield*/, pd.$pluck(i)];
245
- case 2:
246
- _a.sent();
247
- _a.label = 3;
248
- case 3:
249
- i++;
250
- return [3 /*break*/, 1];
251
- case 4: return [2 /*return*/];
252
- }
253
- });
254
- };
255
- Object.defineProperty(ProxyDescriptor.prototype, "length", {
256
- get: function () {
257
- return this.$proxyDescriptor.$length();
258
- },
259
- enumerable: false,
260
- configurable: true
261
- });
262
- return ProxyDescriptor;
263
- }());
264
- ProxyDescriptor.prototype[IS_DERIVABLE_PROXY] = true;
265
- function createDerivable(target, proxyLens) {
266
- if (!proxyLens) {
267
- return target;
268
- }
269
- var get = proxyLens.get, set = proxyLens.set;
270
- if (!set || !sherlock.isSettableDerivable(target)) {
271
- return target.derive(get).autoCache();
272
- }
273
- return sherlock.lens({
274
- get: get,
275
- set: function (newValue, targetValue) {
276
- target.set(set.call(this, newValue, targetValue));
277
- }
278
- }, target).autoCache();
279
- }
280
- function unwrapProxy(obj) {
281
- if (isDerivableProxy(obj)) {
282
- return obj.$derivable;
283
- }
284
- return obj;
285
- }
286
- var proxyHandler = {
287
- get: function (target, prop, receiver) {
288
- if (prop === '$proxyDescriptor') {
289
- return target;
290
- }
291
- if (isPluckableProperty(target, prop)) {
292
- return target.$pluck.call(receiver, prop);
293
- }
294
- return Reflect.get(target, prop, receiver);
295
- },
296
- set: function (target, prop, newValue, receiver) {
297
- if (isPluckableProperty(target, prop)) {
298
- var plucked = target.$pluck.call(receiver, prop);
299
- if (newValue && isDerivableProxy(newValue)) {
300
- plucked.$targetValue = newValue.$targetValue;
301
- }
302
- else {
303
- plucked.$value = newValue && sherlock.isDerivable(newValue) ? newValue.get() : newValue;
304
- }
305
- return true;
306
- }
307
- return Reflect.set(target, prop, newValue, receiver);
308
- },
309
- has: function (target, prop) {
310
- if (prop === Symbol.iterator) {
311
- return target.$length() !== undefined;
312
- }
313
- return isPluckableProperty(target, prop);
314
- },
315
- getOwnPropertyDescriptor: function (target, prop) {
316
- if (isPluckableProperty(target, prop)) {
317
- return {
318
- get: function () { return this[prop]; },
319
- set: function (newValue) { this[prop] = newValue; },
320
- configurable: true,
321
- enumerable: true,
322
- };
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+
22
+ function __generator(thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
323
43
  }
324
- return undefined;
325
- },
326
- ownKeys: function (target) {
327
- return target.$pluckableKeys();
328
- },
329
- };
330
- function isPluckableProperty(target, prop) {
331
- return typeof prop === 'number' || typeof prop === 'string' && prop[0] !== '$' && !Reflect.has(target, prop);
332
- }
333
- /**
334
- * Extends an expression with a property access. Automatically uses bracket notation where appropriate and escapes
335
- * strings in brackets to give a realistic combined expression.
336
- *
337
- * @param expression the (optional) expression to extend
338
- * @param property the property that should be appended to the expression
339
- */
340
- function extendExpression(expression, property) {
341
- if (expression === void 0) { expression = ''; }
342
- if (typeof property === 'string' && /^[a-z_][a-z_0-9]*$/i.test(property)) {
343
- return expression + '.' + property;
344
- }
345
- if (typeof property === 'string') {
346
- return expression + '["' + property.replace(/\\/g, '\\\\').replace(/\"/g, '\\"') + '"]';
347
- }
348
- return expression + '[' + property + ']';
349
- }
350
- /**
351
- * Extends a path with a property access.
352
- *
353
- * @param path the (optional) path to extend
354
- * @param property the property that should be appended to the path
355
- */
356
- function extendPath(path, property) {
357
- if (path === void 0) { path = []; }
358
- return path.concat(property);
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
359
47
  }
48
+ }
49
+
50
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
51
+ var e = new Error(message);
52
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
53
+ };
54
+
55
+ var IS_DERIVABLE_PROXY = Symbol('isDerivableProxy');
56
+ /**
57
+ * Returns whether obj is a DerivableProxy.
58
+ *
59
+ * @param obj the object to test
60
+ */
61
+ function isDerivableProxy(obj) {
62
+ return obj[IS_DERIVABLE_PROXY] === true;
63
+ }
64
+ /**
65
+ * A ProxyDescriptor must be used to create DerivableProxies. It can be used in two ways, either create a new descriptor and
66
+ * change any implementation details (if needed) or create a subclass to extend the behavior. Use the {@link #$create} method
67
+ * to create a DerivableProxy.
68
+ *
69
+ * Note that `this` in methods points to the created proxy, so only methods and properties that start with a $-sign can be accessed
70
+ * without trouble.
71
+ *
72
+ * Note also that properties that start with two $-signs are cleared on $create.
73
+ */
74
+ var ProxyDescriptor = /** @class */ (function () {
75
+ function ProxyDescriptor() {
76
+ this.$$derivable = undefined;
77
+ }
78
+ Object.defineProperty(ProxyDescriptor.prototype, "$derivable", {
79
+ /**
80
+ * The derivable that is the input to all default methods on the Proxy and the {@link #$value} property.
81
+ */
82
+ get: function () {
83
+ var pd = this.$proxyDescriptor;
84
+ return pd.$$derivable || (pd.$$derivable = createDerivable(pd.$target, pd.$lens && pd.$lens()));
85
+ },
86
+ enumerable: false,
87
+ configurable: true
88
+ });
89
+ Object.defineProperty(ProxyDescriptor.prototype, "$value", {
90
+ /**
91
+ * The current value of the DerivableProxy. Can be expensive to calculate. When the target is settable (is an Atom) then $value
92
+ * is writable.
93
+ */
94
+ get: function () {
95
+ var pd = this.$proxyDescriptor;
96
+ try {
97
+ return pd.$derivable.get();
98
+ }
99
+ catch (e) {
100
+ // istanbul ignore next: for debug purposes
101
+ throw Object.assign(new Error("error while getting ".concat(pd.$expression || '$value', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
102
+ }
103
+ },
104
+ set: function (newValue) {
105
+ var pd = this.$proxyDescriptor;
106
+ var atom = pd.$derivable;
107
+ var expression = pd.$expression;
108
+ if (!sherlock.isSettableDerivable(atom)) {
109
+ throw new Error("".concat(expression || '$value', " is readonly"));
110
+ }
111
+ try {
112
+ atom.set(newValue);
113
+ }
114
+ catch (e) {
115
+ throw Object.assign(new Error("error while setting ".concat(expression || '$value', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
116
+ }
117
+ },
118
+ enumerable: false,
119
+ configurable: true
120
+ });
121
+ Object.defineProperty(ProxyDescriptor.prototype, "$targetValue", {
122
+ /**
123
+ * The current value of the target Derivable that was used to create the DerivableProxy.
124
+ */
125
+ get: function () {
126
+ var pd = this.$proxyDescriptor;
127
+ try {
128
+ return pd.$target.get();
129
+ }
130
+ catch (e) {
131
+ // istanbul ignore next: for debug purposes
132
+ throw Object.assign(new Error("error while getting ".concat(pd.$expression || '$targetValue', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
133
+ }
134
+ },
135
+ set: function (newValue) {
136
+ var pd = this.$proxyDescriptor;
137
+ var atom = pd.$target;
138
+ var expression = pd.$expression;
139
+ if (!sherlock.isSettableDerivable(atom)) {
140
+ throw new Error("".concat(expression || '$targetValue', " is readonly"));
141
+ }
142
+ try {
143
+ atom.set(newValue);
144
+ }
145
+ catch (e) {
146
+ throw Object.assign(new Error("error while setting ".concat(expression || '$targetValue', ": ").concat(sherlock._internal.isError(e) && e.message)), { jse_cause: e });
147
+ }
148
+ },
149
+ enumerable: false,
150
+ configurable: true
151
+ });
152
+ Object.defineProperty(ProxyDescriptor.prototype, "$proxyDescriptor", {
153
+ /**
154
+ * In methods of a ProxyDescriptor, `this` is bound to the Proxy Object. Therefore, only $-properties and $-methods can be
155
+ * accessed safely. Use $proxyDescriptor to get access to the ProxyDescriptor Object to prevent the ProxyHandler from messing
156
+ * with your logic.
157
+ */
158
+ get: function () { return this; },
159
+ enumerable: false,
160
+ configurable: true
161
+ });
162
+ /**
163
+ * Wrap a Derivable as DerivableProxy using this ProxyDescriptor.
164
+ *
165
+ * @param obj the object to wrap
166
+ * @param expression the new expression to the created DerivableProxy
167
+ * @param path the new path to the created DerivableProxy
168
+ */
169
+ ProxyDescriptor.prototype.$create = function (obj, expression, path) {
170
+ var descriptor = sherlock.utils.clone(this.$proxyDescriptor);
171
+ descriptor.$target = obj;
172
+ Object.getOwnPropertyNames(descriptor)
173
+ .filter(function (prop) { return prop.startsWith('$$'); })
174
+ .forEach(function (prop) { return descriptor[prop] = undefined; });
175
+ descriptor.$expression = expression;
176
+ descriptor.$path = path;
177
+ return new Proxy(descriptor, proxyHandler);
178
+ };
179
+ /**
180
+ * The $pluck method is the implementation of the pluck mechanism of DerivableProxy. Replace this method to change the
181
+ * pluck behavior. It should return a DerivableProxy.
182
+ *
183
+ * @param prop the property to pluck of the wrapped derivable
184
+ */
185
+ ProxyDescriptor.prototype.$pluck = function (prop) {
186
+ var pd = this.$proxyDescriptor;
187
+ return pd.$create(pd.$derivable.pluck(prop), extendExpression(pd.$expression, prop), extendPath(pd.$path, prop));
188
+ };
189
+ /**
190
+ * The $pluckableKeys returns a list of properties that can be plucked from this object. Returned keys are guaranteed to
191
+ * result in a usable DerivableProxy when used with $pluck. Is used for `for ... in` and `Object.keys(...)` logic.
192
+ */
193
+ ProxyDescriptor.prototype.$pluckableKeys = function () {
194
+ var value = this.$proxyDescriptor.$value;
195
+ return typeof value === 'object' ? Reflect.ownKeys(value) : [];
196
+ };
197
+ /**
198
+ * Method that determines whether the current object is iterable and if so, how many elements it contains. During iteration
199
+ * {@link #pluck} is called with indices up to but not including the result of `$length()`.
200
+ */
201
+ ProxyDescriptor.prototype.$length = function () {
202
+ var maybeArray = this.$proxyDescriptor.$targetValue;
203
+ return Array.isArray(maybeArray) ? maybeArray.length : undefined;
204
+ };
205
+ ProxyDescriptor.prototype.$and = function (other) {
206
+ return this.$proxyDescriptor.$derivable.and(unwrapProxy(other));
207
+ };
208
+ ProxyDescriptor.prototype.$or = function (other) {
209
+ return this.$proxyDescriptor.$derivable.or(unwrapProxy(other));
210
+ };
211
+ ProxyDescriptor.prototype.$not = function () {
212
+ return this.$proxyDescriptor.$derivable.not();
213
+ };
214
+ ProxyDescriptor.prototype.$is = function (other) {
215
+ return this.$proxyDescriptor.$derivable.is(unwrapProxy(other));
216
+ };
217
+ ProxyDescriptor.prototype.$derive = function () {
218
+ var target = this.$proxyDescriptor.$derivable;
219
+ return target.derive.apply(target, arguments);
220
+ };
221
+ ProxyDescriptor.prototype.$react = function (reaction, options) {
222
+ return this.$proxyDescriptor.$derivable.react(reaction, options);
223
+ };
224
+ ProxyDescriptor.prototype.toJSON = function () {
225
+ return this.$proxyDescriptor.$value;
226
+ };
227
+ Object.defineProperty(ProxyDescriptor.prototype, Symbol.toStringTag, {
228
+ get: function () {
229
+ return 'DerivableProxy';
230
+ },
231
+ enumerable: false,
232
+ configurable: true
233
+ });
234
+ ProxyDescriptor.prototype[Symbol.iterator] = function () {
235
+ var pd, length, expression, i;
236
+ return __generator(this, function (_a) {
237
+ switch (_a.label) {
238
+ case 0:
239
+ pd = this.$proxyDescriptor;
240
+ length = pd.$length();
241
+ if (length === undefined) {
242
+ expression = pd.$expression;
243
+ throw Object.assign(new Error("".concat(expression || 'object', " is not iterable")), { value: pd.$value, expression: expression });
244
+ }
245
+ i = 0;
246
+ _a.label = 1;
247
+ case 1:
248
+ if (!(i < length)) return [3 /*break*/, 4];
249
+ return [4 /*yield*/, pd.$pluck(i)];
250
+ case 2:
251
+ _a.sent();
252
+ _a.label = 3;
253
+ case 3:
254
+ i++;
255
+ return [3 /*break*/, 1];
256
+ case 4: return [2 /*return*/];
257
+ }
258
+ });
259
+ };
260
+ Object.defineProperty(ProxyDescriptor.prototype, "length", {
261
+ get: function () {
262
+ return this.$proxyDescriptor.$length();
263
+ },
264
+ enumerable: false,
265
+ configurable: true
266
+ });
267
+ return ProxyDescriptor;
268
+ }());
269
+ ProxyDescriptor.prototype[IS_DERIVABLE_PROXY] = true;
270
+ function createDerivable(target, proxyLens) {
271
+ if (!proxyLens) {
272
+ return target;
273
+ }
274
+ var get = proxyLens.get, set = proxyLens.set;
275
+ if (!set || !sherlock.isSettableDerivable(target)) {
276
+ return target.derive(get).autoCache();
277
+ }
278
+ return sherlock.lens({
279
+ get: get,
280
+ set: function (newValue, targetValue) {
281
+ target.set(set.call(this, newValue, targetValue));
282
+ }
283
+ }, target).autoCache();
284
+ }
285
+ function unwrapProxy(obj) {
286
+ if (isDerivableProxy(obj)) {
287
+ return obj.$derivable;
288
+ }
289
+ return obj;
290
+ }
291
+ var proxyHandler = {
292
+ get: function (target, prop, receiver) {
293
+ if (prop === '$proxyDescriptor') {
294
+ return target;
295
+ }
296
+ if (isPluckableProperty(target, prop)) {
297
+ return target.$pluck.call(receiver, prop);
298
+ }
299
+ return Reflect.get(target, prop, receiver);
300
+ },
301
+ set: function (target, prop, newValue, receiver) {
302
+ if (isPluckableProperty(target, prop)) {
303
+ var plucked = target.$pluck.call(receiver, prop);
304
+ if (newValue && isDerivableProxy(newValue)) {
305
+ plucked.$targetValue = newValue.$targetValue;
306
+ }
307
+ else {
308
+ plucked.$value = newValue && sherlock.isDerivable(newValue) ? newValue.get() : newValue;
309
+ }
310
+ return true;
311
+ }
312
+ return Reflect.set(target, prop, newValue, receiver);
313
+ },
314
+ has: function (target, prop) {
315
+ if (prop === Symbol.iterator) {
316
+ return target.$length() !== undefined;
317
+ }
318
+ return isPluckableProperty(target, prop);
319
+ },
320
+ getOwnPropertyDescriptor: function (target, prop) {
321
+ if (isPluckableProperty(target, prop)) {
322
+ return {
323
+ get: function () { return this[prop]; },
324
+ set: function (newValue) { this[prop] = newValue; },
325
+ configurable: true,
326
+ enumerable: true,
327
+ };
328
+ }
329
+ return undefined;
330
+ },
331
+ ownKeys: function (target) {
332
+ return target.$pluckableKeys();
333
+ },
334
+ };
335
+ function isPluckableProperty(target, prop) {
336
+ return typeof prop === 'number' || typeof prop === 'string' && prop[0] !== '$' && !Reflect.has(target, prop);
337
+ }
338
+ /**
339
+ * Extends an expression with a property access. Automatically uses bracket notation where appropriate and escapes
340
+ * strings in brackets to give a realistic combined expression.
341
+ *
342
+ * @param expression the (optional) expression to extend
343
+ * @param property the property that should be appended to the expression
344
+ */
345
+ function extendExpression(expression, property) {
346
+ if (expression === void 0) { expression = ''; }
347
+ if (typeof property === 'string' && /^[a-z_][a-z_0-9]*$/i.test(property)) {
348
+ return expression + '.' + property;
349
+ }
350
+ if (typeof property === 'string') {
351
+ return expression + '["' + property.replace(/\\/g, '\\\\').replace(/\"/g, '\\"') + '"]';
352
+ }
353
+ return expression + '[' + property + ']';
354
+ }
355
+ /**
356
+ * Extends a path with a property access.
357
+ *
358
+ * @param path the (optional) path to extend
359
+ * @param property the property that should be appended to the path
360
+ */
361
+ function extendPath(path, property) {
362
+ if (path === void 0) { path = []; }
363
+ return path.concat(property);
364
+ }
360
365
 
361
- exports.ProxyDescriptor = ProxyDescriptor;
362
- exports.extendExpression = extendExpression;
363
- exports.extendPath = extendPath;
364
- exports.isDerivableProxy = isDerivableProxy;
365
- exports.unwrapProxy = unwrapProxy;
366
+ exports.ProxyDescriptor = ProxyDescriptor;
367
+ exports.extendExpression = extendExpression;
368
+ exports.extendPath = extendPath;
369
+ exports.isDerivableProxy = isDerivableProxy;
370
+ exports.unwrapProxy = unwrapProxy;
366
371
 
367
- Object.defineProperty(exports, '__esModule', { value: true });
372
+ Object.defineProperty(exports, '__esModule', { value: true });
368
373
 
369
374
  }));
370
375
  //# sourceMappingURL=sherlock-proxy.umd.js.map