@trpc/client 10.0.0-alpha.8 → 10.0.0-proxy-alpha.53
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/README.md +58 -1
- package/dist/TRPCClientError.d.ts +16 -20
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/createTRPCClient.d.ts +2 -3
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClientProxy.d.ts +37 -0
- package/dist/createTRPCClientProxy.d.ts.map +1 -0
- package/dist/httpUtils-44781613.mjs +76 -0
- package/dist/httpUtils-801112a6.js +79 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +260 -280
- package/dist/index.mjs +253 -271
- package/dist/internals/TRPCClient.d.ts +39 -21
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts +7 -6
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/internals/fetchHelpers.d.ts +1 -1
- package/dist/internals/fetchHelpers.d.ts.map +1 -1
- package/dist/internals/retryDelay.d.ts.map +1 -1
- package/dist/links/dedupeLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +2 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +189 -172
- package/dist/links/httpBatchLink.mjs +189 -172
- package/dist/links/httpLink.d.ts +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +31 -35
- package/dist/links/httpLink.mjs +31 -35
- package/dist/links/index.d.ts +9 -0
- package/dist/links/index.d.ts.map +1 -0
- package/dist/links/internals/createChain.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.d.ts +31 -0
- package/dist/links/internals/httpUtils.d.ts.map +1 -0
- package/dist/links/internals/transformResult.d.ts +28 -0
- package/dist/links/internals/transformResult.d.ts.map +1 -0
- package/dist/links/loggerLink.d.ts +3 -3
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +90 -96
- package/dist/links/loggerLink.mjs +90 -91
- package/dist/links/retryLink.d.ts.map +1 -1
- package/dist/links/splitLink.d.ts +1 -1
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +3 -25
- package/dist/links/splitLink.mjs +2 -28
- package/dist/links/types.d.ts +13 -12
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +12 -5
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +250 -358
- package/dist/links/wsLink.mjs +250 -348
- package/dist/splitLink-695ae288.js +48 -0
- package/dist/splitLink-ad44a55d.mjs +45 -0
- package/dist/transformResult-106791d7.mjs +62 -0
- package/dist/transformResult-e15ccdf6.js +65 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +15 -13
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +15 -13
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +12 -10
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +4 -4
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +4 -4
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
- package/package.json +23 -34
- package/src/TRPCClientError.ts +38 -28
- package/src/createTRPCClient.ts +8 -3
- package/src/createTRPCClientProxy.ts +113 -0
- package/src/index.ts +2 -1
- package/src/internals/TRPCClient.ts +118 -77
- package/src/internals/dataLoader.ts +116 -53
- package/src/internals/fetchHelpers.ts +2 -2
- package/src/links/dedupeLink.test.ts +9 -15
- package/src/links/dedupeLink.ts +6 -7
- package/src/links/httpBatchLink.ts +73 -36
- package/src/links/httpLink.ts +18 -6
- package/src/links/index.ts +8 -0
- package/src/links/internals/createChain.test.ts +23 -38
- package/src/links/internals/createChain.ts +8 -3
- package/src/links/internals/httpUtils.ts +117 -0
- package/src/links/internals/transformResult.ts +40 -0
- package/src/links/loggerLink.ts +6 -7
- package/src/links/retryLink.ts +2 -3
- package/src/links/splitLink.test.ts +3 -3
- package/src/links/splitLink.ts +5 -5
- package/src/links/types.ts +15 -12
- package/src/links/wsLink.ts +57 -23
- package/dist/TRPCClientError-17b117e1.mjs +0 -81
- package/dist/TRPCClientError-ec6bd2ca.js +0 -94
- package/dist/createChain-389cc116.mjs +0 -25
- package/dist/createChain-864dbb30.js +0 -27
- package/dist/httpUtils-1f9e4e3c.js +0 -97
- package/dist/httpUtils-c09d7b79.mjs +0 -91
- package/dist/index.ts.js +0 -1
- package/dist/internals/transformRPCResponse.d.ts +0 -15
- package/dist/internals/transformRPCResponse.d.ts.map +0 -1
- package/dist/links/httpBatchLink.ts.js +0 -1
- package/dist/links/httpLink.ts.js +0 -1
- package/dist/links/httpUtils.d.ts +0 -22
- package/dist/links/httpUtils.d.ts.map +0 -1
- package/dist/links/internals/transformOperationResult.d.ts +0 -16
- package/dist/links/internals/transformOperationResult.d.ts.map +0 -1
- package/dist/links/loggerLink.ts.js +0 -1
- package/dist/links/splitLink.ts.js +0 -1
- package/dist/links/transformerLink.d.ts +0 -4
- package/dist/links/transformerLink.d.ts.map +0 -1
- package/dist/links/transformerLink.js +0 -75
- package/dist/links/transformerLink.mjs +0 -67
- package/dist/links/transformerLink.ts.js +0 -1
- package/dist/links/wsLink.ts.js +0 -1
- package/dist/observable-7e66cb56.mjs +0 -102
- package/dist/observable-85669100.js +0 -104
- package/dist/rx/index.d.ts +0 -3
- package/dist/rx/index.d.ts.map +0 -1
- package/dist/rx/index.js +0 -9
- package/dist/rx/index.mjs +0 -1
- package/dist/rx/index.ts.js +0 -1
- package/dist/rx/observable.d.ts +0 -4
- package/dist/rx/observable.d.ts.map +0 -1
- package/dist/rx/operators/index.d.ts +0 -4
- package/dist/rx/operators/index.d.ts.map +0 -1
- package/dist/rx/operators/index.js +0 -38
- package/dist/rx/operators/index.mjs +0 -32
- package/dist/rx/operators/index.ts.js +0 -1
- package/dist/rx/operators/map.d.ts +0 -3
- package/dist/rx/operators/map.d.ts.map +0 -1
- package/dist/rx/operators/share.d.ts +0 -6
- package/dist/rx/operators/share.d.ts.map +0 -1
- package/dist/rx/operators/tap.d.ts +0 -3
- package/dist/rx/operators/tap.d.ts.map +0 -1
- package/dist/rx/types.d.ts +0 -29
- package/dist/rx/types.d.ts.map +0 -1
- package/dist/rx/util/identity.d.ts +0 -2
- package/dist/rx/util/identity.d.ts.map +0 -1
- package/dist/rx/util/observableToPromise.d.ts +0 -10
- package/dist/rx/util/observableToPromise.d.ts.map +0 -1
- package/dist/rx/util/pipe.d.ts +0 -4
- package/dist/rx/util/pipe.d.ts.map +0 -1
- package/dist/share-4d79dc49.js +0 -110
- package/dist/share-671b63fa.mjs +0 -108
- package/dist/tap-538fa07f.mjs +0 -30
- package/dist/tap-799a2d33.js +0 -32
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
- package/links/transformerLink/index.d.ts +0 -1
- package/links/transformerLink/index.js +0 -1
- package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
- package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
- package/rx/dist/trpc-client-rx.cjs.js +0 -7
- package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
- package/rx/dist/trpc-client-rx.esm.js +0 -1
- package/rx/index.d.ts +0 -1
- package/rx/index.js +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
- package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
- package/rx/operators/index.d.ts +0 -1
- package/rx/operators/index.js +0 -1
- package/src/internals/transformRPCResponse.ts +0 -25
- package/src/links/httpUtils.ts +0 -94
- package/src/links/internals/transformOperationResult.ts +0 -25
- package/src/links/transformerLink.ts +0 -70
- package/src/rx/index.ts +0 -2
- package/src/rx/observable.test.ts +0 -83
- package/src/rx/observable.ts +0 -70
- package/src/rx/operators/index.ts +0 -3
- package/src/rx/operators/map.test.ts +0 -92
- package/src/rx/operators/map.ts +0 -25
- package/src/rx/operators/share.test.ts +0 -60
- package/src/rx/operators/share.ts +0 -67
- package/src/rx/operators/tap.ts +0 -26
- package/src/rx/types.ts +0 -69
- package/src/rx/util/identity.ts +0 -3
- package/src/rx/util/observableToPromise.ts +0 -49
- package/src/rx/util/pipe.ts +0 -22
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
|
-
import { observable } from './observable';
|
|
3
|
-
import { tap } from './operators';
|
|
4
|
-
import { share } from './operators/share';
|
|
5
|
-
|
|
6
|
-
test('vanilla observable', () => {
|
|
7
|
-
const obs = observable<number, Error>((observer) => {
|
|
8
|
-
observer.next(1);
|
|
9
|
-
observer.complete();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
const next = jest.fn();
|
|
13
|
-
const error = jest.fn();
|
|
14
|
-
const complete = jest.fn();
|
|
15
|
-
obs.subscribe({
|
|
16
|
-
next,
|
|
17
|
-
error,
|
|
18
|
-
complete,
|
|
19
|
-
});
|
|
20
|
-
expect(next.mock.calls).toHaveLength(1);
|
|
21
|
-
expect(complete.mock.calls).toHaveLength(1);
|
|
22
|
-
expect(error.mock.calls).toHaveLength(0);
|
|
23
|
-
expect(next.mock.calls[0][0]).toBe(1);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('pipe - combine operators', () => {
|
|
27
|
-
const taps = {
|
|
28
|
-
next: jest.fn(),
|
|
29
|
-
complete: jest.fn(),
|
|
30
|
-
error: jest.fn(),
|
|
31
|
-
};
|
|
32
|
-
const obs = observable<number, Error>((observer) => {
|
|
33
|
-
observer.next(1);
|
|
34
|
-
}).pipe(
|
|
35
|
-
// operators:
|
|
36
|
-
share(),
|
|
37
|
-
tap(taps),
|
|
38
|
-
);
|
|
39
|
-
{
|
|
40
|
-
const next = jest.fn();
|
|
41
|
-
const error = jest.fn();
|
|
42
|
-
const complete = jest.fn();
|
|
43
|
-
obs.subscribe({
|
|
44
|
-
next,
|
|
45
|
-
error,
|
|
46
|
-
complete,
|
|
47
|
-
});
|
|
48
|
-
expect(next.mock.calls).toHaveLength(1);
|
|
49
|
-
expect(complete.mock.calls).toHaveLength(0);
|
|
50
|
-
expect(error.mock.calls).toHaveLength(0);
|
|
51
|
-
expect(next.mock.calls[0][0]).toBe(1);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
{
|
|
55
|
-
const next = jest.fn();
|
|
56
|
-
const error = jest.fn();
|
|
57
|
-
const complete = jest.fn();
|
|
58
|
-
obs.subscribe({
|
|
59
|
-
next,
|
|
60
|
-
error,
|
|
61
|
-
complete,
|
|
62
|
-
});
|
|
63
|
-
expect(next.mock.calls).toHaveLength(0);
|
|
64
|
-
expect(complete.mock.calls).toHaveLength(0);
|
|
65
|
-
expect(error.mock.calls).toHaveLength(0);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
expect({
|
|
69
|
-
next: taps.next.mock.calls,
|
|
70
|
-
error: taps.error.mock.calls,
|
|
71
|
-
complete: taps.complete.mock.calls,
|
|
72
|
-
}).toMatchInlineSnapshot(`
|
|
73
|
-
Object {
|
|
74
|
-
"complete": Array [],
|
|
75
|
-
"error": Array [],
|
|
76
|
-
"next": Array [
|
|
77
|
-
Array [
|
|
78
|
-
1,
|
|
79
|
-
],
|
|
80
|
-
],
|
|
81
|
-
}
|
|
82
|
-
`);
|
|
83
|
-
});
|
package/src/rx/observable.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { pipeFromArray } from './util/pipe';
|
|
2
|
-
import { Observable, Observer, OperatorFunction, TeardownLogic } from './types';
|
|
3
|
-
|
|
4
|
-
export type inferObservableValue<TObservable extends Observable<any, any>> =
|
|
5
|
-
TObservable extends Observable<infer TValue, any> ? TValue : never;
|
|
6
|
-
|
|
7
|
-
export function observable<TValue, TError = unknown>(
|
|
8
|
-
subscribe: (observer: Observer<TValue, TError>) => TeardownLogic,
|
|
9
|
-
): Observable<TValue, TError> {
|
|
10
|
-
const self: Observable<TValue, TError> = {
|
|
11
|
-
subscribe(observer) {
|
|
12
|
-
let teardownRef: TeardownLogic | null = null;
|
|
13
|
-
let isDone = false;
|
|
14
|
-
let unsubscribed = false;
|
|
15
|
-
let teardownImmediately = false;
|
|
16
|
-
function unsubscribe() {
|
|
17
|
-
if (teardownRef === null) {
|
|
18
|
-
teardownImmediately = true;
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
if (unsubscribed) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
unsubscribed = true;
|
|
25
|
-
|
|
26
|
-
if (typeof teardownRef === 'function') {
|
|
27
|
-
teardownRef();
|
|
28
|
-
} else if (teardownRef) {
|
|
29
|
-
teardownRef.unsubscribe();
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
teardownRef = subscribe({
|
|
33
|
-
next(value) {
|
|
34
|
-
if (isDone) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
observer.next?.(value);
|
|
38
|
-
},
|
|
39
|
-
error(err) {
|
|
40
|
-
if (isDone) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
isDone = true;
|
|
44
|
-
observer.error?.(err);
|
|
45
|
-
unsubscribe();
|
|
46
|
-
},
|
|
47
|
-
complete() {
|
|
48
|
-
if (isDone) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
isDone = true;
|
|
52
|
-
observer.complete?.();
|
|
53
|
-
unsubscribe();
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
if (teardownImmediately) {
|
|
57
|
-
unsubscribe();
|
|
58
|
-
}
|
|
59
|
-
return {
|
|
60
|
-
unsubscribe,
|
|
61
|
-
};
|
|
62
|
-
},
|
|
63
|
-
pipe(
|
|
64
|
-
...operations: OperatorFunction<any, any, any, any>[]
|
|
65
|
-
): Observable<any, any> {
|
|
66
|
-
return pipeFromArray(operations)(self) as any;
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
return self;
|
|
70
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { expectTypeOf } from 'expect-type';
|
|
2
|
-
import { observable } from '../observable';
|
|
3
|
-
import { map } from '.';
|
|
4
|
-
import { EventEmitter } from 'events';
|
|
5
|
-
|
|
6
|
-
interface SubscriptionEvents<TOutput> {
|
|
7
|
-
data: (data: TOutput) => void;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
declare interface CustomEventEmitter<TOutput> {
|
|
11
|
-
on<U extends keyof SubscriptionEvents<TOutput>>(
|
|
12
|
-
event: U,
|
|
13
|
-
listener: SubscriptionEvents<TOutput>[U],
|
|
14
|
-
): this;
|
|
15
|
-
|
|
16
|
-
once<U extends keyof SubscriptionEvents<TOutput>>(
|
|
17
|
-
event: U,
|
|
18
|
-
listener: SubscriptionEvents<TOutput>[U],
|
|
19
|
-
): this;
|
|
20
|
-
|
|
21
|
-
emit<U extends keyof SubscriptionEvents<TOutput>>(
|
|
22
|
-
event: U,
|
|
23
|
-
...args: Parameters<SubscriptionEvents<TOutput>[U]>
|
|
24
|
-
): boolean;
|
|
25
|
-
}
|
|
26
|
-
class CustomEventEmitter<TOutput>
|
|
27
|
-
extends EventEmitter
|
|
28
|
-
implements CustomEventEmitter<TOutput> {}
|
|
29
|
-
test('map', () => {
|
|
30
|
-
type EventShape = { num: number };
|
|
31
|
-
const ee = new CustomEventEmitter<EventShape>();
|
|
32
|
-
const eventObservable = observable<EventShape, unknown>((observer) => {
|
|
33
|
-
const callback = (data: EventShape) => {
|
|
34
|
-
observer.next(data);
|
|
35
|
-
};
|
|
36
|
-
ee.on('data', callback);
|
|
37
|
-
|
|
38
|
-
return () => {
|
|
39
|
-
ee.off('data', callback);
|
|
40
|
-
};
|
|
41
|
-
});
|
|
42
|
-
const pipeCalls = jest.fn();
|
|
43
|
-
const piped = eventObservable.pipe(
|
|
44
|
-
map((...args) => {
|
|
45
|
-
pipeCalls(...args);
|
|
46
|
-
const [value] = args;
|
|
47
|
-
return value.num;
|
|
48
|
-
}),
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
const next = jest.fn();
|
|
52
|
-
const subscription = piped.subscribe({
|
|
53
|
-
next(value) {
|
|
54
|
-
expectTypeOf<number>(value);
|
|
55
|
-
next(value);
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
expect(next).not.toHaveBeenCalled();
|
|
59
|
-
ee.emit('data', { num: 1 });
|
|
60
|
-
ee.emit('data', { num: 2 });
|
|
61
|
-
expect(next).toHaveBeenCalledTimes(2);
|
|
62
|
-
expect(next.mock.calls).toMatchInlineSnapshot(`
|
|
63
|
-
Array [
|
|
64
|
-
Array [
|
|
65
|
-
1,
|
|
66
|
-
],
|
|
67
|
-
Array [
|
|
68
|
-
2,
|
|
69
|
-
],
|
|
70
|
-
]
|
|
71
|
-
`);
|
|
72
|
-
expect(pipeCalls.mock.calls).toMatchInlineSnapshot(`
|
|
73
|
-
Array [
|
|
74
|
-
Array [
|
|
75
|
-
Object {
|
|
76
|
-
"num": 1,
|
|
77
|
-
},
|
|
78
|
-
0,
|
|
79
|
-
],
|
|
80
|
-
Array [
|
|
81
|
-
Object {
|
|
82
|
-
"num": 2,
|
|
83
|
-
},
|
|
84
|
-
1,
|
|
85
|
-
],
|
|
86
|
-
]
|
|
87
|
-
`);
|
|
88
|
-
|
|
89
|
-
expect(ee.listeners('data')).toHaveLength(1);
|
|
90
|
-
subscription.unsubscribe();
|
|
91
|
-
expect(ee.listeners('data')).toHaveLength(0);
|
|
92
|
-
});
|
package/src/rx/operators/map.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { OperatorFunction } from '../types';
|
|
2
|
-
|
|
3
|
-
export function map<TValueBefore, TError, TValueAfter>(
|
|
4
|
-
project: (value: TValueBefore, index: number) => TValueAfter,
|
|
5
|
-
): OperatorFunction<TValueBefore, TError, TValueAfter, TError> {
|
|
6
|
-
return (originalObserver) => {
|
|
7
|
-
return {
|
|
8
|
-
subscribe(observer) {
|
|
9
|
-
let index = 0;
|
|
10
|
-
const subscription = originalObserver.subscribe({
|
|
11
|
-
next(value) {
|
|
12
|
-
observer.next?.(project(value, index++));
|
|
13
|
-
},
|
|
14
|
-
error(error) {
|
|
15
|
-
observer.error?.(error);
|
|
16
|
-
},
|
|
17
|
-
complete() {
|
|
18
|
-
observer.complete?.();
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
return subscription;
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-var */
|
|
2
|
-
import { observable } from '../observable';
|
|
3
|
-
import { share } from './share';
|
|
4
|
-
|
|
5
|
-
test('share', () => {
|
|
6
|
-
const obs = share()(
|
|
7
|
-
observable<number, Error>((observer) => {
|
|
8
|
-
observer.next(1);
|
|
9
|
-
}),
|
|
10
|
-
);
|
|
11
|
-
|
|
12
|
-
{
|
|
13
|
-
const next = jest.fn();
|
|
14
|
-
const error = jest.fn();
|
|
15
|
-
const complete = jest.fn();
|
|
16
|
-
var subscription1 = obs.subscribe({
|
|
17
|
-
next,
|
|
18
|
-
error,
|
|
19
|
-
complete,
|
|
20
|
-
});
|
|
21
|
-
expect(next.mock.calls).toHaveLength(1);
|
|
22
|
-
expect(complete.mock.calls).toHaveLength(0);
|
|
23
|
-
expect(error.mock.calls).toHaveLength(0);
|
|
24
|
-
expect(next.mock.calls[0][0]).toBe(1);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
{
|
|
28
|
-
// subscribe again - it's shared so should not propagate any results
|
|
29
|
-
const next = jest.fn();
|
|
30
|
-
const error = jest.fn();
|
|
31
|
-
const complete = jest.fn();
|
|
32
|
-
var subscription2 = obs.subscribe({
|
|
33
|
-
next,
|
|
34
|
-
error,
|
|
35
|
-
complete,
|
|
36
|
-
});
|
|
37
|
-
expect(next.mock.calls).toHaveLength(0);
|
|
38
|
-
expect(complete.mock.calls).toHaveLength(0);
|
|
39
|
-
expect(error.mock.calls).toHaveLength(0);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
subscription1.unsubscribe();
|
|
43
|
-
subscription2.unsubscribe();
|
|
44
|
-
// now it should be reset so we can do a new subscription
|
|
45
|
-
{
|
|
46
|
-
const next = jest.fn();
|
|
47
|
-
const error = jest.fn();
|
|
48
|
-
const complete = jest.fn();
|
|
49
|
-
var subscription3 = obs.subscribe({
|
|
50
|
-
next,
|
|
51
|
-
error,
|
|
52
|
-
complete,
|
|
53
|
-
});
|
|
54
|
-
expect(next.mock.calls).toHaveLength(1);
|
|
55
|
-
expect(complete.mock.calls).toHaveLength(0);
|
|
56
|
-
expect(error.mock.calls).toHaveLength(0);
|
|
57
|
-
expect(next.mock.calls[0][0]).toBe(1);
|
|
58
|
-
subscription3.unsubscribe();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { MonoTypeOperatorFunction, Observer, Unsubscribable } from '../types';
|
|
2
|
-
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
4
|
-
interface ShareConfig {}
|
|
5
|
-
export function share<TValue, TError>(
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
-
_opts?: ShareConfig,
|
|
8
|
-
): MonoTypeOperatorFunction<TValue, TError> {
|
|
9
|
-
return (originalObserver) => {
|
|
10
|
-
let refCount = 0;
|
|
11
|
-
|
|
12
|
-
let subscription: Unsubscribable | null = null;
|
|
13
|
-
const observers: Partial<Observer<TValue, TError>>[] = [];
|
|
14
|
-
|
|
15
|
-
function startIfNeeded() {
|
|
16
|
-
if (subscription) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
subscription = originalObserver.subscribe({
|
|
20
|
-
next(value) {
|
|
21
|
-
for (const observer of observers) {
|
|
22
|
-
observer.next?.(value);
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
error(error) {
|
|
26
|
-
for (const observer of observers) {
|
|
27
|
-
observer.error?.(error);
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
complete() {
|
|
31
|
-
for (const observer of observers) {
|
|
32
|
-
observer.complete?.();
|
|
33
|
-
}
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
function resetIfNeeded() {
|
|
38
|
-
// "resetOnRefCountZero"
|
|
39
|
-
if (refCount === 0 && subscription) {
|
|
40
|
-
const _sub = subscription;
|
|
41
|
-
subscription = null;
|
|
42
|
-
_sub.unsubscribe();
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return {
|
|
47
|
-
subscribe(observer) {
|
|
48
|
-
refCount++;
|
|
49
|
-
|
|
50
|
-
observers.push(observer);
|
|
51
|
-
startIfNeeded();
|
|
52
|
-
return {
|
|
53
|
-
unsubscribe() {
|
|
54
|
-
refCount--;
|
|
55
|
-
resetIfNeeded();
|
|
56
|
-
|
|
57
|
-
const index = observers.findIndex((v) => v === observer);
|
|
58
|
-
|
|
59
|
-
if (index > -1) {
|
|
60
|
-
observers.splice(index, 1);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
}
|
package/src/rx/operators/tap.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { MonoTypeOperatorFunction, Observer } from '../types';
|
|
2
|
-
|
|
3
|
-
export function tap<TValue, TError>(
|
|
4
|
-
observer: Partial<Observer<TValue, TError>>,
|
|
5
|
-
): MonoTypeOperatorFunction<TValue, TError> {
|
|
6
|
-
return (originalObserver) => {
|
|
7
|
-
return {
|
|
8
|
-
subscribe(observer2) {
|
|
9
|
-
return originalObserver.subscribe({
|
|
10
|
-
next(v) {
|
|
11
|
-
observer.next?.(v);
|
|
12
|
-
observer2.next?.(v);
|
|
13
|
-
},
|
|
14
|
-
error(v) {
|
|
15
|
-
observer.error?.(v);
|
|
16
|
-
observer2.error?.(v);
|
|
17
|
-
},
|
|
18
|
-
complete() {
|
|
19
|
-
observer.complete?.();
|
|
20
|
-
observer2.complete?.();
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
}
|
package/src/rx/types.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
export interface Unsubscribable {
|
|
2
|
-
unsubscribe(): void;
|
|
3
|
-
}
|
|
4
|
-
export type UnsubscribeFn = () => void;
|
|
5
|
-
export interface Subscribable<TValue, TError> {
|
|
6
|
-
subscribe(observer: Partial<Observer<TValue, TError>>): Unsubscribable;
|
|
7
|
-
}
|
|
8
|
-
export interface Observable<TValue, TError>
|
|
9
|
-
extends Subscribable<TValue, TError> {
|
|
10
|
-
pipe(): Observable<TValue, TError>;
|
|
11
|
-
pipe<V1, E1>(
|
|
12
|
-
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
13
|
-
): Observable<V1, E1>;
|
|
14
|
-
pipe<V1, E1, V2, E2>(
|
|
15
|
-
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
16
|
-
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
17
|
-
): Observable<V2, E2>;
|
|
18
|
-
pipe<V1, E1, V2, E2, V3, E3>(
|
|
19
|
-
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
20
|
-
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
21
|
-
op3: OperatorFunction<V2, E2, V3, E3>,
|
|
22
|
-
): Observable<V2, E2>;
|
|
23
|
-
pipe<V1, E1, V2, E2, V3, E3, V4, E4>(
|
|
24
|
-
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
25
|
-
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
26
|
-
op3: OperatorFunction<V2, E2, V3, E3>,
|
|
27
|
-
op4: OperatorFunction<V3, E3, V4, E4>,
|
|
28
|
-
): Observable<V2, E2>;
|
|
29
|
-
pipe<V1, E1, V2, E2, V3, E3, V4, E4, V5, E5>(
|
|
30
|
-
op1: OperatorFunction<TValue, TError, V1, E1>,
|
|
31
|
-
op2: OperatorFunction<V1, E1, V2, E2>,
|
|
32
|
-
op3: OperatorFunction<V2, E2, V3, E3>,
|
|
33
|
-
op4: OperatorFunction<V3, E3, V4, E4>,
|
|
34
|
-
op5: OperatorFunction<V4, E4, V5, E5>,
|
|
35
|
-
): Observable<V2, E2>;
|
|
36
|
-
}
|
|
37
|
-
export interface SubscriptionLike extends Unsubscribable {
|
|
38
|
-
unsubscribe(): void;
|
|
39
|
-
readonly closed: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface Observer<TValue, TError> {
|
|
43
|
-
next: (value: TValue) => void;
|
|
44
|
-
error: (err: TError) => void;
|
|
45
|
-
complete: () => void;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export type TeardownLogic =
|
|
49
|
-
// | SubscriptionLike
|
|
50
|
-
Unsubscribable | UnsubscribeFn | void;
|
|
51
|
-
|
|
52
|
-
export type UnaryFunction<T, R> = (source: T) => R;
|
|
53
|
-
|
|
54
|
-
export type OperatorFunction<
|
|
55
|
-
TValueBefore,
|
|
56
|
-
TErrorBefore,
|
|
57
|
-
TValueAfter,
|
|
58
|
-
TErrorAfter,
|
|
59
|
-
> = UnaryFunction<
|
|
60
|
-
Subscribable<TValueBefore, TErrorBefore>,
|
|
61
|
-
Subscribable<TValueAfter, TErrorAfter>
|
|
62
|
-
>;
|
|
63
|
-
|
|
64
|
-
export type MonoTypeOperatorFunction<TValue, TError> = OperatorFunction<
|
|
65
|
-
TValue,
|
|
66
|
-
TError,
|
|
67
|
-
TValue,
|
|
68
|
-
TError
|
|
69
|
-
>;
|
package/src/rx/util/identity.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Observable } from '../types';
|
|
2
|
-
|
|
3
|
-
export class ObservableAbortError extends Error {
|
|
4
|
-
constructor(message: string) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = 'ObservableAbortError';
|
|
7
|
-
Object.setPrototypeOf(this, ObservableAbortError.prototype);
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/** @internal */
|
|
12
|
-
export function observableToPromise<TValue>(
|
|
13
|
-
observable: Observable<TValue, unknown>,
|
|
14
|
-
) {
|
|
15
|
-
let abort: () => void;
|
|
16
|
-
const promise = new Promise<TValue>((resolve, reject) => {
|
|
17
|
-
let isDone = false;
|
|
18
|
-
function onDone() {
|
|
19
|
-
if (isDone) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
isDone = true;
|
|
23
|
-
reject(new ObservableAbortError('This operation was cancelled.'));
|
|
24
|
-
obs$.unsubscribe();
|
|
25
|
-
}
|
|
26
|
-
const obs$ = observable.subscribe({
|
|
27
|
-
next(data) {
|
|
28
|
-
isDone = true;
|
|
29
|
-
resolve(data);
|
|
30
|
-
onDone();
|
|
31
|
-
},
|
|
32
|
-
error(data) {
|
|
33
|
-
isDone = true;
|
|
34
|
-
reject(data);
|
|
35
|
-
onDone();
|
|
36
|
-
},
|
|
37
|
-
complete() {
|
|
38
|
-
isDone = true;
|
|
39
|
-
onDone();
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
abort = onDone;
|
|
43
|
-
});
|
|
44
|
-
return {
|
|
45
|
-
promise,
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
47
|
-
abort: abort!,
|
|
48
|
-
};
|
|
49
|
-
}
|
package/src/rx/util/pipe.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { identity } from './identity';
|
|
2
|
-
import { UnaryFunction } from '../types';
|
|
3
|
-
|
|
4
|
-
/** @internal */
|
|
5
|
-
export function pipeFromArray<T, R>(
|
|
6
|
-
fns: Array<UnaryFunction<T, R>>,
|
|
7
|
-
): UnaryFunction<T, R> {
|
|
8
|
-
if (fns.length === 0) {
|
|
9
|
-
return identity as UnaryFunction<any, any>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (fns.length === 1) {
|
|
13
|
-
return fns[0];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return function piped(input: T): R {
|
|
17
|
-
return fns.reduce(
|
|
18
|
-
(prev: any, fn: UnaryFunction<T, R>) => fn(prev),
|
|
19
|
-
input as any,
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
}
|