@adobe-commerce/event-bus 1.0.0 → 1.0.1-beta1
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 +1 -1
- package/src/index.ts +30 -3
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -32,12 +32,28 @@ export class events {
|
|
|
32
32
|
|
|
33
33
|
static _lastEvent: { [key: string]: { payload: any } } = {};
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Returns a scoped event key.
|
|
37
|
+
* @param scope - The scope to get the event from.
|
|
38
|
+
* @returns - The scoped event key.
|
|
39
|
+
*/
|
|
40
|
+
static _getScopedEvent(scope?: string) {
|
|
41
|
+
return <K extends keyof Events>(event: K): K => {
|
|
42
|
+
return (scope ? `${scope}/${event}` : event) as K;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
35
46
|
/**
|
|
36
47
|
* Returns the last payload of the event.
|
|
37
48
|
* @param event – The event to get the last payload from.
|
|
49
|
+
* @param options - Optional configuration for the event.
|
|
38
50
|
* @returns – The last payload of the event.
|
|
39
51
|
*/
|
|
40
|
-
static lastPayload(event: string) {
|
|
52
|
+
static lastPayload<K extends keyof Events>(event: K, options?: { scope?: string }) {
|
|
53
|
+
if (options?.scope) {
|
|
54
|
+
event = this._getScopedEvent(options?.scope)(event);
|
|
55
|
+
}
|
|
56
|
+
|
|
41
57
|
return this._lastEvent[event]?.payload;
|
|
42
58
|
}
|
|
43
59
|
|
|
@@ -50,7 +66,7 @@ export class events {
|
|
|
50
66
|
static on<K extends keyof Events>(
|
|
51
67
|
event: K,
|
|
52
68
|
handler: (payload: Events[K]) => void,
|
|
53
|
-
options?: { eager?: boolean }
|
|
69
|
+
options?: { eager?: boolean, scope?: string }
|
|
54
70
|
) {
|
|
55
71
|
if (typeof BroadcastChannel === 'undefined') {
|
|
56
72
|
return;
|
|
@@ -58,6 +74,11 @@ export class events {
|
|
|
58
74
|
|
|
59
75
|
const subscriber = new BroadcastChannel('ElsieSDK/EventBus');
|
|
60
76
|
|
|
77
|
+
// if scope is provided, get the scoped event key
|
|
78
|
+
if (options?.scope) {
|
|
79
|
+
event = this._getScopedEvent(options?.scope)(event);
|
|
80
|
+
}
|
|
81
|
+
|
|
61
82
|
if (options?.eager) {
|
|
62
83
|
const lastEvent = this._lastEvent[event];
|
|
63
84
|
|
|
@@ -91,15 +112,21 @@ export class events {
|
|
|
91
112
|
* Emits an event.
|
|
92
113
|
* @param event - The event to emit.
|
|
93
114
|
* @param payload - The event payload.
|
|
115
|
+
* @param options - Optional configuration for the event.
|
|
94
116
|
*/
|
|
95
117
|
|
|
96
|
-
static emit<K extends keyof Events>(event: K, payload: Events[K]) {
|
|
118
|
+
static emit<K extends keyof Events>(event: K, payload: Events[K], options?: { scope?: string }) {
|
|
97
119
|
if (typeof BroadcastChannel === 'undefined') {
|
|
98
120
|
return;
|
|
99
121
|
}
|
|
100
122
|
|
|
101
123
|
const publisher = new BroadcastChannel('ElsieSDK/EventBus');
|
|
102
124
|
|
|
125
|
+
// if scope is provided, get the scoped event key
|
|
126
|
+
if (options?.scope) {
|
|
127
|
+
event = this._getScopedEvent(options?.scope)(event);
|
|
128
|
+
}
|
|
129
|
+
|
|
103
130
|
publisher.postMessage({ event, identifier: this._identifier, payload });
|
|
104
131
|
|
|
105
132
|
this._lastEvent[event] = {
|