-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathAdaptableController.js
68 lines (55 loc) · 1.63 KB
/
AdaptableController.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
/*
AdaptableController.js
AdaptableController is the base class for all controllers
that support adapter,
The super class takes care of creating the right instance for the adapter
based on the parameters passed
*/
// _adapter is private, use Symbol
var _adapter = Symbol();
export class AdaptableController {
constructor(adapter, appId, options) {
this.options = options;
this.appId = appId;
this.adapter = adapter;
}
set adapter(adapter) {
this.validateAdapter(adapter);
this[_adapter] = adapter;
}
get adapter() {
return this[_adapter];
}
expectedAdapterType() {
throw new Error('Subclasses should implement expectedAdapterType()');
}
validateAdapter(adapter) {
AdaptableController.validateAdapter(adapter, this);
}
static validateAdapter(adapter, self, ExpectedType) {
if (!adapter) {
throw new Error(this.constructor.name + ' requires an adapter');
}
const Type = ExpectedType || self.expectedAdapterType();
// Allow skipping for testing
if (!Type) {
return;
}
// Makes sure the prototype matches
const mismatches = Object.getOwnPropertyNames(Type.prototype).reduce((obj, key) => {
const adapterType = typeof adapter[key];
const expectedType = typeof Type.prototype[key];
if (adapterType !== expectedType) {
obj[key] = {
expected: expectedType,
actual: adapterType,
};
}
return obj;
}, {});
if (Object.keys(mismatches).length > 0) {
// throw new Error("Adapter prototype don't match expected prototype", adapter, mismatches);
}
}
}
export default AdaptableController;