@trpc/client 10.0.0-alpha.45 → 10.0.0-alpha.46

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 (60) hide show
  1. package/dist/httpUtils-44781613.mjs +76 -0
  2. package/dist/httpUtils-801112a6.js +79 -0
  3. package/dist/index.js +226 -337
  4. package/dist/index.mjs +228 -333
  5. package/dist/internals/TRPCClient.d.ts +3 -1
  6. package/dist/internals/TRPCClient.d.ts.map +1 -1
  7. package/dist/internals/dataLoader.d.ts.map +1 -1
  8. package/dist/internals/fetchHelpers.d.ts.map +1 -1
  9. package/dist/internals/retryDelay.d.ts.map +1 -1
  10. package/dist/links/dedupeLink.d.ts.map +1 -1
  11. package/dist/links/httpBatchLink.d.ts.map +1 -1
  12. package/dist/links/httpBatchLink.js +182 -304
  13. package/dist/links/httpBatchLink.mjs +182 -304
  14. package/dist/links/httpLink.d.ts.map +1 -1
  15. package/dist/links/httpLink.js +30 -53
  16. package/dist/links/httpLink.mjs +30 -53
  17. package/dist/links/index.d.ts.map +1 -1
  18. package/dist/links/internals/createChain.d.ts.map +1 -1
  19. package/dist/links/internals/httpUtils.d.ts.map +1 -1
  20. package/dist/links/internals/transformResult.d.ts.map +1 -1
  21. package/dist/links/loggerLink.d.ts.map +1 -1
  22. package/dist/links/loggerLink.js +89 -94
  23. package/dist/links/loggerLink.mjs +89 -89
  24. package/dist/links/retryLink.d.ts.map +1 -1
  25. package/dist/links/splitLink.d.ts.map +1 -1
  26. package/dist/links/splitLink.js +1 -1
  27. package/dist/links/splitLink.mjs +1 -1
  28. package/dist/links/types.d.ts.map +1 -1
  29. package/dist/links/wsLink.d.ts.map +1 -1
  30. package/dist/links/wsLink.js +248 -379
  31. package/dist/links/wsLink.mjs +248 -368
  32. package/dist/splitLink-695ae288.js +48 -0
  33. package/dist/splitLink-ad44a55d.mjs +45 -0
  34. package/dist/transformResult-106791d7.mjs +62 -0
  35. package/dist/transformResult-e15ccdf6.js +65 -0
  36. package/links/httpBatchLink/index.d.ts +1 -1
  37. package/links/httpBatchLink/index.js +1 -1
  38. package/links/httpLink/index.d.ts +1 -1
  39. package/links/httpLink/index.js +1 -1
  40. package/links/loggerLink/index.d.ts +1 -1
  41. package/links/loggerLink/index.js +1 -1
  42. package/links/splitLink/index.d.ts +1 -1
  43. package/links/splitLink/index.js +1 -1
  44. package/links/wsLink/index.d.ts +1 -1
  45. package/links/wsLink/index.js +1 -1
  46. package/package.json +7 -11
  47. package/src/internals/TRPCClient.ts +10 -9
  48. package/src/links/wsLink.ts +1 -1
  49. package/dist/httpUtils-089853f6.mjs +0 -99
  50. package/dist/httpUtils-a0169eef.js +0 -106
  51. package/dist/index.ts.js +0 -1
  52. package/dist/links/httpBatchLink.ts.js +0 -1
  53. package/dist/links/httpLink.ts.js +0 -1
  54. package/dist/links/loggerLink.ts.js +0 -1
  55. package/dist/links/splitLink.ts.js +0 -1
  56. package/dist/links/wsLink.ts.js +0 -1
  57. package/dist/splitLink-0202cd7b.mjs +0 -54
  58. package/dist/splitLink-23c5ce5c.js +0 -57
  59. package/dist/transformResult-06bec648.mjs +0 -108
  60. package/dist/transformResult-3ccc74ea.js +0 -122
@@ -1,392 +1,272 @@
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
1
  import { observable } from '@trpc/server/observable';
10
- import { T as TRPCClientError, t as transformResult } from '../transformResult-06bec648.mjs';
11
- import '@babel/runtime/helpers/defineProperty';
2
+ import { T as TRPCClientError, t as transformResult } from '../transformResult-106791d7.mjs';
12
3
 
13
- /* istanbul ignore next */
14
- var retryDelay = function retryDelay(attemptIndex) {
15
- return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
16
- };
4
+ /* istanbul ignore next */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
17
5
 
18
- 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); }; }
19
-
20
- 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; } }
21
6
  function createWSClient(opts) {
22
- var url = opts.url,
23
- _opts$WebSocket = opts.WebSocket,
24
- WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
25
- _opts$retryDelayMs = opts.retryDelayMs,
26
- retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs,
27
- onOpen = opts.onOpen,
28
- onClose = opts.onClose;
29
- /* istanbul ignore next */
30
-
31
- if (!WebSocketImpl) {
32
- 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");
33
- }
34
- /**
7
+ const { url , WebSocket: WebSocketImpl = WebSocket , retryDelayMs: retryDelayFn = retryDelay , onOpen , onClose , } = opts;
8
+ /* istanbul ignore next */ if (!WebSocketImpl) {
9
+ 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");
10
+ }
11
+ /**
35
12
  * outgoing messages buffer whilst not open
36
- */
37
-
38
-
39
- var outgoing = [];
40
- /**
41
- * pending outgoing requests that are awaiting callback
42
- */
43
-
44
- var pendingRequests = Object.create(null);
45
- var connectAttempt = 0;
46
- var dispatchTimer = null;
47
- var connectTimer = null;
48
- var activeConnection = createWS();
49
- var state = 'connecting';
50
- /**
13
+ */ let outgoing = [];
14
+ const pendingRequests = Object.create(null);
15
+ let connectAttempt = 0;
16
+ let dispatchTimer = null;
17
+ let connectTimer = null;
18
+ let activeConnection = createWS();
19
+ let state = 'connecting';
20
+ /**
51
21
  * tries to send the list of messages
52
- */
53
-
54
- function dispatch() {
55
- if (state !== 'open' || dispatchTimer) {
56
- return;
57
- }
58
-
59
- dispatchTimer = setTimeout(function () {
60
- dispatchTimer = null;
61
-
62
- if (outgoing.length === 1) {
63
- // single send
64
- activeConnection.send(JSON.stringify(outgoing.pop()));
65
- } else {
66
- // batch send
67
- activeConnection.send(JSON.stringify(outgoing));
68
- } // clear
69
-
70
-
71
- outgoing = [];
72
- });
73
- }
74
-
75
- function tryReconnect() {
76
- if (connectTimer || state === 'closed') {
77
- return;
78
- }
79
-
80
- var timeout = retryDelayFn(connectAttempt++);
81
- reconnectInMs(timeout);
82
- }
83
-
84
- function reconnect() {
85
- state = 'connecting';
86
- var oldConnection = activeConnection;
87
- activeConnection = createWS();
88
- closeIfNoPending(oldConnection);
89
- }
90
-
91
- function reconnectInMs(ms) {
92
- if (connectTimer) {
93
- return;
22
+ */ function dispatch() {
23
+ if (state !== 'open' || dispatchTimer) {
24
+ return;
25
+ }
26
+ dispatchTimer = setTimeout(()=>{
27
+ dispatchTimer = null;
28
+ if (outgoing.length === 1) {
29
+ // single send
30
+ activeConnection.send(JSON.stringify(outgoing.pop()));
31
+ } else {
32
+ // batch send
33
+ activeConnection.send(JSON.stringify(outgoing));
34
+ }
35
+ // clear
36
+ outgoing = [];
37
+ });
94
38
  }
95
-
96
- state = 'connecting';
97
- connectTimer = setTimeout(reconnect, ms);
98
- }
99
-
100
- function closeIfNoPending(conn) {
101
- // disconnect as soon as there are are no pending result
102
- var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
103
- return p.ws === conn;
104
- });
105
-
106
- if (!hasPendingRequests) {
107
- conn.close();
39
+ function tryReconnect() {
40
+ if (connectTimer || state === 'closed') {
41
+ return;
42
+ }
43
+ const timeout = retryDelayFn(connectAttempt++);
44
+ reconnectInMs(timeout);
108
45
  }
109
- }
110
-
111
- function resumeSubscriptionOnReconnect(req) {
112
- if (outgoing.some(function (r) {
113
- return r.id === req.op.id;
114
- })) {
115
- return;
46
+ function reconnect() {
47
+ state = 'connecting';
48
+ const oldConnection = activeConnection;
49
+ activeConnection = createWS();
50
+ closeIfNoPending(oldConnection);
116
51
  }
117
-
118
- request(req.op, req.callbacks);
119
- }
120
-
121
- function createWS() {
122
- var conn = new WebSocketImpl(url);
123
- clearTimeout(connectTimer);
124
- connectTimer = null;
125
- conn.addEventListener('open', function () {
126
- /* istanbul ignore next */
127
- if (conn !== activeConnection) {
128
- return;
129
- }
130
-
131
- connectAttempt = 0;
132
- state = 'open';
133
- onOpen === null || onOpen === void 0 ? void 0 : onOpen();
134
- dispatch();
135
- });
136
- conn.addEventListener('error', function () {
137
- if (conn === activeConnection) {
138
- tryReconnect();
139
- }
140
- });
141
-
142
- var handleIncomingRequest = function handleIncomingRequest(req) {
143
- if (req.method === 'reconnect' && conn === activeConnection) {
144
- if (state === 'open') {
145
- onClose === null || onClose === void 0 ? void 0 : onClose();
52
+ function reconnectInMs(ms) {
53
+ if (connectTimer) {
54
+ return;
146
55
  }
147
-
148
- reconnect(); // notify subscribers
149
-
150
- for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
151
- var pendingReq = _Object$values[_i];
152
-
153
- if (pendingReq.type === 'subscription') {
154
- resumeSubscriptionOnReconnect(pendingReq);
155
- }
156
- }
157
- }
158
- };
159
-
160
- var handleIncomingResponse = function handleIncomingResponse(data) {
161
- var _req$callbacks$next, _req$callbacks;
162
-
163
- var req = data.id !== null && pendingRequests[data.id];
164
-
165
- if (!req) {
166
- // do something?
167
- return;
168
- }
169
-
170
- (_req$callbacks$next = (_req$callbacks = req.callbacks).next) === null || _req$callbacks$next === void 0 ? void 0 : _req$callbacks$next.call(_req$callbacks, data);
171
-
172
- if (req.ws !== activeConnection && conn === activeConnection) {
173
- var oldWs = req.ws; // gracefully replace old connection with this
174
-
175
- req.ws = activeConnection;
176
- closeIfNoPending(oldWs);
177
- }
178
-
179
- if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
180
- req.callbacks.complete();
181
- }
182
- };
183
-
184
- conn.addEventListener('message', function (_ref) {
185
- var data = _ref.data;
186
- var msg = JSON.parse(data);
187
-
188
- if ('method' in msg) {
189
- handleIncomingRequest(msg);
190
- } else {
191
- handleIncomingResponse(msg);
192
- }
193
-
194
- if (conn !== activeConnection || state === 'closed') {
195
- // when receiving a message, we close old connection that has no pending requests
196
- closeIfNoPending(conn);
197
- }
198
- });
199
- conn.addEventListener('close', function () {
200
- if (state === 'open') {
201
- onClose === null || onClose === void 0 ? void 0 : onClose();
202
- }
203
-
204
- if (activeConnection === conn) {
205
- // connection might have been replaced already
206
- tryReconnect();
207
- }
208
-
209
- for (var _i2 = 0, _Object$entries = Object.entries(pendingRequests); _i2 < _Object$entries.length; _i2++) {
210
- var _req$callbacks$error, _req$callbacks2;
211
-
212
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
213
- key = _Object$entries$_i[0],
214
- req = _Object$entries$_i[1];
215
-
216
- if (req.ws !== conn) {
217
- continue;
56
+ state = 'connecting';
57
+ connectTimer = setTimeout(reconnect, ms);
58
+ }
59
+ function closeIfNoPending(conn) {
60
+ // disconnect as soon as there are are no pending result
61
+ const hasPendingRequests = Object.values(pendingRequests).some((p)=>p.ws === conn);
62
+ if (!hasPendingRequests) {
63
+ conn.close();
218
64
  }
219
-
220
- (_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')));
221
-
222
- if (req.type !== 'subscription') {
223
- var _req$callbacks$comple, _req$callbacks3;
224
-
225
- delete pendingRequests[key];
226
- (_req$callbacks$comple = (_req$callbacks3 = req.callbacks).complete) === null || _req$callbacks$comple === void 0 ? void 0 : _req$callbacks$comple.call(_req$callbacks3);
227
- } else if (state !== 'closed') {
228
- // request restart of sub with next connection
229
- resumeSubscriptionOnReconnect(req);
65
+ }
66
+ function resumeSubscriptionOnReconnect(req) {
67
+ if (outgoing.some((r)=>r.id === req.op.id)) {
68
+ return;
230
69
  }
231
- }
232
- });
233
- return conn;
234
- }
235
-
236
- function request(op, callbacks) {
237
- var type = op.type,
238
- input = op.input,
239
- path = op.path,
240
- id = op.id;
241
- var envelope = {
242
- id: id,
243
- method: type,
244
- params: {
245
- input: input,
246
- path: path
247
- }
248
- };
249
- pendingRequests[id] = {
250
- ws: activeConnection,
251
- type: type,
252
- callbacks: callbacks,
253
- op: op
254
- }; // enqueue message
255
-
256
- outgoing.push(envelope);
257
- dispatch();
258
- return function () {
259
- var _pendingRequests$id, _callbacks$complete;
260
-
261
- var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
262
- delete pendingRequests[id];
263
- outgoing = outgoing.filter(function (msg) {
264
- return msg.id !== id;
265
- });
266
- callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$complete = callbacks.complete) === null || _callbacks$complete === void 0 ? void 0 : _callbacks$complete.call(callbacks);
267
-
268
- if (op.type === 'subscription') {
269
- outgoing.push({
270
- id: id,
271
- method: 'subscription.stop'
70
+ request(req.op, req.callbacks);
71
+ }
72
+ function createWS() {
73
+ const conn = new WebSocketImpl(url);
74
+ clearTimeout(connectTimer);
75
+ connectTimer = null;
76
+ conn.addEventListener('open', ()=>{
77
+ /* istanbul ignore next */ if (conn !== activeConnection) {
78
+ return;
79
+ }
80
+ connectAttempt = 0;
81
+ state = 'open';
82
+ onOpen?.();
83
+ dispatch();
84
+ });
85
+ conn.addEventListener('error', ()=>{
86
+ if (conn === activeConnection) {
87
+ tryReconnect();
88
+ }
89
+ });
90
+ const handleIncomingRequest = (req)=>{
91
+ if (req.method === 'reconnect' && conn === activeConnection) {
92
+ if (state === 'open') {
93
+ onClose?.();
94
+ }
95
+ reconnect();
96
+ // notify subscribers
97
+ for (const pendingReq of Object.values(pendingRequests)){
98
+ if (pendingReq.type === 'subscription') {
99
+ resumeSubscriptionOnReconnect(pendingReq);
100
+ }
101
+ }
102
+ }
103
+ };
104
+ const handleIncomingResponse = (data)=>{
105
+ const req = data.id !== null && pendingRequests[data.id];
106
+ if (!req) {
107
+ // do something?
108
+ return;
109
+ }
110
+ req.callbacks.next?.(data);
111
+ if (req.ws !== activeConnection && conn === activeConnection) {
112
+ const oldWs = req.ws;
113
+ // gracefully replace old connection with this
114
+ req.ws = activeConnection;
115
+ closeIfNoPending(oldWs);
116
+ }
117
+ if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
118
+ req.callbacks.complete();
119
+ }
120
+ };
121
+ conn.addEventListener('message', ({ data })=>{
122
+ const msg = JSON.parse(data);
123
+ if ('method' in msg) {
124
+ handleIncomingRequest(msg);
125
+ } else {
126
+ handleIncomingResponse(msg);
127
+ }
128
+ if (conn !== activeConnection || state === 'closed') {
129
+ // when receiving a message, we close old connection that has no pending requests
130
+ closeIfNoPending(conn);
131
+ }
132
+ });
133
+ conn.addEventListener('close', ()=>{
134
+ if (state === 'open') {
135
+ onClose?.();
136
+ }
137
+ if (activeConnection === conn) {
138
+ // connection might have been replaced already
139
+ tryReconnect();
140
+ }
141
+ for (const [key, req] of Object.entries(pendingRequests)){
142
+ if (req.ws !== conn) {
143
+ continue;
144
+ }
145
+ req.callbacks.error?.(TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
146
+ if (req.type !== 'subscription') {
147
+ delete pendingRequests[key];
148
+ req.callbacks.complete?.();
149
+ } else if (state !== 'closed') {
150
+ // request restart of sub with next connection
151
+ resumeSubscriptionOnReconnect(req);
152
+ }
153
+ }
272
154
  });
155
+ return conn;
156
+ }
157
+ function request(op, callbacks) {
158
+ const { type , input , path , id } = op;
159
+ const envelope = {
160
+ id,
161
+ method: type,
162
+ params: {
163
+ input,
164
+ path
165
+ }
166
+ };
167
+ pendingRequests[id] = {
168
+ ws: activeConnection,
169
+ type,
170
+ callbacks,
171
+ op
172
+ };
173
+ // enqueue message
174
+ outgoing.push(envelope);
273
175
  dispatch();
274
- }
176
+ return ()=>{
177
+ const callbacks = pendingRequests[id]?.callbacks;
178
+ delete pendingRequests[id];
179
+ outgoing = outgoing.filter((msg)=>msg.id !== id);
180
+ callbacks?.complete?.();
181
+ if (op.type === 'subscription') {
182
+ outgoing.push({
183
+ id,
184
+ method: 'subscription.stop'
185
+ });
186
+ dispatch();
187
+ }
188
+ };
189
+ }
190
+ return {
191
+ close: ()=>{
192
+ state = 'closed';
193
+ onClose?.();
194
+ closeIfNoPending(activeConnection);
195
+ clearTimeout(connectTimer);
196
+ connectTimer = null;
197
+ },
198
+ request,
199
+ getConnection () {
200
+ return activeConnection;
201
+ }
275
202
  };
276
- }
277
-
278
- return {
279
- close: function close() {
280
- state = 'closed';
281
- onClose === null || onClose === void 0 ? void 0 : onClose();
282
- closeIfNoPending(activeConnection);
283
- clearTimeout(connectTimer);
284
- connectTimer = null;
285
- },
286
- request: request,
287
- getConnection: function getConnection() {
288
- return activeConnection;
203
+ }
204
+ class TRPCWebSocketClosedError extends Error {
205
+ constructor(message){
206
+ super(message);
207
+ this.name = 'TRPCWebSocketClosedError';
208
+ Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);
209
+ }
210
+ }
211
+ class TRPCSubscriptionEndedError extends Error {
212
+ constructor(message){
213
+ super(message);
214
+ this.name = 'TRPCSubscriptionEndedError';
215
+ Object.setPrototypeOf(this, TRPCSubscriptionEndedError.prototype);
289
216
  }
290
- };
291
217
  }
292
-
293
- var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
294
- _inherits(TRPCWebSocketClosedError, _Error);
295
-
296
- var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
297
-
298
- function TRPCWebSocketClosedError(message) {
299
- var _this;
300
-
301
- _classCallCheck(this, TRPCWebSocketClosedError);
302
-
303
- _this = _super.call(this, message);
304
- _this.name = 'TRPCWebSocketClosedError';
305
- Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
306
- return _this;
307
- }
308
-
309
- return _createClass(TRPCWebSocketClosedError);
310
- }( /*#__PURE__*/_wrapNativeSuper(Error));
311
-
312
- var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
313
- _inherits(TRPCSubscriptionEndedError, _Error2);
314
-
315
- var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
316
-
317
- function TRPCSubscriptionEndedError(message) {
318
- var _this2;
319
-
320
- _classCallCheck(this, TRPCSubscriptionEndedError);
321
-
322
- _this2 = _super2.call(this, message);
323
- _this2.name = 'TRPCSubscriptionEndedError';
324
- Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
325
- return _this2;
326
- }
327
-
328
- return _createClass(TRPCSubscriptionEndedError);
329
- }( /*#__PURE__*/_wrapNativeSuper(Error));
330
-
331
218
  function wsLink(opts) {
332
- return function (runtime) {
333
- var client = opts.client;
334
- return function (_ref2) {
335
- var op = _ref2.op;
336
- return observable(function (observer) {
337
- var type = op.type,
338
- path = op.path,
339
- id = op.id,
340
- context = op.context;
341
- var input = runtime.transformer.serialize(op.input);
342
- var isDone = false;
343
- var unsub = client.request({
344
- type: type,
345
- path: path,
346
- input: input,
347
- id: id,
348
- context: context
349
- }, {
350
- error: function error(err) {
351
- isDone = true;
352
- observer.error(err);
353
- unsub();
354
- },
355
- complete: function complete() {
356
- if (!isDone) {
357
- isDone = true;
358
- observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
359
- } else {
360
- observer.complete();
361
- }
362
- },
363
- next: function next(message) {
364
- var transformed = transformResult(message, runtime);
365
-
366
- if (!transformed.ok) {
367
- observer.error(TRPCClientError.from(transformed.error));
368
- return;
369
- }
370
-
371
- observer.next({
372
- result: transformed.result
219
+ return (runtime)=>{
220
+ const { client } = opts;
221
+ return ({ op })=>{
222
+ return observable((observer)=>{
223
+ const { type , path , id , context } = op;
224
+ const input = runtime.transformer.serialize(op.input);
225
+ let isDone = false;
226
+ const unsub = client.request({
227
+ type,
228
+ path,
229
+ input,
230
+ id,
231
+ context
232
+ }, {
233
+ error (err) {
234
+ isDone = true;
235
+ observer.error(err);
236
+ unsub();
237
+ },
238
+ complete () {
239
+ if (!isDone) {
240
+ isDone = true;
241
+ observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
242
+ } else {
243
+ observer.complete();
244
+ }
245
+ },
246
+ next (message) {
247
+ const transformed = transformResult(message, runtime);
248
+ if (!transformed.ok) {
249
+ observer.error(TRPCClientError.from(transformed.error));
250
+ return;
251
+ }
252
+ observer.next({
253
+ result: transformed.result
254
+ });
255
+ if (op.type !== 'subscription') {
256
+ // if it isn't a subscription we don't care about next response
257
+ isDone = true;
258
+ unsub();
259
+ observer.complete();
260
+ }
261
+ }
262
+ });
263
+ return ()=>{
264
+ isDone = true;
265
+ unsub();
266
+ };
373
267
  });
374
-
375
- if (op.type !== 'subscription') {
376
- // if it isn't a subscription we don't care about next response
377
- isDone = true;
378
- unsub();
379
- observer.complete();
380
- }
381
- }
382
- });
383
- return function () {
384
- isDone = true;
385
- unsub();
386
268
  };
387
- });
388
269
  };
389
- };
390
270
  }
391
271
 
392
272
  export { createWSClient, wsLink };