@cascateer/core 2.2.6 → 2.2.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/multicast.ts +65 -49
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cascateer/core",
3
- "version": "2.2.6",
3
+ "version": "2.2.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cascateer/core.git"
package/src/multicast.ts CHANGED
@@ -1,16 +1,19 @@
1
+ import { partition, unionBy } from "lodash";
1
2
  import {
2
- combineLatest,
3
+ distinct,
4
+ filter,
3
5
  groupBy,
4
6
  map,
5
7
  mergeAll,
6
8
  mergeMap,
7
9
  Observable,
8
- partition,
10
+ scan,
11
+ shareReplay,
9
12
  } from "rxjs";
10
13
  import { v4 } from "uuid";
11
- import { nonNullable } from "./lib";
14
+ import { nonNullable, property } from "./lib";
12
15
  import {
13
- concatLeft,
16
+ concat,
14
17
  exchangeWith,
15
18
  flatMap,
16
19
  MulticastActionMessage,
@@ -28,56 +31,59 @@ declare global {
28
31
  }
29
32
  }
30
33
 
31
- const actions = proxyReplaySubject<
32
- Observable<[MulticastConnectMessage<any>, MulticastActionMessage<any>]>,
33
- {
34
- ports: MessagePort[];
35
- action: MulticastActionMessage<any>;
36
- }
37
- >((messages) =>
38
- messages.pipe(
39
- mergeAll(),
40
- groupBy(([connect]) => connect.data.key),
41
- mergeMap((group) =>
42
- group.pipe(
43
- flatMap(([connect, action], index) =>
44
- index
45
- ? action
46
- : [
47
- {
48
- id: v4(),
49
- type: "seedAction" as const,
50
- data: {
51
- seed: connect.data.seed,
52
- },
53
- },
54
- action,
55
- ],
56
- ),
57
- concatLeft(),
58
- flatMap((actions) =>
59
- 0 in actions
60
- ? {
61
- ports: actions.flatMap((action) => action.origin ?? []),
62
- action: actions[0],
63
- }
64
- : [],
34
+ type InMessages = {
35
+ connect: MulticastConnectMessage;
36
+ actions: MulticastActionMessage<any>[];
37
+ };
38
+
39
+ type OutMessages = {
40
+ actions: MulticastActionMessage<any>[];
41
+ ports: MessagePort[];
42
+ };
43
+
44
+ const actions = proxyReplaySubject<Observable<InMessages>, OutMessages>(
45
+ (messages) =>
46
+ messages.pipe(
47
+ mergeAll(),
48
+ groupBy(({ connect }) => connect.data.key),
49
+ mergeMap((group) =>
50
+ group.pipe(
51
+ scan<InMessages, OutMessages>(
52
+ (outMessages, inMessages, index) => ({
53
+ actions: unionBy(
54
+ outMessages.actions.concat(
55
+ ...[
56
+ {
57
+ id: v4(),
58
+ type: "seedAction" as const,
59
+ data: {
60
+ seed: inMessages.connect.data.seed,
61
+ },
62
+ },
63
+ ].slice(0, Math.max(0, 1 - index)),
64
+ ...inMessages.actions,
65
+ ),
66
+ property("id"),
67
+ ),
68
+ ports: outMessages.ports.concat(inMessages.connect.origin ?? []),
69
+ }),
70
+ {
71
+ actions: new Array<MulticastActionMessage<any>>(),
72
+ ports: new Array<MessagePort>(),
73
+ },
74
+ ),
65
75
  ),
66
76
  ),
67
77
  ),
68
- ),
69
78
  );
70
79
 
71
80
  self.addEventListener("connect", ({ ports }) => {
72
81
  for (const port of ports) {
73
82
  actions.next(
74
83
  actions.pipe(
75
- flatMap(({ ports, action: { origin, ...message } }) =>
76
- (message.type === "seedAction" || ports.includes(port)) &&
77
- (!message.sameOrigin || origin === port)
78
- ? message
79
- : [],
80
- ),
84
+ flatMap(({ ports, actions }) => (ports.includes(port) ? actions : [])),
85
+ distinct(property("id")),
86
+ filter((message) => !message.sameOrigin || message.origin === port),
81
87
  sequence(([action, previousAction]) =>
82
88
  action.type === "seedAction"
83
89
  ? action
@@ -88,10 +94,20 @@ self.addEventListener("connect", ({ ports }) => {
88
94
  ),
89
95
  exchangeWith<MulticastClientMessage, MulticastActionMessage<any>>(port),
90
96
  map((event) => ({ ...event, origin: port })),
91
- (messages) =>
92
- combineLatest(
93
- partition(messages, (message) => message.type === "connect"),
94
- ),
97
+ shareReplay(),
98
+ concat(),
99
+ flatMap((messages) => {
100
+ const [[connect], actions] = partition(
101
+ messages,
102
+ (message) => message.type === "connect",
103
+ );
104
+
105
+ if (connect != null) {
106
+ return [{ connect, actions }];
107
+ }
108
+
109
+ return [];
110
+ }),
95
111
  ),
96
112
  );
97
113
  }