complete
This commit is contained in:
+10
-14
@@ -25,7 +25,6 @@ function createBirpc($functions, options) {
|
||||
let $closed = false;
|
||||
const _rpcPromiseMap = /* @__PURE__ */ new Map();
|
||||
let _promiseInit;
|
||||
let rpc;
|
||||
async function _call(method, args, event, optional) {
|
||||
if ($closed)
|
||||
throw new Error(`[birpc] rpc is closed, cannot call "${method}"`);
|
||||
@@ -52,7 +51,7 @@ function createBirpc($functions, options) {
|
||||
if (timeout >= 0) {
|
||||
timeoutId = setTimeout(() => {
|
||||
try {
|
||||
const handleResult = options.onTimeoutError?.call(rpc, method, args);
|
||||
const handleResult = options.onTimeoutError?.(method, args);
|
||||
if (handleResult !== true)
|
||||
throw new Error(`[birpc] timeout on calling "${method}"`);
|
||||
} catch (e) {
|
||||
@@ -69,11 +68,11 @@ function createBirpc($functions, options) {
|
||||
}
|
||||
try {
|
||||
if (options.onRequest)
|
||||
await options.onRequest.call(rpc, req, handler, resolve);
|
||||
await options.onRequest(req, handler, resolve);
|
||||
else
|
||||
await handler();
|
||||
} catch (e) {
|
||||
if (options.onGeneralError?.call(rpc, e) !== true)
|
||||
if (options.onGeneralError?.(e) !== true)
|
||||
throw e;
|
||||
return;
|
||||
} finally {
|
||||
@@ -95,13 +94,10 @@ function createBirpc($functions, options) {
|
||||
get $closed() {
|
||||
return $closed;
|
||||
},
|
||||
get $meta() {
|
||||
return options.meta;
|
||||
},
|
||||
$close,
|
||||
$functions
|
||||
};
|
||||
rpc = new Proxy({}, {
|
||||
const rpc = new Proxy({}, {
|
||||
get(_, method) {
|
||||
if (Object.prototype.hasOwnProperty.call(builtinMethods, method))
|
||||
return builtinMethods[method];
|
||||
@@ -146,14 +142,14 @@ function createBirpc($functions, options) {
|
||||
try {
|
||||
msg = deserialize(data);
|
||||
} catch (e) {
|
||||
if (options.onGeneralError?.call(rpc, e) !== true)
|
||||
if (options.onGeneralError?.(e) !== true)
|
||||
throw e;
|
||||
return;
|
||||
}
|
||||
if (msg.t === TYPE_REQUEST) {
|
||||
const { m: method, a: args, o: optional } = msg;
|
||||
let result, error;
|
||||
let fn = await (resolver ? resolver.call(rpc, method, $functions[method]) : $functions[method]);
|
||||
let fn = await (resolver ? resolver(method, $functions[method]) : $functions[method]);
|
||||
if (optional)
|
||||
fn ||= () => void 0;
|
||||
if (!fn) {
|
||||
@@ -167,9 +163,9 @@ function createBirpc($functions, options) {
|
||||
}
|
||||
if (msg.i) {
|
||||
if (error && options.onError)
|
||||
options.onError.call(rpc, error, method, args);
|
||||
options.onError(error, method, args);
|
||||
if (error && options.onFunctionError) {
|
||||
if (options.onFunctionError.call(rpc, error, method, args) === true)
|
||||
if (options.onFunctionError(error, method, args) === true)
|
||||
return;
|
||||
}
|
||||
if (!error) {
|
||||
@@ -178,14 +174,14 @@ function createBirpc($functions, options) {
|
||||
return;
|
||||
} catch (e) {
|
||||
error = e;
|
||||
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
|
||||
if (options.onGeneralError?.(e, method, args) !== true)
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await post(serialize({ t: TYPE_RESPONSE, i: msg.i, e: error }), ...extra);
|
||||
} catch (e) {
|
||||
if (options.onGeneralError?.call(rpc, e, method, args) !== true)
|
||||
if (options.onGeneralError?.(e, method, args) !== true)
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user