@cascateer/core 2.2.1 → 2.2.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cascateer/core",
3
- "version": "2.2.1",
3
+ "version": "2.2.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cascateer/core.git"
package/src/multicast.ts CHANGED
@@ -1,17 +1,25 @@
1
- import { memoize, thru } from "lodash";
2
- import { mergeAll, Observable, startWith, tap } from "rxjs";
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, property } from "./lib";
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,75 @@ declare global {
21
29
  }
22
30
  }
23
31
 
24
- const memoizedSliceActions = memoize(
25
- <Seed>({ seed }: MulticastConnectMessageData<Seed>) =>
26
- proxyReplaySubject<MulticastActionMessage<any>>((actions) =>
27
- actions.pipe(
28
- startWith({
29
- id: v4(),
30
- type: "seedAction" as const,
31
- data: {
32
- seed,
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
- property("key"),
69
+ ),
38
70
  );
39
71
 
40
72
  self.addEventListener("connect", ({ ports }) => {
41
73
  for (const port of ports) {
42
- thru(
43
- new Future<Observable<MulticastActionMessage<any>>>(),
44
- (sliceActions) =>
45
- proxyReplaySubject<MulticastActionMessage<any>, MulticastClientMessage>(
46
- (actions) =>
47
- sliceActions.pipe(
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
+ : [],
80
+ ),
81
+ sequence(([action, previousAction]) =>
82
+ action.type === "seedAction"
83
+ ? action
84
+ : {
85
+ ...action,
86
+ previousId: nonNullable(previousAction).id,
87
+ },
76
88
  ),
77
- ).subscribe();
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
  });
102
+
103
+ actions.subscribe();
@@ -0,0 +1,10 @@
1
+ import { map, OperatorFunction } from "rxjs";
2
+ import { concat } from "./concat";
3
+
4
+ export const concatLeft =
5
+ <T>(): OperatorFunction<T | T[], T[]> =>
6
+ (source) =>
7
+ source.pipe(
8
+ concat(),
9
+ map((value) => value.toReversed()),
10
+ );
@@ -1,4 +1,5 @@
1
1
  export { concat } from "./concat";
2
+ export { concatLeft } from "./concatLeft";
2
3
  export { every } from "./every";
3
4
  export { exchangeWith } from "./exchangeWith";
4
5
  export { flatMap } from "./flatMap";
@@ -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
  >;