@trpc/client 10.0.0-alpha.10 → 10.0.0-alpha.13

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