-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
294 lines (270 loc) · 7.83 KB
/
index.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const {Root} = require("ndtreejs").ndcls
const fac_bsc = require("nv-facutil-basic");
const STATE_DICT = {
init:"init",
pending:"pending",
resolved:"resolved",
rejected:"rejected",
impossible:"impossible",
paused:"paused",
stopped:"stopped"
}
/*
class Init { [util.inspect.custom] () { return("<init>")}}
const INIT = new Init();
class Pending { [util.inspect.custom] () { return("<pending>")}}
const PENDING = new Pending();
*/
const ERROR_DICT = {
resolved:new Error("use-rslt-or-final"),
rejected:new Error("use-rejected-or-final"),
in_executing:new Error("still-in-executing-use-force-reset"),
need_reset:new Error("reset-before-relaunch")
}
const DFLT_EXEC = (rs,rj,that)=>{}
const SYM_EXEC = Symbol("exec")
const SYM_TASK_NAME = Symbol("task_name")
const SYM_TYPE = Symbol("type")
const SYM_PROMISE = Symbol("promise");
const TYPE_DICT = {
root:"root",
then:"then",
"catch":"catch"
}
const SYM_RESET = Symbol("reset")
const SYM_RESET_EXEC = Symbol("reset_exec")
const SYM_READY = Symbol("ready")
const SYM_IMPOSSIBLE = Symbol("impossible")
const SYM_SET_IMPOSSIBLE = Symbol("set_impossible")
const SYM_UNSET_IMPOSSIBLE = Symbol("unset_impossible")
function _set_impossible(nd) {
let sdfs = nd.$sdfs()
sdfs.forEach(nd=>nd[SYM_SET_IMPOSSIBLE]())
}
function _run_children(that) {
let then = that.then;
let cth = that.catch;
if(then!==null && that.is_resolved()) {
if(cth!==null) {_set_impossible(cth);}
then[SYM_EXEC]();
}
if(cth!==null && that.is_rejected()) {
if(then !==null) {_set_impossible(then);}
cth[SYM_EXEC]();
}
}
function _is_in_executing(sdfs) {
for(let nd of sdfs) {
if(nd.state === 'pending') {return(true)}
}
return(false)
}
function _check_one_state(nd) {
let name = nd[SYM_TASK_NAME]
let typ = nd[SYM_TYPE]
let state = nd.state
let indent = " ".repeat(nd.$depth())
let tem = `${indent}${name}[${typ}] Promise \{ <${state}> \} `
return(tem)
}
class _BtdPromise extends Root {
#type = "root"
#p
#rs
#rj
#exec
#state = "init"
#rslt
#exception
constructor(f=DFLT_EXEC) {
super();
this.#exec = f
this.#p = new Promise((rs,rj)=>{this.#rs = rs;this.#rj = rj});
}
get [SYM_PROMISE]() {return(this.#p)}
[SYM_READY]() {this.#state = "pending"}
[SYM_RESET_EXEC] (f) {this.#exec = f}
[SYM_RESET]() {
this.#p = new Promise((rs,rj)=>{this.#rs = rs;this.#rj = rj});
this.#rslt = undefined;
this.#exception = undefined;
this.#state = "init"
delete this.#p[SYM_IMPOSSIBLE]
}
set [SYM_TYPE](typ) { this.#type = typ}
get [SYM_TYPE]() {return(this.#type)}
[SYM_SET_IMPOSSIBLE]() {
this.#p[SYM_IMPOSSIBLE]=true;
this.#state = "impossible"
}
[SYM_UNSET_IMPOSSIBLE]() {
delete this.#p[SYM_IMPOSSIBLE]
this.#state = "init"
}
set then(f) {
let children = this.$children();
let lngth = children.length;
if(lngth === 0) {
let then = new _BtdPromise(f);
then[SYM_TYPE] = 'then'
this.$prepend_child(then);
} else if(lngth ===1) {
let child = children[0];
if(child[SYM_TYPE] === 'then') {
child[SYM_RESET]();
child[SYM_RESET_EXEC](f);
} else {
let then = new _BtdPromise(f);
then[SYM_TYPE] = 'then';
this.$prepend_child(then);
}
} else {
let child = children[0];
child[SYM_RESET]();
child[SYM_RESET_EXEC](f);
}
}
get then() {
let child = this.$fstch();
if(child!==null && child[SYM_TYPE]==='then') {
return(child)
} else {
return(null)
}
}
set catch(f) {
let children = this.$children();
let lngth = children.length;
if(lngth === 0) {
let cth = new _BtdPromise(f);
cth[SYM_TYPE] = 'catch'
this.$append_child(cth);
} else if(lngth ===1) {
let child = children[0];
if(child[SYM_TYPE] === 'catch') {
child[SYM_RESET]();
child[SYM_RESET_EXEC](f);
} else {
let cth = new _BtdPromise(f);
cth[SYM_TYPE] = 'catch'
this.$append_child(cth);
}
} else {
let child = children[1];
child[SYM_RESET]();
child[SYM_RESET_EXEC](f);
}
}
get catch() {
let child = this.$lstch();
if(child!==null && child[SYM_TYPE]==='catch') {
return(child)
} else {
return(null)
}
}
set both(f) {this.then =f;this.catch = f;}
get both() {return({then:this.then,"catch":this.catch})}
finally(f) {this.#p.finally(f)}
get [SYM_TASK_NAME] () {return(this.#exec.name)}
[SYM_EXEC]() {
if(this.#state === 'pending') {
this.#exec(this.#rs,this.#rj,this);
this.#p.then(
r=>{
this.#state = "resolved";
this.#rslt = r;
_run_children(this);
}
).catch(
err=>{
this.#state="rejected"
this.#exception = err
_run_children(this);
}
)
} else {
}
}
get rslt() {
if(this.#state === "resolved") {
return(this.#rslt)
} else if(this.#state === "rejected") {
throw(ERROR_DICT.rejected)
} else {
}
}
get exception() {
if(this.#state === "rejected") {
return(this.#exception)
} else if(this.#state === "resolved"){
throw(ERROR_DICT.resolved)
} else {
}
}
get final() {
if(this.#state === "resolved") {
return(this.#rslt)
} else if(this.#state === "rejected") {
return(this.#exception)
} else {
}
}
get state() {return(this.#state)}
is_settled() {return(this.#state !== "pending" && this.#state !== "init")}
is_pending() {return(this.#state === "pending")}
is_resolved() {return(this.#state === "resolved")}
is_rejected() {return(this.#state === "rejected")}
}
function _repr(that) {
return({promise:that[SYM_PROMISE],task:that[SYM_TASK_NAME]})
}
fac_bsc.add_repr(_BtdPromise,_repr)
class BtdPromise extends _BtdPromise {
launch() {
if(this.state==="init" || ! _is_in_executing(this.$sdfs())){
let sdfs = this.$sdfs();
sdfs.forEach(nd=>nd[SYM_READY]());
this[SYM_EXEC]();
} else {
throw(ERROR_DICT.need_reset);
}
}
reset(force=false) {
let sdfs = this.$sdfs()
if(force) {
sdfs.forEach(nd=>nd[SYM_RESET]())
} else {
let cond = _is_in_executing(sdfs)
if(cond) {
throw(ERROR_DICT.in_executing)
} else {
sdfs.forEach(nd=>nd[SYM_RESET]())
}
}
}
is_in_executing() { return(_is_in_executing(this.$sdfs()))}
check_state() {
let sdfs = this.$sdfs()
for(let i=0;i<sdfs.length;i++) {console.log(_check_one_state(sdfs[i]))}
}
get exec_routes() {
let sdfs = this.$sdfs()
sdfs = sdfs.filter(nd=>nd.state!=="impossible")
sdfs = sdfs.filter(nd=>nd.$is_leaf())
let routes =sdfs.map(
nd=>{
let route = nd.$ances(true);
route.reverse();
return(route)
}
);
return(routes)
}
[Symbol.iterator]() { return(this.$sdfs()[Symbol.iterator]())}
}
module.exports = {
BtdPromise,
ERROR_DICT,
STATE_DICT,
}