-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntentionQuery.js
More file actions
135 lines (115 loc) · 3.52 KB
/
IntentionQuery.js
File metadata and controls
135 lines (115 loc) · 3.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
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
function queryIntentions() {
return this.formatIntentions()
}
function queryLinkedStorages() {
return this.formatLinkedStorages()
}
export default class IntentionQuery {
#storage;
#updatedIntentions = new Map();
#updatedStorages = new Map();
#statsInterval = 5000;
#statsTimeout;
#iQuery;
#interface = {
queryIntentions: queryIntentions.bind(this),
queryLinkedStorages: queryLinkedStorages.bind(this),
updatedIntentions: [],
updatedStorages: []
};
#sendStats = true;
#createQueryIntention() {
return this.#storage.createIntention({
title: {
en: 'Can query information from intention storage',
ru: 'Запрашиваю информацию у хранилища намерений'
},
input: 'None',
output: 'StorageStats',
onData: async function onData(status) {
if (status == 'accepted') return this.#interface;
}
});
}
constructor (storage) {
this.#storage = storage;
setTimeout(() => {
this.#iQuery = this.#createQueryIntention();
}, 0);
}
#formatIntentions() {
const res = [];
for (let [, intention] of this.#storage.intentions.byId()) {
res.push(intention.toObject());
}
return res;
}
#formatLinkedStorages() {
const res = [];
for (let [, link] of this.#storage.links) {
res.push(link.toObject());
}
return res;
}
queryIntentions() {
return this.#formatIntentions()
}
queryLinkedStorages() {
return this.#formatLinkedStorages()
}
async #updateStats() {
try {
if ((this.#updatedIntentions.size == 0) && (this.#updatedStorages.size == 0)) return;
this.#interface.updatedIntentions = [...this.#updatedIntentions.values()];
this.#interface.updatedStorages = [...this.#updatedStorages.values()];
this.#iQuery.accepted.send(this.#interface);
} catch(e) {
console.log(e);
} finally {
this.#updatedIntentions.clear();
this.#updatedStorages.clear();
if (this.#sendStats)
this.#statsTimeout = setTimeout(() => this.#updateStats(), this.#statsInterval);
}
}
#enableStats() {
if (this.statsInterval == 0) {
this.#sendStats = false;
return;
}
this.#sendStats = true;
this.#statsTimeout = setTimeout(() => this.#updateStats(), this.#statsInterval);
}
#disableStats() {
this.#sendStats = false;
clearTimeout(this.#statsTimeout);
}
updateIntention(intention, status) {
this.#updatedIntentions.set(intention.id, {
intention: intention.toObject(),
status: status
});
}
updateStorage(storage, status) {
this.#updatedStorages.set(storage.id, {
storage: storage.toObject(),
status: status
})
}
set statsInterval(interval) {
this.#disableStats();
this.#statsInterval = interval;
this.#enableStats();
}
get statsInterval() {
return this.#statsInterval;
}
get sendStats() {
return this.#sendStats;
}
set sendStats(value) {
if (typeof(value) != 'boolean') throw new Error('Value must be boolean');
if (!value) return this.#disableStats();
this.#enableStats();
}
};