-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.js
More file actions
138 lines (114 loc) · 3.47 KB
/
Util.js
File metadata and controls
138 lines (114 loc) · 3.47 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
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: wrench;
/**
* Used to hold protected fields of instances
* that should be accessible by other classes in script..
*
* @class ProtectedDataHolder
*/
class ProtectedDataHolder {
static #stateHolder = new WeakMap();
/**
* Used to add whole object with
* protected fields to state.
*
* @static
* @param {Object} stateOwner instance that is owner of provided state
* @param {Object} state object with protected data
* @memberof ProtectedDataHolder
*/
static addToStateMultiple(stateOwner, state) {
const currentState = this.getState(stateOwner);
this.#stateHolder.set(stateOwner, {...currentState, ...state});
}
/**
* Used to add single protected field
* to state.
*
* @static
* @param {Object} stateOwner instance that is owner of provided state
* @param {String} name name of protected field
* @param {Object} name value of protected field
* @memberof ProtectedDataHolder
*/
static addToState(stateOwner, name, value) {
const currentState = this.getState(stateOwner);
this.#stateHolder.set(stateOwner, {...currentState, [name]: value});
}
/**
* Used to get protected field value
* from state.
*
* @static
* @param {*} stateOwner instance that is owner of provided state
* @param {*} name name of protected field
* @param {*} defaultValue value returned when field is not in state
* @return {*} value of protected field
* @memberof ProtectedDataHolder
*/
static getFromState(stateOwner, name, defaultValue) {
const stateValue = this.getState(stateOwner)[name];
if (!stateValue) {
return defaultValue;
}
return stateValue;
}
/**
* Used to get state for the owner.
* If doesn't exist will create empty state.
*
* @param {*} stateOwner instance that is owner state
* @return {*} state
* @memberof ProtectedDataHolder
*/
static getState(stateOwner) {
let state = this.#stateHolder.get(stateOwner);
if (!state) {
state = {};
this.#stateHolder.set(stateOwner, state);
}
return state;
}
}
/**
* Wrapper function that injects
* properties and state of classes into another one.
*
* Used to achieve multi-inheritance.
*
* @param {List<Object>} bases list of clasess
* @return {Object} composed class
*/
function Classes(...bases) {
class Bases {
constructor() {
bases.forEach(base => Object.assign(this, new base()));
}
}
bases.forEach(base => {
Object.getOwnPropertyNames(base.prototype)
.filter(prop => prop != 'constructor')
.forEach(prop => Bases.prototype[prop] = base.prototype[prop])
});
return Bases;
}
function addToState(stateOwner, state) {
ProtectedDataHolder.addToStateMultiple(stateOwner, state);
}
function addOneToState(stateOwner, name, value) {
ProtectedDataHolder.addToState(stateOwner, name, value);
}
function getFromState(stateOwner, name, defaultValue) {
return ProtectedDataHolder.getFromState(stateOwner, name, defaultValue);
}
function getState(stateOwner) {
return ProtectedDataHolder.getState(stateOwner);
}
module.exports = {
Classes,
addToState,
addOneToState,
getFromState,
getState
};