@jscutlery/operators 2.1.4 → 3.0.0
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/CHANGELOG.md +41 -0
- package/LICENSE +21 -0
- package/index.cjs.d.ts +1 -0
- package/index.cjs.js +85 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +80 -0
- package/package.json +4 -2
- package/src/index.d.ts +2 -0
- package/src/lib/suspensify.d.ts +59 -0
- package/src/lib/testing/observer.d.ts +10 -0
- package/.babelrc +0 -10
- package/.eslintrc.json +0 -18
- package/jest.config.ts +0 -17
- package/project.json +0 -57
- package/src/index.ts +0 -10
- package/src/lib/suspensify.spec.ts +0 -223
- package/src/lib/suspensify.ts +0 -169
- package/src/lib/testing/observer.ts +0 -33
- package/tsconfig.json +0 -13
- package/tsconfig.lib.json +0 -11
- package/tsconfig.spec.json +0 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
+
# 3.0.0 (2024-04-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### 🚀 Features
|
|
5
|
+
|
|
6
|
+
- **operator:** ✅ add supensify operator ([e7fc483](https://github.com/jscutlery/devkit/commit/e7fc483))
|
|
7
|
+
|
|
8
|
+
- **operators:** ✨add esm support ([77b5470](https://github.com/jscutlery/devkit/commit/77b5470))
|
|
9
|
+
|
|
10
|
+
- ⚠️ **operators:** ✨ drop support for suspensify's projector ([f4bf6dc](https://github.com/jscutlery/devkit/commit/f4bf6dc))
|
|
11
|
+
|
|
12
|
+
- **operators:** add suspensify's strict mode ([f9dd789](https://github.com/jscutlery/devkit/commit/f9dd789))
|
|
13
|
+
|
|
14
|
+
- **operators:** ✨ expose pending state constant ([caaea8e](https://github.com/jscutlery/devkit/commit/caaea8e))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### 🩹 Fixes
|
|
18
|
+
|
|
19
|
+
- 🐞 bump workspace ([127c808](https://github.com/jscutlery/devkit/commit/127c808))
|
|
20
|
+
|
|
21
|
+
- **operators:** 🏷️ fix typing errors ([91c084c](https://github.com/jscutlery/devkit/commit/91c084c))
|
|
22
|
+
|
|
23
|
+
- **operators:** 🐞 fix suspensify type to be strict by default too ([c5d424c](https://github.com/jscutlery/devkit/commit/c5d424c))
|
|
24
|
+
|
|
25
|
+
- **operators:** 🐞 export missing types + pending const ([96eed66](https://github.com/jscutlery/devkit/commit/96eed66))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
#### ⚠️ Breaking Changes
|
|
29
|
+
|
|
30
|
+
- **operators:** suspensify's operator "projector" function is not supported anymore
|
|
31
|
+
|
|
32
|
+
### ❤️ Thank You
|
|
33
|
+
|
|
34
|
+
- Edouard Bozon @edbzn
|
|
35
|
+
- Julien Brun
|
|
36
|
+
- Younes Jaaidi
|
|
37
|
+
|
|
38
|
+
## 2.1.5 (2024-04-11)
|
|
39
|
+
|
|
40
|
+
This was a version bump only for operators to align it with other projects, there were no code changes.
|
|
41
|
+
|
|
1
42
|
## 2.1.4 (2024-03-24)
|
|
2
43
|
|
|
3
44
|
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 jscutlery
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.cjs.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var rxjs = require('rxjs');
|
|
6
|
+
var operators = require('rxjs/operators');
|
|
7
|
+
|
|
8
|
+
function suspensify({
|
|
9
|
+
strict = true
|
|
10
|
+
} = {}) {
|
|
11
|
+
return source$ => {
|
|
12
|
+
const strictSuspense$ = source$.pipe(_suspensify(), _coalesceFirstEmittedValue());
|
|
13
|
+
return strict ? strictSuspense$ : strictSuspense$.pipe(operators.map(strictSuspense => ({
|
|
14
|
+
error: undefined,
|
|
15
|
+
value: undefined,
|
|
16
|
+
...strictSuspense
|
|
17
|
+
}))
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This is the initial state of the Suspense.
|
|
24
|
+
* It is exposed for convenience and edge cases where the initial state must be set initially.
|
|
25
|
+
* e.g. when using {@link suspensify} on inner observables.
|
|
26
|
+
* @example toSignal(source$.pipe(switchMap(() => fetchData().pipe(suspensify()))), {initialValue: pending});
|
|
27
|
+
*/
|
|
28
|
+
const pending = {
|
|
29
|
+
finalized: false,
|
|
30
|
+
hasError: false,
|
|
31
|
+
hasValue: false,
|
|
32
|
+
pending: true
|
|
33
|
+
};
|
|
34
|
+
function _coalesceFirstEmittedValue() {
|
|
35
|
+
return source$ => {
|
|
36
|
+
return new rxjs.Observable(observer => {
|
|
37
|
+
const isReadySubject = new rxjs.ReplaySubject(1);
|
|
38
|
+
const subscription = source$.pipe( /* Wait for all synchronous processing to be done. */
|
|
39
|
+
operators.debounce(() => isReadySubject)).subscribe(observer);
|
|
40
|
+
/* Sync emitted values have been processed now.
|
|
41
|
+
* Mark source as ready and emit last computed state. */
|
|
42
|
+
isReadySubject.next(undefined);
|
|
43
|
+
return () => subscription.unsubscribe();
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/* Use values as types for better type checking. */
|
|
48
|
+
const TRUE = true;
|
|
49
|
+
const FALSE = false;
|
|
50
|
+
function _suspensify() {
|
|
51
|
+
return source$ => {
|
|
52
|
+
return source$.pipe(operators.materialize(), operators.scan((state, notification) => {
|
|
53
|
+
switch (notification.kind) {
|
|
54
|
+
/* Value. */
|
|
55
|
+
case 'N':
|
|
56
|
+
return {
|
|
57
|
+
finalized: FALSE,
|
|
58
|
+
hasError: FALSE,
|
|
59
|
+
hasValue: TRUE,
|
|
60
|
+
value: notification.value,
|
|
61
|
+
pending: FALSE
|
|
62
|
+
};
|
|
63
|
+
/* Error. */
|
|
64
|
+
case 'E':
|
|
65
|
+
return {
|
|
66
|
+
finalized: TRUE,
|
|
67
|
+
hasError: TRUE,
|
|
68
|
+
hasValue: FALSE,
|
|
69
|
+
pending: FALSE,
|
|
70
|
+
error: notification.error
|
|
71
|
+
};
|
|
72
|
+
/* Complete. */
|
|
73
|
+
case 'C':
|
|
74
|
+
return {
|
|
75
|
+
...state,
|
|
76
|
+
finalized: TRUE,
|
|
77
|
+
pending: FALSE
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}, pending), operators.startWith(pending));
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
exports.pending = pending;
|
|
85
|
+
exports.suspensify = suspensify;
|
package/index.esm.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Observable, ReplaySubject } from 'rxjs';
|
|
2
|
+
import { map, debounce, materialize, scan, startWith } from 'rxjs/operators';
|
|
3
|
+
|
|
4
|
+
function suspensify({
|
|
5
|
+
strict = true
|
|
6
|
+
} = {}) {
|
|
7
|
+
return source$ => {
|
|
8
|
+
const strictSuspense$ = source$.pipe(_suspensify(), _coalesceFirstEmittedValue());
|
|
9
|
+
return strict ? strictSuspense$ : strictSuspense$.pipe(map(strictSuspense => ({
|
|
10
|
+
error: undefined,
|
|
11
|
+
value: undefined,
|
|
12
|
+
...strictSuspense
|
|
13
|
+
}))
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* This is the initial state of the Suspense.
|
|
20
|
+
* It is exposed for convenience and edge cases where the initial state must be set initially.
|
|
21
|
+
* e.g. when using {@link suspensify} on inner observables.
|
|
22
|
+
* @example toSignal(source$.pipe(switchMap(() => fetchData().pipe(suspensify()))), {initialValue: pending});
|
|
23
|
+
*/
|
|
24
|
+
const pending = {
|
|
25
|
+
finalized: false,
|
|
26
|
+
hasError: false,
|
|
27
|
+
hasValue: false,
|
|
28
|
+
pending: true
|
|
29
|
+
};
|
|
30
|
+
function _coalesceFirstEmittedValue() {
|
|
31
|
+
return source$ => {
|
|
32
|
+
return new Observable(observer => {
|
|
33
|
+
const isReadySubject = new ReplaySubject(1);
|
|
34
|
+
const subscription = source$.pipe( /* Wait for all synchronous processing to be done. */
|
|
35
|
+
debounce(() => isReadySubject)).subscribe(observer);
|
|
36
|
+
/* Sync emitted values have been processed now.
|
|
37
|
+
* Mark source as ready and emit last computed state. */
|
|
38
|
+
isReadySubject.next(undefined);
|
|
39
|
+
return () => subscription.unsubscribe();
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/* Use values as types for better type checking. */
|
|
44
|
+
const TRUE = true;
|
|
45
|
+
const FALSE = false;
|
|
46
|
+
function _suspensify() {
|
|
47
|
+
return source$ => {
|
|
48
|
+
return source$.pipe(materialize(), scan((state, notification) => {
|
|
49
|
+
switch (notification.kind) {
|
|
50
|
+
/* Value. */
|
|
51
|
+
case 'N':
|
|
52
|
+
return {
|
|
53
|
+
finalized: FALSE,
|
|
54
|
+
hasError: FALSE,
|
|
55
|
+
hasValue: TRUE,
|
|
56
|
+
value: notification.value,
|
|
57
|
+
pending: FALSE
|
|
58
|
+
};
|
|
59
|
+
/* Error. */
|
|
60
|
+
case 'E':
|
|
61
|
+
return {
|
|
62
|
+
finalized: TRUE,
|
|
63
|
+
hasError: TRUE,
|
|
64
|
+
hasValue: FALSE,
|
|
65
|
+
pending: FALSE,
|
|
66
|
+
error: notification.error
|
|
67
|
+
};
|
|
68
|
+
/* Complete. */
|
|
69
|
+
case 'C':
|
|
70
|
+
return {
|
|
71
|
+
...state,
|
|
72
|
+
finalized: TRUE,
|
|
73
|
+
pending: FALSE
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}, pending), startWith(pending));
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { pending, suspensify };
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jscutlery/operators",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"url": "https://github.com/jscutlery/devkit"
|
|
6
6
|
},
|
|
7
7
|
"peerDependencies": {
|
|
8
8
|
"rxjs": ">=6.0.0"
|
|
9
|
-
}
|
|
9
|
+
},
|
|
10
|
+
"module": "./index.esm.js",
|
|
11
|
+
"main": "./index.cjs.js"
|
|
10
12
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { OperatorFunction } from 'rxjs';
|
|
2
|
+
export interface SuspenseLax<T> {
|
|
3
|
+
finalized: boolean;
|
|
4
|
+
hasError: boolean;
|
|
5
|
+
hasValue: boolean;
|
|
6
|
+
pending: boolean;
|
|
7
|
+
error: undefined | any;
|
|
8
|
+
value: undefined | T;
|
|
9
|
+
}
|
|
10
|
+
export interface SuspensePending {
|
|
11
|
+
finalized: false;
|
|
12
|
+
hasError: false;
|
|
13
|
+
hasValue: false;
|
|
14
|
+
pending: true;
|
|
15
|
+
}
|
|
16
|
+
export interface SuspenseWithValue<T> {
|
|
17
|
+
finalized: boolean;
|
|
18
|
+
hasError: false;
|
|
19
|
+
hasValue: true;
|
|
20
|
+
pending: false;
|
|
21
|
+
value: T;
|
|
22
|
+
}
|
|
23
|
+
export interface SuspenseWithError {
|
|
24
|
+
finalized: true;
|
|
25
|
+
hasError: true;
|
|
26
|
+
hasValue: false;
|
|
27
|
+
pending: false;
|
|
28
|
+
error: unknown;
|
|
29
|
+
}
|
|
30
|
+
export interface SuspenseEmpty {
|
|
31
|
+
finalized: true;
|
|
32
|
+
hasError: false;
|
|
33
|
+
hasValue: false;
|
|
34
|
+
pending: false;
|
|
35
|
+
}
|
|
36
|
+
export type Suspense<T> = SuspensePending | SuspenseWithValue<T> | SuspenseWithError | SuspenseEmpty;
|
|
37
|
+
export interface SuspensifyOptions {
|
|
38
|
+
strict?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @description creates a derivated state from the source observable.
|
|
42
|
+
*
|
|
43
|
+
* @example source$.pipe(suspensify())
|
|
44
|
+
*
|
|
45
|
+
* @returns Observable<SuspenseLax<T> | SuspenseStrict<T>>
|
|
46
|
+
*/
|
|
47
|
+
export declare function suspensify<T>(options: {
|
|
48
|
+
strict: false;
|
|
49
|
+
}): OperatorFunction<T, SuspenseLax<T>>;
|
|
50
|
+
export declare function suspensify<T>(options?: {
|
|
51
|
+
strict: true;
|
|
52
|
+
}): OperatorFunction<T, Suspense<T>>;
|
|
53
|
+
/**
|
|
54
|
+
* This is the initial state of the Suspense.
|
|
55
|
+
* It is exposed for convenience and edge cases where the initial state must be set initially.
|
|
56
|
+
* e.g. when using {@link suspensify} on inner observables.
|
|
57
|
+
* @example toSignal(source$.pipe(switchMap(() => fetchData().pipe(suspensify()))), {initialValue: pending});
|
|
58
|
+
*/
|
|
59
|
+
export declare const pending: SuspensePending;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
export declare function createObserver(): {
|
|
3
|
+
observe<T>(observable: Observable<T>): {
|
|
4
|
+
mockClear(): void;
|
|
5
|
+
mockReset(): void;
|
|
6
|
+
next: jest.MockInstance<void, [value: T], unknown> & ((value: T) => void);
|
|
7
|
+
error: jest.MockInstance<void, [err: any], unknown> & ((err: any) => void);
|
|
8
|
+
complete: jest.MockInstance<void, [], unknown> & (() => void);
|
|
9
|
+
};
|
|
10
|
+
};
|
package/.babelrc
DELETED
package/.eslintrc.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": ["../../.eslintrc.json"],
|
|
3
|
-
"ignorePatterns": ["!**/*"],
|
|
4
|
-
"overrides": [
|
|
5
|
-
{
|
|
6
|
-
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
7
|
-
"rules": {}
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
"files": ["*.ts", "*.tsx"],
|
|
11
|
-
"rules": {}
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
"files": ["*.js", "*.jsx"],
|
|
15
|
-
"rules": {}
|
|
16
|
-
}
|
|
17
|
-
]
|
|
18
|
-
}
|
package/jest.config.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
module.exports = {
|
|
3
|
-
displayName: 'operators',
|
|
4
|
-
preset: '../../jest.preset.js',
|
|
5
|
-
globals: {},
|
|
6
|
-
testEnvironment: 'node',
|
|
7
|
-
transform: {
|
|
8
|
-
'^.+\\.[tj]sx?$': [
|
|
9
|
-
'ts-jest',
|
|
10
|
-
{
|
|
11
|
-
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
12
|
-
},
|
|
13
|
-
],
|
|
14
|
-
},
|
|
15
|
-
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
|
|
16
|
-
coverageDirectory: '../../coverage/packages/operators',
|
|
17
|
-
};
|
package/project.json
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "operators",
|
|
3
|
-
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "packages/operators/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"targets": {
|
|
7
|
-
"build": {
|
|
8
|
-
"executor": "@nx/rollup:rollup",
|
|
9
|
-
"outputs": ["{options.outputPath}"],
|
|
10
|
-
"options": {
|
|
11
|
-
"project": "packages/operators/package.json",
|
|
12
|
-
"outputPath": "dist/packages/operators",
|
|
13
|
-
"entryFile": "packages/operators/src/index.ts",
|
|
14
|
-
"tsConfig": "packages/operators/tsconfig.lib.json",
|
|
15
|
-
"main": "packages/operators/src/index.ts",
|
|
16
|
-
"format": ["cjs", "esm"],
|
|
17
|
-
"assets": [
|
|
18
|
-
{
|
|
19
|
-
"glob": "LICENSE",
|
|
20
|
-
"input": ".",
|
|
21
|
-
"output": "."
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
"glob": "*.md",
|
|
25
|
-
"input": "packages/operators",
|
|
26
|
-
"output": "."
|
|
27
|
-
}
|
|
28
|
-
]
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"lint": {
|
|
32
|
-
"executor": "@nx/eslint:lint",
|
|
33
|
-
"outputs": ["{options.outputFile}"]
|
|
34
|
-
},
|
|
35
|
-
"test": {
|
|
36
|
-
"executor": "@nx/jest:jest",
|
|
37
|
-
"outputs": ["{workspaceRoot}/coverage/packages/operators"],
|
|
38
|
-
"options": {
|
|
39
|
-
"jestConfig": "packages/operators/jest.config.ts"
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
"github": {
|
|
43
|
-
"executor": "@jscutlery/semver:github",
|
|
44
|
-
"options": {
|
|
45
|
-
"notes": "{notes}",
|
|
46
|
-
"tag": "{tag}"
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
"npm": {
|
|
50
|
-
"executor": "ngx-deploy-npm:deploy",
|
|
51
|
-
"options": {
|
|
52
|
-
"access": "public"
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
"tags": []
|
|
57
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from '@jest/globals';
|
|
2
|
-
import {
|
|
3
|
-
EMPTY,
|
|
4
|
-
Observable,
|
|
5
|
-
Subject,
|
|
6
|
-
firstValueFrom,
|
|
7
|
-
of,
|
|
8
|
-
throwError,
|
|
9
|
-
} from 'rxjs';
|
|
10
|
-
import { suspensify } from './suspensify';
|
|
11
|
-
import { createObserver } from './testing/observer';
|
|
12
|
-
|
|
13
|
-
describe(suspensify.name, () => {
|
|
14
|
-
const { observe } = createObserver();
|
|
15
|
-
|
|
16
|
-
describe('strict mode', () => {
|
|
17
|
-
it('should always have pending, finalized, hasValue & hasError properties', async () => {
|
|
18
|
-
const suspense = await getFirstSuspenseValue();
|
|
19
|
-
suspense.pending;
|
|
20
|
-
suspense.finalized;
|
|
21
|
-
suspense.hasError;
|
|
22
|
-
suspense.hasValue;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should not have value or error before narrowing', async () => {
|
|
26
|
-
const suspense = await getFirstSuspenseValue();
|
|
27
|
-
// @ts-expect-error error property should not exist before narrowing
|
|
28
|
-
suspense.error;
|
|
29
|
-
// @ts-expect-error value property should not exist before narrowing
|
|
30
|
-
suspense.value;
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('should narrow value type', async () => {
|
|
34
|
-
const suspense = await getFirstSuspenseValue();
|
|
35
|
-
if (suspense.hasValue) {
|
|
36
|
-
suspense.value;
|
|
37
|
-
// @ts-expect-error error property should not exist on value type
|
|
38
|
-
suspense.error;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should narrow error type', async () => {
|
|
43
|
-
const suspense = await getFirstSuspenseValue();
|
|
44
|
-
if (suspense.hasError) {
|
|
45
|
-
suspense.error;
|
|
46
|
-
// @ts-expect-error value property should not exist on error type
|
|
47
|
-
suspense.value;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should emit pending', () => {
|
|
52
|
-
const { next } = setUpStrict(new Subject<'🍔'>());
|
|
53
|
-
expect(next).toBeCalledTimes(1);
|
|
54
|
-
expect(next).toBeCalledWith({
|
|
55
|
-
finalized: false,
|
|
56
|
-
hasError: false,
|
|
57
|
-
hasValue: false,
|
|
58
|
-
pending: true,
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should emit value', () => {
|
|
63
|
-
/* Unfinished observable. */
|
|
64
|
-
const { next } = setUpStrict(
|
|
65
|
-
new Observable((observer) => observer.next('🍔'))
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
expect(next).lastCalledWith({
|
|
69
|
-
finalized: false,
|
|
70
|
-
hasError: false,
|
|
71
|
-
hasValue: true,
|
|
72
|
-
pending: false,
|
|
73
|
-
value: '🍔',
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should emit once (not pending + value)', () => {
|
|
78
|
-
const { next } = setUpStrict(of('🍔'));
|
|
79
|
-
expect(next).toBeCalledTimes(1);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should emit error', () => {
|
|
83
|
-
const { next } = setUpStrict(throwError(() => new Error('🐞')));
|
|
84
|
-
expect(next).toBeCalledTimes(1);
|
|
85
|
-
expect(next).toBeCalledWith({
|
|
86
|
-
finalized: true,
|
|
87
|
-
hasError: true,
|
|
88
|
-
hasValue: false,
|
|
89
|
-
pending: false,
|
|
90
|
-
error: new Error('🐞'),
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should mark finalized on complete with value', () => {
|
|
95
|
-
const { next } = setUpStrict(of('🍔'));
|
|
96
|
-
|
|
97
|
-
expect(next).toBeCalledWith({
|
|
98
|
-
finalized: true,
|
|
99
|
-
hasError: false,
|
|
100
|
-
hasValue: true,
|
|
101
|
-
pending: false,
|
|
102
|
-
value: '🍔',
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should mark finalized on complete without value', () => {
|
|
107
|
-
const { next } = setUpStrict(EMPTY);
|
|
108
|
-
|
|
109
|
-
expect(next).toBeCalledWith({
|
|
110
|
-
finalized: true,
|
|
111
|
-
hasError: false,
|
|
112
|
-
hasValue: false,
|
|
113
|
-
pending: false,
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should reset pending to false when value is emitted', () => {
|
|
118
|
-
const subject = new Subject<'🍔'>();
|
|
119
|
-
const { next } = setUpStrict(subject);
|
|
120
|
-
|
|
121
|
-
subject.error(new Error('🐞'));
|
|
122
|
-
|
|
123
|
-
expect(next).toBeCalledTimes(2);
|
|
124
|
-
expect(next).lastCalledWith(
|
|
125
|
-
expect.objectContaining({
|
|
126
|
-
pending: false,
|
|
127
|
-
error: new Error('🐞'),
|
|
128
|
-
})
|
|
129
|
-
);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
async function getFirstSuspenseValue() {
|
|
133
|
-
return await firstValueFrom(of('🍔').pipe(suspensify()));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function setUpStrict<T>(source$: Observable<T>) {
|
|
137
|
-
const observer = observe(source$.pipe(suspensify()));
|
|
138
|
-
return {
|
|
139
|
-
next: observer.next,
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
describe('lax mode', () => {
|
|
145
|
-
it('should emit result with value', () => {
|
|
146
|
-
const { next } = setUpLax(of('🍔'));
|
|
147
|
-
expect(next).toBeCalledTimes(1);
|
|
148
|
-
expect(next).toBeCalledWith({
|
|
149
|
-
finalized: true,
|
|
150
|
-
hasError: false,
|
|
151
|
-
hasValue: true,
|
|
152
|
-
pending: false,
|
|
153
|
-
error: undefined,
|
|
154
|
-
value: '🍔',
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
it('should emit result with error', () => {
|
|
159
|
-
const { next } = setUpLax(throwError(() => new Error('🐞')));
|
|
160
|
-
expect(next).toBeCalledTimes(1);
|
|
161
|
-
expect(next).toBeCalledWith({
|
|
162
|
-
finalized: true,
|
|
163
|
-
hasError: true,
|
|
164
|
-
hasValue: false,
|
|
165
|
-
pending: false,
|
|
166
|
-
error: new Error('🐞'),
|
|
167
|
-
value: undefined,
|
|
168
|
-
});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('should emit result with pending=true and without value nor error', () => {
|
|
172
|
-
const { next } = setUpLax(new Subject<'🍔'>());
|
|
173
|
-
expect(next).toBeCalledTimes(1);
|
|
174
|
-
expect(next).toBeCalledWith({
|
|
175
|
-
finalized: false,
|
|
176
|
-
hasError: false,
|
|
177
|
-
hasValue: false,
|
|
178
|
-
pending: true,
|
|
179
|
-
error: undefined,
|
|
180
|
-
value: undefined,
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('should reset pending to false when value is emitted', () => {
|
|
185
|
-
const subject = new Subject<'🍔'>();
|
|
186
|
-
const { next } = setUpLax(subject);
|
|
187
|
-
|
|
188
|
-
subject.next('🍔');
|
|
189
|
-
|
|
190
|
-
expect(next).toBeCalledTimes(2);
|
|
191
|
-
expect(next).lastCalledWith({
|
|
192
|
-
finalized: false,
|
|
193
|
-
hasError: false,
|
|
194
|
-
hasValue: true,
|
|
195
|
-
pending: false,
|
|
196
|
-
error: undefined,
|
|
197
|
-
value: '🍔',
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it('should reset pending to false on error', () => {
|
|
202
|
-
const subject = new Subject<'🍔'>();
|
|
203
|
-
const { next } = setUpLax(subject);
|
|
204
|
-
|
|
205
|
-
subject.error(new Error('🐞'));
|
|
206
|
-
|
|
207
|
-
expect(next).toBeCalledTimes(2);
|
|
208
|
-
expect(next).lastCalledWith(
|
|
209
|
-
expect.objectContaining({
|
|
210
|
-
pending: false,
|
|
211
|
-
error: new Error('🐞'),
|
|
212
|
-
})
|
|
213
|
-
);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
function setUpLax<T>(source$: Observable<T>) {
|
|
217
|
-
const observer = observe(source$.pipe(suspensify({ strict: false })));
|
|
218
|
-
return {
|
|
219
|
-
next: observer.next,
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
});
|
package/src/lib/suspensify.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
MonoTypeOperatorFunction,
|
|
3
|
-
Observable,
|
|
4
|
-
ObservableNotification,
|
|
5
|
-
OperatorFunction,
|
|
6
|
-
ReplaySubject,
|
|
7
|
-
} from 'rxjs';
|
|
8
|
-
import { debounce, map, materialize, scan, startWith } from 'rxjs/operators';
|
|
9
|
-
|
|
10
|
-
export interface SuspenseLax<T> {
|
|
11
|
-
finalized: boolean;
|
|
12
|
-
hasError: boolean;
|
|
13
|
-
hasValue: boolean;
|
|
14
|
-
pending: boolean;
|
|
15
|
-
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
-
error: undefined | any;
|
|
18
|
-
value: undefined | T;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface SuspensePending {
|
|
22
|
-
finalized: false;
|
|
23
|
-
hasError: false;
|
|
24
|
-
hasValue: false;
|
|
25
|
-
pending: true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface SuspenseWithValue<T> {
|
|
29
|
-
finalized: boolean;
|
|
30
|
-
hasError: false;
|
|
31
|
-
hasValue: true;
|
|
32
|
-
pending: false;
|
|
33
|
-
value: T;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface SuspenseWithError {
|
|
37
|
-
finalized: true;
|
|
38
|
-
hasError: true;
|
|
39
|
-
hasValue: false;
|
|
40
|
-
pending: false;
|
|
41
|
-
error: unknown;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export interface SuspenseEmpty {
|
|
45
|
-
finalized: true;
|
|
46
|
-
hasError: false;
|
|
47
|
-
hasValue: false;
|
|
48
|
-
pending: false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type Suspense<T> =
|
|
52
|
-
| SuspensePending
|
|
53
|
-
| SuspenseWithValue<T>
|
|
54
|
-
| SuspenseWithError
|
|
55
|
-
| SuspenseEmpty;
|
|
56
|
-
|
|
57
|
-
export interface SuspensifyOptions {
|
|
58
|
-
strict?: boolean;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* @description creates a derivated state from the source observable.
|
|
63
|
-
*
|
|
64
|
-
* @example source$.pipe(suspensify())
|
|
65
|
-
*
|
|
66
|
-
* @returns Observable<SuspenseLax<T> | SuspenseStrict<T>>
|
|
67
|
-
*/
|
|
68
|
-
export function suspensify<T>(options: {
|
|
69
|
-
strict: false;
|
|
70
|
-
}): OperatorFunction<T, SuspenseLax<T>>;
|
|
71
|
-
export function suspensify<T>(options?: {
|
|
72
|
-
strict: true;
|
|
73
|
-
}): OperatorFunction<T, Suspense<T>>;
|
|
74
|
-
export function suspensify<T>({
|
|
75
|
-
strict = true,
|
|
76
|
-
}: SuspensifyOptions = {}): OperatorFunction<T, SuspenseLax<T> | Suspense<T>> {
|
|
77
|
-
return (source$: Observable<T>): Observable<Suspense<T>> => {
|
|
78
|
-
const strictSuspense$ = source$.pipe(
|
|
79
|
-
_suspensify(),
|
|
80
|
-
_coalesceFirstEmittedValue()
|
|
81
|
-
);
|
|
82
|
-
return strict
|
|
83
|
-
? strictSuspense$
|
|
84
|
-
: (strictSuspense$.pipe(
|
|
85
|
-
map((strictSuspense) => ({
|
|
86
|
-
error: undefined,
|
|
87
|
-
value: undefined,
|
|
88
|
-
...strictSuspense,
|
|
89
|
-
}))
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
-
) as any);
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* This is the initial state of the Suspense.
|
|
97
|
-
* It is exposed for convenience and edge cases where the initial state must be set initially.
|
|
98
|
-
* e.g. when using {@link suspensify} on inner observables.
|
|
99
|
-
* @example toSignal(source$.pipe(switchMap(() => fetchData().pipe(suspensify()))), {initialValue: pending});
|
|
100
|
-
*/
|
|
101
|
-
export const pending: SuspensePending = {
|
|
102
|
-
finalized: false,
|
|
103
|
-
hasError: false,
|
|
104
|
-
hasValue: false,
|
|
105
|
-
pending: true,
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
function _coalesceFirstEmittedValue<T>(): MonoTypeOperatorFunction<T> {
|
|
109
|
-
return (source$: Observable<T>): Observable<T> => {
|
|
110
|
-
return new Observable<T>((observer) => {
|
|
111
|
-
const isReadySubject = new ReplaySubject<unknown>(1);
|
|
112
|
-
|
|
113
|
-
const subscription = source$
|
|
114
|
-
.pipe(
|
|
115
|
-
/* Wait for all synchronous processing to be done. */
|
|
116
|
-
debounce(() => isReadySubject)
|
|
117
|
-
)
|
|
118
|
-
.subscribe(observer);
|
|
119
|
-
|
|
120
|
-
/* Sync emitted values have been processed now.
|
|
121
|
-
* Mark source as ready and emit last computed state. */
|
|
122
|
-
isReadySubject.next(undefined);
|
|
123
|
-
|
|
124
|
-
return () => subscription.unsubscribe();
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/* Use values as types for better type checking. */
|
|
130
|
-
const TRUE = true as const;
|
|
131
|
-
const FALSE = false as const;
|
|
132
|
-
|
|
133
|
-
function _suspensify<T>(): OperatorFunction<T, Suspense<T>> {
|
|
134
|
-
return (source$: Observable<T>): Observable<Suspense<T>> => {
|
|
135
|
-
return source$.pipe(
|
|
136
|
-
materialize(),
|
|
137
|
-
scan<ObservableNotification<T>, Suspense<T>>((state, notification) => {
|
|
138
|
-
switch (notification.kind) {
|
|
139
|
-
/* Value. */
|
|
140
|
-
case 'N':
|
|
141
|
-
return {
|
|
142
|
-
finalized: FALSE,
|
|
143
|
-
hasError: FALSE,
|
|
144
|
-
hasValue: TRUE,
|
|
145
|
-
value: notification.value,
|
|
146
|
-
pending: FALSE,
|
|
147
|
-
};
|
|
148
|
-
/* Error. */
|
|
149
|
-
case 'E':
|
|
150
|
-
return {
|
|
151
|
-
finalized: TRUE,
|
|
152
|
-
hasError: TRUE,
|
|
153
|
-
hasValue: FALSE,
|
|
154
|
-
pending: FALSE,
|
|
155
|
-
error: notification.error,
|
|
156
|
-
};
|
|
157
|
-
/* Complete. */
|
|
158
|
-
case 'C':
|
|
159
|
-
return {
|
|
160
|
-
...state,
|
|
161
|
-
finalized: TRUE,
|
|
162
|
-
pending: FALSE,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
}, pending),
|
|
166
|
-
startWith(pending)
|
|
167
|
-
);
|
|
168
|
-
};
|
|
169
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/// <reference types="jest" />
|
|
2
|
-
import { Observable, Observer, Subscription } from 'rxjs';
|
|
3
|
-
|
|
4
|
-
export function createObserver() {
|
|
5
|
-
let subscription: Subscription;
|
|
6
|
-
|
|
7
|
-
beforeEach(() => (subscription = new Subscription()));
|
|
8
|
-
afterEach(() => subscription.unsubscribe());
|
|
9
|
-
|
|
10
|
-
return {
|
|
11
|
-
observe<T>(observable: Observable<T>) {
|
|
12
|
-
const observer: jest.Mocked<Observer<T>> = {
|
|
13
|
-
next: jest.fn<void, [T]>(),
|
|
14
|
-
error: jest.fn<void, [unknown]>(),
|
|
15
|
-
complete: jest.fn<void, []>(),
|
|
16
|
-
};
|
|
17
|
-
subscription.add(observable.subscribe(observer));
|
|
18
|
-
return {
|
|
19
|
-
...observer,
|
|
20
|
-
mockClear() {
|
|
21
|
-
observer.next.mockClear();
|
|
22
|
-
observer.error.mockClear();
|
|
23
|
-
observer.complete.mockClear();
|
|
24
|
-
},
|
|
25
|
-
mockReset() {
|
|
26
|
-
observer.next.mockReset();
|
|
27
|
-
observer.error.mockReset();
|
|
28
|
-
observer.complete.mockReset();
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
package/tsconfig.json
DELETED
package/tsconfig.lib.json
DELETED
package/tsconfig.spec.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "../../dist/out-tsc",
|
|
5
|
-
"module": "commonjs",
|
|
6
|
-
"types": ["jest", "node"]
|
|
7
|
-
},
|
|
8
|
-
"include": [
|
|
9
|
-
"**/*.spec.ts",
|
|
10
|
-
"**/*.test.ts",
|
|
11
|
-
"**/*.spec.tsx",
|
|
12
|
-
"**/*.test.tsx",
|
|
13
|
-
"**/*.spec.js",
|
|
14
|
-
"**/*.test.js",
|
|
15
|
-
"**/*.spec.jsx",
|
|
16
|
-
"**/*.test.jsx",
|
|
17
|
-
"**/*.d.ts",
|
|
18
|
-
"jest.config.ts"
|
|
19
|
-
]
|
|
20
|
-
}
|