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