-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpart.js
1078 lines (900 loc) · 27.1 KB
/
xpart.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
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
333
334
335
336
337
338
339
340
341
342
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// xpart.js <https://github.com/jsstuff/xpart>
(function($export, $as) {
"use strict";
/**
* XPart namespace - contains provided APIs and constants.
*
* @namespace
* @alias xpart
*/
const xpart = {};
$export[$as] = xpart;
/**
* XPart version in a "major.minor.patch" form.
*
* @alias xpart.VERSION
*/
const VERSION = xpart.VERSION = "1.0.0";
const kFailed =-1;
const kPending = 0;
const kStarting = 1;
const kRunning = 2;
const kStopping = 3;
const kStopped = 4;
const hasOwn = Object.prototype.hasOwnProperty;
const isArray = Array.isArray;
const slice = Array.prototype.slice;
// ============================================================================
// [Helpers]
// ============================================================================
function checkModule(m) {
if (m == null || typeof m !== "object")
return false;
var name = m.name;
if (typeof name !== "string" || name.length === 0 || name === "__proto__")
return false;
return isArray(m.deps) && typeof m.start === "function";
}
function printModule(m) {
if (m == null && typeof m !== "object")
return "<" + (m === null ? "null" : typeof m) + ">";
else
return "<" + (m.name ? m.name : "invalid") + ">";
}
function comparePriority(a, b) {
return (a.priority || 0) - (b.priority || 0);
}
function resolveDependencies(registered, required) {
// All modules to be initialized (map and array)
var map = {};
var req = [];
var module, name;
var deps, dependency;
var i, j;
// Fill all modules if required contains "*".
if (required.indexOf("*") !== -1) {
for (name in registered) {
map[name] = false;
req.push(name);
}
}
// Fill `map` and `req` by module names specified by `required` argument.
for (i = 0; i < required.length; i++) {
name = required[i];
if (hasOwn.call(map, name) || name === "*")
continue;
map[name] = false;
req.push(name);
}
// Add all dependency names to `map` and `req`. The `req` array can grow
// during the loop, but only module names that aren't in `map` are added.
// In other words, `req` will still contain unique module names after the
// loop ends.
for (i = 0; i < req.length; i++) {
name = req[i];
if (!hasOwn.call(registered, name))
return Error("Module '" + name + "' not found");
module = registered[name];
deps = module.deps;
for (j = 0; j < deps.length; j++) {
dependency = deps[j];
if (hasOwn.call(map, dependency))
continue;
if (!hasOwn.call(registered, dependency))
return Error("Module '" + name + "' dependency '" + dependency + "' not found");
if (hasOwn.call(map, dependency))
continue;
map[dependency] = false;
req.push(dependency);
}
}
// Resolve the order of initialization of modules specified in `req`. All
// modules that are already initialized will set `map[name]` to `true`.
var result = [];
var modulesCount = req.length;
var resolved = [];
var unresolved = [];
var tmp;
var isOk;
var hasPriority;
while (result.length !== modulesCount) {
resolved.length = 0;
hasPriority = false;
// Collect all modules that can be initialized right now.
for (i = 0; i < req.length; i++) {
name = req[i];
// Already resolved.
if (map[name] === true)
continue;
module = registered[name];
deps = module.deps;
isOk = true;
for (j = 0; j < deps.length; j++) {
dependency = deps[j];
if (map[dependency] === false) {
isOk = false;
break;
}
}
if (isOk) {
resolved.push(module);
if (module.priority)
hasPriority = true;
}
else {
unresolved.push(name);
}
}
if (resolved.length === 0)
return Error("Cyclic dependency when resolving '" + req.join("', '") + "'");
// If priority has been set in one or more module, sort by priority.
if (hasPriority)
resolved.sort(comparePriority);
// Ok now push all modules from `thisRun` into the `result` array.
for (i = 0; i < resolved.length; i++) {
module = resolved[i];
name = module.name;
map[name] = true;
result.push(name);
}
// Swap `req` and `unresolved` and clear `unresolved`.
tmp = req;
req = unresolved;
unresolved = tmp;
unresolved.length = 0;
}
return result;
}
function makeCallback(app, type, module, next) {
var n = 0;
return function(err) {
if (++n !== 1) {
// Put to log just once.
if (n === 2)
app.error("[xpart.app] Module '" + module.name + "' callbacked " + type + "() twice");
throw new Error("Module '" + module.name + "' callbacked " + type + "() " + n + " times");
}
next(err);
};
}
function makeLogFunc(level) {
return function(msg /*[, ...]*/) {
var logger = this.logger;
var argLen = arguments.length;
if (argLen <= 1) {
logger.log(level, msg);
return this;
}
var args = [level, msg];
for (var i = 1; i < argLen; i++) {
args.push(arguments[i]);
}
logger.log.apply(logger, args);
return this;
};
}
function callAsync(fn, err) {
setImmediate(fn, err);
}
function callHandlers(app, action) {
var handlers = app._internal.handlers;
var list = handlers[action];
// Prevents from adding new handlers for this action.
handlers[action] = null;
for (var i = 0, len = list.length; i < len; i++) {
var handler = list[i];
handler.func.call(handler.thisArg, app);
}
}
// ============================================================================
// [API]
// ============================================================================
/**
* Parses application's arguments from argv[] to an object.
*
* @param {string[]} argv Arguments array.
* @param {number} [start=2] Where to start parsing.
*
* @alias xapp.parseArguments
*/
function parseArguments(argv, start) {
var reOne = /^-(\w+)$/;
var reTwo = /^--([^-][\w-]*)(=.*)?$/;
var obj = {};
var prevKey = "--";
var m, k, v;
// Default is to start processing from the third parameter.
if (start == null)
start = 2;
for (var i = start; i < argv.length; i++) {
v = argv[i];
m = v.match(reOne);
if (m) {
v = m[1];
for (var j = 0; j < v.length; j++)
obj["-" + v[j]] = "true";
continue;
}
m = v.match(reTwo);
if (m) {
k = m[1];
v = m[2] ? m[2].substring(1) : true;
}
else {
k = prevKey;
}
if (hasOwn.call(obj, k) && obj[k] !== true) {
var prev = obj[k];
if (v === true)
continue;
if (isArray(prev))
prev.push(v);
else
obj[k] = [prev, v];
}
else {
obj[k] = v;
}
prevKey = k;
}
for (k in obj) {
if (obj[k] === true)
obj[k] = "true";
}
return obj;
}
xpart.parseArguments = parseArguments;
/**
* Modularizes a module or a class (that has to be instantiated) with `opt`.
*
* What this function does is to return an object that represents an xpart's
* module based on `Module`. It basically returns the expected module object
* that contains `start()` and `stop()` functions (which call start/stop on
* the instantiated module) and other parameters based on `opt`.
*
* The following named parameters (keys in `opt`) are processed:
* `module` - Module or class to be instantiated (by using `new` operator) or
* by calling `module.new()`, which has a priority over using `new`
* operator.
* `as` - Key in `app` to store the instantiated module to. If `as` is not
* present `name` will be used instead.
* `name` - Module name, if not specified `module.name` would be used.
* `deps` - Module dependencies, added to possible deps specified by `module`.
* `config` - Module configuration, passed to the module constructor.
*
* The returned module can be instantiated by `new Module(app, config)`.
*
* @param {object} opt Object that describes the module.
* @return {object} Object that's compatible with xpart's module interface.
*
* @alias xpart.modularize
*/
function modularize(opt) {
var Module = opt.module;
var instance = null;
var name = opt.name || Module.name;
if (!name)
throw new TypeError("xpart.modularize() - Name not specified");
var as = opt.as || name;
var deps = (Module.deps || []).concat(opt.deps || []);
var optCfg = opt.config;
function start(app, cb) {
var config = null;
if (!optCfg)
optCfg = name;
if (typeof optCfg === "object" && optCfg !== null) {
// If the `config` is an object we just pass it to the module as-is.
config = optCfg;
}
else if (typeof optCfg === "string" && hasOwn.call(app.config, optCfg)) {
// If the `config` is a string then it's a key of the `app.config`.
config = app.config[optCfg];
}
// If no configuration has been provided we default to empty object.
if (config == null)
config = {};
if (hasOwn.call(Module, "new"))
instance = Module["new"](app, config);
else
instance = new Module(app, config);
app[as] = instance;
instance.start(cb);
}
function stop(app, cb) {
function resetCb(err) {
// Reset only if the `stop` haven't failed.
if (!err)
app[as] = null;
cb(err);
}
if (typeof instance.stop === "function")
instance.stop(resetCb);
else
setImmediate(resetCb, null);
}
return {
name : name,
deps : deps,
priority: (opt.priority || Module.priority) || 0,
start : start,
stop : stop
};
}
xpart.modularize = modularize;
// ============================================================================
// [BufferedLogger]
// ============================================================================
/**
* Logger that is initialized if no default logger is provided. It buffers all
* logs before the real logger can consume them.
*
* @private
*/
class BufferedLogger {
constructor() {
this._logs = [];
}
log(/* level, msg, ... */) {
this._logs.push(slice.call(arguments, 0));
}
}
// ============================================================================
// [App]
// ============================================================================
/**
* Application.
*
* @class
* @alias xpart.App
*/
class App {
constructor(opt) {
if (!opt)
opt = {};
// Application arguments / configuration [PUBLIC].
this.args = opt.args || {};
this.config = opt.config || {};
// Application logging interface [PUBLIC].
this.logger = opt.logger || null;
// Application internals [PRIVATE].
this._internal = {
state : kPending, // Application's state.
registered : {}, // Modules registered.
running : {}, // Modules running.
initIndex : -1, // Module initialization index.
initOrder : null, // Module initialization order.
handlers: {
afterStart: [], // Handlers called after successful start.
afterStop : [] // Handlers called after successful stop.
},
properties: {
// Stop error.
stopError: null,
// Whether to call `stop()` automatically if `start()` fails.
stopOnFail: Boolean(opt.stopOnFail)
}
};
// Setup logger, bail to BufferedLogger if there is no logger in `opt`.
if (this.logger === null)
this.switchToBufferedLogger();
// Add modules, these can use built-in logger.
if (opt.modules)
this.register(opt.modules);
}
// --------------------------------------------------------------------------
// [Logging Interface]
// --------------------------------------------------------------------------
/**
* Logs a message through the application's logger.
*
* @param {string} level Logging level ("silly", "debug", "info", "warn", "error").
* @param {message} Message (can contain sprintf-like formatting).
* @param {...*} {args} Sprintf-like arguments
*
* @return {this}
*/
log(/*...*/) {
var logger = this.logger;
logger.log.apply(logger, arguments);
return this;
}
/**
* Switches the application's logger to a buffered logger that does only
* buffering of all messages, but doesn't print them.
*
* This logger is set by default on application's startup and buffers all
* messages until a real logger is set by `switchToExternalLogger()` call.
*
* @return {this}
*/
switchToBufferedLogger() {
this.logger = new BufferedLogger();
return this;
}
/**
* Switches the application's logger to the given external `logger`.
*
* If the current application's is a buffered logger then all buffered
* messages will be send to the new `logger`. This ensures that no messages
* will be lost between the real logger is set.
*
* @param {object} logger External logger to use for logging.
* @return {this}
*/
switchToExternalLogger(logger) {
var prev = this.logger;
this.logger = logger;
if (prev && isArray(prev._logs)) {
var logs = prev._logs;
for (var i = 0; i < logs.length; i++)
this.log.apply(this, logs[i]);
}
return this;
}
// --------------------------------------------------------------------------
// [Properties]
// --------------------------------------------------------------------------
/**
* Gets whether the application has a property called `name`.
*
* @param {string} name Property name to check
* @return {boolean} Whether the property exists.
*/
hasProperty(name) {
switch (name) {
case "args":
case "config":
case "logger":
case "state":
return true;
default:
return hasOwn.call(this._internal.properties, name);
}
}
/**
* Returns the content of the property called `name`.
*
* @param {string} name Property name to retrieve.
* @return {*} The content of the property.
*
* @throws {TypeError} If the property doesn't exist.
*/
getProperty(name) {
var internal = this._internal;
switch (name) {
case "args":
case "config":
case "logger":
return this[name];
case "state":
return internal.state;
}
var properties = internal.properties;
if (!hasOwn.call(properties, name))
throw new TypeError("Invalid property '" + name + "'");
return properties[name];
}
/**
* Sets the content of a property `name` to `value`.
*
* @param {string} name Property name to set.
* @param {*} value A new value of the property.
*
* @return {this}
*
* @throws {TypeError} If the property doesn't exist or is read-only.
*/
setProperty(name, value) {
// Handle `setProperty(Object)`.
if (arguments.length === 1 && typeof name === "object") {
for (var k in name)
this.setProperty(k, name[k]);
return this;
}
var internal = this._internal;
switch (name) {
case "args":
case "config":
this[name] = value;
return this;
case "logger":
if (value)
return this.switchToBufferedLogger();
else
return this.switchToExternalLogger(value);
case "state":
throw new TypeError("Property '" + name + "' is read-only");
}
var properties = internal.properties;
if (!hasOwn.call(properties, name))
throw new TypeError("Invalid property '" + name + "'");
properties[name] = value;
return this;
}
// --------------------------------------------------------------------------
// [Module Interface]
// --------------------------------------------------------------------------
/**
* Registers a single module or multiple modules specifed by `m`.
*
* If a module is registered it doesn't mean it has to run, it means that it's
* available to be instantiated. Modules to be run are passed in `App.start()`.
*
* @param {object, object[]) m Module or modules (array) to register.
* @param {string} [path] Optional path of the module (for debugging purposes).
* @return {this}
*/
register(m, path) {
if (isArray(m)) {
var modules = m;
path = path || "";
for (var i = 0, len = modules.length; i < len; i++)
this.register(modules[i], path + "[" + String(i) + "]");
}
else {
this._register(m, path || "<root>");
}
return this;
}
/**
* Registers a single module, called by `register()`.
*
* @param {object} m Module to register
* @param {string} path Module path (for debugging purposes).
*
* @private
*/
_register(m, path) {
if (!checkModule(m))
throw new TypeError("Invalid signature of a module '" + path + "' " + printModule(m));
this._internal.registered[m.name] = m;
}
/**
* Gets if the module `m` has been registered.
*
* @param {string|object} m Module name as string or module instance.
* @return {boolean} True if module has been registered, false otherwise.
*
* @throws {TypeError} If the `m` parameter is invalid (not string nor module).
*/
isModuleRegistered(m) {
var internal = this._internal;
if (typeof m === "string")
return hasOwn.call(internal.registered, m);
else if (checkModule(m))
return hasOwn.call(internal.registered, m.name);
else
throw new TypeError("Invalid argument");
}
/**
* Gets if the module `m` is running.
*
* @param {string|object} m Module name as string or module instance.
* @return {boolean} True if module is running, false otherwise.
*
* @throws {TypeError} If the `m` parameter is invalid (not string nor module).
*/
isModuleRunning(m) {
var internal = this._internal;
if (typeof m === "string")
return hasOwn.call(internal.loaded, m);
else if (checkModule(m))
return hasOwn.call(internal.loaded, m.name);
else
throw new TypeError("Invalid argument");
}
/**
* Returns all modules registered as a mapping between module names and objects.
*
* @return {object} Object where keys are module names and values are module
* objects.
*/
getModulesRegistered() {
return this._internal.registered;
}
/**
* Returns all modules running as a mapping between module names and objects.
*
* @return {object} Object where keys are module names and values are module
* objects.
*/
getModulesRunning() {
return this._internal.running;
}
// --------------------------------------------------------------------------
// [Lifetime Interface]
// --------------------------------------------------------------------------
/**
* Returns the application's state.
*
* @return {number}
*/
getState() {
return this._internal.state;
}
/**
* Gets whether the application is running (i.e. all modules started).
*
* @return {boolean}
*/
isRunning() {
return this._internal.state === kRunning;
}
/**
* Gets whether the application has been stopped (i.e. all modules stopped).
*
* @return {boolean}
*/
isStopped() {
return this._internal.state === kStopped;
}
/**
* Starts the application.
*
* NOTE: The function throws only if the application state is wrong, it never
* throws if a module failed to start or a dependency management failed, in
* such cases it passes an error to the provided start callback.
*
* @param {string[]} required Array of module names, which are required to
* start the application.
* @param {function} cb Start callback, which is called after the application
* starts or when fails to start.
* @return {this}
*
* @throws {Error} If the application state is not `App.kPending`, which means
* that the `App.start()` has been attempted to start multiple times.
*/
start(required, cb) {
var self = this;
var internal = this._internal;
if (internal.state !== kPending) {
var msg = "Attempt to start app multiple times";
self.log("error", "[xpart.app] " + msg);
throw new Error(msg);
}
self.log("silly", "[xpart.app] Starting");
internal.state = kStarting;
var order = resolveDependencies(internal.registered, required);
var module = null;
if (order instanceof Error) {
internal.state = kFailed;
callAsync(cb, order);
return this;
}
var syncOk = 0;
var index;
internal.initIndex = -1;
internal.initOrder = order;
function failed(err) {
internal.state = kFailed;
// Handle the option `stopOnFail`.
if (internal.properties.stopOnFail) {
self.stop(function(stopErr) {
callAsync(cb, err);
});
}
else {
callAsync(cb, err);
}
}
function iterate(err) {
if (err) {
self.log("error", "[xpart.app] Module '" + module.name + "' failed to start: " + err.message);
return failed(err);
}
// Return immediately and handle the result without recursing if sync.
if (--syncOk === 0)
return;
for (;;) {
index = ++internal.initIndex;
syncOk = 1;
if (index >= order.length) {
self.log("silly", "[xpart.app] Running");
internal.state = kRunning;
callAsync(cb, null);
callHandlers(self, "afterStart");
return;
}
module = internal.registered[order[index]];
self.log("silly", "[xpart.app] Module '" + module.name + "' starting");
try {
module.start(self, makeCallback(self, "start", module, iterate));
} catch (ex) {
self.log("error", "[xpart.app] Module '" + module.name + "' failed to start (thrown): " + ex.message);
return failed(ex);
}
if (++syncOk !== 1)
break;
}
}
iterate(null);
return this;
}
/**
* Stops the application.
*
* NOTE: The function throws only if the application state is wrong, it never
* throws if a module failed to stop, in such case it passes an error to the
* provided start callback.
*
* @param {function} cb Stop callback, which is called after the application
* stops or when fails to start.
* @return {this}
*/
stop(cb) {
var self = this;
var internal = this._internal;
var toState = kStopped;
var stopMsg = "";
if (internal.state !== kRunning) {
if (internal.state === kFailed && internal.initIndex !== -1) {
toState = kFailed;
stopMsg = " (stopOnFail)";
}
else {
var msg = internal.state < kRunning
? "Attempt to stop a non-running app"
: "Attempt to stop app multiple times";
self.log("error", "[xpart.app] " + msg);
throw new Error(msg);
}
}
self.log("silly", "[xpart.app] Stopping" + stopMsg);
internal.state = kStopping;
var order = internal.initOrder;
var module = null;
var syncOk = 0;
var index;
function failed(err) {
internal.state = kFailed;
internal.properties.stopError = err;
callAsync(cb, err);
}
function iterate(err) {
if (err) {
self.log("error", "[xpart.app] Module '" + module.name + "' failed to stop: " + err.message);
return failed(err);
}
// Return immediately and handle the result without recursing if sync.
if (--syncOk === 0)
return;
for (;;) {
index = --internal.initIndex;
syncOk = 1;
if (index === -1) {
self.log("silly", "[xpart.app] Stopped" + stopMsg);
internal.state = toState;
callAsync(cb, null);
callHandlers(self, "afterStop");
return;
}
module = internal.registered[order[index]];
self.log("silly", "[xpart.app] Module '" + module.name + "' stopping" + (module.stop ? "" : " (no callback)"));
if (typeof module.stop === "function") {
try {
module.stop(self, makeCallback(self, "stop", module, iterate));
} catch (ex) {
self.log("error", "[xpart.app] Module '" + module.name + "' failed to stop (thrown): " + ex.message);
return failed(ex);
}
if (++syncOk !== 1)
break;
}
}
}
iterate(null);
return this;
}
// --------------------------------------------------------------------------
// [Handlers]
// --------------------------------------------------------------------------
/**
* Adds a handler that will be fired once after the given `action` has happened.
* The following handlers are available:
*
* - `"afterStart"` - Called after the application started successfully.
* - `"afterStop"` - Called after the application stopped successfully.
*/
addHandler(action, func, thisArg) {
var handlers = this._internal.handlers;
if (!hasOwn.call(handlers, action))
throw new Error("Action '" + action + "' doesn't exist");
var list = handlers[action];
if (list === null)
throw new Error("Action '" + action + "' has already fired");
list.push({ func: func, thisArg: thisArg || null });
return this;
}
}
xpart.App = App;
/**
* The application is in a failure state (either start or stop failed).
* @alias xpart.App.kFailed
*/
App.kFailed = kFailed;
/**
* The application is in a pending state (haven't started yet).
* @alias xpart.App.kPending
*/
App.kPending = kPending;
/**
* The application is in a starting state (`start()` called, but haven't finished).
* @alias xpart.App.kStarting
*/
App.kStarting = kStarting;
/**
* The application is in a running state (`start()` finished sucessfully).
* @alias xpart.App.kRunning
*/
App.kRunning = kRunning;
/**