1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.EventManager = exports.EventType = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const log_1 = tslib_1.__importDefault(require("@apify/log"));
|
6 | const utilities_1 = require("@apify/utilities");
|
7 | const async_event_emitter_1 = require("@vladfrangu/async_event_emitter");
|
8 | const configuration_1 = require("../configuration");
|
9 | var EventType;
|
10 | (function (EventType) {
|
11 | EventType["PERSIST_STATE"] = "persistState";
|
12 | EventType["SYSTEM_INFO"] = "systemInfo";
|
13 | EventType["MIGRATING"] = "migrating";
|
14 | EventType["ABORTING"] = "aborting";
|
15 | EventType["EXIT"] = "exit";
|
16 | })(EventType = exports.EventType || (exports.EventType = {}));
|
17 | class EventManager {
|
18 | constructor(config = configuration_1.Configuration.getGlobalConfig()) {
|
19 | Object.defineProperty(this, "config", {
|
20 | enumerable: true,
|
21 | configurable: true,
|
22 | writable: true,
|
23 | value: config
|
24 | });
|
25 | Object.defineProperty(this, "events", {
|
26 | enumerable: true,
|
27 | configurable: true,
|
28 | writable: true,
|
29 | value: new async_event_emitter_1.AsyncEventEmitter()
|
30 | });
|
31 | Object.defineProperty(this, "initialized", {
|
32 | enumerable: true,
|
33 | configurable: true,
|
34 | writable: true,
|
35 | value: false
|
36 | });
|
37 | Object.defineProperty(this, "intervals", {
|
38 | enumerable: true,
|
39 | configurable: true,
|
40 | writable: true,
|
41 | value: {}
|
42 | });
|
43 | Object.defineProperty(this, "log", {
|
44 | enumerable: true,
|
45 | configurable: true,
|
46 | writable: true,
|
47 | value: log_1.default.child({ prefix: 'Events' })
|
48 | });
|
49 | this.events.setMaxListeners(50);
|
50 | }
|
51 | |
52 |
|
53 |
|
54 |
|
55 | async init() {
|
56 | if (this.initialized) {
|
57 | return;
|
58 | }
|
59 | const persistStateIntervalMillis = this.config.get('persistStateIntervalMillis');
|
60 | this.intervals.persistState = (0, utilities_1.betterSetInterval)((intervalCallback) => {
|
61 | this.emit("persistState" , { isMigrating: false });
|
62 | intervalCallback();
|
63 | }, persistStateIntervalMillis);
|
64 | this.initialized = true;
|
65 | }
|
66 | |
67 |
|
68 |
|
69 |
|
70 | async close() {
|
71 | if (!this.initialized) {
|
72 | return;
|
73 | }
|
74 | (0, utilities_1.betterClearInterval)(this.intervals.persistState);
|
75 | this.initialized = false;
|
76 |
|
77 | this.emit("persistState" , { isMigrating: false });
|
78 |
|
79 | await this.waitForAllListenersToComplete();
|
80 | }
|
81 | on(event, listener) {
|
82 | this.events.on(event, listener);
|
83 | }
|
84 | off(event, listener) {
|
85 | if (listener) {
|
86 | this.events.removeListener(event, listener);
|
87 | }
|
88 | else {
|
89 | this.events.removeAllListeners(event);
|
90 | }
|
91 | }
|
92 | emit(event, ...args) {
|
93 | this.events.emit(event, ...args);
|
94 | }
|
95 | isInitialized() {
|
96 | return this.initialized;
|
97 | }
|
98 | |
99 |
|
100 |
|
101 | listenerCount(event) {
|
102 | return this.events.listenerCount(event);
|
103 | }
|
104 | |
105 |
|
106 |
|
107 | listeners(event) {
|
108 | return this.events.listeners(event);
|
109 | }
|
110 | |
111 |
|
112 |
|
113 | waitForAllListenersToComplete() {
|
114 | return this.events.waitForAllListenersToComplete();
|
115 | }
|
116 | }
|
117 | exports.EventManager = EventManager;
|
118 |
|
\ | No newline at end of file |