@trpc/client 10.0.0-alpha.4 → 10.0.0-alpha.42

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 (174) hide show
  1. package/README.md +58 -1
  2. package/dist/TRPCClientError.d.ts +3 -11
  3. package/dist/TRPCClientError.d.ts.map +1 -1
  4. package/dist/createTRPCClient.d.ts +2 -3
  5. package/dist/createTRPCClient.d.ts.map +1 -1
  6. package/dist/createTRPCClientProxy.d.ts +31 -0
  7. package/dist/createTRPCClientProxy.d.ts.map +1 -0
  8. package/dist/{httpUtils-c09d7b79.mjs → httpUtils-089853f6.mjs} +38 -30
  9. package/dist/{httpUtils-1f9e4e3c.js → httpUtils-a0169eef.js} +38 -29
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +256 -107
  13. package/dist/index.mjs +244 -99
  14. package/dist/internals/TRPCClient.d.ts +21 -18
  15. package/dist/internals/TRPCClient.d.ts.map +1 -1
  16. package/dist/internals/dataLoader.d.ts +7 -4
  17. package/dist/internals/dataLoader.d.ts.map +1 -1
  18. package/dist/links/dedupeLink.d.ts.map +1 -1
  19. package/dist/links/httpBatchLink.d.ts +5 -2
  20. package/dist/links/httpBatchLink.d.ts.map +1 -1
  21. package/dist/links/httpBatchLink.js +197 -69
  22. package/dist/links/httpBatchLink.mjs +197 -69
  23. package/dist/links/httpLink.d.ts +1 -1
  24. package/dist/links/httpLink.d.ts.map +1 -1
  25. package/dist/links/httpLink.js +5 -5
  26. package/dist/links/httpLink.mjs +5 -5
  27. package/dist/links/index.d.ts +9 -0
  28. package/dist/links/index.d.ts.map +1 -0
  29. package/dist/links/internals/createChain.d.ts.map +1 -1
  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 +17 -3
  33. package/dist/links/internals/transformOperationResult.d.ts.map +1 -1
  34. package/dist/links/loggerLink.d.ts.map +1 -1
  35. package/dist/links/loggerLink.js +2 -3
  36. package/dist/links/loggerLink.mjs +1 -2
  37. package/dist/links/retryLink.d.ts.map +1 -1
  38. package/dist/links/splitLink.d.ts +1 -1
  39. package/dist/links/splitLink.d.ts.map +1 -1
  40. package/dist/links/splitLink.js +3 -25
  41. package/dist/links/splitLink.mjs +2 -28
  42. package/dist/links/types.d.ts +12 -9
  43. package/dist/links/types.d.ts.map +1 -1
  44. package/dist/links/wsLink.d.ts +5 -3
  45. package/dist/links/wsLink.d.ts.map +1 -1
  46. package/dist/links/wsLink.js +12 -376
  47. package/dist/links/wsLink.mjs +10 -369
  48. package/dist/splitLink-0202cd7b.mjs +54 -0
  49. package/dist/splitLink-23c5ce5c.js +57 -0
  50. package/dist/wsLink-27b93bd0.mjs +451 -0
  51. package/dist/wsLink-44703a83.js +467 -0
  52. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +4 -4
  53. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +4 -4
  54. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +4 -4
  55. package/links/httpBatchLink/index.d.ts +1 -1
  56. package/links/httpBatchLink/index.js +1 -1
  57. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +1 -1
  58. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +1 -1
  59. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
  60. package/links/httpLink/index.d.ts +1 -1
  61. package/links/httpLink/index.js +1 -1
  62. package/links/loggerLink/index.d.ts +1 -1
  63. package/links/loggerLink/index.js +1 -1
  64. package/links/splitLink/index.d.ts +1 -1
  65. package/links/splitLink/index.js +1 -1
  66. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
  67. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
  68. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
  69. package/links/wsLink/index.d.ts +1 -1
  70. package/links/wsLink/index.js +1 -1
  71. package/package.json +23 -30
  72. package/src/TRPCClientError.ts +5 -13
  73. package/src/createTRPCClient.ts +7 -3
  74. package/src/createTRPCClientProxy.ts +98 -0
  75. package/src/index.ts +2 -1
  76. package/src/internals/TRPCClient.ts +70 -42
  77. package/src/internals/dataLoader.ts +120 -45
  78. package/src/links/dedupeLink.test.ts +1 -1
  79. package/src/links/dedupeLink.ts +6 -7
  80. package/src/links/httpBatchLink.ts +64 -31
  81. package/src/links/httpLink.ts +4 -4
  82. package/src/links/index.ts +8 -0
  83. package/src/links/internals/createChain.test.ts +5 -5
  84. package/src/links/internals/createChain.ts +8 -3
  85. package/src/links/internals/httpUtils.ts +116 -0
  86. package/src/links/internals/transformOperationResult.ts +40 -12
  87. package/src/links/loggerLink.ts +1 -2
  88. package/src/links/retryLink.ts +1 -2
  89. package/src/links/splitLink.test.ts +3 -3
  90. package/src/links/splitLink.ts +5 -5
  91. package/src/links/types.ts +14 -9
  92. package/src/links/wsLink.ts +31 -16
  93. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  94. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  95. package/dist/createChain-389cc116.mjs +0 -25
  96. package/dist/createChain-864dbb30.js +0 -27
  97. package/dist/internals/transformRPCResponse.d.ts +0 -15
  98. package/dist/internals/transformRPCResponse.d.ts.map +0 -1
  99. package/dist/links/httpUtils.d.ts +0 -22
  100. package/dist/links/httpUtils.d.ts.map +0 -1
  101. package/dist/links/transformerLink.d.ts +0 -4
  102. package/dist/links/transformerLink.d.ts.map +0 -1
  103. package/dist/links/transformerLink.js +0 -75
  104. package/dist/links/transformerLink.mjs +0 -67
  105. package/dist/links/transformerLink.ts.js +0 -1
  106. package/dist/observable-7e66cb56.mjs +0 -102
  107. package/dist/observable-85669100.js +0 -104
  108. package/dist/rx/index.d.ts +0 -3
  109. package/dist/rx/index.d.ts.map +0 -1
  110. package/dist/rx/index.js +0 -9
  111. package/dist/rx/index.mjs +0 -1
  112. package/dist/rx/index.ts.js +0 -1
  113. package/dist/rx/observable.d.ts +0 -4
  114. package/dist/rx/observable.d.ts.map +0 -1
  115. package/dist/rx/operators/index.d.ts +0 -4
  116. package/dist/rx/operators/index.d.ts.map +0 -1
  117. package/dist/rx/operators/index.js +0 -38
  118. package/dist/rx/operators/index.mjs +0 -32
  119. package/dist/rx/operators/index.ts.js +0 -1
  120. package/dist/rx/operators/map.d.ts +0 -3
  121. package/dist/rx/operators/map.d.ts.map +0 -1
  122. package/dist/rx/operators/share.d.ts +0 -6
  123. package/dist/rx/operators/share.d.ts.map +0 -1
  124. package/dist/rx/operators/tap.d.ts +0 -3
  125. package/dist/rx/operators/tap.d.ts.map +0 -1
  126. package/dist/rx/types.d.ts +0 -29
  127. package/dist/rx/types.d.ts.map +0 -1
  128. package/dist/rx/util/identity.d.ts +0 -2
  129. package/dist/rx/util/identity.d.ts.map +0 -1
  130. package/dist/rx/util/observableToPromise.d.ts +0 -10
  131. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  132. package/dist/rx/util/pipe.d.ts +0 -4
  133. package/dist/rx/util/pipe.d.ts.map +0 -1
  134. package/dist/share-4d79dc49.js +0 -110
  135. package/dist/share-671b63fa.mjs +0 -108
  136. package/dist/tap-538fa07f.mjs +0 -30
  137. package/dist/tap-799a2d33.js +0 -32
  138. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  139. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  140. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  141. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  142. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  143. package/links/transformerLink/index.d.ts +0 -1
  144. package/links/transformerLink/index.js +0 -1
  145. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  146. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  147. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  148. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  149. package/rx/dist/trpc-client-rx.esm.js +0 -1
  150. package/rx/index.d.ts +0 -1
  151. package/rx/index.js +0 -1
  152. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  153. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  154. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  155. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  156. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  157. package/rx/operators/index.d.ts +0 -1
  158. package/rx/operators/index.js +0 -1
  159. package/src/internals/transformRPCResponse.ts +0 -25
  160. package/src/links/httpUtils.ts +0 -94
  161. package/src/links/transformerLink.ts +0 -70
  162. package/src/rx/index.ts +0 -2
  163. package/src/rx/observable.test.ts +0 -83
  164. package/src/rx/observable.ts +0 -70
  165. package/src/rx/operators/index.ts +0 -3
  166. package/src/rx/operators/map.test.ts +0 -92
  167. package/src/rx/operators/map.ts +0 -25
  168. package/src/rx/operators/share.test.ts +0 -60
  169. package/src/rx/operators/share.ts +0 -67
  170. package/src/rx/operators/tap.ts +0 -26
  171. package/src/rx/types.ts +0 -69
  172. package/src/rx/util/identity.ts +0 -3
  173. package/src/rx/util/observableToPromise.ts +0 -49
  174. package/src/rx/util/pipe.ts +0 -22
@@ -0,0 +1,451 @@
1
+ import _createClass from '@babel/runtime/helpers/createClass';
2
+ import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
3
+ import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
4
+ import _inherits from '@babel/runtime/helpers/inherits';
5
+ import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
6
+ import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
7
+ import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
8
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
9
+ import { observable } from '@trpc/server/observable';
10
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
11
+
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
+
16
+ function _createSuper$1(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$1(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
17
+
18
+ function _isNativeReflectConstruct$1() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
19
+
20
+ var TRPCClientError = /*#__PURE__*/function (_Error) {
21
+ _inherits(TRPCClientError, _Error);
22
+
23
+ var _super = /*#__PURE__*/_createSuper$1(TRPCClientError);
24
+
25
+ function TRPCClientError(message, opts) {
26
+ var _opts$result, _opts$result2;
27
+
28
+ var _this;
29
+
30
+ _classCallCheck(this, TRPCClientError);
31
+
32
+ var cause = opts === null || opts === void 0 ? void 0 : opts.cause; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
+ // @ts-ignore https://github.com/tc39/proposal-error-cause
34
+
35
+ _this = _super.call(this, message, {
36
+ cause: cause
37
+ });
38
+ _this.cause = void 0;
39
+ _this.shape = void 0;
40
+ _this.data = void 0;
41
+ _this.meta = void 0;
42
+ _this.meta = opts === null || opts === void 0 ? void 0 : opts.meta;
43
+ _this.cause = cause;
44
+ _this.shape = opts === null || opts === void 0 ? void 0 : (_opts$result = opts.result) === null || _opts$result === void 0 ? void 0 : _opts$result.error;
45
+ _this.data = opts === null || opts === void 0 ? void 0 : (_opts$result2 = opts.result) === null || _opts$result2 === void 0 ? void 0 : _opts$result2.error.data;
46
+ _this.name = 'TRPCClientError';
47
+ Object.setPrototypeOf(_assertThisInitialized(_this), TRPCClientError.prototype);
48
+ return _this;
49
+ }
50
+
51
+ _createClass(TRPCClientError, null, [{
52
+ key: "from",
53
+ value: function from(cause) {
54
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
55
+
56
+ if (!(cause instanceof Error)) {
57
+ var _message;
58
+
59
+ return new TRPCClientError((_message = cause.error.message) !== null && _message !== void 0 ? _message : '', _objectSpread(_objectSpread({}, opts), {}, {
60
+ cause: undefined,
61
+ result: cause
62
+ }));
63
+ }
64
+
65
+ if (cause.name === 'TRPCClientError') {
66
+ return cause;
67
+ }
68
+
69
+ return new TRPCClientError(cause.message, _objectSpread(_objectSpread({}, opts), {}, {
70
+ cause: cause,
71
+ result: null
72
+ }));
73
+ }
74
+ }]);
75
+
76
+ return TRPCClientError;
77
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
78
+
79
+ /* istanbul ignore next */
80
+ var retryDelay = function retryDelay(attemptIndex) {
81
+ return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
82
+ };
83
+
84
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
85
+
86
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
87
+ function createWSClient(opts) {
88
+ var url = opts.url,
89
+ _opts$WebSocket = opts.WebSocket,
90
+ WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
91
+ _opts$retryDelayMs = opts.retryDelayMs,
92
+ retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs,
93
+ onOpen = opts.onOpen,
94
+ onClose = opts.onClose;
95
+ /* istanbul ignore next */
96
+
97
+ if (!WebSocketImpl) {
98
+ throw new Error("No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill");
99
+ }
100
+ /**
101
+ * outgoing messages buffer whilst not open
102
+ */
103
+
104
+
105
+ var outgoing = [];
106
+ /**
107
+ * pending outgoing requests that are awaiting callback
108
+ */
109
+
110
+ var pendingRequests = Object.create(null);
111
+ var connectAttempt = 0;
112
+ var dispatchTimer = null;
113
+ var connectTimer = null;
114
+ var activeConnection = createWS();
115
+ var state = 'connecting';
116
+ /**
117
+ * tries to send the list of messages
118
+ */
119
+
120
+ function dispatch() {
121
+ if (state !== 'open' || dispatchTimer) {
122
+ return;
123
+ }
124
+
125
+ dispatchTimer = setTimeout(function () {
126
+ dispatchTimer = null;
127
+
128
+ if (outgoing.length === 1) {
129
+ // single send
130
+ activeConnection.send(JSON.stringify(outgoing.pop()));
131
+ } else {
132
+ // batch send
133
+ activeConnection.send(JSON.stringify(outgoing));
134
+ } // clear
135
+
136
+
137
+ outgoing = [];
138
+ });
139
+ }
140
+
141
+ function tryReconnect() {
142
+ if (connectTimer || state === 'closed') {
143
+ return;
144
+ }
145
+
146
+ var timeout = retryDelayFn(connectAttempt++);
147
+ reconnectInMs(timeout);
148
+ }
149
+
150
+ function reconnect() {
151
+ state = 'connecting';
152
+ var oldConnection = activeConnection;
153
+ activeConnection = createWS();
154
+ closeIfNoPending(oldConnection);
155
+ }
156
+
157
+ function reconnectInMs(ms) {
158
+ if (connectTimer) {
159
+ return;
160
+ }
161
+
162
+ state = 'connecting';
163
+ connectTimer = setTimeout(reconnect, ms);
164
+ }
165
+
166
+ function closeIfNoPending(conn) {
167
+ // disconnect as soon as there are are no pending result
168
+ var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
169
+ return p.ws === conn;
170
+ });
171
+
172
+ if (!hasPendingRequests) {
173
+ conn.close();
174
+ }
175
+ }
176
+
177
+ function resumeSubscriptionOnReconnect(req) {
178
+ if (outgoing.some(function (r) {
179
+ return r.id === req.op.id;
180
+ })) {
181
+ return;
182
+ }
183
+
184
+ request(req.op, req.callbacks);
185
+ }
186
+
187
+ function createWS() {
188
+ var conn = new WebSocketImpl(url);
189
+ clearTimeout(connectTimer);
190
+ connectTimer = null;
191
+ conn.addEventListener('open', function () {
192
+ /* istanbul ignore next */
193
+ if (conn !== activeConnection) {
194
+ return;
195
+ }
196
+
197
+ connectAttempt = 0;
198
+ state = 'open';
199
+ onOpen === null || onOpen === void 0 ? void 0 : onOpen();
200
+ dispatch();
201
+ });
202
+ conn.addEventListener('error', function () {
203
+ if (conn === activeConnection) {
204
+ tryReconnect();
205
+ }
206
+ });
207
+
208
+ var handleIncomingRequest = function handleIncomingRequest(req) {
209
+ if (req.method === 'reconnect' && conn === activeConnection) {
210
+ if (state === 'open') {
211
+ onClose === null || onClose === void 0 ? void 0 : onClose();
212
+ }
213
+
214
+ reconnect(); // notify subscribers
215
+
216
+ for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
217
+ var pendingReq = _Object$values[_i];
218
+
219
+ if (pendingReq.type === 'subscription') {
220
+ resumeSubscriptionOnReconnect(pendingReq);
221
+ }
222
+ }
223
+ }
224
+ };
225
+
226
+ var handleIncomingResponse = function handleIncomingResponse(data) {
227
+ var _req$callbacks$next, _req$callbacks;
228
+
229
+ var req = data.id !== null && pendingRequests[data.id];
230
+
231
+ if (!req) {
232
+ // do something?
233
+ return;
234
+ }
235
+
236
+ (_req$callbacks$next = (_req$callbacks = req.callbacks).next) === null || _req$callbacks$next === void 0 ? void 0 : _req$callbacks$next.call(_req$callbacks, {
237
+ data: data
238
+ });
239
+
240
+ if (req.ws !== activeConnection && conn === activeConnection) {
241
+ var oldWs = req.ws; // gracefully replace old connection with this
242
+
243
+ req.ws = activeConnection;
244
+ closeIfNoPending(oldWs);
245
+ }
246
+
247
+ if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
248
+ req.callbacks.complete();
249
+ }
250
+ };
251
+
252
+ conn.addEventListener('message', function (_ref) {
253
+ var data = _ref.data;
254
+ var msg = JSON.parse(data);
255
+
256
+ if ('method' in msg) {
257
+ handleIncomingRequest(msg);
258
+ } else {
259
+ handleIncomingResponse(msg);
260
+ }
261
+
262
+ if (conn !== activeConnection || state === 'closed') {
263
+ // when receiving a message, we close old connection that has no pending requests
264
+ closeIfNoPending(conn);
265
+ }
266
+ });
267
+ conn.addEventListener('close', function () {
268
+ if (state === 'open') {
269
+ onClose === null || onClose === void 0 ? void 0 : onClose();
270
+ }
271
+
272
+ if (activeConnection === conn) {
273
+ // connection might have been replaced already
274
+ tryReconnect();
275
+ }
276
+
277
+ for (var _i2 = 0, _Object$entries = Object.entries(pendingRequests); _i2 < _Object$entries.length; _i2++) {
278
+ var _req$callbacks$error, _req$callbacks2;
279
+
280
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
281
+ key = _Object$entries$_i[0],
282
+ req = _Object$entries$_i[1];
283
+
284
+ if (req.ws !== conn) {
285
+ continue;
286
+ }
287
+
288
+ (_req$callbacks$error = (_req$callbacks2 = req.callbacks).error) === null || _req$callbacks$error === void 0 ? void 0 : _req$callbacks$error.call(_req$callbacks2, TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
289
+
290
+ if (req.type !== 'subscription') {
291
+ var _req$callbacks$comple, _req$callbacks3;
292
+
293
+ delete pendingRequests[key];
294
+ (_req$callbacks$comple = (_req$callbacks3 = req.callbacks).complete) === null || _req$callbacks$comple === void 0 ? void 0 : _req$callbacks$comple.call(_req$callbacks3);
295
+ } else if (state !== 'closed') {
296
+ // request restart of sub with next connection
297
+ resumeSubscriptionOnReconnect(req);
298
+ }
299
+ }
300
+ });
301
+ return conn;
302
+ }
303
+
304
+ function request(op, callbacks) {
305
+ var type = op.type,
306
+ input = op.input,
307
+ path = op.path,
308
+ id = op.id;
309
+ var envelope = {
310
+ id: id,
311
+ method: type,
312
+ params: {
313
+ input: input,
314
+ path: path
315
+ }
316
+ };
317
+ pendingRequests[id] = {
318
+ ws: activeConnection,
319
+ type: type,
320
+ callbacks: callbacks,
321
+ op: op
322
+ }; // enqueue message
323
+
324
+ outgoing.push(envelope);
325
+ dispatch();
326
+ return function () {
327
+ var _pendingRequests$id, _callbacks$complete;
328
+
329
+ var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
330
+ delete pendingRequests[id];
331
+ outgoing = outgoing.filter(function (msg) {
332
+ return msg.id !== id;
333
+ });
334
+ callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$complete = callbacks.complete) === null || _callbacks$complete === void 0 ? void 0 : _callbacks$complete.call(callbacks);
335
+
336
+ if (op.type === 'subscription') {
337
+ outgoing.push({
338
+ id: id,
339
+ method: 'subscription.stop'
340
+ });
341
+ dispatch();
342
+ }
343
+ };
344
+ }
345
+
346
+ return {
347
+ close: function close() {
348
+ state = 'closed';
349
+ onClose === null || onClose === void 0 ? void 0 : onClose();
350
+ closeIfNoPending(activeConnection);
351
+ clearTimeout(connectTimer);
352
+ connectTimer = null;
353
+ },
354
+ request: request,
355
+ getConnection: function getConnection() {
356
+ return activeConnection;
357
+ }
358
+ };
359
+ }
360
+
361
+ var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
362
+ _inherits(TRPCWebSocketClosedError, _Error);
363
+
364
+ var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
365
+
366
+ function TRPCWebSocketClosedError(message) {
367
+ var _this;
368
+
369
+ _classCallCheck(this, TRPCWebSocketClosedError);
370
+
371
+ _this = _super.call(this, message);
372
+ _this.name = 'TRPCWebSocketClosedError';
373
+ Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
374
+ return _this;
375
+ }
376
+
377
+ return _createClass(TRPCWebSocketClosedError);
378
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
379
+
380
+ var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
381
+ _inherits(TRPCSubscriptionEndedError, _Error2);
382
+
383
+ var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
384
+
385
+ function TRPCSubscriptionEndedError(message) {
386
+ var _this2;
387
+
388
+ _classCallCheck(this, TRPCSubscriptionEndedError);
389
+
390
+ _this2 = _super2.call(this, message);
391
+ _this2.name = 'TRPCSubscriptionEndedError';
392
+ Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
393
+ return _this2;
394
+ }
395
+
396
+ return _createClass(TRPCSubscriptionEndedError);
397
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
398
+
399
+ function wsLink(opts) {
400
+ return function (runtime) {
401
+ var client = opts.client;
402
+ return function (_ref2) {
403
+ var op = _ref2.op;
404
+ return observable(function (observer) {
405
+ var type = op.type,
406
+ path = op.path,
407
+ id = op.id,
408
+ context = op.context;
409
+ var input = runtime.transformer.serialize(op.input);
410
+ var isDone = false;
411
+ var unsub = client.request({
412
+ type: type,
413
+ path: path,
414
+ input: input,
415
+ id: id,
416
+ context: context
417
+ }, {
418
+ error: function error(err) {
419
+ isDone = true;
420
+ observer.error(err);
421
+ unsub();
422
+ },
423
+ complete: function complete() {
424
+ if (!isDone) {
425
+ isDone = true;
426
+ observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
427
+ } else {
428
+ observer.complete();
429
+ }
430
+ },
431
+ next: function next(result) {
432
+ observer.next(result);
433
+
434
+ if (op.type !== 'subscription') {
435
+ // if it isn't a subscription we don't care about next response
436
+ isDone = true;
437
+ unsub();
438
+ observer.complete();
439
+ }
440
+ }
441
+ });
442
+ return function () {
443
+ isDone = true;
444
+ unsub();
445
+ };
446
+ });
447
+ };
448
+ };
449
+ }
450
+
451
+ export { TRPCClientError as T, createWSClient as c, wsLink as w };