-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeeplink.js
70 lines (63 loc) · 2.01 KB
/
deeplink.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
//A super-simple framework to implement the basics of MVC, using only the properties you care about!
//By TinyTinfoil (https://github.com/TinyTinfoil)
class DeepLink {
static DL = {
//Array that holds *true* var values
origin:{},
//Array subset that holds reactive functions
reactive:[],
//Contains indexies of reactive declarations
updateTable:{}
};
constructor(packedVar) {
let [varName , varValue] = DeepLink.extractVarName(packedVar);
DeepLink.DL.origin[varName] = varValue;
DeepLink.DL.updateTable[varName] = [];
this.watch(varName);
}
//watches variable changes
watch(varName) {
Object.defineProperty(globalThis, varName, {
get: function () {
return DeepLink.DL.origin[varName];
},
configurable: true,
set: function (v) {
DeepLink.DL.origin[varName] = v;
for(let i of DeepLink.DL.updateTable[varName]){
(DeepLink.DL.reactive[i])();
};
},
});
}
/**
* @param {Object} varNames The names of the DeepLinked variables that will trigger the function on change
* @param {Function} func The lazily evaluated function that will run
*/
static react(varNames,func){
let index = DeepLink.DL.reactive.push(func) - 1;
for(let v in varNames){
DeepLink.DL.updateTable[v].push(index)
}
}
/**
* An internal function for extracting string repersentations of variable names, along with the variable itself
* @param {Object} obj An object as the result of doing {AnyVar}
* @returns {List} [the variable name, the variable value]
*/
static extractVarName(obj) {
return [Object.keys(obj)[0], obj[Object.keys(obj)[0]]];
}
/**
* Removes the variable (both from DeepLink and from the window)
* @param {Object} varNames the names of all the variables, encapuslated in an object
*/
static remove(varNames) {
for (let n in varNames){
//removes variable
delete DeepLink.DL.origin[n];
delete DeepLink.DL.updateTable[n];
delete globalThis[n];
}
}
}