@nizza/core 0.4.0-alpha.9 → 0.4.1

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.
@@ -9,16 +9,16 @@
9
9
  */
10
10
  export declare const EventBusWildcards: {
11
11
  readonly ALL: "*";
12
- readonly CORE: "core:*";
13
- readonly LIVE_STREAMING_EVENT: "event:*";
14
- readonly EVENT_CHAT: "event:chat:*";
15
- readonly EVENT_PRODUCTS: "event:products:*";
16
- readonly CUSTOMIZE_LIVE: "customize-live:*";
17
- readonly PLAYER: "player:*";
18
- readonly SNACK_SIZE_VIDEO: "ssv:*";
19
- readonly SNACK_SIZE_VIDEO_ADMIN: "ssv:admin:*";
20
- readonly UI: "ui:*";
21
- readonly UTILS: "utils:*";
12
+ readonly CORE: "core:**";
13
+ readonly LIVE_STREAMING_EVENT: "event:**";
14
+ readonly EVENT_CHAT: "event:chat:**";
15
+ readonly EVENT_PRODUCTS: "event:products:**";
16
+ readonly CUSTOMIZE_LIVE: "customize-live:**";
17
+ readonly PLAYER: "player:**";
18
+ readonly SNACK_SIZE_VIDEO: "ssv:**";
19
+ readonly SNACK_SIZE_VIDEO_ADMIN: "ssv-admin:**";
20
+ readonly UI: "ui:**";
21
+ readonly UTILS: "utils:**";
22
22
  };
23
23
  /**
24
24
  * Enumeration that defines the names of events used in the application.
@@ -1,6 +1,6 @@
1
1
  import log from 'loglevel';
2
2
  import { NizzaConfiguration, NizzaProductDataSource } from '../types';
3
- import { DefaultEventBus } from './DefaultEventBus';
3
+ import { BroadcastChannelEventBus } from './event-buses';
4
4
  export declare class NizzaStore {
5
5
  private static _instance;
6
6
  private _productDataSource;
@@ -16,7 +16,7 @@ export declare class NizzaStore {
16
16
  get ok(): boolean;
17
17
  get accountName(): string;
18
18
  get productDataSource(): NizzaProductDataSource | null;
19
- get eventBus(): DefaultEventBus;
19
+ get eventBus(): BroadcastChannelEventBus;
20
20
  setProductDataSource(dataSource: NizzaProductDataSource): void;
21
21
  }
22
22
  export declare const nizza: NizzaStore;
@@ -0,0 +1,88 @@
1
+ import { Attributes, EventBus, EventBusListener, EventBusSubscription } from '../../types';
2
+ /**
3
+ * Implementation of EventBus using BroadcastChannel for cross-window communication.
4
+ */
5
+ export declare class BroadcastChannelEventBus implements EventBus {
6
+ private channel;
7
+ private listeners;
8
+ private eventQueue;
9
+ constructor();
10
+ /**
11
+ * Subscribe a listener function to a specific event with wildcards.
12
+ *
13
+ * @param {string} event - The event name to subscribe to. You can use wildcards like 'user:*' or 'chat:**'.
14
+ * @param {EventBusListener} listener - The listener function to handle the event.
15
+ * @returns {EventBusSubscription} - A subscription object that includes the event name and the `off()` function for unsubscribing.
16
+ * @example
17
+ * const subscription = eventBus.on('user:*', (event) => {
18
+ * console.log('User event:', event.name);
19
+ * });
20
+ * const chatSubscription = eventBus.on('chat:**', (event) => {
21
+ * console.log('Chat event:', event.name);
22
+ * });
23
+ */
24
+ on(event: string, listener: EventBusListener): EventBusSubscription;
25
+ /**
26
+ * Emit an event through the BroadcastChannel.
27
+ *
28
+ * @param {string} event - The event name to emit.
29
+ * @param {any} data - The data associated with the event.
30
+ * @param {Attributes} attrs - Optional attributes associated with the event.
31
+ * @example
32
+ * // Emit a user login event
33
+ * eventBus.emit('user:login', { username: 'user123' });
34
+ *
35
+ * // Emit a chat message event
36
+ * eventBus.emit('chat:message', { text: 'Hello, world!' });
37
+ */
38
+ emit(event: string, data: any, attrs?: Attributes): void;
39
+ /**
40
+ * Handle messages received through the BroadcastChannel and call the corresponding listener functions.
41
+ *
42
+ * @private
43
+ * @param {MessageEvent} event - The received message event.
44
+ */
45
+ private handleMessage;
46
+ /**
47
+ * Subscribe a listener function to a specific event.
48
+ *
49
+ * @private
50
+ * @param {string} event - The event name to subscribe to.
51
+ * @param {EventBusListener} listener - The listener function to handle the event.
52
+ */
53
+ private subscribeToEvent;
54
+ /**
55
+ * Emit an event and deliver it to the corresponding listeners.
56
+ *
57
+ * @private
58
+ * @param {string} event - The event name to emit.
59
+ * @param {any} data - The data associated with the event.
60
+ * @param {Attributes} attrs - Optional attributes associated with the event.
61
+ */
62
+ private emitEvent;
63
+ /**
64
+ * Get listeners that match the event name pattern.
65
+ *
66
+ * @private
67
+ * @param {string} event - The event name to match.
68
+ * @returns {Iterable<[string, EventBusListener]>} - Iterable of matching listeners and their patterns.
69
+ */
70
+ private getMatchingListeners;
71
+ /**
72
+ * Unsubscribe a listener function from a specific event.
73
+ *
74
+ * @private
75
+ * @param {string} event - The event name to unsubscribe from.
76
+ * @param {EventBusListener} listener - The listener function to unsubscribe.
77
+ */
78
+ private unsubscribeFromEvent;
79
+ /**
80
+ * Match wildcards in event names.
81
+ *
82
+ * @private
83
+ * @param {string} pattern - The wildcard pattern to match.
84
+ * @param {string} eventName - The event name to check.
85
+ * @returns {boolean} - `true` if the event name matches the pattern, otherwise `false`.
86
+ */
87
+ private matchWildcard;
88
+ }
@@ -0,0 +1 @@
1
+ export * from './BroadcastChannelEventBus';
@@ -1,15 +1,17 @@
1
- export interface EventBusPayload {
1
+ export type Attributes = Record<string, any>;
2
+ export interface BaseEventBusPayload {
2
3
  name: string;
3
- wildcard: string;
4
4
  data: any;
5
- attrs: Record<string, any>;
5
+ attrs: Attributes;
6
+ }
7
+ export interface EventBusPayload extends BaseEventBusPayload {
8
+ wildcard: string;
6
9
  }
7
10
  export interface EventBusSubscription {
8
11
  off: () => void;
9
- event: string;
10
12
  }
11
13
  export type EventBusListener = (event: EventBusPayload) => void;
12
14
  export interface EventBus {
13
15
  on(event: string, listener: EventBusListener): EventBusSubscription;
14
- emit(event: string, data: any): void;
16
+ emit(event: string, data: any, attrs?: Attributes): void;
15
17
  }
@@ -1,5 +1,6 @@
1
1
  import { Logger } from 'loglevel';
2
2
  export declare const logger: Logger;
3
+ export declare const coreLogger: Logger;
3
4
  /**
4
5
  * Create a LogLevel instance for given context
5
6
  * @param {string} context - The context to create logger for
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nizza/core",
3
- "version": "0.4.0-alpha.9",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.cjs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nizza/core",
3
- "version": "0.4.0-alpha.9",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.cjs",
@@ -1,7 +0,0 @@
1
- import { EventBus, EventBusListener, EventBusSubscription } from '../types';
2
- export declare class DefaultEventBus implements EventBus {
3
- private emitter;
4
- constructor();
5
- on(event: string, listener: EventBusListener): EventBusSubscription;
6
- emit(event: string, data: any): void;
7
- }