@jscutlery/operators 2.1.1 → 2.1.3

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 CHANGED
@@ -2,6 +2,42 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
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)
10
+
11
+
12
+
13
+ ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
14
+
15
+
16
+
17
+ ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
18
+
19
+
20
+
21
+ ## [2.1.3](https://github.com/jscutlery/devkit/compare/operators-2.1.2...operators-2.1.3) (2023-10-25)
22
+
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
+ ## [2.1.2](https://github.com/jscutlery/devkit/compare/operators-2.1.1...operators-2.1.2) (2023-05-22)
38
+
39
+
40
+
5
41
  ## [2.1.1](https://github.com/jscutlery/devkit/compare/operators-2.1.0...operators-2.1.1) (2023-04-28)
6
42
 
7
43
 
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.js ADDED
@@ -0,0 +1,91 @@
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/package.json CHANGED
@@ -1,16 +1,12 @@
1
1
  {
2
2
  "name": "@jscutlery/operators",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "repository": {
5
5
  "url": "https://github.com/jscutlery/devkit"
6
6
  },
7
7
  "peerDependencies": {
8
- "rxjs": ">=6.0.0",
9
- "@jest/globals": "28.1.3"
8
+ "rxjs": ">=6.0.0"
10
9
  },
11
- "module": "./index.js",
12
- "main": "./index.cjs",
13
- "type": "module",
14
- "types": "./src/index.d.ts",
15
- "dependencies": {}
10
+ "module": "./index.esm.js",
11
+ "main": "./index.cjs.js"
16
12
  }
package/src/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { pending, suspensify } from './lib/suspensify';
2
- export type { SuspenseLax, Suspense, SuspenseEmpty, SuspensePending, SuspenseWithError, SuspenseWithValue, SuspensifyOptions, } from './lib/suspensify';
1
+ export { pending, suspensify } from './lib/suspensify';
2
+ export type { SuspenseLax, Suspense, SuspenseEmpty, SuspensePending, SuspenseWithError, SuspenseWithValue, SuspensifyOptions, } from './lib/suspensify';
@@ -1,59 +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 declare 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
+ 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 +1,11 @@
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]> & ((value: T) => void);
8
- error: jest.MockInstance<void, [err: any]> & ((err: any) => void);
9
- complete: jest.MockInstance<void, []> & (() => void);
10
- };
11
- };
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
+ };