mm_machine 2.9.2 → 2.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/drive.js +668 -490
- package/index.js +1141 -921
- package/mod.js +742 -711
- package/package.json +5 -5
package/mod.js
CHANGED
|
@@ -1,711 +1,742 @@
|
|
|
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
|
-
this.
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
this.
|
|
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
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
*
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
*
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
*
|
|
332
|
-
* @returns {object}
|
|
333
|
-
*/
|
|
334
|
-
Mod.prototype.
|
|
335
|
-
return this;
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
*
|
|
340
|
-
* @
|
|
341
|
-
*/
|
|
342
|
-
Mod.prototype.
|
|
343
|
-
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
*
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
*
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
};
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
*
|
|
415
|
-
* @
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
*
|
|
424
|
-
* @
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
/**
|
|
460
|
-
*
|
|
461
|
-
*
|
|
462
|
-
* @returns {Promise<
|
|
463
|
-
*/
|
|
464
|
-
Mod.prototype.
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
*
|
|
476
|
-
* @
|
|
477
|
-
*/
|
|
478
|
-
Mod.prototype.
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
*
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
};
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
if (
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
*
|
|
702
|
-
* @returns {
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
return
|
|
707
|
-
};
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Mod 基础模块类
|
|
3
|
+
* 提供生命周期管理、方法/数据存储、事件系统、钩子机制等基础能力
|
|
4
|
+
* @class Mod
|
|
5
|
+
* @augments Base
|
|
6
|
+
*/
|
|
7
|
+
// eslint-disable-next-line mm_eslint/object-name, mm_eslint/variable-name
|
|
8
|
+
var Base = require('mm_expand').Base;
|
|
9
|
+
|
|
10
|
+
class Mod extends Base {
|
|
11
|
+
/**
|
|
12
|
+
* 构造函数
|
|
13
|
+
* @param {object} config 配置参数
|
|
14
|
+
* @param {string} [config.name] 模块名称
|
|
15
|
+
* @param {string} [config.lifecycle_state='created'] 初始生命周期状态
|
|
16
|
+
*/
|
|
17
|
+
constructor(config) {
|
|
18
|
+
super(config);
|
|
19
|
+
this._methods = {};
|
|
20
|
+
this._datas = {};
|
|
21
|
+
this.lifecycle_state = (config && config.lifecycle_state) || 'created';
|
|
22
|
+
this.is_loaded = false;
|
|
23
|
+
|
|
24
|
+
// 父对象
|
|
25
|
+
if (config && config.parent) {
|
|
26
|
+
this.getParent = function () {
|
|
27
|
+
return config.parent;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ==================== 生命周期公共方法 ====================
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 加载阶段:created → loaded
|
|
37
|
+
* @returns {Promise<void>}
|
|
38
|
+
*/
|
|
39
|
+
Mod.prototype.load = async function () {
|
|
40
|
+
return this._loadCore();
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 初始化阶段:loaded → inited
|
|
45
|
+
* @returns {Promise<void>}
|
|
46
|
+
*/
|
|
47
|
+
Mod.prototype.init = async function () {
|
|
48
|
+
return this._initCore();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 启动阶段:inited → started
|
|
53
|
+
* @returns {Promise<void>}
|
|
54
|
+
*/
|
|
55
|
+
Mod.prototype.start = async function () {
|
|
56
|
+
return this._startCore();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 暂停阶段:started → paused
|
|
61
|
+
* @returns {Promise<void>}
|
|
62
|
+
*/
|
|
63
|
+
Mod.prototype.pause = async function () {
|
|
64
|
+
return this._pauseCore();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 恢复阶段:paused → started
|
|
69
|
+
* @returns {Promise<void>}
|
|
70
|
+
*/
|
|
71
|
+
Mod.prototype.resume = async function () {
|
|
72
|
+
return this._resumeCore();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 结束阶段:started/paused → ended
|
|
77
|
+
* @returns {Promise<void>}
|
|
78
|
+
*/
|
|
79
|
+
Mod.prototype.end = async function () {
|
|
80
|
+
return this._endCore();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 销毁阶段:ended → destroyed
|
|
85
|
+
* @returns {Promise<void>}
|
|
86
|
+
*/
|
|
87
|
+
Mod.prototype.destroy = async function () {
|
|
88
|
+
return this._destroyCore();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// ==================== 生命周期 Core 方法 ====================
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 加载核心
|
|
95
|
+
* 由 Drive 子类重写以添加脚本加载逻辑
|
|
96
|
+
* @returns {Promise<void>}
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
Mod.prototype._loadCore = async function () {
|
|
100
|
+
if (this.lifecycle_state !== 'created') {
|
|
101
|
+
throw new Error('模块未创建,无法加载');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.emit('load:before');
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
// 执行加载操作,由 Drive 子类重写
|
|
108
|
+
this.lifecycle_state = 'loaded';
|
|
109
|
+
this.emit('load:success');
|
|
110
|
+
} catch (err) {
|
|
111
|
+
this.emit('load:error', err);
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 初始化核心
|
|
118
|
+
* 调用 _init 钩子,Manager 类型自动调用 boot()
|
|
119
|
+
* @returns {Promise<void>}
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
Mod.prototype._initCore = async function () {
|
|
123
|
+
if (this.lifecycle_state !== 'loaded') {
|
|
124
|
+
throw new Error('模块未加载,无法初始化');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.emit('init:before');
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
var init_hook = this.getMethod('_init');
|
|
131
|
+
if (init_hook) {
|
|
132
|
+
var result = init_hook(this);
|
|
133
|
+
if (result instanceof Promise) {
|
|
134
|
+
var timeout = new Promise((resolve, reject) => {
|
|
135
|
+
setTimeout(() => {
|
|
136
|
+
reject(new Error('初始化超时'));
|
|
137
|
+
}, 30000);
|
|
138
|
+
});
|
|
139
|
+
await Promise.race([result, timeout]);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Manager 类型模块自动调用 boot()(在设置状态前执行)
|
|
144
|
+
if (typeof this.boot === 'function') {
|
|
145
|
+
await this.boot();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.lifecycle_state = 'inited';
|
|
149
|
+
this.emit('init:success');
|
|
150
|
+
} catch (err) {
|
|
151
|
+
this.emit('init:error', err);
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 启动核心
|
|
158
|
+
* 调用 _start 钩子,传播到子模块
|
|
159
|
+
* @returns {Promise<void>}
|
|
160
|
+
* @private
|
|
161
|
+
*/
|
|
162
|
+
Mod.prototype._startCore = async function () {
|
|
163
|
+
if (this.lifecycle_state !== 'inited') {
|
|
164
|
+
if (this.lifecycle_state === 'started') {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
throw new Error('模块未初始化,无法启动');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this.emit('start:before');
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
var start_hook = this.getMethod('_start');
|
|
174
|
+
if (start_hook) {
|
|
175
|
+
var result = start_hook(this);
|
|
176
|
+
if (result instanceof Promise) {
|
|
177
|
+
var timeout = new Promise((resolve, reject) => {
|
|
178
|
+
setTimeout(() => {
|
|
179
|
+
reject(new Error('启动超时'));
|
|
180
|
+
}, 30000);
|
|
181
|
+
});
|
|
182
|
+
await Promise.race([result, timeout]);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
this.lifecycle_state = 'started';
|
|
187
|
+
this.emit('start:success');
|
|
188
|
+
} catch (err) {
|
|
189
|
+
this.emit('start:error', err);
|
|
190
|
+
throw err;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 暂停核心
|
|
196
|
+
* 调用 _pause 钩子,传播到子模块
|
|
197
|
+
* @returns {Promise<void>}
|
|
198
|
+
* @private
|
|
199
|
+
*/
|
|
200
|
+
Mod.prototype._pauseCore = async function () {
|
|
201
|
+
if (this.lifecycle_state !== 'started') {
|
|
202
|
+
throw new Error('模块未启动,无法暂停');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.emit('pause:before');
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
var pause_hook = this.getMethod('_pause');
|
|
209
|
+
if (pause_hook) {
|
|
210
|
+
pause_hook(this);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this.lifecycle_state = 'paused';
|
|
214
|
+
this.emit('pause:success');
|
|
215
|
+
} catch (err) {
|
|
216
|
+
this.emit('pause:error', err);
|
|
217
|
+
throw err;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 恢复核心
|
|
223
|
+
* 调用 _resume 钩子,传播到子模块
|
|
224
|
+
* @returns {Promise<void>}
|
|
225
|
+
* @private
|
|
226
|
+
*/
|
|
227
|
+
Mod.prototype._resumeCore = async function () {
|
|
228
|
+
if (this.lifecycle_state !== 'paused') {
|
|
229
|
+
throw new Error('模块未暂停,无法恢复');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
this.emit('resume:before');
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
var resume_hook = this.getMethod('_resume');
|
|
236
|
+
if (resume_hook) {
|
|
237
|
+
resume_hook(this);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
this.lifecycle_state = 'started';
|
|
241
|
+
this.emit('resume:success');
|
|
242
|
+
} catch (err) {
|
|
243
|
+
this.emit('resume:error', err);
|
|
244
|
+
throw err;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 结束核心
|
|
250
|
+
* 调用 _end 钩子,传播到子模块
|
|
251
|
+
* @returns {Promise<void>}
|
|
252
|
+
* @private
|
|
253
|
+
*/
|
|
254
|
+
Mod.prototype._endCore = async function () {
|
|
255
|
+
if (this.lifecycle_state !== 'started' && this.lifecycle_state !== 'paused') {
|
|
256
|
+
throw new Error('模块未启动或暂停,无法结束');
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
this.emit('end:before');
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
var end_hook = this.getMethod('_end');
|
|
263
|
+
if (end_hook) {
|
|
264
|
+
end_hook(this);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
this.lifecycle_state = 'ended';
|
|
268
|
+
this.emit('end:success');
|
|
269
|
+
} catch (err) {
|
|
270
|
+
this.emit('end:error', err);
|
|
271
|
+
throw err;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 销毁核心
|
|
277
|
+
* 调用 _onDestroy 钩子,释放引用
|
|
278
|
+
* @returns {Promise<void>}
|
|
279
|
+
* @private
|
|
280
|
+
*/
|
|
281
|
+
Mod.prototype._destroyCore = async function () {
|
|
282
|
+
if (this.lifecycle_state !== 'ended') {
|
|
283
|
+
throw new Error('模块未结束,无法销毁');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
this.emit('destroy:before');
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
var destroy_hook = this.getMethod('_onDestroy');
|
|
290
|
+
if (destroy_hook) {
|
|
291
|
+
destroy_hook(this);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
this._methods = {};
|
|
295
|
+
this._datas = {};
|
|
296
|
+
this.lifecycle_state = 'destroyed';
|
|
297
|
+
this.emit('destroy:success');
|
|
298
|
+
} catch (err) {
|
|
299
|
+
this.emit('destroy:error', err);
|
|
300
|
+
throw err;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// ==================== 实用方法 ====================
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 检查模块是否已加载
|
|
308
|
+
* @returns {boolean} 是否已加载
|
|
309
|
+
*/
|
|
310
|
+
Mod.prototype.isLoaded = function () {
|
|
311
|
+
return this.is_loaded;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 日志输出
|
|
316
|
+
* @param {string} level 日志级别
|
|
317
|
+
* @param {string} message 日志消息
|
|
318
|
+
* @param {...any} params 日志参数
|
|
319
|
+
*/
|
|
320
|
+
Mod.prototype.log = function (level, message) {
|
|
321
|
+
var logger = this.getLogger ? this.getLogger() : console;
|
|
322
|
+
var prefix = '[' + (this.constructor.name || 'Mod') + '.' + ((this.config && this.config.name) || '') + ']';
|
|
323
|
+
if (logger[level]) {
|
|
324
|
+
logger[level](prefix + ' ' + message);
|
|
325
|
+
} else {
|
|
326
|
+
console.log(prefix + ' [' + level + '] ' + message);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 获取事件管理器
|
|
332
|
+
* @returns {object} 事件管理器
|
|
333
|
+
*/
|
|
334
|
+
Mod.prototype.getEventer = function () {
|
|
335
|
+
return this._eventer || (typeof $.eventer !== 'undefined' ? $.eventer : this);
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 设置事件管理器
|
|
340
|
+
* @param {object} eventer 事件管理器
|
|
341
|
+
*/
|
|
342
|
+
Mod.prototype.setEventer = function (eventer) {
|
|
343
|
+
this._eventer = eventer;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* 触发事件
|
|
348
|
+
* @param {string} event 事件名
|
|
349
|
+
* @param {...any} args 事件参数
|
|
350
|
+
*/
|
|
351
|
+
Mod.prototype.emitEvent = async function (event) {
|
|
352
|
+
var eventer = this.getEventer();
|
|
353
|
+
if (eventer && eventer.emitWait) {
|
|
354
|
+
await eventer.emitWait(event);
|
|
355
|
+
} else if (eventer && eventer.emit) {
|
|
356
|
+
await eventer.emit(event);
|
|
357
|
+
} else if (this.emit) {
|
|
358
|
+
this.emit(event);
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* 触发事件 - 随机触发
|
|
364
|
+
* @param {string} event 事件名
|
|
365
|
+
* @param {...any} args 事件参数
|
|
366
|
+
* @returns {Promise<any>} 事件触发结果
|
|
367
|
+
*/
|
|
368
|
+
Mod.prototype.emitEventRace = async function (event) {
|
|
369
|
+
var eventer = this.getEventer();
|
|
370
|
+
if (eventer && eventer.emitRace) {
|
|
371
|
+
return await eventer.emitRace(event);
|
|
372
|
+
}
|
|
373
|
+
return null;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* 触发事件 - 返回第一个非空结果
|
|
378
|
+
* @param {string} event 事件名
|
|
379
|
+
* @param {...any} args 事件参数
|
|
380
|
+
* @returns {Promise<any>} 事件触发结果
|
|
381
|
+
*/
|
|
382
|
+
Mod.prototype.emitEventFirst = async function (event) {
|
|
383
|
+
var eventer = this.getEventer();
|
|
384
|
+
if (eventer && eventer.emitFirst) {
|
|
385
|
+
return await eventer.emitFirst(event);
|
|
386
|
+
}
|
|
387
|
+
return null;
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* 注册事件监听
|
|
392
|
+
* @param {string} event 事件名
|
|
393
|
+
* @param {Function} callback 事件回调函数
|
|
394
|
+
*/
|
|
395
|
+
Mod.prototype.onEvent = function (event, callback) {
|
|
396
|
+
var eventer = this.getEventer();
|
|
397
|
+
if (eventer && eventer.on) {
|
|
398
|
+
eventer.on(event, callback);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* 移除事件监听
|
|
404
|
+
* @param {string} event 事件名
|
|
405
|
+
*/
|
|
406
|
+
Mod.prototype.offEvent = function (event) {
|
|
407
|
+
var eventer = this.getEventer();
|
|
408
|
+
if (eventer && eventer.off) {
|
|
409
|
+
eventer.off(event);
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 是否运行中
|
|
415
|
+
* @returns {boolean} 是否运行中
|
|
416
|
+
*/
|
|
417
|
+
Mod.prototype.isRunning = function () {
|
|
418
|
+
return this.lifecycle_state === 'started';
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* 重载
|
|
423
|
+
* 记录当前状态,清除缓存,重新执行生命周期到原状态
|
|
424
|
+
* @returns {Promise<void>}
|
|
425
|
+
*/
|
|
426
|
+
Mod.prototype.reload = async function () {
|
|
427
|
+
var state = this.lifecycle_state;
|
|
428
|
+
var priority = 0;
|
|
429
|
+
if (state === 'loaded') {
|
|
430
|
+
priority = 1;
|
|
431
|
+
} else if (state === 'inited') {
|
|
432
|
+
priority = 2;
|
|
433
|
+
} else if (state === 'started' || state === 'paused') {
|
|
434
|
+
priority = 3;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// 结束当前状态
|
|
438
|
+
if (this.lifecycle_state === 'started' || this.lifecycle_state === 'paused') {
|
|
439
|
+
await this._endCore();
|
|
440
|
+
}
|
|
441
|
+
if (this.lifecycle_state === 'ended') {
|
|
442
|
+
await this._destroyCore();
|
|
443
|
+
}
|
|
444
|
+
// 恢复为 created 以重新加载
|
|
445
|
+
this.lifecycle_state = 'created';
|
|
446
|
+
|
|
447
|
+
// 重新加载
|
|
448
|
+
if (priority >= 1) {
|
|
449
|
+
await this.load();
|
|
450
|
+
}
|
|
451
|
+
if (priority >= 2) {
|
|
452
|
+
await this.init();
|
|
453
|
+
}
|
|
454
|
+
if (priority >= 3) {
|
|
455
|
+
await this.start();
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* 重启
|
|
461
|
+
* 从 started 重新执行 start
|
|
462
|
+
* @returns {Promise<void>}
|
|
463
|
+
*/
|
|
464
|
+
Mod.prototype.restart = async function () {
|
|
465
|
+
if (this.lifecycle_state !== 'started') {
|
|
466
|
+
throw new Error('模块未启动,无法重启');
|
|
467
|
+
}
|
|
468
|
+
await this._endCore();
|
|
469
|
+
this.lifecycle_state = 'inited';
|
|
470
|
+
await this._startCore();
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* 主方法名
|
|
475
|
+
* @param {...any} args 方法参数
|
|
476
|
+
* @returns {Promise<any>} 方法执行结果
|
|
477
|
+
*/
|
|
478
|
+
Mod.prototype.main = async function () {
|
|
479
|
+
var method = arguments[0];
|
|
480
|
+
var func = this.getMethod(method);
|
|
481
|
+
if (!func) {
|
|
482
|
+
throw new Error('方法 [' + method + '] 不存在');
|
|
483
|
+
}
|
|
484
|
+
var params = Array.prototype.slice.call(arguments, 1);
|
|
485
|
+
return await func.apply(this, params);
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
// ==================== 数据管理 ====================
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* 批量设置脚本方法
|
|
492
|
+
* @param {object} methods 脚本方法对象
|
|
493
|
+
*/
|
|
494
|
+
Mod.prototype.setMethods = function (methods) {
|
|
495
|
+
if (typeof methods !== 'object') {
|
|
496
|
+
throw new Error('参数(methods)必须是对象');
|
|
497
|
+
}
|
|
498
|
+
if (!methods) return;
|
|
499
|
+
for (var key in methods) {
|
|
500
|
+
this.setMethod(key, methods[key]);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* 获取数据
|
|
506
|
+
* @param {string} key 数据键
|
|
507
|
+
* @returns {*} 数据值
|
|
508
|
+
*/
|
|
509
|
+
Mod.prototype.getData = function (key) {
|
|
510
|
+
return this[key];
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* 设置数据
|
|
515
|
+
* @param {string} key 数据键
|
|
516
|
+
* @param {*} value 数据值
|
|
517
|
+
*/
|
|
518
|
+
Mod.prototype.setData = function (key, value) {
|
|
519
|
+
if (typeof key !== 'string') {
|
|
520
|
+
throw new Error('第一个参数(key)必须是字符串');
|
|
521
|
+
}
|
|
522
|
+
if (key === '_datas' || key === '_methods') {
|
|
523
|
+
throw new Error('key 不能为保留键');
|
|
524
|
+
}
|
|
525
|
+
if (value === undefined || value === null) {
|
|
526
|
+
throw new Error('第二个参数(value)不能为undefined或null');
|
|
527
|
+
}
|
|
528
|
+
if (typeof value === 'function') {
|
|
529
|
+
throw new Error('第二个参数(value)不能为函数');
|
|
530
|
+
}
|
|
531
|
+
this._datas[key] = typeof value;
|
|
532
|
+
this[key] = value;
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* 获取所有数据
|
|
537
|
+
* @returns {object} 数据对象
|
|
538
|
+
*/
|
|
539
|
+
Mod.prototype.getDatas = function () {
|
|
540
|
+
var result = {};
|
|
541
|
+
for (var key in this._datas) {
|
|
542
|
+
result[key] = this[key];
|
|
543
|
+
}
|
|
544
|
+
return result;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* 批量设置数据
|
|
549
|
+
* @param {object} datas 数据对象
|
|
550
|
+
*/
|
|
551
|
+
Mod.prototype.setDatas = function (datas) {
|
|
552
|
+
if (typeof datas !== 'object') {
|
|
553
|
+
throw new Error('参数(datas)必须是对象');
|
|
554
|
+
}
|
|
555
|
+
if (!datas) return;
|
|
556
|
+
for (var key in datas) {
|
|
557
|
+
// eslint-disable-next-line mm_eslint/function-name
|
|
558
|
+
this.setData(key, datas[key]);
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* 保存数据到缓存文件
|
|
564
|
+
*/
|
|
565
|
+
Mod.prototype.saveDatas = function () {
|
|
566
|
+
var fs = require('fs');
|
|
567
|
+
var path = require('path');
|
|
568
|
+
var file = (this.config && this.config.cache_file) || '';
|
|
569
|
+
if (!file) {
|
|
570
|
+
file = path.resolve(process.cwd(), 'cache', (this.config && this.config.name) || 'mod', 'data.json');
|
|
571
|
+
}
|
|
572
|
+
var dir = path.dirname(file);
|
|
573
|
+
if (!fs.existsSync(dir)) {
|
|
574
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
575
|
+
}
|
|
576
|
+
var data = this.getDatas();
|
|
577
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2), 'utf8');
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* 从缓存文件加载数据
|
|
582
|
+
*/
|
|
583
|
+
Mod.prototype.loadDatas = function () {
|
|
584
|
+
var fs = require('fs');
|
|
585
|
+
var path = require('path');
|
|
586
|
+
var file = (this.config && this.config.cache_file) || '';
|
|
587
|
+
if (!file) {
|
|
588
|
+
file = path.resolve(process.cwd(), 'cache', (this.config && this.config.name) || 'mod', 'data.json');
|
|
589
|
+
}
|
|
590
|
+
try {
|
|
591
|
+
if (fs.existsSync(file)) {
|
|
592
|
+
var content = fs.readFileSync(file, 'utf8');
|
|
593
|
+
var data = JSON.parse(content);
|
|
594
|
+
this.setDatas(data);
|
|
595
|
+
}
|
|
596
|
+
} catch {
|
|
597
|
+
// 文件不存在或解析失败
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
// ==================== 方法管理 ====================
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* 注册方法
|
|
605
|
+
* @param {string} name 方法名
|
|
606
|
+
* @param {Function} func 函数
|
|
607
|
+
*/
|
|
608
|
+
Mod.prototype.setMethod = function (name, func) {
|
|
609
|
+
if (typeof name !== 'string') {
|
|
610
|
+
throw new TypeError('方法名必须是字符串');
|
|
611
|
+
}
|
|
612
|
+
if (typeof func !== 'function') {
|
|
613
|
+
throw new TypeError('方法必须是函数');
|
|
614
|
+
}
|
|
615
|
+
this._methods[name] = func;
|
|
616
|
+
this[name] = func;
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* 获取方法
|
|
621
|
+
* @param {string} name 方法名
|
|
622
|
+
* @returns {Function|null} 方法函数,不存在返回 null
|
|
623
|
+
*/
|
|
624
|
+
Mod.prototype.getMethod = function (name) {
|
|
625
|
+
return this._methods[name] || null;
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* 删除方法
|
|
630
|
+
* @param {string} name 方法名
|
|
631
|
+
*/
|
|
632
|
+
Mod.prototype.delMethod = function (name) {
|
|
633
|
+
delete this._methods[name];
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* 检查方法是否存在
|
|
638
|
+
* @param {string} name 方法名
|
|
639
|
+
* @returns {boolean} 是否存在
|
|
640
|
+
*/
|
|
641
|
+
Mod.prototype.hasMethod = function (name) {
|
|
642
|
+
return typeof this._methods[name] === 'function';
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* 调用方法
|
|
647
|
+
* 检查 lifecycle_state 和 method 是否存在,使用 _callBefore/_callMain/_callAfter 钩子模式
|
|
648
|
+
* @param {string} name 方法名
|
|
649
|
+
* @param {*} [params] 参数
|
|
650
|
+
* @returns {Promise<*>} 方法执行结果
|
|
651
|
+
*/
|
|
652
|
+
Mod.prototype.call = async function (name, params) {
|
|
653
|
+
var func = this.getMethod(name);
|
|
654
|
+
if (!func) {
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (this.lifecycle_state !== 'started' && this.lifecycle_state !== 'inited') {
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
var result;
|
|
663
|
+
|
|
664
|
+
try {
|
|
665
|
+
result = await this._callBefore(name, params);
|
|
666
|
+
if (result !== undefined) return result;
|
|
667
|
+
result = await this._callMain(func, params);
|
|
668
|
+
result = await this._callAfter(name, params, result);
|
|
669
|
+
} catch (err) {
|
|
670
|
+
this.log('error', '执行方法 ' + name + ' 失败: ', err);
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
return result;
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* 执行前置钩子
|
|
679
|
+
* 查找 methodBefore 方法并执行
|
|
680
|
+
* @param {string} method 方法名
|
|
681
|
+
* @param {*} params 参数
|
|
682
|
+
* @returns {Promise<*>} 前置钩子结果
|
|
683
|
+
* @private
|
|
684
|
+
*/
|
|
685
|
+
Mod.prototype._callBefore = async function (method, params) {
|
|
686
|
+
var before_method = method + 'Before';
|
|
687
|
+
var before_func = this.getMethod(before_method);
|
|
688
|
+
if (typeof before_func !== 'function') return undefined;
|
|
689
|
+
|
|
690
|
+
try {
|
|
691
|
+
return await before_func.call(this, params);
|
|
692
|
+
} catch (err) {
|
|
693
|
+
this.log('error', '执行前置钩子 ' + before_method + ' 失败: ', err);
|
|
694
|
+
return undefined;
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* 执行主方法
|
|
700
|
+
* @param {Function} method_func 方法函数
|
|
701
|
+
* @param {*} params 参数
|
|
702
|
+
* @returns {Promise<*>} 主方法结果
|
|
703
|
+
* @private
|
|
704
|
+
*/
|
|
705
|
+
Mod.prototype._callMain = async function (method_func, params) {
|
|
706
|
+
return await method_func.call(this, params);
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* 执行后置钩子
|
|
711
|
+
* 查找 methodAfter 方法并执行
|
|
712
|
+
* @param {string} method 方法名
|
|
713
|
+
* @param {*} params 参数
|
|
714
|
+
* @param {*} result 主方法结果
|
|
715
|
+
* @returns {Promise<*>} 后置钩子结果
|
|
716
|
+
* @private
|
|
717
|
+
*/
|
|
718
|
+
Mod.prototype._callAfter = async function (method, params, result) {
|
|
719
|
+
var after_method = method + 'After';
|
|
720
|
+
var after_func = this.getMethod(after_method);
|
|
721
|
+
if (typeof after_func !== 'function') return result;
|
|
722
|
+
|
|
723
|
+
try {
|
|
724
|
+
var after = await after_func.call(this, result, params);
|
|
725
|
+
return after !== undefined ? after : result;
|
|
726
|
+
} catch (err) {
|
|
727
|
+
this.log('error', '执行后置钩子 ' + after_method + ' 失败: ', err);
|
|
728
|
+
return result;
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* 获取模块目录
|
|
734
|
+
* @returns {string} 模块目录
|
|
735
|
+
*/
|
|
736
|
+
Mod.prototype.getDir = function () {
|
|
737
|
+
if (!this.getParent) return '';
|
|
738
|
+
var parent = this.getParent();
|
|
739
|
+
return parent ? parent.getDir() || '' : '';
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
module.exports = { Mod };
|