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