@trpc/client 11.1.3-alpha-tmp-issues-6785.34 → 11.1.3-alpha-tmp-tsdown.24

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 (244) hide show
  1. package/dist/TRPCClientError.d.mts +32 -0
  2. package/dist/TRPCClientError.d.mts.map +1 -0
  3. package/dist/TRPCClientError.d.ts +28 -25
  4. package/dist/TRPCClientError.d.ts.map +1 -1
  5. package/dist/TRPCClientError.js +48 -72
  6. package/dist/TRPCClientError.mjs +48 -71
  7. package/dist/TRPCClientError.mjs.map +1 -0
  8. package/dist/_virtual/rolldown_runtime.js +39 -0
  9. package/dist/_virtual/rolldown_runtime.mjs +27 -0
  10. package/dist/createTRPCClient.d.mts +69 -0
  11. package/dist/createTRPCClient.d.mts.map +1 -0
  12. package/dist/createTRPCClient.d.ts +34 -34
  13. package/dist/createTRPCClient.d.ts.map +1 -1
  14. package/dist/createTRPCClient.js +34 -34
  15. package/dist/createTRPCClient.mjs +33 -31
  16. package/dist/createTRPCClient.mjs.map +1 -0
  17. package/dist/createTRPCUntypedClient.d.mts +8 -0
  18. package/dist/createTRPCUntypedClient.d.mts.map +1 -0
  19. package/dist/createTRPCUntypedClient.d.ts +7 -6
  20. package/dist/createTRPCUntypedClient.d.ts.map +1 -1
  21. package/dist/createTRPCUntypedClient.js +5 -6
  22. package/dist/createTRPCUntypedClient.mjs +6 -3
  23. package/dist/createTRPCUntypedClient.mjs.map +1 -0
  24. package/dist/getFetch.d.mts +9 -0
  25. package/dist/getFetch.d.mts.map +1 -0
  26. package/dist/getFetch.d.ts +8 -2
  27. package/dist/getFetch.d.ts.map +1 -1
  28. package/dist/getFetch.js +8 -13
  29. package/dist/getFetch.mjs +8 -11
  30. package/dist/getFetch.mjs.map +1 -0
  31. package/dist/index.d.mts +21 -0
  32. package/dist/index.d.ts +21 -16
  33. package/dist/index.js +42 -42
  34. package/dist/index.mjs +19 -15
  35. package/dist/internals/TRPCUntypedClient.d.mts +45 -0
  36. package/dist/internals/TRPCUntypedClient.d.mts.map +1 -0
  37. package/dist/internals/TRPCUntypedClient.d.ts +39 -33
  38. package/dist/internals/TRPCUntypedClient.d.ts.map +1 -1
  39. package/dist/internals/TRPCUntypedClient.js +94 -113
  40. package/dist/internals/TRPCUntypedClient.mjs +93 -110
  41. package/dist/internals/TRPCUntypedClient.mjs.map +1 -0
  42. package/dist/internals/dataLoader.js +101 -112
  43. package/dist/internals/dataLoader.mjs +101 -110
  44. package/dist/internals/dataLoader.mjs.map +1 -0
  45. package/dist/internals/inputWithTrackedEventId.js +9 -12
  46. package/dist/internals/inputWithTrackedEventId.mjs +9 -10
  47. package/dist/internals/inputWithTrackedEventId.mjs.map +1 -0
  48. package/dist/internals/signals.js +39 -40
  49. package/dist/internals/signals.mjs +39 -39
  50. package/dist/internals/signals.mjs.map +1 -0
  51. package/dist/internals/transformer.d.mts +46 -0
  52. package/dist/internals/transformer.d.mts.map +1 -0
  53. package/dist/internals/transformer.d.ts +26 -22
  54. package/dist/internals/transformer.d.ts.map +1 -1
  55. package/dist/internals/transformer.js +25 -26
  56. package/dist/internals/transformer.mjs +25 -24
  57. package/dist/internals/transformer.mjs.map +1 -0
  58. package/dist/internals/types.d.mts +84 -0
  59. package/dist/internals/types.d.mts.map +1 -0
  60. package/dist/internals/types.d.ts +44 -42
  61. package/dist/internals/types.d.ts.map +1 -1
  62. package/dist/links/HTTPBatchLinkOptions.d.mts +25 -0
  63. package/dist/links/HTTPBatchLinkOptions.d.mts.map +1 -0
  64. package/dist/links/HTTPBatchLinkOptions.d.ts +23 -18
  65. package/dist/links/HTTPBatchLinkOptions.d.ts.map +1 -1
  66. package/dist/links/httpBatchLink.d.mts +15 -0
  67. package/dist/links/httpBatchLink.d.mts.map +1 -0
  68. package/dist/links/httpBatchLink.d.ts +11 -4
  69. package/dist/links/httpBatchLink.d.ts.map +1 -1
  70. package/dist/links/httpBatchLink.js +90 -106
  71. package/dist/links/httpBatchLink.mjs +89 -103
  72. package/dist/links/httpBatchLink.mjs.map +1 -0
  73. package/dist/links/httpBatchStreamLink.d.mts +19 -0
  74. package/dist/links/httpBatchStreamLink.d.mts.map +1 -0
  75. package/dist/links/httpBatchStreamLink.d.ts +12 -5
  76. package/dist/links/httpBatchStreamLink.d.ts.map +1 -1
  77. package/dist/links/httpBatchStreamLink.js +120 -144
  78. package/dist/links/httpBatchStreamLink.mjs +119 -141
  79. package/dist/links/httpBatchStreamLink.mjs.map +1 -0
  80. package/dist/links/httpLink.d.mts +23 -0
  81. package/dist/links/httpLink.d.mts.map +1 -0
  82. package/dist/links/httpLink.d.ts +18 -12
  83. package/dist/links/httpLink.d.ts.map +1 -1
  84. package/dist/links/httpLink.js +74 -90
  85. package/dist/links/httpLink.mjs +73 -87
  86. package/dist/links/httpLink.mjs.map +1 -0
  87. package/dist/links/httpSubscriptionLink.d.mts +29 -0
  88. package/dist/links/httpSubscriptionLink.d.mts.map +1 -0
  89. package/dist/links/httpSubscriptionLink.d.ts +20 -17
  90. package/dist/links/httpSubscriptionLink.d.ts.map +1 -1
  91. package/dist/links/httpSubscriptionLink.js +134 -179
  92. package/dist/links/httpSubscriptionLink.mjs +133 -176
  93. package/dist/links/httpSubscriptionLink.mjs.map +1 -0
  94. package/dist/links/internals/contentTypes.d.mts +9 -0
  95. package/dist/links/internals/contentTypes.d.mts.map +1 -0
  96. package/dist/links/internals/contentTypes.d.ts +8 -3
  97. package/dist/links/internals/contentTypes.d.ts.map +1 -1
  98. package/dist/links/internals/contentTypes.js +6 -6
  99. package/dist/links/internals/contentTypes.mjs +6 -4
  100. package/dist/links/internals/contentTypes.mjs.map +1 -0
  101. package/dist/links/internals/createChain.js +23 -23
  102. package/dist/links/internals/createChain.mjs +22 -20
  103. package/dist/links/internals/createChain.mjs.map +1 -0
  104. package/dist/links/internals/httpUtils.d.mts +25 -0
  105. package/dist/links/internals/httpUtils.d.mts.map +1 -0
  106. package/dist/links/internals/httpUtils.d.ts +20 -63
  107. package/dist/links/internals/httpUtils.d.ts.map +1 -1
  108. package/dist/links/internals/httpUtils.js +93 -116
  109. package/dist/links/internals/httpUtils.mjs +94 -113
  110. package/dist/links/internals/httpUtils.mjs.map +1 -0
  111. package/dist/links/internals/subscriptions.d.mts +19 -0
  112. package/dist/links/internals/subscriptions.d.mts.map +1 -0
  113. package/dist/links/internals/subscriptions.d.ts +10 -8
  114. package/dist/links/internals/subscriptions.d.ts.map +1 -1
  115. package/dist/links/internals/urlWithConnectionParams.d.mts +24 -0
  116. package/dist/links/internals/urlWithConnectionParams.d.mts.map +1 -0
  117. package/dist/links/internals/urlWithConnectionParams.d.ts +19 -18
  118. package/dist/links/internals/urlWithConnectionParams.d.ts.map +1 -1
  119. package/dist/links/internals/urlWithConnectionParams.js +8 -6
  120. package/dist/links/internals/urlWithConnectionParams.mjs +8 -4
  121. package/dist/links/internals/urlWithConnectionParams.mjs.map +1 -0
  122. package/dist/links/localLink.d.mts +21 -0
  123. package/dist/links/localLink.d.mts.map +1 -0
  124. package/dist/links/localLink.d.ts +21 -0
  125. package/dist/links/localLink.d.ts.map +1 -0
  126. package/dist/links/localLink.js +192 -0
  127. package/dist/links/localLink.mjs +193 -0
  128. package/dist/links/localLink.mjs.map +1 -0
  129. package/dist/links/loggerLink.d.mts +55 -0
  130. package/dist/links/loggerLink.d.mts.map +1 -0
  131. package/dist/links/loggerLink.d.ts +39 -36
  132. package/dist/links/loggerLink.d.ts.map +1 -1
  133. package/dist/links/loggerLink.js +113 -166
  134. package/dist/links/loggerLink.mjs +112 -163
  135. package/dist/links/loggerLink.mjs.map +1 -0
  136. package/dist/links/retryLink.d.mts +36 -0
  137. package/dist/links/retryLink.d.mts.map +1 -0
  138. package/dist/links/retryLink.d.ts +28 -25
  139. package/dist/links/retryLink.d.ts.map +1 -1
  140. package/dist/links/retryLink.js +60 -68
  141. package/dist/links/retryLink.mjs +59 -65
  142. package/dist/links/retryLink.mjs.map +1 -0
  143. package/dist/links/splitLink.d.mts +20 -0
  144. package/dist/links/splitLink.d.mts.map +1 -0
  145. package/dist/links/splitLink.d.ts +18 -12
  146. package/dist/links/splitLink.d.ts.map +1 -1
  147. package/dist/links/splitLink.js +20 -21
  148. package/dist/links/splitLink.mjs +19 -18
  149. package/dist/links/splitLink.mjs.map +1 -0
  150. package/dist/links/types.d.mts +68 -0
  151. package/dist/links/types.d.mts.map +1 -0
  152. package/dist/links/types.d.ts +34 -29
  153. package/dist/links/types.d.ts.map +1 -1
  154. package/dist/links/wsLink/createWsClient.d.mts +9 -0
  155. package/dist/links/wsLink/createWsClient.d.mts.map +1 -0
  156. package/dist/links/wsLink/createWsClient.d.ts +8 -5
  157. package/dist/links/wsLink/createWsClient.d.ts.map +1 -1
  158. package/dist/links/wsLink/createWsClient.js +5 -5
  159. package/dist/links/wsLink/createWsClient.mjs +5 -2
  160. package/dist/links/wsLink/createWsClient.mjs.map +1 -0
  161. package/dist/links/wsLink/wsClient/options.d.mts +69 -0
  162. package/dist/links/wsLink/wsClient/options.d.mts.map +1 -0
  163. package/dist/links/wsLink/wsClient/options.d.ts +53 -63
  164. package/dist/links/wsLink/wsClient/options.d.ts.map +1 -1
  165. package/dist/links/wsLink/wsClient/options.js +14 -12
  166. package/dist/links/wsLink/wsClient/options.mjs +14 -10
  167. package/dist/links/wsLink/wsClient/options.mjs.map +1 -0
  168. package/dist/links/wsLink/wsClient/requestManager.js +133 -140
  169. package/dist/links/wsLink/wsClient/requestManager.mjs +133 -137
  170. package/dist/links/wsLink/wsClient/requestManager.mjs.map +1 -0
  171. package/dist/links/wsLink/wsClient/utils.js +64 -81
  172. package/dist/links/wsLink/wsClient/utils.mjs +64 -78
  173. package/dist/links/wsLink/wsClient/utils.mjs.map +1 -0
  174. package/dist/links/wsLink/wsClient/wsClient.d.mts +101 -0
  175. package/dist/links/wsLink/wsClient/wsClient.d.mts.map +1 -0
  176. package/dist/links/wsLink/wsClient/wsClient.d.ts +95 -78
  177. package/dist/links/wsLink/wsClient/wsClient.d.ts.map +1 -1
  178. package/dist/links/wsLink/wsClient/wsClient.js +273 -322
  179. package/dist/links/wsLink/wsClient/wsClient.mjs +272 -319
  180. package/dist/links/wsLink/wsClient/wsClient.mjs.map +1 -0
  181. package/dist/links/wsLink/wsClient/wsConnection.js +155 -178
  182. package/dist/links/wsLink/wsClient/wsConnection.mjs +154 -175
  183. package/dist/links/wsLink/wsClient/wsConnection.mjs.map +1 -0
  184. package/dist/links/wsLink/wsLink.d.mts +14 -0
  185. package/dist/links/wsLink/wsLink.d.mts.map +1 -0
  186. package/dist/links/wsLink/wsLink.d.ts +12 -9
  187. package/dist/links/wsLink/wsLink.d.ts.map +1 -1
  188. package/dist/links/wsLink/wsLink.js +30 -31
  189. package/dist/links/wsLink/wsLink.mjs +29 -28
  190. package/dist/links/wsLink/wsLink.mjs.map +1 -0
  191. package/dist/links.js +11 -0
  192. package/dist/links.mjs +11 -0
  193. package/dist/node_modules/.pnpm/@oxc-project_runtime@0.72.2/node_modules/@oxc-project/runtime/src/helpers/usingCtx.js +68 -0
  194. package/dist/node_modules/.pnpm/@oxc-project_runtime@0.72.2/node_modules/@oxc-project/runtime/src/helpers/usingCtx.mjs +66 -0
  195. package/dist/node_modules/.pnpm/@oxc-project_runtime@0.72.2/node_modules/@oxc-project/runtime/src/helpers/usingCtx.mjs.map +1 -0
  196. package/dist/unstable-internals.d.mts +3 -0
  197. package/dist/unstable-internals.d.ts +3 -3
  198. package/dist/unstable-internals.js +2 -6
  199. package/dist/unstable-internals.mjs +3 -1
  200. package/links/httpBatchLink/package.json +1 -0
  201. package/links/httpLink/package.json +1 -0
  202. package/links/loggerLink/package.json +1 -0
  203. package/links/splitLink/package.json +1 -0
  204. package/links/wsLink/wsLink/package.json +1 -0
  205. package/package.json +63 -30
  206. package/src/TRPCClientError.ts +4 -9
  207. package/src/internals/signals.ts +16 -0
  208. package/src/links/httpSubscriptionLink.ts +3 -18
  209. package/src/links/localLink.ts +277 -0
  210. package/src/links.ts +1 -0
  211. package/unstable-internals/package.json +1 -0
  212. package/dist/bundle-analysis.json +0 -433
  213. package/dist/index.d.ts.map +0 -1
  214. package/dist/internals/dataLoader.d.ts +0 -13
  215. package/dist/internals/dataLoader.d.ts.map +0 -1
  216. package/dist/internals/inputWithTrackedEventId.d.ts +0 -2
  217. package/dist/internals/inputWithTrackedEventId.d.ts.map +0 -1
  218. package/dist/internals/signals.d.ts +0 -15
  219. package/dist/internals/signals.d.ts.map +0 -1
  220. package/dist/links/internals/createChain.d.ts +0 -8
  221. package/dist/links/internals/createChain.d.ts.map +0 -1
  222. package/dist/links/internals/dedupeLink.d.ts +0 -7
  223. package/dist/links/internals/dedupeLink.d.ts.map +0 -1
  224. package/dist/links/wsLink/wsClient/requestManager.d.ts +0 -106
  225. package/dist/links/wsLink/wsClient/requestManager.d.ts.map +0 -1
  226. package/dist/links/wsLink/wsClient/utils.d.ts +0 -38
  227. package/dist/links/wsLink/wsClient/utils.d.ts.map +0 -1
  228. package/dist/links/wsLink/wsClient/wsConnection.d.ts +0 -79
  229. package/dist/links/wsLink/wsClient/wsConnection.d.ts.map +0 -1
  230. package/dist/links.d.ts +0 -11
  231. package/dist/links.d.ts.map +0 -1
  232. package/dist/unstable-internals.d.ts.map +0 -1
  233. package/links/httpBatchLink/index.d.ts +0 -1
  234. package/links/httpBatchLink/index.js +0 -1
  235. package/links/httpLink/index.d.ts +0 -1
  236. package/links/httpLink/index.js +0 -1
  237. package/links/loggerLink/index.d.ts +0 -1
  238. package/links/loggerLink/index.js +0 -1
  239. package/links/splitLink/index.d.ts +0 -1
  240. package/links/splitLink/index.js +0 -1
  241. package/links/wsLink/wsLink/index.d.ts +0 -1
  242. package/links/wsLink/wsLink/index.js +0 -1
  243. package/unstable-internals/index.d.ts +0 -1
  244. package/unstable-internals/index.js +0 -1
@@ -1,117 +1,106 @@
1
- 'use strict';
2
1
 
3
- /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
4
- * A function that should never be called unless we messed something up.
5
- */ const throwFatalError = ()=>{
6
- throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
2
+ //#region src/internals/dataLoader.ts
3
+ /**
4
+ * A function that should never be called unless we messed something up.
5
+ */
6
+ const throwFatalError = () => {
7
+ throw new Error("Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new");
7
8
  };
8
9
  /**
9
- * Dataloader that's very inspired by https://github.com/graphql/dataloader
10
- * Less configuration, no caching, and allows you to cancel requests
11
- * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
12
- */ function dataLoader(batchLoader) {
13
- let pendingItems = null;
14
- let dispatchTimer = null;
15
- const destroyTimerAndPendingItems = ()=>{
16
- clearTimeout(dispatchTimer);
17
- dispatchTimer = null;
18
- pendingItems = null;
19
- };
20
- /**
21
- * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
22
- */ function groupItems(items) {
23
- const groupedItems = [
24
- []
25
- ];
26
- let index = 0;
27
- while(true){
28
- const item = items[index];
29
- if (!item) {
30
- break;
31
- }
32
- const lastGroup = groupedItems[groupedItems.length - 1];
33
- if (item.aborted) {
34
- // Item was aborted before it was dispatched
35
- item.reject?.(new Error('Aborted'));
36
- index++;
37
- continue;
38
- }
39
- const isValid = batchLoader.validate(lastGroup.concat(item).map((it)=>it.key));
40
- if (isValid) {
41
- lastGroup.push(item);
42
- index++;
43
- continue;
44
- }
45
- if (lastGroup.length === 0) {
46
- item.reject?.(new Error('Input is too big for a single dispatch'));
47
- index++;
48
- continue;
49
- }
50
- // Create new group, next iteration will try to add the item to that
51
- groupedItems.push([]);
52
- }
53
- return groupedItems;
54
- }
55
- function dispatch() {
56
- const groupedItems = groupItems(pendingItems);
57
- destroyTimerAndPendingItems();
58
- // Create batches for each group of items
59
- for (const items of groupedItems){
60
- if (!items.length) {
61
- continue;
62
- }
63
- const batch = {
64
- items
65
- };
66
- for (const item of items){
67
- item.batch = batch;
68
- }
69
- const promise = batchLoader.fetch(batch.items.map((_item)=>_item.key));
70
- promise.then(async (result)=>{
71
- await Promise.all(result.map(async (valueOrPromise, index)=>{
72
- const item = batch.items[index];
73
- try {
74
- const value = await Promise.resolve(valueOrPromise);
75
- item.resolve?.(value);
76
- } catch (cause) {
77
- item.reject?.(cause);
78
- }
79
- item.batch = null;
80
- item.reject = null;
81
- item.resolve = null;
82
- }));
83
- for (const item of batch.items){
84
- item.reject?.(new Error('Missing result'));
85
- item.batch = null;
86
- }
87
- }).catch((cause)=>{
88
- for (const item of batch.items){
89
- item.reject?.(cause);
90
- item.batch = null;
91
- }
92
- });
93
- }
94
- }
95
- function load(key) {
96
- const item = {
97
- aborted: false,
98
- key,
99
- batch: null,
100
- resolve: throwFatalError,
101
- reject: throwFatalError
102
- };
103
- const promise = new Promise((resolve, reject)=>{
104
- item.reject = reject;
105
- item.resolve = resolve;
106
- pendingItems ?? (pendingItems = []);
107
- pendingItems.push(item);
108
- });
109
- dispatchTimer ?? (dispatchTimer = setTimeout(dispatch));
110
- return promise;
111
- }
112
- return {
113
- load
114
- };
10
+ * Dataloader that's very inspired by https://github.com/graphql/dataloader
11
+ * Less configuration, no caching, and allows you to cancel requests
12
+ * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
13
+ */
14
+ function dataLoader(batchLoader) {
15
+ let pendingItems = null;
16
+ let dispatchTimer = null;
17
+ const destroyTimerAndPendingItems = () => {
18
+ clearTimeout(dispatchTimer);
19
+ dispatchTimer = null;
20
+ pendingItems = null;
21
+ };
22
+ /**
23
+ * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
24
+ */
25
+ function groupItems(items) {
26
+ const groupedItems = [[]];
27
+ let index = 0;
28
+ while (true) {
29
+ const item = items[index];
30
+ if (!item) break;
31
+ const lastGroup = groupedItems[groupedItems.length - 1];
32
+ if (item.aborted) {
33
+ item.reject?.(new Error("Aborted"));
34
+ index++;
35
+ continue;
36
+ }
37
+ const isValid = batchLoader.validate(lastGroup.concat(item).map((it) => it.key));
38
+ if (isValid) {
39
+ lastGroup.push(item);
40
+ index++;
41
+ continue;
42
+ }
43
+ if (lastGroup.length === 0) {
44
+ item.reject?.(new Error("Input is too big for a single dispatch"));
45
+ index++;
46
+ continue;
47
+ }
48
+ groupedItems.push([]);
49
+ }
50
+ return groupedItems;
51
+ }
52
+ function dispatch() {
53
+ const groupedItems = groupItems(pendingItems);
54
+ destroyTimerAndPendingItems();
55
+ for (const items of groupedItems) {
56
+ if (!items.length) continue;
57
+ const batch = { items };
58
+ for (const item of items) item.batch = batch;
59
+ const promise = batchLoader.fetch(batch.items.map((_item) => _item.key));
60
+ promise.then(async (result) => {
61
+ await Promise.all(result.map(async (valueOrPromise, index) => {
62
+ const item = batch.items[index];
63
+ try {
64
+ const value = await Promise.resolve(valueOrPromise);
65
+ item.resolve?.(value);
66
+ } catch (cause) {
67
+ item.reject?.(cause);
68
+ }
69
+ item.batch = null;
70
+ item.reject = null;
71
+ item.resolve = null;
72
+ }));
73
+ for (const item of batch.items) {
74
+ item.reject?.(new Error("Missing result"));
75
+ item.batch = null;
76
+ }
77
+ }).catch((cause) => {
78
+ for (const item of batch.items) {
79
+ item.reject?.(cause);
80
+ item.batch = null;
81
+ }
82
+ });
83
+ }
84
+ }
85
+ function load(key) {
86
+ const item = {
87
+ aborted: false,
88
+ key,
89
+ batch: null,
90
+ resolve: throwFatalError,
91
+ reject: throwFatalError
92
+ };
93
+ const promise = new Promise((resolve, reject) => {
94
+ item.reject = reject;
95
+ item.resolve = resolve;
96
+ pendingItems ??= [];
97
+ pendingItems.push(item);
98
+ });
99
+ dispatchTimer ??= setTimeout(dispatch);
100
+ return promise;
101
+ }
102
+ return { load };
115
103
  }
116
104
 
117
- exports.dataLoader = dataLoader;
105
+ //#endregion
106
+ exports.dataLoader = dataLoader;
@@ -1,115 +1,106 @@
1
- /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
2
- * A function that should never be called unless we messed something up.
3
- */ const throwFatalError = ()=>{
4
- throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
1
+ //#region src/internals/dataLoader.ts
2
+ /**
3
+ * A function that should never be called unless we messed something up.
4
+ */
5
+ const throwFatalError = () => {
6
+ throw new Error("Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new");
5
7
  };
6
8
  /**
7
- * Dataloader that's very inspired by https://github.com/graphql/dataloader
8
- * Less configuration, no caching, and allows you to cancel requests
9
- * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
10
- */ function dataLoader(batchLoader) {
11
- let pendingItems = null;
12
- let dispatchTimer = null;
13
- const destroyTimerAndPendingItems = ()=>{
14
- clearTimeout(dispatchTimer);
15
- dispatchTimer = null;
16
- pendingItems = null;
17
- };
18
- /**
19
- * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
20
- */ function groupItems(items) {
21
- const groupedItems = [
22
- []
23
- ];
24
- let index = 0;
25
- while(true){
26
- const item = items[index];
27
- if (!item) {
28
- break;
29
- }
30
- const lastGroup = groupedItems[groupedItems.length - 1];
31
- if (item.aborted) {
32
- // Item was aborted before it was dispatched
33
- item.reject?.(new Error('Aborted'));
34
- index++;
35
- continue;
36
- }
37
- const isValid = batchLoader.validate(lastGroup.concat(item).map((it)=>it.key));
38
- if (isValid) {
39
- lastGroup.push(item);
40
- index++;
41
- continue;
42
- }
43
- if (lastGroup.length === 0) {
44
- item.reject?.(new Error('Input is too big for a single dispatch'));
45
- index++;
46
- continue;
47
- }
48
- // Create new group, next iteration will try to add the item to that
49
- groupedItems.push([]);
50
- }
51
- return groupedItems;
52
- }
53
- function dispatch() {
54
- const groupedItems = groupItems(pendingItems);
55
- destroyTimerAndPendingItems();
56
- // Create batches for each group of items
57
- for (const items of groupedItems){
58
- if (!items.length) {
59
- continue;
60
- }
61
- const batch = {
62
- items
63
- };
64
- for (const item of items){
65
- item.batch = batch;
66
- }
67
- const promise = batchLoader.fetch(batch.items.map((_item)=>_item.key));
68
- promise.then(async (result)=>{
69
- await Promise.all(result.map(async (valueOrPromise, index)=>{
70
- const item = batch.items[index];
71
- try {
72
- const value = await Promise.resolve(valueOrPromise);
73
- item.resolve?.(value);
74
- } catch (cause) {
75
- item.reject?.(cause);
76
- }
77
- item.batch = null;
78
- item.reject = null;
79
- item.resolve = null;
80
- }));
81
- for (const item of batch.items){
82
- item.reject?.(new Error('Missing result'));
83
- item.batch = null;
84
- }
85
- }).catch((cause)=>{
86
- for (const item of batch.items){
87
- item.reject?.(cause);
88
- item.batch = null;
89
- }
90
- });
91
- }
92
- }
93
- function load(key) {
94
- const item = {
95
- aborted: false,
96
- key,
97
- batch: null,
98
- resolve: throwFatalError,
99
- reject: throwFatalError
100
- };
101
- const promise = new Promise((resolve, reject)=>{
102
- item.reject = reject;
103
- item.resolve = resolve;
104
- pendingItems ?? (pendingItems = []);
105
- pendingItems.push(item);
106
- });
107
- dispatchTimer ?? (dispatchTimer = setTimeout(dispatch));
108
- return promise;
109
- }
110
- return {
111
- load
112
- };
9
+ * Dataloader that's very inspired by https://github.com/graphql/dataloader
10
+ * Less configuration, no caching, and allows you to cancel requests
11
+ * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
12
+ */
13
+ function dataLoader(batchLoader) {
14
+ let pendingItems = null;
15
+ let dispatchTimer = null;
16
+ const destroyTimerAndPendingItems = () => {
17
+ clearTimeout(dispatchTimer);
18
+ dispatchTimer = null;
19
+ pendingItems = null;
20
+ };
21
+ /**
22
+ * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
23
+ */
24
+ function groupItems(items) {
25
+ const groupedItems = [[]];
26
+ let index = 0;
27
+ while (true) {
28
+ const item = items[index];
29
+ if (!item) break;
30
+ const lastGroup = groupedItems[groupedItems.length - 1];
31
+ if (item.aborted) {
32
+ item.reject?.(new Error("Aborted"));
33
+ index++;
34
+ continue;
35
+ }
36
+ const isValid = batchLoader.validate(lastGroup.concat(item).map((it) => it.key));
37
+ if (isValid) {
38
+ lastGroup.push(item);
39
+ index++;
40
+ continue;
41
+ }
42
+ if (lastGroup.length === 0) {
43
+ item.reject?.(new Error("Input is too big for a single dispatch"));
44
+ index++;
45
+ continue;
46
+ }
47
+ groupedItems.push([]);
48
+ }
49
+ return groupedItems;
50
+ }
51
+ function dispatch() {
52
+ const groupedItems = groupItems(pendingItems);
53
+ destroyTimerAndPendingItems();
54
+ for (const items of groupedItems) {
55
+ if (!items.length) continue;
56
+ const batch = { items };
57
+ for (const item of items) item.batch = batch;
58
+ const promise = batchLoader.fetch(batch.items.map((_item) => _item.key));
59
+ promise.then(async (result) => {
60
+ await Promise.all(result.map(async (valueOrPromise, index) => {
61
+ const item = batch.items[index];
62
+ try {
63
+ const value = await Promise.resolve(valueOrPromise);
64
+ item.resolve?.(value);
65
+ } catch (cause) {
66
+ item.reject?.(cause);
67
+ }
68
+ item.batch = null;
69
+ item.reject = null;
70
+ item.resolve = null;
71
+ }));
72
+ for (const item of batch.items) {
73
+ item.reject?.(new Error("Missing result"));
74
+ item.batch = null;
75
+ }
76
+ }).catch((cause) => {
77
+ for (const item of batch.items) {
78
+ item.reject?.(cause);
79
+ item.batch = null;
80
+ }
81
+ });
82
+ }
83
+ }
84
+ function load(key) {
85
+ const item = {
86
+ aborted: false,
87
+ key,
88
+ batch: null,
89
+ resolve: throwFatalError,
90
+ reject: throwFatalError
91
+ };
92
+ const promise = new Promise((resolve, reject) => {
93
+ item.reject = reject;
94
+ item.resolve = resolve;
95
+ pendingItems ??= [];
96
+ pendingItems.push(item);
97
+ });
98
+ dispatchTimer ??= setTimeout(dispatch);
99
+ return promise;
100
+ }
101
+ return { load };
113
102
  }
114
103
 
104
+ //#endregion
115
105
  export { dataLoader };
106
+ //# sourceMappingURL=dataLoader.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dataLoader.mjs","names":["batchLoader: BatchLoader<TKey, TValue>","pendingItems: BatchItem<TKey, TValue>[] | null","dispatchTimer: ReturnType<typeof setTimeout> | null","items: BatchItem<TKey, TValue>[]","groupedItems: BatchItem<TKey, TValue>[][]","batch: Batch<TKey, TValue>","key: TKey","item: BatchItem<TKey, TValue>"],"sources":["../../src/internals/dataLoader.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-non-null-assertion */\n\ntype BatchItem<TKey, TValue> = {\n aborted: boolean;\n key: TKey;\n resolve: ((value: TValue) => void) | null;\n reject: ((error: Error) => void) | null;\n batch: Batch<TKey, TValue> | null;\n};\ntype Batch<TKey, TValue> = {\n items: BatchItem<TKey, TValue>[];\n};\nexport type BatchLoader<TKey, TValue> = {\n validate: (keys: TKey[]) => boolean;\n fetch: (keys: TKey[]) => Promise<TValue[] | Promise<TValue>[]>;\n};\n\n/**\n * A function that should never be called unless we messed something up.\n */\nconst throwFatalError = () => {\n throw new Error(\n 'Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new',\n );\n};\n\n/**\n * Dataloader that's very inspired by https://github.com/graphql/dataloader\n * Less configuration, no caching, and allows you to cancel requests\n * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled\n */\nexport function dataLoader<TKey, TValue>(\n batchLoader: BatchLoader<TKey, TValue>,\n) {\n let pendingItems: BatchItem<TKey, TValue>[] | null = null;\n let dispatchTimer: ReturnType<typeof setTimeout> | null = null;\n\n const destroyTimerAndPendingItems = () => {\n clearTimeout(dispatchTimer as any);\n dispatchTimer = null;\n pendingItems = null;\n };\n\n /**\n * Iterate through the items and split them into groups based on the `batchLoader`'s validate function\n */\n function groupItems(items: BatchItem<TKey, TValue>[]) {\n const groupedItems: BatchItem<TKey, TValue>[][] = [[]];\n let index = 0;\n while (true) {\n const item = items[index];\n if (!item) {\n // we're done\n break;\n }\n const lastGroup = groupedItems[groupedItems.length - 1]!;\n\n if (item.aborted) {\n // Item was aborted before it was dispatched\n item.reject?.(new Error('Aborted'));\n index++;\n continue;\n }\n\n const isValid = batchLoader.validate(\n lastGroup.concat(item).map((it) => it.key),\n );\n\n if (isValid) {\n lastGroup.push(item);\n index++;\n continue;\n }\n\n if (lastGroup.length === 0) {\n item.reject?.(new Error('Input is too big for a single dispatch'));\n index++;\n continue;\n }\n // Create new group, next iteration will try to add the item to that\n groupedItems.push([]);\n }\n return groupedItems;\n }\n\n function dispatch() {\n const groupedItems = groupItems(pendingItems!);\n destroyTimerAndPendingItems();\n\n // Create batches for each group of items\n for (const items of groupedItems) {\n if (!items.length) {\n continue;\n }\n const batch: Batch<TKey, TValue> = {\n items,\n };\n for (const item of items) {\n item.batch = batch;\n }\n const promise = batchLoader.fetch(batch.items.map((_item) => _item.key));\n\n promise\n .then(async (result) => {\n await Promise.all(\n result.map(async (valueOrPromise, index) => {\n const item = batch.items[index]!;\n try {\n const value = await Promise.resolve(valueOrPromise);\n\n item.resolve?.(value);\n } catch (cause) {\n item.reject?.(cause as Error);\n }\n\n item.batch = null;\n item.reject = null;\n item.resolve = null;\n }),\n );\n\n for (const item of batch.items) {\n item.reject?.(new Error('Missing result'));\n item.batch = null;\n }\n })\n .catch((cause) => {\n for (const item of batch.items) {\n item.reject?.(cause);\n item.batch = null;\n }\n });\n }\n }\n function load(key: TKey): Promise<TValue> {\n const item: BatchItem<TKey, TValue> = {\n aborted: false,\n key,\n batch: null,\n resolve: throwFatalError,\n reject: throwFatalError,\n };\n\n const promise = new Promise<TValue>((resolve, reject) => {\n item.reject = reject;\n item.resolve = resolve;\n\n pendingItems ??= [];\n pendingItems.push(item);\n });\n\n dispatchTimer ??= setTimeout(dispatch);\n\n return promise;\n }\n\n return {\n load,\n };\n}\n"],"mappings":";;;;AAoBA,MAAM,kBAAkB,MAAM;AAC5B,OAAM,IAAI,MACR;AAEH;;;;;;AAOD,SAAgB,WACdA,aACA;CACA,IAAIC,eAAiD;CACrD,IAAIC,gBAAsD;CAE1D,MAAM,8BAA8B,MAAM;AACxC,eAAa,cAAqB;AAClC,kBAAgB;AAChB,iBAAe;CAChB;;;;CAKD,SAAS,WAAWC,OAAkC;EACpD,MAAMC,eAA4C,CAAC,CAAE,CAAC;EACtD,IAAI,QAAQ;AACZ,SAAO,MAAM;GACX,MAAM,OAAO,MAAM;AACnB,QAAK,KAEH;GAEF,MAAM,YAAY,aAAa,aAAa,SAAS;AAErD,OAAI,KAAK,SAAS;AAEhB,SAAK,SAAS,IAAI,MAAM,WAAW;AACnC;AACA;GACD;GAED,MAAM,UAAU,YAAY,SAC1B,UAAU,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,GAAG,IAAI,CAC3C;AAED,OAAI,SAAS;AACX,cAAU,KAAK,KAAK;AACpB;AACA;GACD;AAED,OAAI,UAAU,WAAW,GAAG;AAC1B,SAAK,SAAS,IAAI,MAAM,0CAA0C;AAClE;AACA;GACD;AAED,gBAAa,KAAK,CAAE,EAAC;EACtB;AACD,SAAO;CACR;CAED,SAAS,WAAW;EAClB,MAAM,eAAe,WAAW,aAAc;AAC9C,+BAA6B;AAG7B,OAAK,MAAM,SAAS,cAAc;AAChC,QAAK,MAAM,OACT;GAEF,MAAMC,QAA6B,EACjC,MACD;AACD,QAAK,MAAM,QAAQ,MACjB,MAAK,QAAQ;GAEf,MAAM,UAAU,YAAY,MAAM,MAAM,MAAM,IAAI,CAAC,UAAU,MAAM,IAAI,CAAC;AAExE,WACG,KAAK,OAAO,WAAW;AACtB,UAAM,QAAQ,IACZ,OAAO,IAAI,OAAO,gBAAgB,UAAU;KAC1C,MAAM,OAAO,MAAM,MAAM;AACzB,SAAI;MACF,MAAM,QAAQ,MAAM,QAAQ,QAAQ,eAAe;AAEnD,WAAK,UAAU,MAAM;KACtB,SAAQ,OAAO;AACd,WAAK,SAAS,MAAe;KAC9B;AAED,UAAK,QAAQ;AACb,UAAK,SAAS;AACd,UAAK,UAAU;IAChB,EAAC,CACH;AAED,SAAK,MAAM,QAAQ,MAAM,OAAO;AAC9B,UAAK,SAAS,IAAI,MAAM,kBAAkB;AAC1C,UAAK,QAAQ;IACd;GACF,EAAC,CACD,MAAM,CAAC,UAAU;AAChB,SAAK,MAAM,QAAQ,MAAM,OAAO;AAC9B,UAAK,SAAS,MAAM;AACpB,UAAK,QAAQ;IACd;GACF,EAAC;EACL;CACF;CACD,SAAS,KAAKC,KAA4B;EACxC,MAAMC,OAAgC;GACpC,SAAS;GACT;GACA,OAAO;GACP,SAAS;GACT,QAAQ;EACT;EAED,MAAM,UAAU,IAAI,QAAgB,CAAC,SAAS,WAAW;AACvD,QAAK,SAAS;AACd,QAAK,UAAU;AAEf,oBAAiB,CAAE;AACnB,gBAAa,KAAK,KAAK;EACxB;AAED,oBAAkB,WAAW,SAAS;AAEtC,SAAO;CACR;AAED,QAAO,EACL,KACD;AACF"}
@@ -1,16 +1,13 @@
1
- 'use strict';
2
1
 
2
+ //#region src/internals/inputWithTrackedEventId.ts
3
3
  function inputWithTrackedEventId(input, lastEventId) {
4
- if (!lastEventId) {
5
- return input;
6
- }
7
- if (input != null && typeof input !== 'object') {
8
- return input;
9
- }
10
- return {
11
- ...input ?? {},
12
- lastEventId
13
- };
4
+ if (!lastEventId) return input;
5
+ if (input != null && typeof input !== "object") return input;
6
+ return {
7
+ ...input ?? {},
8
+ lastEventId
9
+ };
14
10
  }
15
11
 
16
- exports.inputWithTrackedEventId = inputWithTrackedEventId;
12
+ //#endregion
13
+ exports.inputWithTrackedEventId = inputWithTrackedEventId;
@@ -1,14 +1,13 @@
1
+ //#region src/internals/inputWithTrackedEventId.ts
1
2
  function inputWithTrackedEventId(input, lastEventId) {
2
- if (!lastEventId) {
3
- return input;
4
- }
5
- if (input != null && typeof input !== 'object') {
6
- return input;
7
- }
8
- return {
9
- ...input ?? {},
10
- lastEventId
11
- };
3
+ if (!lastEventId) return input;
4
+ if (input != null && typeof input !== "object") return input;
5
+ return {
6
+ ...input ?? {},
7
+ lastEventId
8
+ };
12
9
  }
13
10
 
11
+ //#endregion
14
12
  export { inputWithTrackedEventId };
13
+ //# sourceMappingURL=inputWithTrackedEventId.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inputWithTrackedEventId.mjs","names":["input: unknown","lastEventId: string | undefined"],"sources":["../../src/internals/inputWithTrackedEventId.ts"],"sourcesContent":["export function inputWithTrackedEventId(\n input: unknown,\n lastEventId: string | undefined,\n) {\n if (!lastEventId) {\n return input;\n }\n if (input != null && typeof input !== 'object') {\n return input;\n }\n return {\n ...(input ?? {}),\n lastEventId,\n };\n}\n"],"mappings":";AAAA,SAAgB,wBACdA,OACAC,aACA;AACA,MAAK,YACH,QAAO;AAET,KAAI,SAAS,eAAe,UAAU,SACpC,QAAO;AAET,QAAO;EACL,GAAI,SAAS,CAAE;EACf;CACD;AACF"}
@@ -1,47 +1,46 @@
1
- 'use strict';
2
1
 
2
+ //#region src/internals/signals.ts
3
3
  /**
4
- * Like `Promise.all()` but for abort signals
5
- * - When all signals have been aborted, the merged signal will be aborted
6
- * - If one signal is `null`, no signal will be aborted
7
- */ function allAbortSignals(...signals) {
8
- const ac = new AbortController();
9
- const count = signals.length;
10
- let abortedCount = 0;
11
- const onAbort = ()=>{
12
- if (++abortedCount === count) {
13
- ac.abort();
14
- }
15
- };
16
- for (const signal of signals){
17
- if (signal?.aborted) {
18
- onAbort();
19
- } else {
20
- signal?.addEventListener('abort', onAbort, {
21
- once: true
22
- });
23
- }
24
- }
25
- return ac.signal;
4
+ * Like `Promise.all()` but for abort signals
5
+ * - When all signals have been aborted, the merged signal will be aborted
6
+ * - If one signal is `null`, no signal will be aborted
7
+ */
8
+ function allAbortSignals(...signals) {
9
+ const ac = new AbortController();
10
+ const count = signals.length;
11
+ let abortedCount = 0;
12
+ const onAbort = () => {
13
+ if (++abortedCount === count) ac.abort();
14
+ };
15
+ for (const signal of signals) if (signal?.aborted) onAbort();
16
+ else signal?.addEventListener("abort", onAbort, { once: true });
17
+ return ac.signal;
26
18
  }
27
19
  /**
28
- * Like `Promise.race` but for abort signals
29
- *
30
- * Basically, a ponyfill for
31
- * [`AbortSignal.any`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static).
32
- */ function raceAbortSignals(...signals) {
33
- const ac = new AbortController();
34
- for (const signal of signals){
35
- if (signal?.aborted) {
36
- ac.abort();
37
- } else {
38
- signal?.addEventListener('abort', ()=>ac.abort(), {
39
- once: true
40
- });
41
- }
42
- }
43
- return ac.signal;
20
+ * Like `Promise.race` but for abort signals
21
+ *
22
+ * Basically, a ponyfill for
23
+ * [`AbortSignal.any`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static).
24
+ */
25
+ function raceAbortSignals(...signals) {
26
+ const ac = new AbortController();
27
+ for (const signal of signals) if (signal?.aborted) ac.abort();
28
+ else signal?.addEventListener("abort", () => ac.abort(), { once: true });
29
+ return ac.signal;
30
+ }
31
+ function abortSignalToPromise(signal) {
32
+ return new Promise((_, reject) => {
33
+ if (signal.aborted) {
34
+ reject(signal.reason);
35
+ return;
36
+ }
37
+ signal.addEventListener("abort", () => {
38
+ reject(signal.reason);
39
+ }, { once: true });
40
+ });
44
41
  }
45
42
 
43
+ //#endregion
44
+ exports.abortSignalToPromise = abortSignalToPromise;
46
45
  exports.allAbortSignals = allAbortSignals;
47
- exports.raceAbortSignals = raceAbortSignals;
46
+ exports.raceAbortSignals = raceAbortSignals;