-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdummyCode_block.js
More file actions
66 lines (58 loc) · 1.52 KB
/
dummyCode_block.js
File metadata and controls
66 lines (58 loc) · 1.52 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
// const clone = obj => JSON.parse(JSON.stringify(obj));
const clone = obj => structuredClone(obj);
class Block extends EventTarget {
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget
constructor (
{ type = "customType", position = [0, 0], input = [], output = [], fields = {} }
) {
#data = { position, input, output, fields };
#data.entries().forEach(([key, value]) => {
if(typeof value != "object")
throw new TypeError(
`Invalid initial value: the type of the property ${key} is incorrect.`
);
#data[key] = new Proxy(value, {
set: (t, tKey, tValue) => { // t for target
this.dispatchEvent(new CustomEvent(
`${key}Change`,
{ key: tKey, oldValue: t[tKey], newValue: t[tKey] = tValue}
));
return true;
}
})
});
#data.type = type;
// thanksfully, string is not an object
#data = new Proxy(
#data,
{
set: (target, key, value) => {
if(key == "type") {
throw new TypeError(
`Invalid assignment: ` +
`the property "type" shouldn't be changed after the Block created.`
);
return false;
}
this.dispatchEvent(new CustomEvent(
`${key}Change`,
{ key: null, oldValue: target[key], newValue: value }
));
target[key] = value;
return true;
}
}
);
get data() {
return clone(this.#data);
}
}
get data() {
return clone(this.#data);
}
set data() {
throw new TypeError(`Invalid assignment to read-only property "data"`);
return false;
}
}
export {Block};