@nizza/core 0.4.0-alpha.1 → 0.4.0-alpha.11

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.
@@ -0,0 +1,6 @@
1
+ export declare enum NizzaEventStatus {
2
+ CREATED = "CREATED",
3
+ STARTING = "STARTING",
4
+ LIVE = "LIVE",
5
+ FINALIZED = "FINALIZED"
6
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Enumeration of common event wildcards used in the EventBus.
3
+ * This enumeration provides predefined wildcards for different
4
+ * event categories, facilitating event subscription and management.
5
+ *
6
+ * Each wildcard represents a specific category or group of events
7
+ * in the EventBus and is used as a pattern to match multiple events.
8
+ *
9
+ */
10
+ export declare const EventBusWildcards: {
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:**";
22
+ };
23
+ /**
24
+ * Enumeration that defines the names of events used in the application.
25
+ * This enumeration provides a list of event names used throughout
26
+ * the application to facilitate event communication and management.
27
+ *
28
+ * Each event name should be unique and represent a specific action that
29
+ * has occurred in the application.
30
+ *
31
+ */
32
+ export declare const EventNames: {
33
+ readonly EVENT_STATUS_CHANGED: "event:statusChanged";
34
+ };
@@ -1,3 +1,4 @@
1
- export * from './eventBusWildcards';
1
+ export * from './event';
2
+ export * from './eventBus';
2
3
  export * from './i18n';
3
4
  export * from './products';
@@ -1,11 +1,13 @@
1
+ import log from 'loglevel';
1
2
  import { NizzaConfiguration, NizzaProductDataSource } from '../types';
2
- import { DefaultEventBus } from './DefaultEventBus';
3
+ import { BroadcastChannelEventBus } from './event-buses';
3
4
  export declare class NizzaStore {
4
5
  private static _instance;
5
6
  private _productDataSource;
6
7
  private _configManager;
7
8
  private _eventBus;
8
9
  readonly instanceId: string;
10
+ readonly log: log.RootLogger;
9
11
  private constructor();
10
12
  static instance(): NizzaStore;
11
13
  configure(config: NizzaConfiguration): Promise<this>;
@@ -14,7 +16,7 @@ export declare class NizzaStore {
14
16
  get ok(): boolean;
15
17
  get accountName(): string;
16
18
  get productDataSource(): NizzaProductDataSource | null;
17
- get eventBus(): DefaultEventBus;
19
+ get eventBus(): BroadcastChannelEventBus;
18
20
  setProductDataSource(dataSource: NizzaProductDataSource): void;
19
21
  }
20
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 EBPayload {
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
- export interface EBSubscription {
10
+ export interface EventBusSubscription {
8
11
  off: () => void;
9
- event: string;
10
12
  }
11
- export type EBListener = (event: EBPayload) => void;
13
+ export type EventBusListener = (event: EventBusPayload) => void;
12
14
  export interface EventBus {
13
- on(event: string, listener: EBListener): EBSubscription;
14
- emit(event: string, data: any): void;
15
+ on(event: string, listener: EventBusListener): EventBusSubscription;
16
+ emit(event: string, data: any, attrs?: Attributes): void;
15
17
  }
@@ -22,3 +22,7 @@ export type Environment<T> = typeof environment & T;
22
22
  export type RequiredKeys<T> = {
23
23
  [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
24
24
  }[keyof T];
25
+ export type ScriptRenderResponse = {
26
+ data?: any;
27
+ unmount?: () => void;
28
+ };
@@ -1,6 +1,6 @@
1
- import log from 'loglevel';
2
- type Logger = log.Logger;
3
- export declare const logger: log.Logger;
1
+ import { Logger } from 'loglevel';
2
+ export declare const logger: Logger;
3
+ export declare const coreLogger: Logger;
4
4
  /**
5
5
  * Create a LogLevel instance for given context
6
6
  * @param {string} context - The context to create logger for
@@ -25,4 +25,3 @@ export declare function getLogger(context: string): Logger;
25
25
  * @returns {Logger} The initialized LogLevel logger instance
26
26
  */
27
27
  export declare function initializeLogger(context: string): Logger;
28
- export {};
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nizza/core",
3
- "version": "0.4.0-alpha.1",
3
+ "version": "0.4.0-alpha.11",
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.1",
3
+ "version": "0.4.0-alpha.11",
4
4
  "type": "module",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.cjs",
@@ -1,16 +0,0 @@
1
- /**
2
- * Enumeration of common shortcuts for event wildcards.
3
- */
4
- export declare const EventBusWildcards: {
5
- ALL: string;
6
- CORE: string;
7
- CUSTOMIZE_LIVE: string;
8
- EVENT_CHAT: string;
9
- EVENT_PRODUCTS: string;
10
- LIVE_STREAMING_EVENT: string;
11
- PLAYER: string;
12
- SNACK_SIZE_VIDEO: string;
13
- SNACK_SIZE_VIDEO_ADMIN: string;
14
- UI: string;
15
- UTILS: string;
16
- };
@@ -1,7 +0,0 @@
1
- import { EventBus, EBListener, EBSubscription } from '../types';
2
- export declare class DefaultEventBus implements EventBus {
3
- private emitter;
4
- constructor();
5
- on(event: string, listener: EBListener): EBSubscription;
6
- emit(event: string, data: any): void;
7
- }