@fozy-labs/rx-toolkit 0.4.16 → 0.4.17
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/dist/common/react/index.d.ts +1 -0
- package/dist/common/react/index.js +1 -0
- package/dist/common/react/useSyncObservable.js +4 -5
- package/dist/common/react/useUnmount.d.ts +1 -0
- package/dist/common/react/useUnmount.js +24 -0
- package/dist/query/api/cleanAllQueriesCache.d.ts +1 -0
- package/dist/query/api/cleanAllQueriesCache.js +4 -0
- package/dist/query/core/{CleanAllResourcesSignal.d.ts → CleanAllQueriesSignal.d.ts} +1 -1
- package/dist/query/core/{CleanAllResourcesSignal.js → CleanAllQueriesSignal.js} +3 -3
- package/dist/query/core/Opertation/Operation.js +4 -0
- package/dist/query/core/Opertation/OperationAgent.d.ts +2 -0
- package/dist/query/core/Opertation/OperationAgent.js +18 -1
- package/dist/query/core/Resource/Resource.js +3 -3
- package/dist/query/index.d.ts +1 -1
- package/dist/query/index.js +1 -1
- package/dist/query/react/useOperationAgent.js +4 -1
- package/dist/query/react/useResourceAgent.js +4 -4
- package/dist/query/types/Operation.types.d.ts +2 -0
- package/package.json +1 -1
- package/dist/query/api/cleanAllResources.d.ts +0 -1
- package/dist/query/api/cleanAllResources.js +0 -4
|
@@ -2,6 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import { BehaviorSubject } from 'rxjs';
|
|
3
3
|
import { useConstant } from "./useConstant";
|
|
4
4
|
import { useEventHandler } from "./useEventHandler";
|
|
5
|
+
import { useUnmount } from "../../common/react/useUnmount";
|
|
5
6
|
const NONE = Symbol('NONE');
|
|
6
7
|
/**
|
|
7
8
|
* Hook for automatically subscribing and unsubscribing from an Observable.
|
|
@@ -29,11 +30,9 @@ export function useSyncObservable(input$, initialValue = NONE) {
|
|
|
29
30
|
subscription,
|
|
30
31
|
};
|
|
31
32
|
}, [input$]);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
subject$.complete();
|
|
36
|
-
};
|
|
33
|
+
useUnmount(() => {
|
|
34
|
+
subscription.unsubscribe();
|
|
35
|
+
subject$.complete();
|
|
37
36
|
}, [subject$]);
|
|
38
37
|
const subscribe = React.useCallback((updateStore) => {
|
|
39
38
|
const subjectSubscription = subject$.subscribe(updateStore);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useUnmount(fn: () => void, deps?: any[]): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export function useUnmount(fn, deps = []) {
|
|
3
|
+
const preventUnmountRef = React.useRef(null);
|
|
4
|
+
React.useEffect(() => {
|
|
5
|
+
return () => {
|
|
6
|
+
let isPrevented = false;
|
|
7
|
+
preventUnmountRef.current = () => {
|
|
8
|
+
isPrevented = true;
|
|
9
|
+
};
|
|
10
|
+
new Promise((resolve) => {
|
|
11
|
+
resolve();
|
|
12
|
+
}).then(() => {
|
|
13
|
+
if (isPrevented)
|
|
14
|
+
return;
|
|
15
|
+
preventUnmountRef.current = null;
|
|
16
|
+
fn();
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
}, deps);
|
|
20
|
+
React.useEffect(() => {
|
|
21
|
+
preventUnmountRef.current?.();
|
|
22
|
+
preventUnmountRef.current = null;
|
|
23
|
+
}, deps);
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function cleanAllQueriesCache(): void;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Subject } from "rxjs";
|
|
2
2
|
import { Batcher } from "../../signals";
|
|
3
|
-
export class
|
|
3
|
+
export class CleanAllQueriesSignal {
|
|
4
4
|
static subject$ = new Subject();
|
|
5
|
-
static clean$ =
|
|
5
|
+
static clean$ = CleanAllQueriesSignal.subject$;
|
|
6
6
|
static clean() {
|
|
7
7
|
Batcher.batch(() => {
|
|
8
|
-
|
|
8
|
+
CleanAllQueriesSignal.subject$.next();
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
}
|
|
@@ -3,6 +3,7 @@ import { Batcher } from "../../../signals";
|
|
|
3
3
|
import { QueriesCache } from "../QueriesCache";
|
|
4
4
|
import { QueriesLifetimeHooks } from "../QueriesLifetimeHooks";
|
|
5
5
|
import { OperationAgent } from "./OperationAgent";
|
|
6
|
+
import { CleanAllQueriesSignal } from "../../../query/core/CleanAllQueriesSignal";
|
|
6
7
|
class OperationQueryState {
|
|
7
8
|
static create() {
|
|
8
9
|
return {
|
|
@@ -65,6 +66,9 @@ export class Operation {
|
|
|
65
66
|
devtoolsName: _options.devtoolsName,
|
|
66
67
|
});
|
|
67
68
|
this._createLinks();
|
|
69
|
+
CleanAllQueriesSignal.clean$.subscribe(() => {
|
|
70
|
+
this._queriesCache.clear();
|
|
71
|
+
});
|
|
68
72
|
}
|
|
69
73
|
_createLinks() {
|
|
70
74
|
this._options.link?.((linkOptions) => {
|
|
@@ -4,6 +4,7 @@ import type { Operation } from "./Operation";
|
|
|
4
4
|
export declare class OperationAgent<D extends OperationDefinition> implements OperationAgentInstanse<D> {
|
|
5
5
|
private _operation;
|
|
6
6
|
private _operations$;
|
|
7
|
+
private _effect;
|
|
7
8
|
state$: Computed<{
|
|
8
9
|
isLoading: boolean;
|
|
9
10
|
isDone: boolean;
|
|
@@ -17,4 +18,5 @@ export declare class OperationAgent<D extends OperationDefinition> implements Op
|
|
|
17
18
|
private _next;
|
|
18
19
|
initiate(args: D["Args"]): void;
|
|
19
20
|
createAgent(): OperationAgentInstanse<D>;
|
|
21
|
+
complete(): void;
|
|
20
22
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import { Computed, Signal } from "../../../signals";
|
|
1
|
+
import { Computed, Effect, Signal } from "../../../signals";
|
|
2
2
|
export class OperationAgent {
|
|
3
3
|
_operation;
|
|
4
4
|
_operations$ = new Signal({
|
|
5
5
|
current$: null,
|
|
6
6
|
}, { isDisabled: true });
|
|
7
|
+
_effect = new Effect(() => {
|
|
8
|
+
const current$ = this._operations$.value.current$;
|
|
9
|
+
// Если ресурс который мы слушаем очистился, то инициируем его заново с теми же аргументами
|
|
10
|
+
const sub = current$?.onClean$.subscribe(() => {
|
|
11
|
+
this._operations$.next({
|
|
12
|
+
current$: null,
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
return () => {
|
|
16
|
+
sub?.unsubscribe();
|
|
17
|
+
};
|
|
18
|
+
});
|
|
7
19
|
state$ = new Computed(() => {
|
|
8
20
|
const operations = this._operations$.value;
|
|
9
21
|
const currState = operations.current$?.value$.value;
|
|
@@ -51,4 +63,9 @@ export class OperationAgent {
|
|
|
51
63
|
createAgent() {
|
|
52
64
|
return new OperationAgent(this._operation);
|
|
53
65
|
}
|
|
66
|
+
complete() {
|
|
67
|
+
this._effect.complete();
|
|
68
|
+
this._operations$.complete();
|
|
69
|
+
this.state$.complete();
|
|
70
|
+
}
|
|
54
71
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { SharedOptions } from "../../../common/options/SharedOptions";
|
|
1
2
|
import { QueriesCache } from "../QueriesCache";
|
|
2
3
|
import { QueriesLifetimeHooks } from "../QueriesLifetimeHooks";
|
|
3
|
-
import {
|
|
4
|
+
import { CleanAllQueriesSignal } from "../CleanAllQueriesSignal";
|
|
4
5
|
import { ResourceAgent } from "./ResourceAgent";
|
|
5
6
|
import { ResourceRef } from "./ResourceRef";
|
|
6
|
-
import { SharedOptions } from "../../../common/options/SharedOptions";
|
|
7
7
|
class ResourceQueryState {
|
|
8
8
|
static create(args) {
|
|
9
9
|
return {
|
|
@@ -117,7 +117,7 @@ export class Resource {
|
|
|
117
117
|
devtoolsName: _options.devtoolsName,
|
|
118
118
|
});
|
|
119
119
|
this._queriesCache = new QueriesCache(_options.cacheLifetime ?? this._DEFAULT_CACHE_LIFETIME);
|
|
120
|
-
|
|
120
|
+
CleanAllQueriesSignal.clean$.subscribe(() => {
|
|
121
121
|
this._queriesCache.clear();
|
|
122
122
|
});
|
|
123
123
|
}
|
package/dist/query/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './api/createResource';
|
|
2
2
|
export * from './api/createOperation';
|
|
3
|
-
export * from './api/
|
|
3
|
+
export * from './api/cleanAllQueriesCache';
|
|
4
4
|
export * from './SKIP_TOKEN';
|
|
5
5
|
export * from './react/useResourceAgent';
|
|
6
6
|
export * from './react/useResourceRef';
|
package/dist/query/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './api/createResource';
|
|
2
2
|
export * from './api/createOperation';
|
|
3
|
-
export * from './api/
|
|
3
|
+
export * from './api/cleanAllQueriesCache';
|
|
4
4
|
export * from './SKIP_TOKEN';
|
|
5
5
|
export * from './react/useResourceAgent';
|
|
6
6
|
export * from './react/useResourceRef';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useConstant, useEventHandler } from "../../common/react";
|
|
1
|
+
import { useConstant, useEventHandler, useUnmount } from "../../common/react";
|
|
2
2
|
import { useSignal } from "../../signals/react";
|
|
3
3
|
export function useOperationAgent(op) {
|
|
4
4
|
const agent = useConstant(() => op.createAgent());
|
|
@@ -19,5 +19,8 @@ export function useOperationAgent(op) {
|
|
|
19
19
|
});
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
|
+
useUnmount(() => {
|
|
23
|
+
agent.complete();
|
|
24
|
+
}, [agent]);
|
|
22
25
|
return [trigger, state];
|
|
23
26
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import { useConstant } from "../../common/react";
|
|
2
|
+
import { useConstant, useUnmount } from "../../common/react";
|
|
3
3
|
import { useSignal } from "../../signals/react";
|
|
4
4
|
import { SKIP } from "../../query/SKIP_TOKEN";
|
|
5
5
|
export function useResourceAgent(res, ...argss) {
|
|
6
6
|
const args = (argss[0] === SKIP ? SKIP : argss[0]);
|
|
7
|
-
const prevArgsRef = React.useRef(
|
|
7
|
+
const prevArgsRef = React.useRef(SKIP);
|
|
8
8
|
const agent = useConstant(() => {
|
|
9
9
|
const agent = res.createAgent();
|
|
10
10
|
if (args !== SKIP) {
|
|
@@ -16,8 +16,8 @@ export function useResourceAgent(res, ...argss) {
|
|
|
16
16
|
prevArgsRef.current = args;
|
|
17
17
|
agent.initiate(args);
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
useUnmount(() => {
|
|
20
20
|
agent.complete();
|
|
21
|
-
}
|
|
21
|
+
});
|
|
22
22
|
return useSignal(agent.state$);
|
|
23
23
|
}
|
|
@@ -132,6 +132,8 @@ export type OperationAgentInstanse<D extends OperationDefinition> = {
|
|
|
132
132
|
initiate(args: D["Args"]): void;
|
|
133
133
|
/** Создает новый агент операции */
|
|
134
134
|
createAgent(): OperationAgentInstanse<D>;
|
|
135
|
+
/** Завершает все текущие выполнения операции и очищает ресурсы */
|
|
136
|
+
complete(): void;
|
|
135
137
|
};
|
|
136
138
|
/**
|
|
137
139
|
* Состояние выполнения операции
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function cleanAllResources(): void;
|