Skip to content

Commit

Permalink
add: Receiving and Handling Events
Browse files Browse the repository at this point in the history
  • Loading branch information
weixiangmeng521 committed Dec 20, 2023
1 parent 20ea44f commit e9e31c8
Show file tree
Hide file tree
Showing 10 changed files with 687 additions and 64 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ teleport.receive('eventName', (data) => {
});
```

### Receiving and Handling Events

```typescript
teleport.multiReceive(['eventName1', 'eventName2'], (data1:any, data2:any) => {
// Handle the event data
console.log('Events data:', data1, data2);
});
```

### Removing a Specific Event Handler

```typescript
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export declare class Teleport {
* @param {(data: T) => void} handler - The handler function to process the event data.
*/
receive<T>(name: string | symbol, handler: (data: T) => void): void;
/**
* Listens for multiple events with a common handler.
* When any of the specified events is emitted, the provided handler will be called.
* @template T - The type of data received by the handler.
* @param nameList - The list of event names to listen for.
* @param handler - The handler function to process the event data.
*/
multiReceive(nameList: string[], handler: (...data: any[]) => void): void;
/**
* Removes a specific event handler for the specified event.
* @param {string | symbol} name - The name or symbol of the event.
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ class Teleport {
receive(name, handler) {
this._teleportSingleton.receive(name, handler);
}
/**
* Listens for multiple events with a common handler.
* When any of the specified events is emitted, the provided handler will be called.
* @template T - The type of data received by the handler.
* @param nameList - The list of event names to listen for.
* @param handler - The handler function to process the event data.
*/
multiReceive(nameList, handler) {
this._teleportSingleton.multiReceive(nameList, handler);
}
/**
* Removes a specific event handler for the specified event.
* @param {string | symbol} name - The name or symbol of the event.
Expand Down
100 changes: 100 additions & 0 deletions lib/internal/teleport.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,103 @@ export declare class TeleportSingleton {
* Singleton instance.
*/
private static _instance;
/**
* Map to store the count of times each individual event has been triggered.
* Key: Event name or symbol, Value: Number of times triggered.
* @protected
*/
protected _eventCountMap: Map<string | symbol, number>;
/**
* Array to store lists of events that are considered as a single multi-event.
* Each list represents a combination of events to be treated collectively.
* @protected
*/
protected _multiEventsList: string[][];
/**
* Map to store the last recorded trace of the total number of times a multi-event has been triggered.
* Key: Combined event name or symbol, Value: Last recorded trace of triggered times.
* @protected
*/
protected _eventsUpdateMap: Map<string | symbol, number>;
/**
* Map to store arbitrary data associated with individual events.
* Key: Event name or symbol, Value: Associated data.
* @protected
*/
protected _eventsDataMap: Map<string | symbol, any>;
/**
* Private constructor to enforce singleton pattern.
*/
private constructor();
/**
* Clears the map storing the count of times each individual event has been triggered.
*/
protected _clearEventCountMap: () => void;
/**
* Clears the array storing lists of events that are considered as single multi-events.
*/
protected _clearMultiEventsList: () => void;
/**
* Clears the map storing the last recorded trace of the total number of times a multi-event has been triggered.
*/
protected _clearEventsUpdateMap: () => void;
/**
* Clears the map storing arbitrary data associated with individual events.
*/
protected _clearEventsDataMap: () => void;
/**
* Adds or updates the total number of times a multi-event has been triggered to the map.
* @param name - Combined event name or symbol.
* @param times - Number of times the multi-event has been triggered.
*/
protected _add2EventsUpdateMap: (name: string | symbol, times: number) => void;
/**
* Retrieves the last recorded trace of the total number of times a multi-event has been triggered from the map.
* @param name - Combined event name.
* @returns The last recorded trace of triggered times for the multi-event.
*/
protected _getEventsUpdateMap: (name: string) => number;
/**
* Adds or updates arbitrary data associated with individual events to the map.
* @param name - Event name.
* @param data - Associated data.
*/
protected _add2EventsDataMap: (name: string | symbol, data: any) => void;
/**
* Retrieves arbitrary data associated with an individual event from the map.
* @param name - Event name.
* @returns The associated data for the event.
*/
protected _getEventsDataMap: (name: string) => any;
/**
* Increments the count of times an individual event has been triggered.
* @param name - Event name or symbol.
*/
protected _addEventsTimes: (name: string | symbol) => void;
/**
* Retrieves the count of times an individual event has been triggered.
* @param name - Event name.
* @returns The count of times the event has been triggered.
*/
protected _getEventsTimes: (name: string) => number;
/**
* Generates a unique token representing the combination of events in the provided list.
* @param nameList - List of event names.
* @returns The generated token.
*/
protected _generateMultiEventsToken: (nameList: string[]) => string;
/**
* Checks for multi-event conditions and triggers automatic emission if conditions are met.
* @protected
*/
protected _checkMultiEvents(): void;
/**
* Automatically emits a multi-event with the provided list of events and their associated data.
* Creates a combined event with a unique token and triggers emission to subscribers.
*
* @param {string[]} eventsList - The list of events to be combined and emitted.
*/
protected _autoEmit(eventsList: string[]): void;
/**
* Method to get or create the singleton instance.
* @returns The singleton instance of TeleportSingleton.
Expand Down Expand Up @@ -52,6 +145,13 @@ export declare class TeleportSingleton {
* @returns The TeleportSingleton instance for chaining.
*/
receive<T>(name: string | symbol, handler: (data: T) => void): TeleportSingleton;
/**
* Method to handle multiple events with a common handler.
* @param nameList - The list of event names.
* @param handler - The handler function to process the event data.
* @returns The TeleportSingleton instance for chaining.
*/
multiReceive(nameList: string[], handler: (...data: any[]) => void): TeleportSingleton;
/**
* Method to remove a specific event handler by name.
* @param name - The name of the event handler to be removed.
Expand Down
Loading

0 comments on commit e9e31c8

Please sign in to comment.