@cascateer/core 2.2.1 → 2.2.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/package.json +1 -1
- package/src/multicast.ts +74 -52
- package/src/operators/concatLeft.ts +10 -0
- package/src/operators/index.ts +1 -0
- package/src/operators/multicast.ts +3 -2
package/package.json
CHANGED
package/src/multicast.ts
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { tap } from "lodash";
|
|
2
|
+
import {
|
|
3
|
+
combineLatest,
|
|
4
|
+
groupBy,
|
|
5
|
+
map,
|
|
6
|
+
mergeAll,
|
|
7
|
+
mergeMap,
|
|
8
|
+
Observable,
|
|
9
|
+
partition,
|
|
10
|
+
} from "rxjs";
|
|
3
11
|
import { v4 } from "uuid";
|
|
4
|
-
import { nonNullable
|
|
5
|
-
import { Future } from "./observable";
|
|
12
|
+
import { nonNullable } from "./lib";
|
|
6
13
|
import {
|
|
14
|
+
concatLeft,
|
|
7
15
|
exchangeWith,
|
|
8
16
|
flatMap,
|
|
9
17
|
MulticastActionMessage,
|
|
10
18
|
MulticastClientMessage,
|
|
11
|
-
MulticastConnectMessageData,
|
|
12
19
|
proxyReplaySubject,
|
|
13
20
|
sequence,
|
|
14
21
|
} from "./operators";
|
|
22
|
+
import { MulticastConnectMessage } from "./operators/multicast";
|
|
15
23
|
|
|
16
24
|
declare var self: ServiceWorkerGlobalScope;
|
|
17
25
|
|
|
@@ -21,59 +29,73 @@ declare global {
|
|
|
21
29
|
}
|
|
22
30
|
}
|
|
23
31
|
|
|
24
|
-
const
|
|
25
|
-
<
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const actions = proxyReplaySubject<
|
|
33
|
+
Observable<[MulticastConnectMessage<any>, MulticastActionMessage<any>]>,
|
|
34
|
+
{
|
|
35
|
+
ports: MessagePort[];
|
|
36
|
+
action: MulticastActionMessage<any>;
|
|
37
|
+
}
|
|
38
|
+
>((messages) =>
|
|
39
|
+
messages.pipe(
|
|
40
|
+
mergeAll(),
|
|
41
|
+
groupBy(([connect]) => connect.data.key),
|
|
42
|
+
mergeMap((group) =>
|
|
43
|
+
group.pipe(
|
|
44
|
+
flatMap(([connect, action], index) =>
|
|
45
|
+
index
|
|
46
|
+
? action
|
|
47
|
+
: [
|
|
48
|
+
{
|
|
49
|
+
id: v4(),
|
|
50
|
+
type: "seedAction" as const,
|
|
51
|
+
data: {
|
|
52
|
+
seed: connect.data.seed,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
action,
|
|
56
|
+
],
|
|
57
|
+
),
|
|
58
|
+
concatLeft(),
|
|
59
|
+
flatMap((actions) =>
|
|
60
|
+
0 in actions
|
|
61
|
+
? {
|
|
62
|
+
ports: actions.flatMap((action) => action.origin ?? []),
|
|
63
|
+
action: actions[0],
|
|
64
|
+
}
|
|
65
|
+
: [],
|
|
66
|
+
),
|
|
35
67
|
),
|
|
36
68
|
),
|
|
37
|
-
|
|
69
|
+
),
|
|
38
70
|
);
|
|
39
71
|
|
|
40
72
|
self.addEventListener("connect", ({ ports }) => {
|
|
41
73
|
for (const port of ports) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
mergeAll(),
|
|
49
|
-
flatMap(({ origin, ...message }) =>
|
|
50
|
-
!message.sameOrigin || origin === port ? message : [],
|
|
51
|
-
),
|
|
52
|
-
sequence(([action, previousAction]) =>
|
|
53
|
-
action.type === "seedAction"
|
|
54
|
-
? action
|
|
55
|
-
: {
|
|
56
|
-
...action,
|
|
57
|
-
previousId: nonNullable(previousAction).id,
|
|
58
|
-
},
|
|
59
|
-
),
|
|
60
|
-
exchangeWith<MulticastClientMessage, MulticastActionMessage<any>>(
|
|
61
|
-
port,
|
|
62
|
-
),
|
|
63
|
-
flatMap((event) => {
|
|
64
|
-
if (event.type === "connect") {
|
|
65
|
-
actions.subscribe(
|
|
66
|
-
sliceActions.completeWith(memoizedSliceActions(event.data)),
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
return [];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return { ...event, origin: port };
|
|
73
|
-
}),
|
|
74
|
-
tap(actions),
|
|
75
|
-
),
|
|
74
|
+
actions
|
|
75
|
+
.pipe(
|
|
76
|
+
flatMap(({ ports, action: { origin, ...message } }) =>
|
|
77
|
+
ports.includes(port) && (!message.sameOrigin || origin === port)
|
|
78
|
+
? message
|
|
79
|
+
: [],
|
|
76
80
|
),
|
|
77
|
-
|
|
81
|
+
sequence(([action, previousAction]) =>
|
|
82
|
+
action.type === "seedAction"
|
|
83
|
+
? action
|
|
84
|
+
: {
|
|
85
|
+
...action,
|
|
86
|
+
previousId: nonNullable(previousAction).id,
|
|
87
|
+
},
|
|
88
|
+
),
|
|
89
|
+
exchangeWith<MulticastClientMessage, MulticastActionMessage<any>>(port),
|
|
90
|
+
map((event) => ({ ...event, origin: port })),
|
|
91
|
+
(messages) =>
|
|
92
|
+
tap(
|
|
93
|
+
combineLatest(
|
|
94
|
+
partition(messages, (message) => message.type === "connect"),
|
|
95
|
+
),
|
|
96
|
+
(sliceActions) => actions.next(sliceActions),
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
.subscribe();
|
|
78
100
|
}
|
|
79
101
|
});
|
package/src/operators/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { concatMap, shareReplay, startWith, UnaryFunction } from "rxjs";
|
|
2
2
|
import { v4 } from "uuid";
|
|
3
3
|
import { ComputedSignal, ProxySubject } from "../observable";
|
|
4
|
-
import { exchangeWith, proxyReplaySubject } from "../operators";
|
|
5
4
|
import { Transform } from "../types";
|
|
5
|
+
import { exchangeWith } from "./exchangeWith";
|
|
6
|
+
import { proxyReplaySubject } from "./proxyReplaySubject";
|
|
6
7
|
|
|
7
8
|
interface MulticastBaseMessage<Type, Data> {
|
|
8
9
|
id: string;
|
|
@@ -56,7 +57,7 @@ export interface MulticastConnectMessageData<Seed> {
|
|
|
56
57
|
seed: Seed;
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
type MulticastConnectMessage<Seed = any> = MulticastBaseMessage<
|
|
60
|
+
export type MulticastConnectMessage<Seed = any> = MulticastBaseMessage<
|
|
60
61
|
"connect",
|
|
61
62
|
MulticastConnectMessageData<Seed>
|
|
62
63
|
>;
|