-
Notifications
You must be signed in to change notification settings - Fork 0
/
PublicClassBuilder.js
265 lines (219 loc) · 10.7 KB
/
PublicClassBuilder.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @module PublicClassBuilder
* @author Simon Petrac
*/
const
_privateMap = new WeakMap();
/**
* Returns the private instance, if any public instances has been found.
* @name PublicClassBuilder~filterPublic
* @param {*} obj Any object to check. Values of arrays or promises will also be considered.
* @returns {*} Returns the private instance if possible.
* @this {PublicClassBuilder}
* @private
*/
function filterPublic(obj) {
const _private = _privateMap.get(this);
if (_private.publicInstancesMap.has(obj))
return _private.publicInstancesMap.get(obj);
if (_private.publicClassesMap.has(obj))
return _private.publicClassesMap.get(obj);
else if (Array.isArray(obj))
return obj.map(elem => filterPublic.call(this, elem));
else if (typeof obj === 'promise')
return new Promise((resolve, reject) => obj.then(result => resolve(filterPublic.call(this, result))).catch(reject));
else
return obj;
} // PublicClassBuilder~filterPublic
/**
* Returns the public instance, if any private instances has been found.
* @name PublicClassBuilder~filterPrivate
* @param {*} obj Any object to check. Values of arrays or promises will also be considered.
* @returns {*} Returns the public instance if possible.
* @this {PublicClassBuilder}
* @private
*/
function filterPrivate(obj) {
const _private = _privateMap.get(this);
if (_private.privateInstancesMap.has(obj))
return _private.privateInstancesMap.get(obj);
if (_private.privateClassesMap.has(obj))
return _private.privateClassesMap.get(obj);
else if (Array.isArray(obj))
return obj.map(elem => filterPrivate.call(this, elem));
else if (typeof obj === 'promise')
return new Promise((resolve, reject) => obj.then(result => resolve(filterPrivate.call(this, result))).catch(reject));
else
return obj;
} // PublicClassBuilder~filterPrivate
/**
* Creates a public class out of another class, using just the prototype and skipping all _ properties.
* @name PublicClassBuilder~buildPublicClass
* @param {class} PrivateClass The base class for the public class.
* @returns {class} The public class that got created.
* @this {PublicClassBuilder}
* @private
*/
function buildPublicClass(PrivateClass) {
const
_builderInstance = this,
_private = _privateMap.get(this);
/* 1. create the public class */
class PublicClass {
constructor(...publicArgs) {
let privateInstance;
if (publicArgs[0] instanceof PrivateClass) {
/* 1.1a. constructed with a private instance, to get the public instance */
privateInstance = publicArgs[0];
if (_private.privateInstancesMap.has(privateInstance))
throw new Error(`${PublicClass.name}#constructor(privateInstance) -> privateInstance already has an associated publicInstance`);
} else {
/* 1.1b. constructed from the public class */
let privatArgs = filterPublic.call(_builderInstance, publicArgs);
privateInstance = new PrivateClass(...privatArgs);
}
/* 1.2. save the relation between the public and private instance in the WeakMaps */
_private.publicInstancesMap.set(this, privateInstance);
_private.privateInstancesMap.set(privateInstance, this);
} // PublicClass#constructor
static _get(privateInstance) {
if (_private.privateInstancesMap.has(privateInstance))
return _private.privateInstancesMap.get(privateInstance);
else if (privateInstance instanceof publicClassesMap.get(PublicClass))
return new PublicClass(privateInstance);
else
throw new Error(`${PublicClass.name}._get(privateInstance) -> privateInstance does not relate to this public class`);
} // PublicClass._get
} // PublicClass
/* 2. transfer all public properties of the class */
for (let key of Reflect.ownKeys(PrivateClass)) {
/* 2.1. skip private properties, etc. */
if (typeof key !== 'string') continue;
if (key.startsWith('_')) continue;
if (key === 'prototype') continue;
/* 2.2. get the complete property definition of the class */
let privateProperty = Reflect.getOwnPropertyDescriptor(PrivateClass, key);
let publicProperty = {};
/* 2.3. some attributes of the property can easyly be transfered */
if (privateProperty.configurable) publicProperty.configurable = true;
if (privateProperty.enumerable) publicProperty.enumerable = true;
if (privateProperty.get || privateProperty.set) {
/* 2.4a. if the property is a getter/setter */
if (privateProperty.get) {
/* 2.4a.1. the getter */
publicProperty.get = function () {
let privateResult = privateProperty.get.call(PrivateClass);
return filterPrivate.call(_builderInstance, privateResult);
};
}
if (privateProperty.set) {
/* 2.4a.2. the setter */
publicProperty.set = function (publicValue) {
let privatValue = filterPublic.call(_builderInstance, publicValue);
privateProperty.set.call(PrivateClass, privatValue);
};
}
} else {
/* 2.4b. if the property is a value */
if (privateProperty.writable) publicProperty.writable = true;
if (typeof privateProperty.value === 'function') {
/* 2.4b.1 usually its a function */
publicProperty.value = function (...publicArgs) {
let privatArgs = filterPublic.call(_builderInstance, publicArgs);
let privateResult = privateProperty.value.apply(PrivateClass, privatArgs);
return filterPrivate.call(_builderInstance, privateResult);
};
} else {
/* 2.4b.2 sometimes not */
publicProperty.get = function () {
let privateResult = privateProperty.value;
return filterPrivate.call(_builderInstance, privateResult);
};
}
}
/* 2.5. write the public property to the class */
Object.defineProperty(PublicClass, key, publicProperty);
}
/* 3. transfer all public properties for the instances into the prototype */
for (let protoKey of Reflect.ownKeys(PrivateClass.prototype)) {
/** 3.1. skip private properties, etc. */
if (typeof protoKey !== 'string') continue;
if (protoKey.startsWith('_')) continue;
if (protoKey === 'constructor') continue;
/* 3.2. get the complete property definition of the prototype */
let privateProtoProperty = Reflect.getOwnPropertyDescriptor(PrivateClass.prototype, protoKey);
let publicProtoProperty = {};
/* 3.3. some attributes of the property can easyly be transfered */
if (privateProtoProperty.configurable) publicProtoProperty.configurable = true;
if (privateProtoProperty.enumerable) publicProtoProperty.enumerable = true;
if (privateProtoProperty.get || privateProtoProperty.set) {
/* 3.4a. if the property is a getter/setter */
if (privateProtoProperty.get) {
/* 3.4a.1. the getter */
publicProtoProperty.get = function () {
if (!_private.publicInstancesMap.has(this)) return;
let privateResult = privateProtoProperty.get.call(_private.publicInstancesMap.get(this));
return filterPrivate.call(_builderInstance, privateResult);
};
}
if (privateProtoProperty.set) {
/* 3.4a.2. the setter */
publicProtoProperty.set = function (publicValue) {
if (!_private.publicInstancesMap.has(this)) return;
let privatValue = filterPublic.call(_builderInstance, publicValue);
privateProtoProperty.set.call(_private.publicInstancesMap.get(this), privatValue);
};
}
} else {
/* 3.4b. if the property is a value */
if (privateProtoProperty.writable) publicProtoProperty.writable = true;
if (typeof privateProtoProperty.value === 'function') {
/* 3.4b.1 usually its a function */
publicProtoProperty.value = function (...publicArgs) {
if (!_private.publicInstancesMap.has(this)) return;
let privatArgs = filterPublic.call(_builderInstance, publicArgs);
let privateResult = privateProtoProperty.value.apply(_private.publicInstancesMap.get(this), privatArgs);
return filterPrivate.call(_builderInstance, privateResult);
};
} else {
/* 3.4b.2 sometimes not */
publicProtoProperty.get = function () {
if (!_private.publicInstancesMap.has(this)) return;
let privateResult = privateProtoProperty.value;
return filterPrivate.call(_builderInstance, privateResult);
};
}
}
/* 3.5. write the public property to the prototype */
Object.defineProperty(PublicClass.prototype, protoKey, publicProtoProperty);
}
/* 4. public class is finished */
return PublicClass;
} // PublicClassBuilder~buildPublicClass
class PublicClassBuilder {
constructor() {
const _private = {};
_private.publicClassesMap = new Map();
_private.privateClassesMap = new Map();
_private.publicInstancesMap = new Map();
_private.privateInstancesMap = new Map();
_privateMap.set(this, _private);
} // PublicClassBuilder#constructor
getPublicClass(PrivateClass) {
const _private = _privateMap.get(this);
if (typeof PrivateClass !== 'function' || !PrivateClass.prototype)
throw new Error(`PublicClassBuilder#getPublicClass(PrivateClass) -> PrivateClass has to be a class`);
if (_private.privateClassesMap.has(PrivateClass)) {
return _private.privateClassesMap.get(PrivateClass);
} else {
let PublicClass = buildPublicClass.call(this, PrivateClass);
_private.privateClassesMap.set(PrivateClass, PublicClass);
_private.publicClassesMap.set(PublicClass, PrivateClass);
return PublicClass;
}
} // PublicClassBuilder#getPublicClass
getPrivateClass(PublicClass) {
// TODO PublicClassBuilder#getPrivateClass
} // PublicClassBuilder#getPrivateClass
} // PublicClassBuilder
module.exports = PublicClassBuilder;