diff --git a/package.json b/package.json index 11cd822..d3d2a90 100644 --- a/package.json +++ b/package.json @@ -245,7 +245,7 @@ "@vue/reactivity": "^3.2.33", "acorn": "^8.12.0", "acorn-walk": "^8.3.3", - "birpc": "^0.2.17", + "birpc": "^0.2.19", "bumpp": "^9.3.0", "chai": "^5.1.0", "changelogithub": "^0.13.3", @@ -268,11 +268,6 @@ "which": "^4.0.0", "ws": "^8.16.0" }, - "pnpm": { - "patchedDependencies": { - "birpc@0.2.17": "patches/birpc@0.2.17.patch" - } - }, "lint-staged": { "*.{js,ts,tsx,vue,md}": [ "eslint --fix" diff --git a/patches/birpc@0.2.17.patch b/patches/birpc@0.2.17.patch deleted file mode 100644 index e35b22c..0000000 --- a/patches/birpc@0.2.17.patch +++ /dev/null @@ -1,275 +0,0 @@ -diff --git a/dist/index.cjs b/dist/index.cjs -index d8119336758abcb11f5e247dfe579b1b5c5fefd5..00e4b1a01cfa480a874f5da6f8dcdd4db2b89e99 100644 ---- a/dist/index.cjs -+++ b/dist/index.cjs -@@ -11,18 +11,24 @@ function createBirpc(functions, options) { - const { - post, - on, -+ off = () => { -+ }, - eventNames = [], - serialize = defaultSerialize, - deserialize = defaultDeserialize, - resolver, -+ bind = "rpc", - timeout = DEFAULT_TIMEOUT - } = options; - const rpcPromiseMap = /* @__PURE__ */ new Map(); - let _promise; -+ let closed = false; - const rpc = new Proxy({}, { - get(_, method) { - if (method === "$functions") - return functions; -+ if (method === "$close") -+ return close; - if (method === "then" && !eventNames.includes("then") && !("then" in functions)) - return void 0; - const sendEvent = (...args) => { -@@ -33,7 +39,15 @@ function createBirpc(functions, options) { - return sendEvent; - } - const sendCall = async (...args) => { -- await _promise; -+ if (closed) -+ throw new Error(`[birpc] rpc is closed, cannot call "${method}"`); -+ if (_promise) { -+ try { -+ await _promise; -+ } finally { -+ _promise = void 0; -+ } -+ } - return new Promise((resolve, reject) => { - const id = nanoid(); - let timeoutId; -@@ -58,7 +72,15 @@ function createBirpc(functions, options) { - return sendCall; - } - }); -- _promise = on(async (data, ...extra) => { -+ function close() { -+ closed = true; -+ rpcPromiseMap.forEach(({ reject }) => { -+ reject(new Error("[birpc] rpc is closed")); -+ }); -+ rpcPromiseMap.clear(); -+ off(onMessage); -+ } -+ async function onMessage(data, ...extra) { - const msg = deserialize(data); - if (msg.t === "q") { - const { m: method, a: args } = msg; -@@ -68,7 +90,7 @@ function createBirpc(functions, options) { - error = new Error(`[birpc] function "${method}" not found`); - } else { - try { -- result = await fn.apply(rpc, args); -+ result = await fn.apply(bind === "rpc" ? rpc : functions, args); - } catch (e) { - error = e; - } -@@ -90,7 +112,8 @@ function createBirpc(functions, options) { - } - rpcPromiseMap.delete(ack); - } -- }); -+ } -+ _promise = on(onMessage); - return rpc; - } - const cacheMap = /* @__PURE__ */ new WeakMap(); -diff --git a/dist/index.d.cts b/dist/index.d.cts -index 010f33e6343281a894b6835d89b7fcf1157250a7..f7618f7f8118d24723693415dc321adfa5881458 100644 ---- a/dist/index.d.cts -+++ b/dist/index.d.cts -@@ -11,6 +11,10 @@ interface ChannelOptions { - * Listener to receive raw message - */ - on: (fn: (data: any, ...extras: any[]) => void) => any | Promise; -+ /** -+ * Clear the listener when `$close` is called -+ */ -+ off?: (fn: (data: any, ...extras: any[]) => void) => any | Promise; - /** - * Custom function to serialize data - * -@@ -23,6 +27,10 @@ interface ChannelOptions { - * by default it passes the data as-is - */ - deserialize?: (data: any) => any; -+ /** -+ * Call the methods with the RPC context or the original functions object -+ */ -+ bind?: 'rpc' | 'functions'; - } - interface EventOptions { - /** -@@ -71,6 +79,7 @@ type BirpcReturn> = { - [K in keyof RemoteFunctions]: BirpcFn; - } & { - $functions: LocalFunctions; -+ $close: () => void; - }; - type BirpcGroupReturn = { - [K in keyof RemoteFunctions]: BirpcGroupFn; -diff --git a/dist/index.d.mts b/dist/index.d.mts -index 010f33e6343281a894b6835d89b7fcf1157250a7..f7618f7f8118d24723693415dc321adfa5881458 100644 ---- a/dist/index.d.mts -+++ b/dist/index.d.mts -@@ -11,6 +11,10 @@ interface ChannelOptions { - * Listener to receive raw message - */ - on: (fn: (data: any, ...extras: any[]) => void) => any | Promise; -+ /** -+ * Clear the listener when `$close` is called -+ */ -+ off?: (fn: (data: any, ...extras: any[]) => void) => any | Promise; - /** - * Custom function to serialize data - * -@@ -23,6 +27,10 @@ interface ChannelOptions { - * by default it passes the data as-is - */ - deserialize?: (data: any) => any; -+ /** -+ * Call the methods with the RPC context or the original functions object -+ */ -+ bind?: 'rpc' | 'functions'; - } - interface EventOptions { - /** -@@ -71,6 +79,7 @@ type BirpcReturn> = { - [K in keyof RemoteFunctions]: BirpcFn; - } & { - $functions: LocalFunctions; -+ $close: () => void; - }; - type BirpcGroupReturn = { - [K in keyof RemoteFunctions]: BirpcGroupFn; -diff --git a/dist/index.d.ts b/dist/index.d.ts -index 010f33e6343281a894b6835d89b7fcf1157250a7..f7618f7f8118d24723693415dc321adfa5881458 100644 ---- a/dist/index.d.ts -+++ b/dist/index.d.ts -@@ -11,6 +11,10 @@ interface ChannelOptions { - * Listener to receive raw message - */ - on: (fn: (data: any, ...extras: any[]) => void) => any | Promise; -+ /** -+ * Clear the listener when `$close` is called -+ */ -+ off?: (fn: (data: any, ...extras: any[]) => void) => any | Promise; - /** - * Custom function to serialize data - * -@@ -23,6 +27,10 @@ interface ChannelOptions { - * by default it passes the data as-is - */ - deserialize?: (data: any) => any; -+ /** -+ * Call the methods with the RPC context or the original functions object -+ */ -+ bind?: 'rpc' | 'functions'; - } - interface EventOptions { - /** -@@ -71,6 +79,7 @@ type BirpcReturn> = { - [K in keyof RemoteFunctions]: BirpcFn; - } & { - $functions: LocalFunctions; -+ $close: () => void; - }; - type BirpcGroupReturn = { - [K in keyof RemoteFunctions]: BirpcGroupFn; -diff --git a/dist/index.mjs b/dist/index.mjs -index 8396fdbfbd7e1df8935c0806af9e7b31f8ccc261..4544cebb4378902b8e08686ed882689d7102f0f5 100644 ---- a/dist/index.mjs -+++ b/dist/index.mjs -@@ -9,18 +9,24 @@ function createBirpc(functions, options) { - const { - post, - on, -+ off = () => { -+ }, - eventNames = [], - serialize = defaultSerialize, - deserialize = defaultDeserialize, - resolver, -+ bind = "rpc", - timeout = DEFAULT_TIMEOUT - } = options; - const rpcPromiseMap = /* @__PURE__ */ new Map(); - let _promise; -+ let closed = false; - const rpc = new Proxy({}, { - get(_, method) { - if (method === "$functions") - return functions; -+ if (method === "$close") -+ return close; - if (method === "then" && !eventNames.includes("then") && !("then" in functions)) - return void 0; - const sendEvent = (...args) => { -@@ -31,7 +37,15 @@ function createBirpc(functions, options) { - return sendEvent; - } - const sendCall = async (...args) => { -- await _promise; -+ if (closed) -+ throw new Error(`[birpc] rpc is closed, cannot call "${method}"`); -+ if (_promise) { -+ try { -+ await _promise; -+ } finally { -+ _promise = void 0; -+ } -+ } - return new Promise((resolve, reject) => { - const id = nanoid(); - let timeoutId; -@@ -48,7 +62,7 @@ function createBirpc(functions, options) { - if (typeof timeoutId === "object") - timeoutId = timeoutId.unref?.(); - } -- rpcPromiseMap.set(id, { resolve, reject, timeoutId }); -+ rpcPromiseMap.set(id, { resolve, reject, timeoutId, method }); - post(serialize({ m: method, a: args, i: id, t: "q" })); - }); - }; -@@ -56,7 +70,15 @@ function createBirpc(functions, options) { - return sendCall; - } - }); -- _promise = on(async (data, ...extra) => { -+ function close() { -+ closed = true; -+ rpcPromiseMap.forEach(({ reject, method }) => { -+ reject(new Error(`[birpc] rpc is closed, cannot call "${method}"`)); -+ }); -+ rpcPromiseMap.clear(); -+ off(onMessage); -+ } -+ async function onMessage(data, ...extra) { - const msg = deserialize(data); - if (msg.t === "q") { - const { m: method, a: args } = msg; -@@ -66,7 +88,7 @@ function createBirpc(functions, options) { - error = new Error(`[birpc] function "${method}" not found`); - } else { - try { -- result = await fn.apply(rpc, args); -+ result = await fn.apply(bind === "rpc" ? rpc : functions, args); - } catch (e) { - error = e; - } -@@ -88,7 +110,8 @@ function createBirpc(functions, options) { - } - rpcPromiseMap.delete(ack); - } -- }); -+ } -+ _promise = on(onMessage); - return rpc; - } - const cacheMap = /* @__PURE__ */ new WeakMap(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e3ff637..388c43f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,11 +4,6 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false -patchedDependencies: - birpc@0.2.17: - hash: l7ecerzjhm2ujumvxksgxehwau - path: patches/birpc@0.2.17.patch - devDependencies: '@antfu/eslint-config': specifier: ^2.23.2 @@ -65,8 +60,8 @@ devDependencies: specifier: ^8.3.3 version: 8.3.3 birpc: - specifier: ^0.2.17 - version: 0.2.17(patch_hash=l7ecerzjhm2ujumvxksgxehwau) + specifier: ^0.2.19 + version: 0.2.19 bumpp: specifier: ^9.3.0 version: 9.3.0 @@ -1679,10 +1674,9 @@ packages: engines: {node: '>=8'} dev: true - /birpc@0.2.17(patch_hash=l7ecerzjhm2ujumvxksgxehwau): - resolution: {integrity: sha512-+hkTxhot+dWsLpp3gia5AkVHIsKlZybNT5gIYiDlNzJrmYPcTM9k5/w2uaj3IPpd7LlEYpmCj4Jj1nC41VhDFg==} + /birpc@0.2.19: + resolution: {integrity: sha512-5WeXXAvTmitV1RqJFppT5QtUiz2p1mRSYU000Jkft5ZUCLJIk4uQriYNO50HknxKwM6jd8utNc66K1qGIwwWBQ==} dev: true - patched: true /bl@4.1.0: resolution: {integrity: sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==}