@trpc/client 10.0.0-alpha.16 → 10.0.0-alpha.19
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/dist/TRPCClientError.d.ts +0 -8
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/index.js +582 -586
- package/dist/index.mjs +581 -585
- package/dist/internals/TRPCClient.d.ts +1 -9
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/links/dedupeLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +1 -1
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpLink.d.ts +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/splitLink.d.ts +1 -1
- package/dist/links/splitLink.d.ts.map +1 -1
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.d.ts +1 -11
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +214 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.js +6 -15
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +214 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +210 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.d.ts +1 -11
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +67 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.js +6 -15
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +67 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +63 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.d.ts +1 -11
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.dev.js +89 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.js +6 -15
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.prod.js +89 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.esm.js +85 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.d.ts +1 -11
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.dev.js +19 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.js +6 -15
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.prod.js +19 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.esm.js +15 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.d.ts +1 -11
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +395 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.js +6 -15
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +395 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +390 -0
- package/observable/dist/trpc-client-rx.cjs.d.ts +1 -0
- package/observable/dist/trpc-client-rx.cjs.dev.js +9 -0
- package/observable/dist/trpc-client-rx.cjs.js +7 -0
- package/observable/dist/trpc-client-rx.cjs.prod.js +9 -0
- package/observable/dist/trpc-client-rx.esm.js +1 -0
- package/observable/index.d.ts +1 -0
- package/observable/index.js +1 -0
- package/package.json +9 -44
- package/src/TRPCClientError.ts +2 -10
- package/src/internals/TRPCClient.ts +7 -12
- package/src/links/dedupeLink.test.ts +1 -1
- package/src/links/dedupeLink.ts +1 -2
- package/src/links/httpBatchLink.ts +2 -2
- package/src/links/httpLink.ts +2 -2
- package/src/links/internals/createChain.test.ts +1 -1
- package/src/links/loggerLink.ts +1 -2
- package/src/links/splitLink.test.ts +2 -2
- package/src/links/splitLink.ts +2 -2
- package/src/observable/observable.test.ts +1 -3
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
|
|
2
|
+
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
|
|
3
|
+
|
|
4
|
+
var palette = {
|
|
5
|
+
query: ['72e3ff', '3fb0d8'],
|
|
6
|
+
mutation: ['c5a3fc', '904dfc'],
|
|
7
|
+
subscription: ['ff49e1', 'd83fbe']
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// maybe this should be moved to it's own package
|
|
11
|
+
var defaultLogger = function defaultLogger() {
|
|
12
|
+
var c = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console;
|
|
13
|
+
return function (props) {
|
|
14
|
+
var direction = props.direction,
|
|
15
|
+
input = props.input,
|
|
16
|
+
type = props.type,
|
|
17
|
+
path = props.path,
|
|
18
|
+
context = props.context,
|
|
19
|
+
id = props.id;
|
|
20
|
+
|
|
21
|
+
var _palette$type = _slicedToArray(palette[type], 2),
|
|
22
|
+
light = _palette$type[0],
|
|
23
|
+
dark = _palette$type[1];
|
|
24
|
+
|
|
25
|
+
var css = "\n background-color: #".concat(direction === 'up' ? light : dark, "; \n color: ").concat(direction === 'up' ? 'black' : 'white', ";\n padding: 2px;\n ");
|
|
26
|
+
var parts = ['%c', direction === 'up' ? '>>' : '<<', type, "#".concat(id), "%c".concat(path, "%c"), '%O'];
|
|
27
|
+
var args = [css, "".concat(css, "; font-weight: bold;"), "".concat(css, "; font-weight: normal;")];
|
|
28
|
+
|
|
29
|
+
if (props.direction === 'up') {
|
|
30
|
+
args.push({
|
|
31
|
+
input: input,
|
|
32
|
+
context: context
|
|
33
|
+
});
|
|
34
|
+
} else {
|
|
35
|
+
args.push({
|
|
36
|
+
input: input,
|
|
37
|
+
result: props.result,
|
|
38
|
+
elapsedMs: props.elapsedMs,
|
|
39
|
+
context: context
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
var fn = props.direction === 'down' && props.result && props.result instanceof Error ? 'error' : 'log';
|
|
44
|
+
c[fn].apply(null, [parts.join(' ')].concat(args));
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function loggerLink() {
|
|
49
|
+
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
50
|
+
var _opts$enabled = opts.enabled,
|
|
51
|
+
enabled = _opts$enabled === void 0 ? function () {
|
|
52
|
+
return true;
|
|
53
|
+
} : _opts$enabled;
|
|
54
|
+
var _opts$logger = opts.logger,
|
|
55
|
+
logger = _opts$logger === void 0 ? defaultLogger(opts.console) : _opts$logger;
|
|
56
|
+
return function () {
|
|
57
|
+
return function (_ref) {
|
|
58
|
+
var op = _ref.op,
|
|
59
|
+
next = _ref.next,
|
|
60
|
+
prev = _ref.prev;
|
|
61
|
+
// ->
|
|
62
|
+
enabled(_objectSpread(_objectSpread({}, op), {}, {
|
|
63
|
+
direction: 'up'
|
|
64
|
+
})) && logger(_objectSpread(_objectSpread({}, op), {}, {
|
|
65
|
+
direction: 'up'
|
|
66
|
+
}));
|
|
67
|
+
var requestStartTime = Date.now();
|
|
68
|
+
next(op, function (result) {
|
|
69
|
+
var elapsedMs = Date.now() - requestStartTime;
|
|
70
|
+
enabled(_objectSpread(_objectSpread({}, op), {}, {
|
|
71
|
+
direction: 'down',
|
|
72
|
+
result: result
|
|
73
|
+
})) && logger(_objectSpread(_objectSpread({}, op), {}, {
|
|
74
|
+
direction: 'down',
|
|
75
|
+
elapsedMs: elapsedMs,
|
|
76
|
+
result: result
|
|
77
|
+
})); // <-
|
|
78
|
+
|
|
79
|
+
prev(result);
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { loggerLink };
|
|
@@ -1,11 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
|
|
3
|
-
|
|
4
|
-
// curious why you need to?
|
|
5
|
-
// this file exists so that you can import from the entrypoint normally
|
|
6
|
-
// except that it points to your source file and you don't need to run build constantly
|
|
7
|
-
// which means we need to re-export all of the modules from your source file
|
|
8
|
-
// and since export * doesn't include default exports, we need to read your source file
|
|
9
|
-
// to check for a default export and re-export it if it exists
|
|
10
|
-
// it's not ideal, but it works pretty well ¯\_(ツ)_/¯
|
|
11
|
-
export * from "../../../src/links/splitLink";
|
|
1
|
+
export * from "../../../dist/declarations/src/links/splitLink";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function splitLink(opts) {
|
|
6
|
+
return function (rt) {
|
|
7
|
+
var links = 'left' in opts ? {
|
|
8
|
+
true: opts.left,
|
|
9
|
+
false: opts.right
|
|
10
|
+
} : opts;
|
|
11
|
+
var yes = links.true(rt);
|
|
12
|
+
var no = links.false(rt);
|
|
13
|
+
return function (props) {
|
|
14
|
+
opts.condition(props.op) ? yes(props) : no(props);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
exports.splitLink = splitLink;
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
// this file might look strange and you might be wondering what it's for
|
|
3
|
-
// it's lets you import your source files by importing this entrypoint
|
|
4
|
-
// as you would import it if it was built with preconstruct build
|
|
5
|
-
// this file is slightly different to some others though
|
|
6
|
-
// it has a require hook which compiles your code with Babel
|
|
7
|
-
// this means that you don't have to set up @babel/register or anything like that
|
|
8
|
-
// but you can still require this module and it'll be compiled
|
|
1
|
+
'use strict';
|
|
9
2
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
unregister();
|
|
3
|
+
if (process.env.NODE_ENV === "production") {
|
|
4
|
+
module.exports = require("./trpc-client-links-splitLink.cjs.prod.js");
|
|
5
|
+
} else {
|
|
6
|
+
module.exports = require("./trpc-client-links-splitLink.cjs.dev.js");
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function splitLink(opts) {
|
|
6
|
+
return function (rt) {
|
|
7
|
+
var links = 'left' in opts ? {
|
|
8
|
+
true: opts.left,
|
|
9
|
+
false: opts.right
|
|
10
|
+
} : opts;
|
|
11
|
+
var yes = links.true(rt);
|
|
12
|
+
var no = links.false(rt);
|
|
13
|
+
return function (props) {
|
|
14
|
+
opts.condition(props.op) ? yes(props) : no(props);
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
exports.splitLink = splitLink;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function splitLink(opts) {
|
|
2
|
+
return function (rt) {
|
|
3
|
+
var links = 'left' in opts ? {
|
|
4
|
+
true: opts.left,
|
|
5
|
+
false: opts.right
|
|
6
|
+
} : opts;
|
|
7
|
+
var yes = links.true(rt);
|
|
8
|
+
var no = links.false(rt);
|
|
9
|
+
return function (props) {
|
|
10
|
+
opts.condition(props.op) ? yes(props) : no(props);
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { splitLink };
|
|
@@ -1,11 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
|
|
3
|
-
|
|
4
|
-
// curious why you need to?
|
|
5
|
-
// this file exists so that you can import from the entrypoint normally
|
|
6
|
-
// except that it points to your source file and you don't need to run build constantly
|
|
7
|
-
// which means we need to re-export all of the modules from your source file
|
|
8
|
-
// and since export * doesn't include default exports, we need to read your source file
|
|
9
|
-
// to check for a default export and re-export it if it exists
|
|
10
|
-
// it's not ideal, but it works pretty well ¯\_(ツ)_/¯
|
|
11
|
-
export * from "../../../src/links/wsLink";
|
|
1
|
+
export * from "../../../dist/declarations/src/links/wsLink";
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
6
|
+
var _createClass = require('@babel/runtime/helpers/createClass');
|
|
7
|
+
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
8
|
+
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
|
|
9
|
+
var _inherits = require('@babel/runtime/helpers/inherits');
|
|
10
|
+
var _createSuper = require('@babel/runtime/helpers/createSuper');
|
|
11
|
+
var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
|
|
12
|
+
var TRPCClientError = require('../../../dist/TRPCClientError-bfee2bf5.cjs.dev.js');
|
|
13
|
+
|
|
14
|
+
/* istanbul ignore next */
|
|
15
|
+
var retryDelay = function retryDelay(attemptIndex) {
|
|
16
|
+
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function createWSClient(opts) {
|
|
20
|
+
var url = opts.url,
|
|
21
|
+
_opts$WebSocket = opts.WebSocket,
|
|
22
|
+
WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
|
|
23
|
+
_opts$retryDelayMs = opts.retryDelayMs,
|
|
24
|
+
retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs;
|
|
25
|
+
/* istanbul ignore next */
|
|
26
|
+
|
|
27
|
+
if (!WebSocketImpl) {
|
|
28
|
+
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");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* outgoing messages buffer whilst not open
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
var outgoing = [];
|
|
36
|
+
/**
|
|
37
|
+
* pending outgoing requests that are awaiting callback
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
var pendingRequests = Object.create(null);
|
|
41
|
+
var connectAttempt = 0;
|
|
42
|
+
var dispatchTimer = null;
|
|
43
|
+
var connectTimer = null;
|
|
44
|
+
var activeConnection = createWS();
|
|
45
|
+
var state = 'connecting';
|
|
46
|
+
/**
|
|
47
|
+
* tries to send the list of messages
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
function dispatch() {
|
|
51
|
+
if (state !== 'open' || dispatchTimer) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
dispatchTimer = setTimeout(function () {
|
|
56
|
+
dispatchTimer = null;
|
|
57
|
+
|
|
58
|
+
if (outgoing.length === 1) {
|
|
59
|
+
// single send
|
|
60
|
+
activeConnection.send(JSON.stringify(outgoing.pop()));
|
|
61
|
+
} else {
|
|
62
|
+
// batch send
|
|
63
|
+
activeConnection.send(JSON.stringify(outgoing));
|
|
64
|
+
} // clear
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
outgoing = [];
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function tryReconnect() {
|
|
72
|
+
if (connectTimer || state === 'closed') {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
var timeout = retryDelayFn(connectAttempt++);
|
|
77
|
+
reconnectInMs(timeout);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function reconnect() {
|
|
81
|
+
state = 'connecting';
|
|
82
|
+
var oldConnection = activeConnection;
|
|
83
|
+
activeConnection = createWS();
|
|
84
|
+
closeIfNoPending(oldConnection);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function reconnectInMs(ms) {
|
|
88
|
+
if (connectTimer) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
state = 'connecting';
|
|
93
|
+
connectTimer = setTimeout(reconnect, ms);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function closeIfNoPending(conn) {
|
|
97
|
+
// disconnect as soon as there are are no pending result
|
|
98
|
+
var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
|
|
99
|
+
return p.ws === conn;
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (!hasPendingRequests) {
|
|
103
|
+
conn.close();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function resumeSubscriptionOnReconnect(req) {
|
|
108
|
+
if (outgoing.some(function (r) {
|
|
109
|
+
return r.id === req.op.id;
|
|
110
|
+
})) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
request(req.op, req.callbacks);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function createWS() {
|
|
118
|
+
var conn = new WebSocket(url);
|
|
119
|
+
clearTimeout(connectTimer);
|
|
120
|
+
connectTimer = null;
|
|
121
|
+
conn.addEventListener('open', function () {
|
|
122
|
+
/* istanbul ignore next */
|
|
123
|
+
if (conn !== activeConnection) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
connectAttempt = 0;
|
|
128
|
+
state = 'open';
|
|
129
|
+
dispatch();
|
|
130
|
+
});
|
|
131
|
+
conn.addEventListener('error', function () {
|
|
132
|
+
if (conn === activeConnection) {
|
|
133
|
+
tryReconnect();
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
var handleIncomingRequest = function handleIncomingRequest(req) {
|
|
138
|
+
if (req.method === 'reconnect' && conn === activeConnection) {
|
|
139
|
+
reconnect(); // notify subscribers
|
|
140
|
+
|
|
141
|
+
for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
|
|
142
|
+
var pendingReq = _Object$values[_i];
|
|
143
|
+
|
|
144
|
+
if (pendingReq.type === 'subscription') {
|
|
145
|
+
resumeSubscriptionOnReconnect(pendingReq);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
var handleIncomingResponse = function handleIncomingResponse(res) {
|
|
152
|
+
var _req$callbacks$onNext, _req$callbacks2;
|
|
153
|
+
|
|
154
|
+
var req = res.id !== null && pendingRequests[res.id];
|
|
155
|
+
|
|
156
|
+
if (!req) {
|
|
157
|
+
// do something?
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if ('error' in res) {
|
|
162
|
+
var _req$callbacks$onErro, _req$callbacks;
|
|
163
|
+
|
|
164
|
+
(_req$callbacks$onErro = (_req$callbacks = req.callbacks).onError) === null || _req$callbacks$onErro === void 0 ? void 0 : _req$callbacks$onErro.call(_req$callbacks, res);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
(_req$callbacks$onNext = (_req$callbacks2 = req.callbacks).onNext) === null || _req$callbacks$onNext === void 0 ? void 0 : _req$callbacks$onNext.call(_req$callbacks2, res.result);
|
|
169
|
+
|
|
170
|
+
if (req.ws !== activeConnection && conn === activeConnection) {
|
|
171
|
+
var oldWs = req.ws; // gracefully replace old connection with this
|
|
172
|
+
|
|
173
|
+
req.ws = activeConnection;
|
|
174
|
+
closeIfNoPending(oldWs);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (res.result.type === 'stopped' && conn === activeConnection) {
|
|
178
|
+
var _req$callbacks$onDone, _req$callbacks3;
|
|
179
|
+
|
|
180
|
+
(_req$callbacks$onDone = (_req$callbacks3 = req.callbacks).onDone) === null || _req$callbacks$onDone === void 0 ? void 0 : _req$callbacks$onDone.call(_req$callbacks3);
|
|
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 (activeConnection === conn) {
|
|
201
|
+
// connection might have been replaced already
|
|
202
|
+
tryReconnect();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (var key in pendingRequests) {
|
|
206
|
+
var _req$callbacks$onErro2, _req$callbacks4;
|
|
207
|
+
|
|
208
|
+
var req = pendingRequests[key];
|
|
209
|
+
|
|
210
|
+
if (req.ws !== conn) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
(_req$callbacks$onErro2 = (_req$callbacks4 = req.callbacks).onError) === null || _req$callbacks$onErro2 === void 0 ? void 0 : _req$callbacks$onErro2.call(_req$callbacks4, TRPCClientError.TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
|
|
215
|
+
|
|
216
|
+
if (req.type !== 'subscription') {
|
|
217
|
+
var _req$callbacks$onDone2, _req$callbacks5;
|
|
218
|
+
|
|
219
|
+
delete pendingRequests[key];
|
|
220
|
+
(_req$callbacks$onDone2 = (_req$callbacks5 = req.callbacks).onDone) === null || _req$callbacks$onDone2 === void 0 ? void 0 : _req$callbacks$onDone2.call(_req$callbacks5);
|
|
221
|
+
} else if (state !== 'closed') {
|
|
222
|
+
// request restart of sub with next connection
|
|
223
|
+
resumeSubscriptionOnReconnect(req);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return conn;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function request(op, callbacks) {
|
|
231
|
+
var type = op.type,
|
|
232
|
+
input = op.input,
|
|
233
|
+
path = op.path,
|
|
234
|
+
id = op.id;
|
|
235
|
+
var envelope = {
|
|
236
|
+
id: id,
|
|
237
|
+
jsonrpc: '2.0',
|
|
238
|
+
method: type,
|
|
239
|
+
params: {
|
|
240
|
+
input: input,
|
|
241
|
+
path: path
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
pendingRequests[id] = {
|
|
245
|
+
ws: activeConnection,
|
|
246
|
+
type: type,
|
|
247
|
+
callbacks: callbacks,
|
|
248
|
+
op: op
|
|
249
|
+
}; // enqueue message
|
|
250
|
+
|
|
251
|
+
outgoing.push(envelope);
|
|
252
|
+
dispatch();
|
|
253
|
+
return function () {
|
|
254
|
+
var _pendingRequests$id, _callbacks$onDone;
|
|
255
|
+
|
|
256
|
+
var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
|
|
257
|
+
delete pendingRequests[id];
|
|
258
|
+
outgoing = outgoing.filter(function (msg) {
|
|
259
|
+
return msg.id !== id;
|
|
260
|
+
});
|
|
261
|
+
callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$onDone = callbacks.onDone) === null || _callbacks$onDone === void 0 ? void 0 : _callbacks$onDone.call(callbacks);
|
|
262
|
+
|
|
263
|
+
if (op.type === 'subscription') {
|
|
264
|
+
outgoing.push({
|
|
265
|
+
id: id,
|
|
266
|
+
method: 'subscription.stop',
|
|
267
|
+
params: undefined
|
|
268
|
+
});
|
|
269
|
+
dispatch();
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
close: function close() {
|
|
276
|
+
state = 'closed';
|
|
277
|
+
closeIfNoPending(activeConnection);
|
|
278
|
+
},
|
|
279
|
+
request: request,
|
|
280
|
+
getConnection: function getConnection() {
|
|
281
|
+
return activeConnection;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
|
|
287
|
+
_inherits(TRPCWebSocketClosedError, _Error);
|
|
288
|
+
|
|
289
|
+
var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
|
|
290
|
+
|
|
291
|
+
function TRPCWebSocketClosedError(message) {
|
|
292
|
+
var _this;
|
|
293
|
+
|
|
294
|
+
_classCallCheck(this, TRPCWebSocketClosedError);
|
|
295
|
+
|
|
296
|
+
_this = _super.call(this, message);
|
|
297
|
+
_this.name = 'TRPCWebSocketClosedError';
|
|
298
|
+
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
|
|
299
|
+
return _this;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return _createClass(TRPCWebSocketClosedError);
|
|
303
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
304
|
+
|
|
305
|
+
var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
|
|
306
|
+
_inherits(TRPCSubscriptionEndedError, _Error2);
|
|
307
|
+
|
|
308
|
+
var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
|
|
309
|
+
|
|
310
|
+
function TRPCSubscriptionEndedError(message) {
|
|
311
|
+
var _this2;
|
|
312
|
+
|
|
313
|
+
_classCallCheck(this, TRPCSubscriptionEndedError);
|
|
314
|
+
|
|
315
|
+
_this2 = _super2.call(this, message);
|
|
316
|
+
_this2.name = 'TRPCSubscriptionEndedError';
|
|
317
|
+
Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
|
|
318
|
+
return _this2;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return _createClass(TRPCSubscriptionEndedError);
|
|
322
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
323
|
+
|
|
324
|
+
function wsLink(opts) {
|
|
325
|
+
// initialized config
|
|
326
|
+
return function (rt) {
|
|
327
|
+
var client = opts.client;
|
|
328
|
+
return function (_ref2) {
|
|
329
|
+
var op = _ref2.op,
|
|
330
|
+
prev = _ref2.prev,
|
|
331
|
+
onDestroy = _ref2.onDestroy;
|
|
332
|
+
var type = op.type,
|
|
333
|
+
rawInput = op.input,
|
|
334
|
+
path = op.path,
|
|
335
|
+
id = op.id;
|
|
336
|
+
var input = rt.transformer.serialize(rawInput);
|
|
337
|
+
var isDone = false;
|
|
338
|
+
var unsub = client.request({
|
|
339
|
+
type: type,
|
|
340
|
+
path: path,
|
|
341
|
+
input: input,
|
|
342
|
+
id: id
|
|
343
|
+
}, {
|
|
344
|
+
onNext: function onNext(result) {
|
|
345
|
+
if (isDone) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if ('data' in result) {
|
|
350
|
+
var data = rt.transformer.deserialize(result.data);
|
|
351
|
+
prev({
|
|
352
|
+
type: 'data',
|
|
353
|
+
data: data
|
|
354
|
+
});
|
|
355
|
+
} else {
|
|
356
|
+
prev(result);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (op.type !== 'subscription') {
|
|
360
|
+
// if it isn't a subscription we don't care about next response
|
|
361
|
+
isDone = true;
|
|
362
|
+
unsub();
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
onError: function onError(err) {
|
|
366
|
+
if (isDone) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
prev(err instanceof Error ? err : TRPCClientError.TRPCClientError.from(_objectSpread(_objectSpread({}, err), {}, {
|
|
371
|
+
error: rt.transformer.deserialize(err.error)
|
|
372
|
+
})));
|
|
373
|
+
},
|
|
374
|
+
onDone: function onDone() {
|
|
375
|
+
if (isDone) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
var result = new TRPCSubscriptionEndedError('Operation ended prematurely');
|
|
380
|
+
prev(TRPCClientError.TRPCClientError.from(result, {
|
|
381
|
+
isDone: true
|
|
382
|
+
}));
|
|
383
|
+
isDone = true;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
onDestroy(function () {
|
|
387
|
+
isDone = true;
|
|
388
|
+
unsub();
|
|
389
|
+
});
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
exports.createWSClient = createWSClient;
|
|
395
|
+
exports.wsLink = wsLink;
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
// this file might look strange and you might be wondering what it's for
|
|
3
|
-
// it's lets you import your source files by importing this entrypoint
|
|
4
|
-
// as you would import it if it was built with preconstruct build
|
|
5
|
-
// this file is slightly different to some others though
|
|
6
|
-
// it has a require hook which compiles your code with Babel
|
|
7
|
-
// this means that you don't have to set up @babel/register or anything like that
|
|
8
|
-
// but you can still require this module and it'll be compiled
|
|
1
|
+
'use strict';
|
|
9
2
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
unregister();
|
|
3
|
+
if (process.env.NODE_ENV === "production") {
|
|
4
|
+
module.exports = require("./trpc-client-links-wsLink.cjs.prod.js");
|
|
5
|
+
} else {
|
|
6
|
+
module.exports = require("./trpc-client-links-wsLink.cjs.dev.js");
|
|
7
|
+
}
|