@jscutlery/operators 2.1.3 → 2.1.4

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/.babelrc ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@nx/js/babel",
5
+ {
6
+ "useBuiltIns": "usage"
7
+ }
8
+ ]
9
+ ]
10
+ }
package/.eslintrc.json ADDED
@@ -0,0 +1,18 @@
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/CHANGELOG.md CHANGED
@@ -1,177 +1,110 @@
1
- # Changelog
1
+ ## 2.1.4 (2024-03-24)
2
2
 
3
- This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
3
 
5
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
6
-
7
-
8
-
9
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
4
+ ### 🩹 Fixes
10
5
 
6
+ - import of zone.js and zone.js/testing ([f14b091](https://github.com/jscutlery/devkit/commit/f14b091))
11
7
 
12
8
 
13
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
14
-
9
+ ### ❤️ Thank You
15
10
 
11
+ - Adrian Baran
12
+ - Younes Jaaidi
16
13
 
17
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
14
+ ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2024-03-22)
18
15
 
16
+ ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-31)
19
17
 
18
+ ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-28)
20
19
 
21
20
  ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
22
21
 
23
-
24
-
25
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
26
-
27
-
28
-
29
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
30
-
31
-
32
-
33
- ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
34
-
35
-
36
-
37
22
  ## [2.1.2](https://github.com/jscutlery/devkit/compare/operators-2.1.1...operators-2.1.2) (2023-05-22)
38
23
 
39
-
40
-
41
24
  ## [2.1.1](https://github.com/jscutlery/devkit/compare/operators-2.1.0...operators-2.1.1) (2023-04-28)
42
25
 
43
-
44
26
  ### Bug Fixes
45
27
 
46
- * **operators:** 🐞 export missing types + pending const ([96eed66](https://github.com/jscutlery/devkit/commit/96eed664a96e86e9e21e107b89d220a2f0addd9e))
47
-
48
-
28
+ - **operators:** 🐞 export missing types + pending const ([96eed66](https://github.com/jscutlery/devkit/commit/96eed664a96e86e9e21e107b89d220a2f0addd9e))
49
29
 
50
30
  # [2.1.0](https://github.com/jscutlery/devkit/compare/operators-2.0.1...operators-2.1.0) (2023-04-27)
51
31
 
52
-
53
32
  ### Features
54
33
 
55
- * **operators:** ✨ expose pending state constant ([caaea8e](https://github.com/jscutlery/devkit/commit/caaea8e3144aca71ecf75830777bcadd2ba23c37))
56
-
57
-
34
+ - **operators:** ✨ expose pending state constant ([caaea8e](https://github.com/jscutlery/devkit/commit/caaea8e3144aca71ecf75830777bcadd2ba23c37))
58
35
 
59
36
  ## [2.0.1](https://github.com/jscutlery/devkit/compare/operators-2.0.0...operators-2.0.1) (2023-04-27)
60
37
 
61
-
62
38
  ### Bug Fixes
63
39
 
64
- * **operators:** 🐞 fix suspensify type to be strict by default too ([c5d424c](https://github.com/jscutlery/devkit/commit/c5d424cea4faee0df5ab112851565a36fb575ebe))
65
-
66
-
40
+ - **operators:** 🐞 fix suspensify type to be strict by default too ([c5d424c](https://github.com/jscutlery/devkit/commit/c5d424cea4faee0df5ab112851565a36fb575ebe))
67
41
 
68
42
  # [2.0.0](https://github.com/jscutlery/devkit/compare/operators-1.1.1...operators-2.0.0) (2023-04-18)
69
43
 
70
-
71
44
  ### Tests
72
45
 
73
- * **operators:** make suspensify strict by default ([93b8c05](https://github.com/jscutlery/devkit/commit/93b8c0576086ff476e9239ead6175c8832f39c0e))
74
-
46
+ - **operators:** make suspensify strict by default ([93b8c05](https://github.com/jscutlery/devkit/commit/93b8c0576086ff476e9239ead6175c8832f39c0e))
75
47
 
76
48
  ### BREAKING CHANGES
77
49
 
78
- * **operators:** suspensify() without options will return a strict suspense object
79
-
80
-
50
+ - **operators:** suspensify() without options will return a strict suspense object
81
51
 
82
52
  ## [1.1.1](https://github.com/jscutlery/devkit/compare/operators-1.1.0...operators-1.1.1) (2023-04-18)
83
53
 
84
-
85
-
86
54
  # [1.1.0](https://github.com/jscutlery/devkit/compare/operators-1.0.2...operators-1.1.0) (2023-04-18)
87
55
 
88
-
89
56
  ### Features
90
57
 
91
- * **operators:** add suspensify's strict mode ([f9dd789](https://github.com/jscutlery/devkit/commit/f9dd789d82cdff2fffb2241e8b0aab9bd76cb363)), closes [#237](https://github.com/jscutlery/devkit/issues/237)
92
-
93
-
58
+ - **operators:** add suspensify's strict mode ([f9dd789](https://github.com/jscutlery/devkit/commit/f9dd789d82cdff2fffb2241e8b0aab9bd76cb363)), closes [#237](https://github.com/jscutlery/devkit/issues/237)
94
59
 
95
60
  ## [1.0.2](https://github.com/jscutlery/devkit/compare/operators-1.0.1...operators-1.0.2) (2023-04-18)
96
61
 
97
-
98
-
99
62
  ## [1.0.1](https://github.com/jscutlery/devkit/compare/operators-1.0.0...operators-1.0.1) (2023-04-18)
100
63
 
101
-
102
-
103
64
  # [1.0.0](https://github.com/jscutlery/devkit/compare/operators-0.2.1...operators-1.0.0) (2023-04-18)
104
65
 
105
-
106
66
  ### Bug Fixes
107
67
 
108
- * **operators:** 🏷️ fix typing errors ([91c084c](https://github.com/jscutlery/devkit/commit/91c084c29c6ad760376c10bc17004a2bfcd3a306))
109
-
68
+ - **operators:** 🏷️ fix typing errors ([91c084c](https://github.com/jscutlery/devkit/commit/91c084c29c6ad760376c10bc17004a2bfcd3a306))
110
69
 
111
70
  ### Features
112
71
 
113
- * **operators:** ✨ drop support for suspensify's projector ([f4bf6dc](https://github.com/jscutlery/devkit/commit/f4bf6dc413e866b5c047578162c1c1cbcb3c73f9)), closes [#236](https://github.com/jscutlery/devkit/issues/236)
114
-
72
+ - **operators:** ✨ drop support for suspensify's projector ([f4bf6dc](https://github.com/jscutlery/devkit/commit/f4bf6dc413e866b5c047578162c1c1cbcb3c73f9)), closes [#236](https://github.com/jscutlery/devkit/issues/236)
115
73
 
116
74
  ### BREAKING CHANGES
117
75
 
118
- * **operators:** suspensify's operator "projector" function is not supported anymore
119
-
120
-
76
+ - **operators:** suspensify's operator "projector" function is not supported anymore
121
77
 
122
78
  ## [0.2.1](https://github.com/jscutlery/devkit/compare/operators-0.2.0...operators-0.2.1) (2022-12-26)
123
79
 
124
-
125
-
126
80
  # [0.2.0](https://github.com/jscutlery/devkit/compare/operators-0.1.8...operators-0.2.0) (2022-12-02)
127
81
 
128
-
129
82
  ### Features
130
83
 
131
- * **operators:** ✨add esm support ([77b5470](https://github.com/jscutlery/devkit/commit/77b54704d26de058747ee919080873a434cd10b3))
132
-
133
-
84
+ - **operators:** ✨add esm support ([77b5470](https://github.com/jscutlery/devkit/commit/77b54704d26de058747ee919080873a434cd10b3))
134
85
 
135
86
  ## [0.1.8](https://github.com/jscutlery/devkit/compare/operators-0.1.7...operators-0.1.8) (2022-12-01)
136
87
 
137
-
138
-
139
88
  ## [0.1.7](https://github.com/jscutlery/devkit/compare/operators-0.1.6...operators-0.1.7) (2022-06-24)
140
89
 
141
-
142
-
143
90
  ## [0.1.6](https://github.com/jscutlery/devkit/compare/operators-0.1.5...operators-0.1.6) (2022-03-27)
144
91
 
145
-
146
92
  ### Bug Fixes
147
93
 
148
- * 🐞 bump workspace ([127c808](https://github.com/jscutlery/devkit/commit/127c808e61cd9e8be3f3e3fd32f64fe7ad1f7e55))
149
-
150
-
94
+ - 🐞 bump workspace ([127c808](https://github.com/jscutlery/devkit/commit/127c808e61cd9e8be3f3e3fd32f64fe7ad1f7e55))
151
95
 
152
96
  ## [0.1.5](https://github.com/jscutlery/devkit/compare/operators-0.1.4...operators-0.1.5) (2022-03-26)
153
97
 
154
-
155
-
156
98
  ## [0.1.4](https://github.com/jscutlery/devkit/compare/operators-0.1.3...operators-0.1.4) (2021-12-08)
157
99
 
158
-
159
-
160
100
  ## [0.1.3](https://github.com/jscutlery/devkit/compare/operators-0.1.2...operators-0.1.3) (2021-10-26)
161
101
 
162
-
163
-
164
102
  ## [0.1.2](https://github.com/jscutlery/devkit/compare/operators-0.1.1...operators-0.1.2) (2021-10-26)
165
103
 
166
-
167
-
168
104
  ## [0.1.1](https://github.com/jscutlery/test-utils/compare/operators-0.1.0...operators-0.1.1) (2021-10-26)
169
105
 
170
-
171
-
172
106
  # 0.1.0 (2021-10-25)
173
107
 
174
-
175
108
  ### Features
176
109
 
177
- * **operator:** ✅ add supensify operator ([e7fc483](https://github.com/jscutlery/test-utils/commit/e7fc483f197edda038da0a2a757620d49720d1db))
110
+ - **operator:** ✅ add supensify operator ([e7fc483](https://github.com/jscutlery/test-utils/commit/e7fc483f197edda038da0a2a757620d49720d1db))
package/jest.config.ts ADDED
@@ -0,0 +1,17 @@
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/package.json CHANGED
@@ -1,12 +1,10 @@
1
1
  {
2
2
  "name": "@jscutlery/operators",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "repository": {
5
5
  "url": "https://github.com/jscutlery/devkit"
6
6
  },
7
7
  "peerDependencies": {
8
8
  "rxjs": ">=6.0.0"
9
- },
10
- "module": "./index.esm.js",
11
- "main": "./index.cjs.js"
9
+ }
12
10
  }
package/project.json ADDED
@@ -0,0 +1,57 @@
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 ADDED
@@ -0,0 +1,10 @@
1
+ export { pending, suspensify } from './lib/suspensify';
2
+ export type {
3
+ SuspenseLax,
4
+ Suspense,
5
+ SuspenseEmpty,
6
+ SuspensePending,
7
+ SuspenseWithError,
8
+ SuspenseWithValue,
9
+ SuspensifyOptions,
10
+ } from './lib/suspensify';
@@ -0,0 +1,223 @@
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
+ });
@@ -0,0 +1,169 @@
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
+ }
@@ -0,0 +1,33 @@
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 ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "files": [],
4
+ "include": [],
5
+ "references": [
6
+ {
7
+ "path": "./tsconfig.lib.json"
8
+ },
9
+ {
10
+ "path": "./tsconfig.spec.json"
11
+ }
12
+ ]
13
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "outDir": "../../dist/out-tsc",
6
+ "declaration": true,
7
+ "types": ["node"]
8
+ },
9
+ "exclude": ["**/*.spec.ts", "**/*.test.ts", "jest.config.ts"],
10
+ "include": ["**/*.ts"]
11
+ }
@@ -0,0 +1,20 @@
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
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
package/index.cjs.js DELETED
@@ -1,85 +0,0 @@
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.js DELETED
@@ -1,91 +0,0 @@
1
- import { Observable, ReplaySubject } from 'rxjs';
2
- import { map, debounce, materialize, scan, startWith } from 'rxjs/operators';
3
-
4
- /**
5
- * @description creates a derivated state from the source observable.
6
- *
7
- * @example source$.pipe(suspensify())
8
- *
9
- * @returns Observable<SuspenseLax<T> | SuspenseStrict<T>>
10
- */
11
-
12
- function suspensify({
13
- strict = true
14
- } = {}) {
15
- return source$ => {
16
- const strictSuspense$ = source$.pipe(_suspensify(), _coalesceFirstEmittedValue());
17
- return strict ? strictSuspense$ : strictSuspense$.pipe(map(strictSuspense => ({
18
- error: undefined,
19
- value: undefined,
20
- ...strictSuspense
21
- }))
22
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
- );
24
- };
25
- }
26
-
27
- /**
28
- * This is the initial state of the Suspense.
29
- * It is exposed for convenience and edge cases where the initial state must be set initially.
30
- * e.g. when using {@link suspensify} on inner observables.
31
- * @example toSignal(source$.pipe(switchMap(() => fetchData().pipe(suspensify()))), {initialValue: pending});
32
- */
33
- const pending = {
34
- finalized: false,
35
- hasError: false,
36
- hasValue: false,
37
- pending: true
38
- };
39
- function _coalesceFirstEmittedValue() {
40
- return source$ => {
41
- return new Observable(observer => {
42
- const isReadySubject = new ReplaySubject(1);
43
- const subscription = source$.pipe( /* Wait for all synchronous processing to be done. */
44
- debounce(() => isReadySubject)).subscribe(observer);
45
-
46
- /* Sync emitted values have been processed now.
47
- * Mark source as ready and emit last computed state. */
48
- isReadySubject.next(undefined);
49
- return () => subscription.unsubscribe();
50
- });
51
- };
52
- }
53
-
54
- /* Use values as types for better type checking. */
55
- const TRUE = true;
56
- const FALSE = false;
57
- function _suspensify() {
58
- return source$ => {
59
- return source$.pipe(materialize(), scan((state, notification) => {
60
- switch (notification.kind) {
61
- /* Value. */
62
- case 'N':
63
- return {
64
- finalized: FALSE,
65
- hasError: FALSE,
66
- hasValue: TRUE,
67
- value: notification.value,
68
- pending: FALSE
69
- };
70
- /* Error. */
71
- case 'E':
72
- return {
73
- finalized: TRUE,
74
- hasError: TRUE,
75
- hasValue: FALSE,
76
- pending: FALSE,
77
- error: notification.error
78
- };
79
- /* Complete. */
80
- case 'C':
81
- return {
82
- ...state,
83
- finalized: TRUE,
84
- pending: FALSE
85
- };
86
- }
87
- }, pending), startWith(pending));
88
- };
89
- }
90
-
91
- export { pending, suspensify };
package/src/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { pending, suspensify } from './lib/suspensify';
2
- export type { SuspenseLax, Suspense, SuspenseEmpty, SuspensePending, SuspenseWithError, SuspenseWithValue, SuspensifyOptions, } from './lib/suspensify';
@@ -1,59 +0,0 @@
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;
@@ -1,11 +0,0 @@
1
- /// <reference types="jest" />
2
- import { Observable } from 'rxjs';
3
- export declare function createObserver(): {
4
- observe<T>(observable: Observable<T>): {
5
- mockClear(): void;
6
- mockReset(): void;
7
- next: jest.MockInstance<void, [value: T], unknown> & ((value: T) => void);
8
- error: jest.MockInstance<void, [err: any], unknown> & ((err: any) => void);
9
- complete: jest.MockInstance<void, [], unknown> & (() => void);
10
- };
11
- };