@bloqz/relay 1.2.0 → 2.0.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.
- package/dist/create.d.ts +2 -2
- package/dist/create.js +10 -54
- package/dist/create.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +25 -23
- package/package.json +2 -2
package/dist/create.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Relay } from "./
|
|
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(): Relay
|
|
6
|
+
export declare function createRelay<Events extends RelayEventsMap>(): 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(
|
|
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
|
-
//
|
|
69
|
-
filter(
|
|
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
|
-
|
|
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();
|
package/dist/create.js.map
CHANGED
|
@@ -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;
|
|
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;AAUxC;;;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":"
|
|
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
|
|
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:
|
|
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:
|
|
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
|
|
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
|
|
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
|
|
65
|
-
* on('cart', (topic, event) => { ... }); // same as cart.*
|
|
71
|
+
* on('user', (event) => { ... });
|
|
66
72
|
* on('*', (topic, event) => { ... });
|
|
67
73
|
*/
|
|
68
|
-
on(
|
|
74
|
+
on<T extends keyof Events>(topic: T, callback: RelayHandler<Events[T]>): () => void;
|
|
69
75
|
/**
|
|
70
|
-
* Registers a callback for events
|
|
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
|
|
74
|
-
* @param callback The callback to execute
|
|
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(
|
|
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": "
|
|
3
|
+
"version": "2.0.1",
|
|
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.
|
|
42
|
+
"@bloqz/core": "^1.2.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {},
|
|
45
45
|
"scripts": {
|