@h3ravel/events 1.29.0-alpha.16 → 2.0.0
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/package.json +3 -5
- package/dist/index.cjs +0 -343
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/events",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Events package for H3ravel.",
|
|
5
5
|
"h3ravel": {
|
|
6
6
|
"providers": [
|
|
@@ -8,9 +8,7 @@
|
|
|
8
8
|
]
|
|
9
9
|
},
|
|
10
10
|
"type": "module",
|
|
11
|
-
"main": "./dist/index.cjs",
|
|
12
11
|
"types": "./dist/index.d.ts",
|
|
13
|
-
"module": "./dist/index.js",
|
|
14
12
|
"exports": {
|
|
15
13
|
".": {
|
|
16
14
|
"import": "./dist/index.js",
|
|
@@ -42,14 +40,14 @@
|
|
|
42
40
|
"laravel"
|
|
43
41
|
],
|
|
44
42
|
"peerDependencies": {
|
|
45
|
-
"@h3ravel/core": "^
|
|
43
|
+
"@h3ravel/core": "^2.0.0"
|
|
46
44
|
},
|
|
47
45
|
"devDependencies": {
|
|
48
46
|
"typescript": "^6.0.0"
|
|
49
47
|
},
|
|
50
48
|
"scripts": {
|
|
51
49
|
"build": "tsdown --config-loader unrun",
|
|
52
|
-
"dev": "
|
|
50
|
+
"dev": "tsdown --watch --config-loader unrun",
|
|
53
51
|
"start": "node dist/index.js",
|
|
54
52
|
"lint": "eslint . --ext .ts",
|
|
55
53
|
"test": "jest --passWithNoTests",
|
package/dist/index.cjs
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let _h3ravel_support = require("@h3ravel/support");
|
|
3
|
-
let _h3ravel_core = require("@h3ravel/core");
|
|
4
|
-
let _h3ravel_contracts = require("@h3ravel/contracts");
|
|
5
|
-
//#region src/Dispatcher.ts
|
|
6
|
-
var Dispatcher = class {
|
|
7
|
-
/**
|
|
8
|
-
* The IoC container instance.
|
|
9
|
-
*/
|
|
10
|
-
container;
|
|
11
|
-
/**
|
|
12
|
-
* The registered event listeners.
|
|
13
|
-
*/
|
|
14
|
-
listeners = {};
|
|
15
|
-
/**
|
|
16
|
-
* The wildcard listeners.
|
|
17
|
-
*/
|
|
18
|
-
wildcards = {};
|
|
19
|
-
/**
|
|
20
|
-
* The cached wildcard listeners.
|
|
21
|
-
*/
|
|
22
|
-
wildcardsCache = {};
|
|
23
|
-
/**
|
|
24
|
-
* The queue resolver instance.
|
|
25
|
-
*/
|
|
26
|
-
queueResolver;
|
|
27
|
-
/**
|
|
28
|
-
* The database transaction manager resolver instance.
|
|
29
|
-
*/
|
|
30
|
-
transactionManagerResolver;
|
|
31
|
-
/**
|
|
32
|
-
* The currently deferred events.
|
|
33
|
-
*/
|
|
34
|
-
deferredEvents = {};
|
|
35
|
-
/**
|
|
36
|
-
* Indicates if events should be deferred.
|
|
37
|
-
*/
|
|
38
|
-
deferringEvents = false;
|
|
39
|
-
/**
|
|
40
|
-
* The specific events to defer (null means defer all events).
|
|
41
|
-
*/
|
|
42
|
-
eventsToDefer;
|
|
43
|
-
/**
|
|
44
|
-
* Create a new event dispatcher instance.
|
|
45
|
-
*/
|
|
46
|
-
constructor(container) {
|
|
47
|
-
this.container = container ?? new _h3ravel_core.Container();
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Register an event listener with the dispatcher.
|
|
51
|
-
*
|
|
52
|
-
* @param events
|
|
53
|
-
* @param listener
|
|
54
|
-
*/
|
|
55
|
-
listen(events, listener) {
|
|
56
|
-
for (const event of _h3ravel_support.Arr.wrap(events)) if (typeof event === "string" && listener) if (event.includes("*")) this.setupWildcardListen(event, listener);
|
|
57
|
-
else this.listeners[event].push(listener);
|
|
58
|
-
else if (typeof event === "function") event(listener);
|
|
59
|
-
else if (typeof listener === "function") listener();
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Setup a wildcard listener callback.
|
|
63
|
-
*
|
|
64
|
-
* @param event
|
|
65
|
-
* @param listener
|
|
66
|
-
*/
|
|
67
|
-
setupWildcardListen(event, listener) {
|
|
68
|
-
this.wildcards[event].push(listener);
|
|
69
|
-
this.wildcardsCache = {};
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Determine if a given event has listeners.
|
|
73
|
-
*
|
|
74
|
-
* @param eventName
|
|
75
|
-
* @return bool
|
|
76
|
-
*/
|
|
77
|
-
hasListeners(eventName) {
|
|
78
|
-
return this.listeners[eventName] || this.wildcards[eventName] || this.hasWildcardListeners(eventName);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Determine if the given event has any wildcard listeners.
|
|
82
|
-
*
|
|
83
|
-
* @param eventName
|
|
84
|
-
*/
|
|
85
|
-
hasWildcardListeners(eventName) {
|
|
86
|
-
for (const [key] of Object.entries(this.wildcards)) if (_h3ravel_support.Str.is(key, eventName)) return true;
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Register an event and payload to be fired later.
|
|
91
|
-
*
|
|
92
|
-
* @para event
|
|
93
|
-
* @param payload
|
|
94
|
-
* @return void
|
|
95
|
-
*/
|
|
96
|
-
push(event, payload = []) {
|
|
97
|
-
this.listen(event + "_pushed", () => {
|
|
98
|
-
this.dispatch(event, payload);
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Flush a set of pushed events.
|
|
103
|
-
*
|
|
104
|
-
* @param event
|
|
105
|
-
*/
|
|
106
|
-
flush(event) {
|
|
107
|
-
this.dispatch(event + "_pushed");
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Resolve the subscriber instance.
|
|
111
|
-
*
|
|
112
|
-
* @param subscriber
|
|
113
|
-
*/
|
|
114
|
-
resolveSubscriber(subscriber) {
|
|
115
|
-
if (typeof subscriber === "string") return this.container.make(subscriber);
|
|
116
|
-
return subscriber;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Fire an event until the first non-null response is returned.
|
|
120
|
-
*
|
|
121
|
-
* @param event
|
|
122
|
-
* @param mixed payload
|
|
123
|
-
* @return mixed
|
|
124
|
-
*/
|
|
125
|
-
until(event, payload = {}) {
|
|
126
|
-
return this.dispatch(event, payload, true);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Fire an event and call the listeners.
|
|
130
|
-
*
|
|
131
|
-
* @param event
|
|
132
|
-
* @param payload
|
|
133
|
-
* @param halt
|
|
134
|
-
*/
|
|
135
|
-
dispatch(event, _payload = [], _halt = false) {}
|
|
136
|
-
/**
|
|
137
|
-
* Remove a set of listeners from the dispatcher.
|
|
138
|
-
*
|
|
139
|
-
* @param event
|
|
140
|
-
*/
|
|
141
|
-
forget(event) {
|
|
142
|
-
if (event.includes("*")) delete this.wildcards[event];
|
|
143
|
-
else delete this.listeners[event];
|
|
144
|
-
for (const [key] of Object.entries(this.wildcardsCache)) if (_h3ravel_support.Str.is(event, key)) delete this.wildcardsCache[key];
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Forget all of the pushed listeners.
|
|
148
|
-
*
|
|
149
|
-
* @return void
|
|
150
|
-
*/
|
|
151
|
-
forgetPushed() {
|
|
152
|
-
for (const [key] of Object.entries(this.listeners)) if (key.endsWith("_pushed")) this.forget(key);
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Get the queue implementation from the resolver.
|
|
156
|
-
*/
|
|
157
|
-
resolveQueue() {
|
|
158
|
-
return this.queueResolver?.();
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Set the queue resolver implementation.
|
|
162
|
-
*
|
|
163
|
-
* @param callable $resolver
|
|
164
|
-
* @return this
|
|
165
|
-
*/
|
|
166
|
-
setQueueResolver(resolver) {
|
|
167
|
-
this.queueResolver = resolver;
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Get the database transaction manager implementation from the resolver.
|
|
172
|
-
*/
|
|
173
|
-
resolveTransactionManager() {
|
|
174
|
-
return this.transactionManagerResolver?.();
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Set the database transaction manager resolver implementation.
|
|
178
|
-
*
|
|
179
|
-
* @param resolver
|
|
180
|
-
*/
|
|
181
|
-
setTransactionManagerResolver(resolver) {
|
|
182
|
-
this.transactionManagerResolver = resolver;
|
|
183
|
-
return this;
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Execute the given callback while deferring events, then dispatch all deferred events.
|
|
187
|
-
*
|
|
188
|
-
* @param callback
|
|
189
|
-
* @param events
|
|
190
|
-
*/
|
|
191
|
-
defer(callback, events) {
|
|
192
|
-
const wasDeferring = this.deferringEvents;
|
|
193
|
-
const previousDeferredEvents = this.deferredEvents;
|
|
194
|
-
const previousEventsToDefer = this.eventsToDefer;
|
|
195
|
-
this.deferringEvents = true;
|
|
196
|
-
this.deferredEvents = {};
|
|
197
|
-
this.eventsToDefer = events;
|
|
198
|
-
try {
|
|
199
|
-
const result = callback();
|
|
200
|
-
this.deferringEvents = false;
|
|
201
|
-
for (const args of Object.entries(this.deferredEvents)) this.dispatch(...args);
|
|
202
|
-
return result;
|
|
203
|
-
} finally {
|
|
204
|
-
this.deferringEvents = wasDeferring;
|
|
205
|
-
this.deferredEvents = previousDeferredEvents;
|
|
206
|
-
this.eventsToDefer = previousEventsToDefer;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Determine if the given event should be deferred.
|
|
211
|
-
*
|
|
212
|
-
* @param event
|
|
213
|
-
*/
|
|
214
|
-
shouldDeferEvent(event) {
|
|
215
|
-
return this.deferringEvents && (this.eventsToDefer === null || this.eventsToDefer?.includes(event));
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Gets the raw, unprepared listeners.
|
|
219
|
-
*
|
|
220
|
-
* @return array
|
|
221
|
-
*/
|
|
222
|
-
getRawListeners() {
|
|
223
|
-
return this.listeners;
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
//#endregion
|
|
227
|
-
//#region src/Providers/EventsServiceProvider.ts
|
|
228
|
-
/**
|
|
229
|
-
* Events handling.
|
|
230
|
-
*/
|
|
231
|
-
var EventsServiceProvider = class extends _h3ravel_core.ServiceProvider {
|
|
232
|
-
static priority = 992;
|
|
233
|
-
static order = "before:RouteServiceProvider";
|
|
234
|
-
register() {
|
|
235
|
-
this.app.singleton("app.events", (app) => {
|
|
236
|
-
return new Dispatcher(app).setQueueResolver(() => {}).setTransactionManagerResolver(function() {});
|
|
237
|
-
});
|
|
238
|
-
this.app.alias([
|
|
239
|
-
["events", "app.events"],
|
|
240
|
-
[Dispatcher, "app.events"],
|
|
241
|
-
[_h3ravel_contracts.IDispatcher, "app.events"]
|
|
242
|
-
]);
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
//#endregion
|
|
246
|
-
//#region src/QueuedListenerCalller.ts
|
|
247
|
-
var QueuedListenerCalller = class {
|
|
248
|
-
/**
|
|
249
|
-
* The underlying queue job instance.
|
|
250
|
-
*/
|
|
251
|
-
job;
|
|
252
|
-
/**
|
|
253
|
-
* The listener class.
|
|
254
|
-
*/
|
|
255
|
-
className;
|
|
256
|
-
/**
|
|
257
|
-
* The listener method.
|
|
258
|
-
*/
|
|
259
|
-
method;
|
|
260
|
-
/**
|
|
261
|
-
* The data to be passed to the listener.
|
|
262
|
-
*/
|
|
263
|
-
data;
|
|
264
|
-
/**
|
|
265
|
-
* The number of times the job may be attempted.
|
|
266
|
-
*/
|
|
267
|
-
tries;
|
|
268
|
-
/**
|
|
269
|
-
* The maximum number of exceptions allowed, regardless of attempts.
|
|
270
|
-
*/
|
|
271
|
-
maxExceptions;
|
|
272
|
-
/**
|
|
273
|
-
* The number of seconds to wait before retrying a job that encountered an uncaught exception.
|
|
274
|
-
*/
|
|
275
|
-
backoff;
|
|
276
|
-
/**
|
|
277
|
-
* The timestamp indicating when the job should timeout.
|
|
278
|
-
*/
|
|
279
|
-
retryUntil;
|
|
280
|
-
/**
|
|
281
|
-
* The number of seconds the job can run before timing out.
|
|
282
|
-
*/
|
|
283
|
-
timeout;
|
|
284
|
-
/**
|
|
285
|
-
* Indicates if the job should fail if the timeout is exceeded.
|
|
286
|
-
*/
|
|
287
|
-
failOnTimeout = false;
|
|
288
|
-
/**
|
|
289
|
-
* Indicates if the job should be encrypted.
|
|
290
|
-
*/
|
|
291
|
-
shouldBeEncrypted = false;
|
|
292
|
-
/**
|
|
293
|
-
* Create a new job instance.
|
|
294
|
-
*
|
|
295
|
-
* @param class
|
|
296
|
-
* @param method
|
|
297
|
-
* @param data
|
|
298
|
-
*/
|
|
299
|
-
constructor(className, method, data) {
|
|
300
|
-
this.data = data;
|
|
301
|
-
this.className = className;
|
|
302
|
-
this.method = method;
|
|
303
|
-
}
|
|
304
|
-
/**
|
|
305
|
-
* Handle the queued job.
|
|
306
|
-
*/
|
|
307
|
-
handle(_container) {}
|
|
308
|
-
/**
|
|
309
|
-
* Set the job instance of the given class if necessary.
|
|
310
|
-
*
|
|
311
|
-
* @param job
|
|
312
|
-
* @param instance
|
|
313
|
-
*/
|
|
314
|
-
setJobInstanceIfNecessary(job, instance) {
|
|
315
|
-
return {};
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Call the failed method on the job instance.
|
|
319
|
-
*
|
|
320
|
-
* The event instance and the exception will be passed.
|
|
321
|
-
*
|
|
322
|
-
* @param e
|
|
323
|
-
*/
|
|
324
|
-
failed(_e) {}
|
|
325
|
-
/**
|
|
326
|
-
* Unserialize the data if needed.
|
|
327
|
-
*
|
|
328
|
-
* @return void
|
|
329
|
-
*/
|
|
330
|
-
prepareData() {}
|
|
331
|
-
/**
|
|
332
|
-
* Get the display name for the queued job.
|
|
333
|
-
*
|
|
334
|
-
* @return string
|
|
335
|
-
*/
|
|
336
|
-
displayName() {
|
|
337
|
-
return this.className;
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
//#endregion
|
|
341
|
-
exports.Dispatcher = Dispatcher;
|
|
342
|
-
exports.EventsServiceProvider = EventsServiceProvider;
|
|
343
|
-
exports.QueuedListenerCalller = QueuedListenerCalller;
|