proxy.js
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.softMixProtos = exports.proxyMix = exports.getIngredientWithProp = void 0;
const util_1 = require("./util");
/**
* Finds the ingredient with the given prop, searching in reverse order and breadth-first if searching ingredient
* prototypes is required.
*/
const getIngredientWithProp = (prop, ingredients) => {
const protoChains = ingredients.map(ingredient => util_1.protoChain(ingredient));
// since we search breadth-first, we need to keep track of our depth in the prototype chains
let protoDepth = 0;
// not all prototype chains are the same depth, so this remains true as long as at least one of the ingredients'
// prototype chains has an object at this depth
let protosAreLeftToSearch = true;
while (protosAreLeftToSearch) {
// with the start of each horizontal slice, we assume this is the one that's deeper than any of the proto chains
protosAreLeftToSearch = false;
// scan through the ingredients right to left
for (let i = ingredients.length - 1; i >= 0; i--) {
const searchTarget = protoChains[i][protoDepth];
if (searchTarget !== undefined && searchTarget !== null) {
// if we find something, this is proof that this horizontal slice potentially more objects to search
protosAreLeftToSearch = true;
// eureka, we found it
if (Object.getOwnPropertyDescriptor(searchTarget, prop) != undefined) {
return protoChains[i][0];
}
}
}
protoDepth++;
}
return undefined;
};
exports.getIngredientWithProp = getIngredientWithProp;
/**
* "Mixes" ingredients by wrapping them in a Proxy. The optional prototype argument allows the mixed object to sit
* downstream of an existing prototype chain. Note that "properties" cannot be added, deleted, or modified.
*/
const proxyMix = (ingredients, prototype = Object.prototype) => new Proxy({}, {
getPrototypeOf() {
return prototype;
},
setPrototypeOf() {
throw Error('Cannot set prototype of Proxies created by ts-mixer');
},
getOwnPropertyDescriptor(_, prop) {
return Object.getOwnPropertyDescriptor(exports.getIngredientWithProp(prop, ingredients) || {}, prop);
},
defineProperty() {
throw new Error('Cannot define new properties on Proxies created by ts-mixer');
},
has(_, prop) {
return exports.getIngredientWithProp(prop, ingredients) !== undefined || prototype[prop] !== undefined;
},
get(_, prop) {
return (exports.getIngredientWithProp(prop, ingredients) || prototype)[prop];
},
set(_, prop, val) {
const ingredientWithProp = exports.getIngredientWithProp(prop, ingredients);
if (ingredientWithProp === undefined)
throw new Error('Cannot set new properties on Proxies created by ts-mixer');
ingredientWithProp[prop] = val;
return true;
},
deleteProperty() {
throw new Error('Cannot delete properties on Proxies created by ts-mixer');
},
ownKeys() {
return ingredients
.map(Object.getOwnPropertyNames)
.reduce((prev, curr) => curr.concat(prev.filter(key => curr.indexOf(key) < 0)));
},
});
exports.proxyMix = proxyMix;
/**
* Creates a new proxy-prototype object that is a "soft" mixture of the given prototypes. The mixing is achieved by
* proxying all property access to the ingredients. This is not ES5 compatible and less performant. However, any
* changes made to the source prototypes will be reflected in the proxy-prototype, which may be desirable.
*/
const softMixProtos = (ingredients, constructor) => exports.proxyMix([...ingredients, { constructor }]);
exports.softMixProtos = softMixProtos;