@cascateer/core 2.2.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cascateer/core",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cascateer/core.git"
package/src/api.ts CHANGED
@@ -4,9 +4,7 @@ import {
4
4
  intersectionWith,
5
5
  isEqual,
6
6
  isFunction,
7
- memoize,
8
7
  } from "lodash";
9
- import objectHash from "object-hash";
10
8
  import {
11
9
  combineLatest,
12
10
  filter,
@@ -22,6 +20,7 @@ import {
22
20
  UnaryFunction,
23
21
  } from "rxjs";
24
22
  import { asObservable, ExtendableDictionary, property } from "./lib";
23
+ import { memoizeHashed } from "./lib/memoizeHashed";
25
24
  import { ProxyObservable } from "./observable";
26
25
  import { Action, MaybeArray, MaybeObservable, ProxyEffect } from "./types";
27
26
 
@@ -48,7 +47,7 @@ class Memoizable<Args, Result> {
48
47
  this.tags = isFunction(tags) ? tags : constant([tags ?? []].flat());
49
48
 
50
49
  this.subscribe = (invalidatedTags) => {
51
- const memoizedEffect: ProxyEffect<Args, Result> = memoize(
50
+ const memoizedEffect: ProxyEffect<Args, Result> = memoizeHashed(
52
51
  (args) =>
53
52
  new ProxyObservable((pending) =>
54
53
  this.predicate(args).pipe(
@@ -72,7 +71,6 @@ class Memoizable<Args, Result> {
72
71
  shareReplay({ bufferSize: 1, refCount: false }),
73
72
  ),
74
73
  ),
75
- (args) => objectHash(args ?? null),
76
74
  );
77
75
 
78
76
  return memoizedEffect;
@@ -0,0 +1,5 @@
1
+ import { memoize } from "lodash";
2
+ import objectHash from "object-hash";
3
+
4
+ export const memoizeHashed = <T extends (...args: any) => any>(func: T) =>
5
+ memoize(func, (...args: Parameters<T>) => objectHash(args ?? null));
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,73 @@ 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
+ : [],
76
80
  ),
77
- ).subscribe();
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
  });
@@ -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
  >;
package/src/types.ts CHANGED
@@ -2,6 +2,7 @@ import { Dictionary, mapValues, tap } from "lodash";
2
2
  import { combineLatest, ReplaySubject, switchMap, UnaryFunction } from "rxjs";
3
3
  import { Observable } from "rxjs/internal/Observable";
4
4
  import { ObservableInput } from "rxjs/internal/types";
5
+ import { memoizeHashed } from "./lib/memoizeHashed";
5
6
  import { ProxyObservable } from "./observable";
6
7
  import { concat, every, some } from "./operators";
7
8
 
@@ -34,15 +35,15 @@ export class ProxyEffectInterceptor extends ReplaySubject<
34
35
  intercept<Effects extends Dictionary<ProxyEffect<any, any>>>(
35
36
  effects: Effects,
36
37
  ): ProxyEffects<Effects> {
37
- return mapValues(
38
- effects,
39
- (effect) => (args) =>
38
+ return mapValues(effects, (effect) =>
39
+ memoizeHashed((args) =>
40
40
  tap(
41
41
  new ProxyObservable(effect(args), (target, receiver) =>
42
42
  combineLatest([target.pending, receiver.refCount]).pipe(every()),
43
43
  ),
44
44
  (source) => this.next(source),
45
45
  ),
46
+ ),
46
47
  );
47
48
  }
48
49