@trpc/client 10.0.0-alpha.2 → 10.0.0-alpha.22

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.
Files changed (183) hide show
  1. package/dist/{declarations/src/TRPCClientError.d.ts → TRPCClientError.d.ts} +21 -29
  2. package/dist/TRPCClientError.d.ts.map +1 -0
  3. package/dist/{declarations/src/createTRPCClient.d.ts → createTRPCClient.d.ts} +5 -5
  4. package/dist/createTRPCClient.d.ts.map +1 -0
  5. package/dist/{declarations/src/getFetch.d.ts → getFetch.d.ts} +1 -1
  6. package/dist/getFetch.d.ts.map +1 -0
  7. package/dist/{declarations/src/index.d.ts → index.d.ts} +4 -4
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +1417 -0
  10. package/dist/index.mjs +1391 -0
  11. package/dist/index.ts.js +1 -0
  12. package/dist/{declarations/src/internals → internals}/TRPCClient.d.ts +61 -64
  13. package/dist/internals/TRPCClient.d.ts.map +1 -0
  14. package/dist/{declarations/src/internals → internals}/dataLoader.d.ts +17 -14
  15. package/dist/internals/dataLoader.d.ts.map +1 -0
  16. package/dist/{declarations/src/internals → internals}/fetchHelpers.d.ts +2 -2
  17. package/dist/internals/fetchHelpers.d.ts.map +1 -0
  18. package/dist/{declarations/src/internals → internals}/retryDelay.d.ts +1 -1
  19. package/dist/internals/retryDelay.d.ts.map +1 -0
  20. package/dist/links/dedupeLink.d.ts +4 -0
  21. package/dist/links/dedupeLink.d.ts.map +1 -0
  22. package/dist/links/httpBatchLink.d.ts +8 -0
  23. package/dist/links/httpBatchLink.d.ts.map +1 -0
  24. package/dist/{declarations/src/links → links}/httpLink.d.ts +4 -4
  25. package/dist/links/httpLink.d.ts.map +1 -0
  26. package/dist/links/index.d.ts +9 -0
  27. package/dist/links/index.d.ts.map +1 -0
  28. package/dist/{declarations/src/links → links}/internals/createChain.d.ts +7 -7
  29. package/dist/links/internals/createChain.d.ts.map +1 -0
  30. package/dist/links/internals/httpUtils.d.ts +30 -0
  31. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  32. package/dist/links/internals/transformOperationResult.d.ts +30 -0
  33. package/dist/links/internals/transformOperationResult.d.ts.map +1 -0
  34. package/dist/{declarations/src/links → links}/loggerLink.d.ts +38 -38
  35. package/dist/links/loggerLink.d.ts.map +1 -0
  36. package/dist/links/retryLink.d.ts +6 -0
  37. package/dist/links/retryLink.d.ts.map +1 -0
  38. package/dist/{declarations/src/links → links}/splitLink.d.ts +13 -13
  39. package/dist/links/splitLink.d.ts.map +1 -0
  40. package/dist/{declarations/src/links → links}/types.d.ts +42 -39
  41. package/dist/links/types.d.ts.map +1 -0
  42. package/dist/{declarations/src/links → links}/wsLink.d.ts +19 -19
  43. package/dist/links/wsLink.d.ts.map +1 -0
  44. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +68 -38
  45. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +68 -38
  46. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +67 -37
  47. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +52 -29
  48. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +52 -29
  49. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +52 -29
  50. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.dev.js +19 -31
  51. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.prod.js +19 -31
  52. package/links/loggerLink/dist/trpc-client-links-loggerLink.esm.js +19 -31
  53. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.dev.js +7 -20
  54. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.prod.js +7 -20
  55. package/links/splitLink/dist/trpc-client-links-splitLink.esm.js +7 -20
  56. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +84 -60
  57. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +84 -60
  58. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +84 -60
  59. package/package.json +26 -23
  60. package/src/TRPCClientError.ts +3 -11
  61. package/src/createTRPCClient.ts +1 -1
  62. package/src/index.ts +1 -1
  63. package/src/internals/TRPCClient.ts +52 -36
  64. package/src/internals/dataLoader.ts +120 -45
  65. package/src/links/dedupeLink.test.ts +1 -1
  66. package/src/links/dedupeLink.ts +3 -5
  67. package/src/links/httpBatchLink.ts +64 -31
  68. package/src/links/httpLink.ts +4 -4
  69. package/src/links/index.ts +8 -0
  70. package/src/links/internals/createChain.test.ts +5 -5
  71. package/src/links/internals/createChain.ts +3 -3
  72. package/src/links/internals/httpUtils.ts +116 -0
  73. package/src/links/internals/transformOperationResult.ts +40 -12
  74. package/src/links/loggerLink.ts +1 -2
  75. package/src/links/retryLink.ts +1 -2
  76. package/src/links/splitLink.test.ts +3 -3
  77. package/src/links/splitLink.ts +5 -5
  78. package/src/links/types.ts +13 -8
  79. package/src/links/wsLink.ts +12 -12
  80. package/dist/TRPCClientError-a5312a5f.cjs.dev.js +0 -74
  81. package/dist/TRPCClientError-e1da1ef0.esm.js +0 -72
  82. package/dist/TRPCClientError-ea7d89b2.cjs.prod.js +0 -74
  83. package/dist/createChain-93a38e1c.cjs.dev.js +0 -27
  84. package/dist/createChain-e45e052a.cjs.prod.js +0 -27
  85. package/dist/createChain-e45e6cf2.esm.js +0 -25
  86. package/dist/declarations/src/TRPCClientError.d.ts.map +0 -1
  87. package/dist/declarations/src/createTRPCClient.d.ts.map +0 -1
  88. package/dist/declarations/src/getFetch.d.ts.map +0 -1
  89. package/dist/declarations/src/index.d.ts.map +0 -1
  90. package/dist/declarations/src/internals/TRPCClient.d.ts.map +0 -1
  91. package/dist/declarations/src/internals/dataLoader.d.ts.map +0 -1
  92. package/dist/declarations/src/internals/fetchHelpers.d.ts.map +0 -1
  93. package/dist/declarations/src/internals/retryDelay.d.ts.map +0 -1
  94. package/dist/declarations/src/links/httpBatchLink.d.ts +0 -5
  95. package/dist/declarations/src/links/httpBatchLink.d.ts.map +0 -1
  96. package/dist/declarations/src/links/httpLink.d.ts.map +0 -1
  97. package/dist/declarations/src/links/httpUtils.d.ts +0 -22
  98. package/dist/declarations/src/links/httpUtils.d.ts.map +0 -1
  99. package/dist/declarations/src/links/internals/createChain.d.ts.map +0 -1
  100. package/dist/declarations/src/links/internals/transformOperationResult.d.ts +0 -16
  101. package/dist/declarations/src/links/internals/transformOperationResult.d.ts.map +0 -1
  102. package/dist/declarations/src/links/loggerLink.d.ts.map +0 -1
  103. package/dist/declarations/src/links/splitLink.d.ts.map +0 -1
  104. package/dist/declarations/src/links/transformerLink.d.ts +0 -4
  105. package/dist/declarations/src/links/transformerLink.d.ts.map +0 -1
  106. package/dist/declarations/src/links/types.d.ts.map +0 -1
  107. package/dist/declarations/src/links/wsLink.d.ts.map +0 -1
  108. package/dist/declarations/src/rx/index.d.ts +0 -3
  109. package/dist/declarations/src/rx/index.d.ts.map +0 -1
  110. package/dist/declarations/src/rx/observable.d.ts +0 -4
  111. package/dist/declarations/src/rx/observable.d.ts.map +0 -1
  112. package/dist/declarations/src/rx/operators/index.d.ts +0 -4
  113. package/dist/declarations/src/rx/operators/index.d.ts.map +0 -1
  114. package/dist/declarations/src/rx/operators/map.d.ts +0 -3
  115. package/dist/declarations/src/rx/operators/map.d.ts.map +0 -1
  116. package/dist/declarations/src/rx/operators/share.d.ts +0 -6
  117. package/dist/declarations/src/rx/operators/share.d.ts.map +0 -1
  118. package/dist/declarations/src/rx/operators/tap.d.ts +0 -3
  119. package/dist/declarations/src/rx/operators/tap.d.ts.map +0 -1
  120. package/dist/declarations/src/rx/types.d.ts +0 -29
  121. package/dist/declarations/src/rx/types.d.ts.map +0 -1
  122. package/dist/declarations/src/rx/util/identity.d.ts +0 -2
  123. package/dist/declarations/src/rx/util/identity.d.ts.map +0 -1
  124. package/dist/declarations/src/rx/util/observableToPromise.d.ts +0 -10
  125. package/dist/declarations/src/rx/util/observableToPromise.d.ts.map +0 -1
  126. package/dist/declarations/src/rx/util/pipe.d.ts +0 -4
  127. package/dist/declarations/src/rx/util/pipe.d.ts.map +0 -1
  128. package/dist/httpUtils-299a66f6.cjs.prod.js +0 -89
  129. package/dist/httpUtils-a1426e14.cjs.dev.js +0 -89
  130. package/dist/httpUtils-e45c9056.esm.js +0 -87
  131. package/dist/observable-01534ea8.esm.js +0 -102
  132. package/dist/observable-9aad8c3e.cjs.dev.js +0 -104
  133. package/dist/observable-ccb54234.cjs.prod.js +0 -104
  134. package/dist/share-5a8c2543.cjs.prod.js +0 -106
  135. package/dist/share-ca585761.esm.js +0 -104
  136. package/dist/share-f2ac9332.cjs.dev.js +0 -106
  137. package/dist/tap-2d565250.cjs.dev.js +0 -32
  138. package/dist/tap-606957cb.esm.js +0 -30
  139. package/dist/tap-d907998d.cjs.prod.js +0 -32
  140. package/dist/trpc-client.cjs.d.ts +0 -1
  141. package/dist/trpc-client.cjs.dev.js +0 -300
  142. package/dist/trpc-client.cjs.js +0 -7
  143. package/dist/trpc-client.cjs.prod.js +0 -300
  144. package/dist/trpc-client.esm.js +0 -295
  145. package/links/httpBatchLink/package.json +0 -4
  146. package/links/httpLink/package.json +0 -4
  147. package/links/loggerLink/package.json +0 -4
  148. package/links/splitLink/package.json +0 -4
  149. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  150. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  151. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  152. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  153. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  154. package/links/transformerLink/package.json +0 -4
  155. package/links/wsLink/package.json +0 -4
  156. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  157. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  158. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  159. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  160. package/rx/dist/trpc-client-rx.esm.js +0 -1
  161. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  162. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  163. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  164. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  165. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  166. package/rx/operators/package.json +0 -4
  167. package/rx/package.json +0 -4
  168. package/src/internals/transformRPCResponse.ts +0 -25
  169. package/src/links/httpUtils.ts +0 -94
  170. package/src/links/transformerLink.ts +0 -70
  171. package/src/rx/index.ts +0 -2
  172. package/src/rx/observable.test.ts +0 -83
  173. package/src/rx/observable.ts +0 -70
  174. package/src/rx/operators/index.ts +0 -3
  175. package/src/rx/operators/map.test.ts +0 -92
  176. package/src/rx/operators/map.ts +0 -25
  177. package/src/rx/operators/share.test.ts +0 -60
  178. package/src/rx/operators/share.ts +0 -67
  179. package/src/rx/operators/tap.ts +0 -26
  180. package/src/rx/types.ts +0 -69
  181. package/src/rx/util/identity.ts +0 -3
  182. package/src/rx/util/observableToPromise.ts +0 -49
  183. package/src/rx/util/pipe.ts +0 -22
@@ -1,33 +0,0 @@
1
- export { s as share } from '../../../dist/share-ca585761.esm.js';
2
- export { t as tap } from '../../../dist/tap-606957cb.esm.js';
3
- import '@babel/runtime/helpers/createForOfIteratorHelper';
4
-
5
- function map(project) {
6
- return function (originalObserver) {
7
- return {
8
- subscribe: function subscribe(observer) {
9
- var index = 0;
10
- var subscription = originalObserver.subscribe({
11
- next: function next(value) {
12
- var _observer$next;
13
-
14
- (_observer$next = observer.next) === null || _observer$next === void 0 ? void 0 : _observer$next.call(observer, project(value, index++));
15
- },
16
- error: function error(_error) {
17
- var _observer$error;
18
-
19
- (_observer$error = observer.error) === null || _observer$error === void 0 ? void 0 : _observer$error.call(observer, _error);
20
- },
21
- complete: function complete() {
22
- var _observer$complete;
23
-
24
- (_observer$complete = observer.complete) === null || _observer$complete === void 0 ? void 0 : _observer$complete.call(observer);
25
- }
26
- });
27
- return subscription;
28
- }
29
- };
30
- };
31
- }
32
-
33
- export { map };
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-rx-operators.cjs.js",
3
- "module": "dist/trpc-client-rx-operators.esm.js"
4
- }
package/rx/package.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-rx.cjs.js",
3
- "module": "dist/trpc-client-rx.esm.js"
4
- }
@@ -1,25 +0,0 @@
1
- import { TRPCResponse } from '@trpc/server/rpc';
2
- import { TRPCClientError } from '../TRPCClientError';
3
- import { LinkRuntime } from '../links/types';
4
-
5
- export function transformRPCResponse({
6
- envelope,
7
- runtime,
8
- }: {
9
- envelope: TRPCResponse;
10
- runtime: LinkRuntime;
11
- }) {
12
- if ('error' in envelope) {
13
- return TRPCClientError.from({
14
- ...envelope,
15
- error: runtime.transformer.deserialize(envelope.error),
16
- });
17
- }
18
- if (envelope.result.type === 'data') {
19
- return {
20
- ...envelope.result,
21
- data: runtime.transformer.deserialize(envelope.result.data),
22
- };
23
- }
24
- return envelope.result;
25
- }
@@ -1,94 +0,0 @@
1
- import { ProcedureType } from '@trpc/server';
2
- import { LinkRuntime, PromiseAndCancel } from './types';
3
- export interface HTTPLinkOptions {
4
- url: string;
5
- }
6
-
7
- // https://github.com/trpc/trpc/pull/669
8
- function arrayToDict(array: unknown[]) {
9
- const dict: Record<number, unknown> = {};
10
- for (let index = 0; index < array.length; index++) {
11
- const element = array[index];
12
- dict[index] = element;
13
- }
14
- return dict;
15
- }
16
- const METHOD = {
17
- query: 'GET',
18
- mutation: 'POST',
19
- subscription: 'PATCH',
20
- } as const;
21
-
22
- export interface ResponseShape {
23
- json: unknown;
24
- meta: {
25
- response: Response;
26
- };
27
- }
28
- export function httpRequest(
29
- props: {
30
- runtime: LinkRuntime;
31
- type: ProcedureType;
32
- path: string;
33
- url: string;
34
- } & ({ inputs: unknown[] } | { input: unknown }),
35
- ): PromiseAndCancel<ResponseShape> {
36
- const { type, runtime: rt, path } = props;
37
- const ac = rt.AbortController ? new rt.AbortController() : null;
38
-
39
- const input = 'input' in props ? props.input : arrayToDict(props.inputs);
40
-
41
- function getUrl() {
42
- let url = props.url + '/' + path;
43
- const queryParts: string[] = [];
44
- if ('inputs' in props) {
45
- queryParts.push('batch=1');
46
- }
47
- if (type === 'query' && input !== undefined) {
48
- queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
49
- }
50
- if (queryParts.length) {
51
- url += '?' + queryParts.join('&');
52
- }
53
- return url;
54
- }
55
- function getBody() {
56
- if (type === 'query') {
57
- return undefined;
58
- }
59
- return input !== undefined ? JSON.stringify(input) : undefined;
60
- }
61
-
62
- const promise = new Promise<ResponseShape>((resolve, reject) => {
63
- const url = getUrl();
64
-
65
- const meta = {} as any as ResponseShape['meta'];
66
- Promise.resolve(rt.headers())
67
- .then((headers) =>
68
- rt.fetch(url, {
69
- method: METHOD[type],
70
- signal: ac?.signal,
71
- body: getBody(),
72
- headers: {
73
- 'content-type': 'application/json',
74
- ...headers,
75
- },
76
- }),
77
- )
78
- .then((_res) => {
79
- meta.response = _res;
80
- return _res.json();
81
- })
82
- .then((json) => {
83
- resolve({
84
- json,
85
- meta,
86
- });
87
- })
88
- .catch(reject);
89
- });
90
- const cancel = () => {
91
- ac?.abort();
92
- };
93
- return { promise, cancel };
94
- }
@@ -1,70 +0,0 @@
1
- import {
2
- AnyRouter,
3
- ClientDataTransformerOptions,
4
- DataTransformer,
5
- } from '@trpc/server';
6
- import { observable } from '../rx/observable';
7
- import { OperationResult, TRPCLink } from './types';
8
-
9
- /**
10
- * @internal
11
- */
12
- function transformOperationResult<TResult extends OperationResult<any, any>>(
13
- result: TResult,
14
- transformer: DataTransformer,
15
- ) {
16
- if ('error' in result.data) {
17
- return {
18
- ...result,
19
- data: {
20
- ...result.data,
21
- error: transformer.deserialize(result.data.error),
22
- },
23
- };
24
- }
25
- if (result.data.result.type !== 'data') {
26
- return result;
27
- }
28
- return {
29
- ...result,
30
- data: {
31
- ...result.data,
32
- result: {
33
- ...result.data.result,
34
- data: transformer.deserialize(result.data.result.data),
35
- },
36
- },
37
- };
38
- }
39
- export function transformerLink<TRouter extends AnyRouter = AnyRouter>(
40
- transformer: ClientDataTransformerOptions,
41
- ): TRPCLink<TRouter> {
42
- const _transformer: DataTransformer = transformer
43
- ? 'input' in transformer
44
- ? {
45
- serialize: transformer.input.serialize,
46
- deserialize: transformer.output.deserialize,
47
- }
48
- : transformer
49
- : {
50
- serialize: (data) => data,
51
- deserialize: (data) => data,
52
- };
53
-
54
- return () => {
55
- return (props) => {
56
- const input = _transformer.serialize(props.op.input);
57
- return observable((observer) => {
58
- const next$ = props.next({ ...props.op, input }).subscribe({
59
- next(value) {
60
- const transformed = transformOperationResult(value, _transformer);
61
- observer.next(transformed);
62
- },
63
- error: observer.error,
64
- complete: observer.complete,
65
- });
66
- return next$;
67
- });
68
- };
69
- };
70
- }
package/src/rx/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './observable';
2
- export * from './types';
@@ -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
- });
@@ -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,3 +0,0 @@
1
- export * from './share';
2
- export * from './map';
3
- export * from './tap';
@@ -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
- });
@@ -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
- }
@@ -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
- }