@bloqz/relay 2.0.0 → 2.0.2

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/README.md CHANGED
@@ -1,13 +1,12 @@
1
1
  # @bloqz/relay
2
2
 
3
- The `@bloqz/relay` package provides a lightweight, RxJS-powered event bus for enabling communication between different parts of an application. It uses a flexible subscription model that supports both simple string patterns and advanced predicate functions, making it a powerful solution for decoupled architectures.
3
+ The `@bloqz/relay` package provides a lightweight, RxJS-powered event bus for enabling communication between different parts of an application. It uses a flexible subscription model and supports TypeScript generics for full type safety.
4
4
 
5
5
  ## Core Concepts
6
6
 
7
7
  - **Relay**: An event bus that allows for emitting and listening to events.
8
8
  - **Topics**: Named channels for events (e.g., 'user', 'cart'), allowing for targeted communication.
9
- - **Events**: The data payload associated with a topic, which must have a `type` property.
10
- - **Pattern Matching**: A simple yet powerful syntax for subscribing to events based on their topic and type.
9
+ - **Events**: The data payload associated with a topic. All events must be objects and are recommended to have a `type` property.
11
10
 
12
11
  ## Installation
13
12
 
@@ -15,7 +14,7 @@ This package is part of the Bloqz monorepo. To use it, add it as a dependency in
15
14
 
16
15
  ```json
17
16
  "dependencies": {
18
- "@bloqz/relay": "1.0.0"
17
+ "@bloqz/relay": "2.0.2"
19
18
  }
20
19
  ```
21
20
 
@@ -32,53 +31,60 @@ import { createRelay } from '@bloqz/relay';
32
31
  const appRelay = createRelay();
33
32
  ```
34
33
 
34
+ ### Type Safety
35
+
36
+ You can define the events available in your relay to get full TypeScript support.
37
+
38
+ ```typescript
39
+ import { createRelay, RelayEventsMap } from '@bloqz/relay';
40
+
41
+ interface AppEvents extends RelayEventsMap {
42
+ user: { type: 'login'; userId: string } | { type: 'logout' };
43
+ cart: { type: 'add'; productId: string };
44
+ }
45
+
46
+ const appRelay = createRelay<AppEvents>();
47
+
48
+ // Types are checked here!
49
+ appRelay.emit('user', { type: 'login', userId: '123' });
50
+ ```
51
+
35
52
  ### Emitting Events
36
53
 
37
- You can emit an event to a specific topic using the `emit` method. The event payload must be an object with a `type` property.
54
+ You can emit an event to a specific topic using the `emit` method.
38
55
 
39
56
  ```typescript
40
57
  appRelay.emit('user', { type: 'login', userId: '123' });
41
58
  appRelay.emit('notifications', { type: 'new', message: 'Welcome!' });
42
59
  ```
43
60
 
44
- ### Listening with String Patterns
61
+ ### Listening for Events
62
+
63
+ You can listen for events on a specific topic or use the wildcard `*` to listen to all events. The `on` method returns an `unsubscribe` function.
64
+
65
+ #### Specific Topic
45
66
 
46
- You can listen for events using a string pattern with the `on` method. This is the most common way to subscribe. The method returns an `unsubscribe` function.
67
+ When listening to a specific topic, the handler receives the event object.
47
68
 
48
69
  ```typescript
49
- // Listen for login and logout events on the 'user' topic
50
- const unsubscribe = appRelay.on('user.{login|logout}', (topic, event) => {
51
- console.log(`Received event '${event.type}' on topic '${topic}'`);
70
+ const unsubscribe = appRelay.on('user', (event) => {
71
+ console.log(`User event received: ${event.type}`);
52
72
  });
53
73
 
54
74
  // To stop listening
55
75
  unsubscribe();
56
76
  ```
57
77
 
58
- ### Listening with a Predicate Function
78
+ #### Wildcard
59
79
 
60
- For more complex scenarios, you can use a predicate function to filter events. The predicate receives the topic and event and should return `true` if the handler should be executed.
80
+ When listening with `*`, the handler receives both the topic name and the event object.
61
81
 
62
82
  ```typescript
63
- const unsubscribe = appRelay.on((topic, event) => {
64
- return topic === 'user' && event.type === 'login';
65
- }, (topic, event) => {
66
- console.log('User logged in:', event);
83
+ const unsubscribe = appRelay.on('*', (topic, event) => {
84
+ console.log(`Event '${event.type}' received on topic '${topic}'`);
67
85
  });
68
86
  ```
69
87
 
70
- ### Pattern Matching Syntax
71
-
72
- The pattern matching syntax provides a concise way to subscribe to events:
73
-
74
- | Pattern | Description |
75
- | ----------------------- | ----------------------------------------------------- |
76
- | `*` | Matches any topic and any event type. |
77
- | `user` | Matches any event on the `user` topic. |
78
- | `user|cart` | Matches any event on the `user` or `cart` topics. |
79
- | `*.login` | Matches `login` events on any topic. |
80
- | `user.{login|logout}` | Matches `login` or `logout` events on the `user` topic. |
81
-
82
88
  ### Disposing the Relay
83
89
 
84
90
  When a relay is no longer needed, you can dispose of it to complete the underlying event stream and unsubscribe all listeners.
package/dist/create.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Relay, RelayEvent } from "./models.js";
1
+ import { Relay, RelayEventsMap } from "./models.js";
2
2
  /**
3
3
  * Factory function to create a new Relay instance using RxJS.
4
4
  * @returns A new Relay instance.
5
5
  */
6
- export declare function createRelay<Events extends RelayEvent = any>(): Relay<Events>;
6
+ export declare function createRelay<Events extends RelayEventsMap>(): Relay<Events>;
@@ -1 +1 @@
1
- {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AASxC;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,6CAA6C;IAC7C,MAAM,YAAY,GAAG,IAAI,OAAO,EAAwC,CAAC;IAEzE,OAAO;QACL,IAAI,CAAyB,KAAQ,EAAE,KAAgB;YACrD,6CAA6C;YAC7C,YAAY,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,KAAe,EAAE,KAAK,EAAE,CAAC,CAAC;QACvD,CAAC;QACD,EAAE,CACA,cAAuB,EACvB,QAAyE;YAEzE,gDAAgD;YAChD,MAAM,YAAY,GAAG,YAAY;iBAC9B,IAAI;YACH,4CAA4C;YAC5C,MAAM,CACJ,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,cAAc,KAAK,GAAG,IAAI,KAAK,KAAK,cAAc,CAClE,CACF;iBACA,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE;gBAC9B,4DAA4D;gBAC5D,IAAI,cAAc,KAAK,GAAG,EAAE,CAAC;oBAC1B,QAAkD,CAAC,KAAK,EAAE,KAAK,CAAC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AEL,gEAAgE;YAChE,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;QAC1C,CAAC;QACD,OAAO;YACL,wEAAwE;YACxE,YAAY,CAAC,QAAQ,EAAE,CAAC;QAC1B,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bloqz/relay",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "A package for enabling inter-bloc communication using an event relay system.",
5
5
  "keywords": [
6
6
  "bloc",