eshala dorost she
This commit is contained in:
+23
-2
@@ -31,6 +31,11 @@ function createBirpc(functions, options) {
|
||||
return functions;
|
||||
if (method === "$close")
|
||||
return close;
|
||||
if (method === "$rejectPendingCalls") {
|
||||
return rejectPendingCalls;
|
||||
}
|
||||
if (method === "$closed")
|
||||
return closed;
|
||||
if (method === "then" && !eventNames.includes("then") && !("then" in functions))
|
||||
return void 0;
|
||||
const sendEvent = (...args) => {
|
||||
@@ -75,14 +80,30 @@ function createBirpc(functions, options) {
|
||||
return sendCall;
|
||||
}
|
||||
});
|
||||
function close(error) {
|
||||
function close(customError) {
|
||||
closed = true;
|
||||
rpcPromiseMap.forEach(({ reject, method }) => {
|
||||
reject(error || new Error(`[birpc] rpc is closed, cannot call "${method}"`));
|
||||
const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`);
|
||||
if (customError) {
|
||||
customError.cause ??= error;
|
||||
return reject(customError);
|
||||
}
|
||||
reject(error);
|
||||
});
|
||||
rpcPromiseMap.clear();
|
||||
off(onMessage);
|
||||
}
|
||||
function rejectPendingCalls(handler) {
|
||||
const entries = Array.from(rpcPromiseMap.values());
|
||||
const handlerResults = entries.map(({ method, reject }) => {
|
||||
if (!handler) {
|
||||
return reject(new Error(`[birpc]: rejected pending call "${method}".`));
|
||||
}
|
||||
return handler({ method, reject });
|
||||
});
|
||||
rpcPromiseMap.clear();
|
||||
return handlerResults;
|
||||
}
|
||||
async function onMessage(data, ...extra) {
|
||||
let msg;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user