@bloqz/relay 1.2.0 → 2.0.0

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/dist/create.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Relay } from "./index.js";
1
+ import { Relay, RelayEvent } 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(): Relay;
6
+ export declare function createRelay<Events extends RelayEvent = any>(): Relay<Events>;
package/dist/create.js CHANGED
@@ -7,69 +7,25 @@ import { filter } from "rxjs/operators";
7
7
  export function createRelay() {
8
8
  // The single, central stream for all events.
9
9
  const eventStream$ = new Subject();
10
- /**
11
- * Parses a pattern and checks if it matches a given topic and event.
12
- * (This helper function remains the same)
13
- */
14
- const matchesPattern = (pattern, topic, event) => {
15
- if (pattern === "*")
16
- return true;
17
- // Helper to split by '|' but ignore it inside '{}'
18
- const splitSubPatterns = (p) => {
19
- const patterns = [];
20
- let lastSplit = 0;
21
- let braceDepth = 0;
22
- for (let i = 0; i < p.length; i++) {
23
- if (p[i] === "{")
24
- braceDepth++;
25
- else if (p[i] === "}")
26
- braceDepth--;
27
- else if (p[i] === "|" && braceDepth === 0) {
28
- patterns.push(p.substring(lastSplit, i));
29
- lastSplit = i + 1;
30
- }
31
- }
32
- patterns.push(p.substring(lastSplit));
33
- return patterns;
34
- };
35
- const subPatterns = splitSubPatterns(pattern);
36
- return subPatterns.some((subPattern) => {
37
- const [topicPattern, typePattern] = subPattern.split(".");
38
- if (topicPattern !== "*" && topicPattern !== topic)
39
- return false;
40
- if (!typePattern)
41
- return true;
42
- if (typePattern === "*")
43
- return true;
44
- if (typePattern.startsWith("{") && typePattern.endsWith("}")) {
45
- const types = typePattern.slice(1, -1).split("|");
46
- return types.includes(event.type);
47
- }
48
- return typePattern === event.type;
49
- });
50
- };
51
10
  return {
52
11
  emit(topic, event) {
53
12
  // Simply push the new event into the stream.
54
- eventStream$.next({ topic, event });
13
+ eventStream$.next({ topic: topic, event });
55
14
  },
56
- on(patternOrPredicate, callback) {
57
- // Create the filter predicate once, before subscribing.
58
- const filterFn = ({ topic, event, }) => {
59
- if (typeof patternOrPredicate === "string") {
60
- return matchesPattern(patternOrPredicate, topic, event);
61
- }
62
- // For predicates, just execute them.
63
- return patternOrPredicate(topic, event);
64
- };
15
+ on(topicOrPattern, callback) {
65
16
  // Create a new subscription to the main stream.
66
17
  const subscription = eventStream$
67
18
  .pipe(
68
- // Use the pre-built filter function. This is more efficient.
69
- filter(filterFn))
19
+ // Filter by topic or allow all if wildcard.
20
+ filter(({ topic }) => topicOrPattern === "*" || topic === topicOrPattern))
70
21
  .subscribe(({ topic, event }) => {
71
22
  // When an event passes the filter, call the user's handler.
72
- callback(topic, event);
23
+ if (topicOrPattern === "*") {
24
+ callback(topic, event);
25
+ }
26
+ else {
27
+ callback(event);
28
+ }
73
29
  });
74
30
  // Return a function that tears down this specific subscription.
75
31
  return () => subscription.unsubscribe();
@@ -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;AAIxC;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,6CAA6C;IAC7C,MAAM,YAAY,GAAG,IAAI,OAAO,EAAwC,CAAC;IAEzE;;;OAGG;IACH,MAAM,cAAc,GAAG,CACrB,OAAe,EACf,KAAa,EACb,KAAiB,EACR,EAAE;QACX,IAAI,OAAO,KAAK,GAAG;YAAE,OAAO,IAAI,CAAC;QAEjC,mDAAmD;QACnD,MAAM,gBAAgB,GAAG,CAAC,CAAS,EAAY,EAAE;YAC/C,MAAM,QAAQ,GAAa,EAAE,CAAC;YAC9B,IAAI,SAAS,GAAG,CAAC,CAAC;YAClB,IAAI,UAAU,GAAG,CAAC,CAAC;Y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AAM,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,CAAC;gBACpE,CAAC;qBAAM,CAAC;oBACL,QAAoC,CAAC,KAAkB,CAAC,CAAC;gBAC5D,CAAC;YACH,CAAC,CAAC,CAAC;YAEL,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/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export type { RelayEvent, RelayHandler, RelayPredicate, Relay } from "./models.js";
1
+ export type { RelayEventsMap, RelayEvent, RelayHandler, RelayPredicate, Relay, } from "./models.js";
2
2
  export { createRelay } from "./create.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
package/dist/models.d.ts CHANGED
@@ -1,3 +1,10 @@
1
+ /**
2
+ * A map of event topics to their corresponding event payloads.
3
+ * This is used to provide type safety for relay operations.
4
+ */
5
+ export type RelayEventsMap = {
6
+ readonly [topic: string]: RelayEvent;
7
+ };
1
8
  /**
2
9
  * The standard shape for any event payload passed through the relay.
3
10
  * It must include a `type` string to allow for pattern-based filtering.
@@ -16,7 +23,13 @@ export type RelayEvent = {
16
23
  * @param topic The topic the event was emitted on (e.g., 'user', 'cart').
17
24
  * @param event The event payload.
18
25
  */
19
- export type RelayHandler = (topic: string, event: RelayEvent) => void;
26
+ export type RelayTopicHandler<T, E> = (topic: T, event: E) => void;
27
+ /**
28
+ * A handler function that receives the event for a matched subscription.
29
+ *
30
+ * @param event The event payload.
31
+ */
32
+ export type RelayHandler<E> = (event: E) => void;
20
33
  /**
21
34
  * A predicate function used for advanced event filtering. It receives the full
22
35
  * event context and returns `true` if the handler should be called.
@@ -25,13 +38,13 @@ export type RelayHandler = (topic: string, event: RelayEvent) => void;
25
38
  * @param event The event payload.
26
39
  * @returns `true` if the subscription handler should be invoked.
27
40
  */
28
- export type RelayPredicate = (topic: string, event: RelayEvent) => boolean;
41
+ export type RelayPredicate<T, E> = (topic: T, event: E) => boolean;
29
42
  /**
30
43
  * A non-generic, RxJS-powered event bus that supports pattern-based
31
44
  * and predicate-based subscriptions. It serves as a central hub for
32
45
  * cross-cutting communication, such as between Blocs.
33
46
  */
34
- export interface Relay {
47
+ export interface Relay<Events extends RelayEventsMap> {
35
48
  /**
36
49
  * Emits an event to a specific topic. All active subscriptions will be
37
50
  * evaluated against the event, and matching handlers will be invoked.
@@ -40,7 +53,7 @@ export interface Relay {
40
53
  * @param event The event payload, which MUST include a `type` property
41
54
  * (e.g., `{ type: 'login', userId: '123' }`).
42
55
  */
43
- emit(topic: string, event: RelayEvent): void;
56
+ emit<T extends keyof Events>(topic: T, event: Events[T]): void;
44
57
  /**
45
58
  * Disposes of the relay, completing its internal event stream and
46
59
  * unsubscribing all listeners. After disposal, the relay can no longer
@@ -48,34 +61,23 @@ export interface Relay {
48
61
  */
49
62
  dispose(): void;
50
63
  /**
51
- * Registers a callback for events matching a specific string pattern.
52
- * The pattern allows for filtering by topic and event type.
64
+ * Registers a callback for events matching a specific topic.
53
65
  *
54
- * @param pattern A string pattern to match against topics and event types.
55
- * - `*`: Wildcard, matches any topic and event.
56
- * - `topic.*`: Matches any event on a specific topic.
57
- * - `*.{type1|type2}`: Matches specific event types on any topic.
58
- * - `topic.{type1|type2}`: Matches specific event types on a specific topic.
59
- * - `topic1|topic2`: Matches any event on a list of topics.
66
+ * @param topic The topic to listen to.
60
67
  * @param callback The callback to execute when a matching event is emitted.
61
68
  * @returns A function to unregister the callback and unsubscribe.
62
69
  *
63
70
  * @example
64
- * on('user.{login|logout}', (topic, event) => { ... });
65
- * on('cart', (topic, event) => { ... }); // same as cart.*
71
+ * on('user', (event) => { ... });
66
72
  * on('*', (topic, event) => { ... });
67
73
  */
68
- on(pattern: string, callback: RelayHandler): () => void;
74
+ on<T extends keyof Events>(topic: T, callback: RelayHandler<Events[T]>): () => void;
69
75
  /**
70
- * Registers a callback for events that pass a custom predicate function.
71
- * This allows for more complex filtering logic than string patterns.
76
+ * Registers a callback for all events emitted on the relay.
72
77
  *
73
- * @param predicate A function that returns `true` for events to listen to.
74
- * @param callback The callback to execute for events that pass the predicate.
78
+ * @param topic The wildcard pattern '*'.
79
+ * @param callback The callback to execute when any event is emitted.
75
80
  * @returns A function to unregister the callback and unsubscribe.
76
- *
77
- * @example
78
- * on((topic, event) => topic === 'user' && event.type === 'login', callback);
79
81
  */
80
- on(predicate: RelayPredicate, callback: RelayHandler): () => void;
82
+ on(topic: "*", callback: RelayTopicHandler<string, RelayEvent>): () => void;
81
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bloqz/relay",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "A package for enabling inter-bloc communication using an event relay system.",
5
5
  "keywords": [
6
6
  "bloc",
@@ -39,7 +39,7 @@
39
39
  "rxjs": "^7.8.2"
40
40
  },
41
41
  "peerDependencies": {
42
- "@bloqz/core": "^1.2.0"
42
+ "@bloqz/core": "^1.2.1"
43
43
  },
44
44
  "devDependencies": {},
45
45
  "scripts": {