@cascateer/core 2.1.12 → 2.1.14

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.1.12",
3
+ "version": "2.1.14",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cascateer/core.git"
package/src/api.ts CHANGED
@@ -8,7 +8,9 @@ import {
8
8
  } from "lodash";
9
9
  import objectHash from "object-hash";
10
10
  import {
11
+ BehaviorSubject,
11
12
  combineLatest,
13
+ defer,
12
14
  filter,
13
15
  finalize,
14
16
  lastValueFrom,
@@ -48,26 +50,51 @@ class Memoizable<Args, Result> {
48
50
 
49
51
  this.subscribe = (invalidatedTags) => {
50
52
  const memoizedEffect: AsyncEffect<Args, Result> = memoize(
51
- (args) =>
52
- new AsyncObservable((complete) =>
53
- this.predicate(args).pipe(
54
- finalize(() => (console.log("completed"), complete())),
55
- repeat({
56
- delay: () =>
57
- combineLatest([
58
- memoizedEffect(args).pipe(
59
- map((result) => this.tags(args, result)),
60
- ),
61
- invalidatedTags,
62
- ]).pipe(
63
- filter(([tags, invalidatedTags]) =>
64
- isEqual(tags, intersectionWith(tags, invalidatedTags)),
65
- ),
53
+ (args) => {
54
+ const pending = new BehaviorSubject(false);
55
+
56
+ return new (class
57
+ extends Observable<Result>
58
+ implements AsyncObservable<Result>
59
+ {
60
+ pending: Observable<boolean>;
61
+
62
+ constructor(
63
+ source: Observable<Result>,
64
+ tags: TagsConstructor<Args, Result>,
65
+ ) {
66
+ super((subscriber) =>
67
+ defer(
68
+ () => (
69
+ pending.next(true),
70
+ source.pipe(
71
+ finalize(() => pending.next(false)),
72
+ repeat({
73
+ delay: () =>
74
+ combineLatest([
75
+ memoizedEffect(args).pipe(
76
+ map((result) => tags(args, result)),
77
+ ),
78
+ invalidatedTags,
79
+ ]).pipe(
80
+ filter(([tags, invalidatedTags]) =>
81
+ isEqual(
82
+ tags,
83
+ intersectionWith(tags, invalidatedTags),
84
+ ),
85
+ ),
86
+ ),
87
+ }),
88
+ shareReplay({ bufferSize: 1, refCount: false }),
89
+ )
66
90
  ),
67
- }),
68
- shareReplay({ bufferSize: 1, refCount: false }),
69
- ),
70
- ),
91
+ ).subscribe(subscriber),
92
+ );
93
+
94
+ this.pending = pending;
95
+ }
96
+ })(this.predicate(args), this.tags);
97
+ },
71
98
  (args) => objectHash(args ?? null),
72
99
  );
73
100
 
@@ -7,12 +7,13 @@ import {
7
7
  Observable,
8
8
  UnaryFunction,
9
9
  } from "rxjs";
10
+ import { ProxyObservable } from "./ProxyObservable";
10
11
 
11
12
  export interface AsyncObservable<T> {
12
13
  pending: Observable<boolean>;
13
14
  }
14
15
 
15
- export class AsyncObservable<T> extends Observable<T> {
16
+ export class AsyncObservable<T> extends ProxyObservable<T> {
16
17
  constructor(
17
18
  source: Observable<T> | UnaryFunction<() => void, Observable<T>>,
18
19
  ) {
@@ -23,10 +24,8 @@ export class AsyncObservable<T> extends Observable<T> {
23
24
  source = constant(source);
24
25
  }
25
26
 
26
- super((subscriber) =>
27
- defer(() => (pending.next(true), source(complete)))
28
- .pipe(finalize(complete))
29
- .subscribe(subscriber),
27
+ super(source(complete), (source) =>
28
+ defer(() => (pending.next(true), source)).pipe(finalize(complete)),
30
29
  );
31
30
 
32
31
  this.pending = pending;
package/src/types.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { Dictionary, mapValues, memoize, tap } from "lodash";
2
- import objectHash from "object-hash";
1
+ import { Dictionary, mapValues, tap } from "lodash";
3
2
  import {
4
3
  combineLatest,
5
4
  distinct,
@@ -43,40 +42,35 @@ export class AsyncEffectInterceptor extends ReplaySubject<
43
42
  intercept<Effects extends Dictionary<AsyncEffect<any, any>>>(
44
43
  effects: Effects,
45
44
  ): AsyncEffects<Effects> {
46
- return mapValues(effects, (effect) =>
47
- memoize(
48
- (args) => tap(effect(args), (source) => this.next(source)),
49
- (args) => objectHash(args ?? null),
50
- ),
45
+ return mapValues(
46
+ effects,
47
+ (effect) => (args) => tap(effect(args), (source) => this.next(source)),
51
48
  );
52
49
  }
53
50
 
54
51
  toAsyncEffect<Args, Result>(
55
52
  effect: Effect<Args, Result>,
56
53
  ): AsyncEffect<Args, Result> {
57
- return memoize(
58
- (args) =>
59
- new (class
60
- extends ProxyObservable<Result>
61
- implements AsyncObservable<Result>
62
- {
63
- pending: Observable<boolean>;
54
+ return (args) =>
55
+ new (class
56
+ extends ProxyObservable<Result>
57
+ implements AsyncObservable<Result>
58
+ {
59
+ pending: Observable<boolean>;
64
60
 
65
- constructor(interceptor: AsyncEffectInterceptor) {
66
- super(effect(args), identity);
61
+ constructor(interceptor: AsyncEffectInterceptor) {
62
+ super(effect(args), identity);
67
63
 
68
- this.pending = interceptor.pipe(
69
- distinct(),
70
- concat(),
71
- switchMap((sources) =>
72
- combineLatest(sources.map((source) => source.pending)),
73
- ),
74
- map((values) => values.some(Boolean)),
75
- );
76
- }
77
- })(this),
78
- (args) => objectHash(args ?? null),
79
- );
64
+ this.pending = interceptor.pipe(
65
+ distinct(),
66
+ concat(),
67
+ switchMap((sources) =>
68
+ combineLatest(sources.map((source) => source.pending)),
69
+ ),
70
+ map((values) => values.some(Boolean)),
71
+ );
72
+ }
73
+ })(this);
80
74
  }
81
75
  }
82
76