From f0451e0a723bb6638bbb8fa2e88c82eaf72fd9cd Mon Sep 17 00:00:00 2001 From: ZiuChen Date: Tue, 2 Apr 2024 14:22:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=20Promiseify=20PostMe?= =?UTF-8?q?ssage=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/demos/promiseify-post-message/base.ts | 13 +- docs/demos/promiseify-post-message/sdk.ts | 49 ++++--- docs/demos/promiseify-post-message/types.ts | 2 +- package.json | 3 - pnpm-lock.yaml | 154 +------------------- 5 files changed, 40 insertions(+), 181 deletions(-) diff --git a/docs/demos/promiseify-post-message/base.ts b/docs/demos/promiseify-post-message/base.ts index 169bf0b6..b8ab49b4 100644 --- a/docs/demos/promiseify-post-message/base.ts +++ b/docs/demos/promiseify-post-message/base.ts @@ -1,5 +1,4 @@ -import get from 'lodash-es/get' -import { FuncMap } from './types' +import { BridgeMap } from './types' import { JSBridgeParams } from './sdk' /** @@ -10,7 +9,7 @@ export function registerBase(target: Window) { const params = e.data as JSBridgeParams const namespace = params.namespace const action = params.action - const callback = get(callbackMap, [namespace, action]) + const callback = callbackMap?.[namespace]?.[action] if (callback) { const result = await callback(params.payload) target.postMessage( @@ -24,10 +23,10 @@ export function registerBase(target: Window) { }) } -type Namespace = keyof FuncMap -type Action = keyof FuncMap[Namespace] -type Payload = FuncMap[Namespace][Action] extends { payload: infer P } ? P : never -type Result = FuncMap[Namespace][Action] extends { result: infer R } ? R : never +type Namespace = keyof BridgeMap +type Action = keyof BridgeMap[Namespace] +type Payload = BridgeMap[Namespace][Action] extends { payload: infer P } ? P : never +type Result = BridgeMap[Namespace][Action] extends { result: infer R } ? R : never const callbackMap: Record< Namespace, diff --git a/docs/demos/promiseify-post-message/sdk.ts b/docs/demos/promiseify-post-message/sdk.ts index bb2277cb..98365247 100644 --- a/docs/demos/promiseify-post-message/sdk.ts +++ b/docs/demos/promiseify-post-message/sdk.ts @@ -1,46 +1,47 @@ -import { FuncMap } from './types' +import { BridgeMap } from './types' -export interface JSBridgeParams { +export interface JSBridgeParams { namespace: T - action: keyof FuncMap[T] - payload: FuncMap[T][keyof FuncMap[T]] extends { payload: infer P } ? P : never + action: keyof BridgeMap[T] + payload: BridgeMap[T][keyof BridgeMap[T]] extends { payload: infer P } ? P : never } /** * 注册 SDK 应用 */ export function registerSdk() { - window.addEventListener('message', messageHandler) + window.addEventListener('message', ({ data, type }: MessageEvent) => { + if (type === 'message') { + const { params, result } = data + const key = `${params.namespace}.${params.action}_${params.id}` + const callback = postMessageCallbackMap.get(key) + if (callback) { + callback(result) + postMessageCallbackMap.delete(key) + } + } + }) } const postMessageCallbackMap = new Map() -function messageHandler({ data, type }: MessageEvent) { - if (type === 'message') { - const { params, result } = data - const key = [params.namespace, params.action, params.id].join('.') - const callback = postMessageCallbackMap.get(key) - if (callback) { - callback(result) - postMessageCallbackMap.delete(key) - } - } -} - let uniqueId = 0 -export function postMessage( +export function postMessage( params: JSBridgeParams -): Promise { +): Promise { return new Promise((resolve) => { if (window.parent) { const id = uniqueId++ - const key = [params.namespace, params.action, id].join('.') + const key = `${params.namespace}.${String(params.action)}_${id}` postMessageCallbackMap.set(key, resolve) - const _params = structuredClone(params) - // @ts-expect-error - id is private field. - _params.id = id - window.parent.postMessage(_params, '*') + window.parent.postMessage( + { + id, + ...params + }, + '*' + ) } }) } diff --git a/docs/demos/promiseify-post-message/types.ts b/docs/demos/promiseify-post-message/types.ts index 77c00b2f..599ff2d2 100644 --- a/docs/demos/promiseify-post-message/types.ts +++ b/docs/demos/promiseify-post-message/types.ts @@ -1,7 +1,7 @@ /** * 基座与 SDK 共享同一份类型定义 */ -export interface FuncMap { +export interface BridgeMap { user: { getUserToken: { payload: { userId: string } diff --git a/package.json b/package.json index 11145a79..09005208 100644 --- a/package.json +++ b/package.json @@ -6,16 +6,13 @@ "preview": "vitepress preview docs" }, "devDependencies": { - "@types/lodash-es": "^4.17.12", "@types/node": "^20.12.2", "@vitejs/plugin-vue-jsx": "^3.1.0", "ant-design-vue": "^4.1.2", - "less": "^4.2.0", "vite": "^5.2.7", "vue": "^3.4.21" }, "dependencies": { - "lodash-es": "^4.17.21", "medium-zoom": "^1.1.0", "swiper": "^11.1.0", "vitepress": "1.0.2" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ef5577d..fb2d1324 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,9 +5,6 @@ settings: excludeLinksFromLockfile: false dependencies: - lodash-es: - specifier: ^4.17.21 - version: 4.17.21 medium-zoom: specifier: ^1.1.0 version: 1.1.0 @@ -16,12 +13,9 @@ dependencies: version: 11.1.0 vitepress: specifier: 1.0.2 - version: 1.0.2(@algolia/client-search@4.23.2)(@types/node@20.12.2)(less@4.2.0)(search-insights@2.13.0) + version: 1.0.2(@algolia/client-search@4.23.2)(@types/node@20.12.2)(search-insights@2.13.0) devDependencies: - '@types/lodash-es': - specifier: ^4.17.12 - version: 4.17.12 '@types/node': specifier: ^20.12.2 version: 20.12.2 @@ -31,12 +25,9 @@ devDependencies: ant-design-vue: specifier: ^4.1.2 version: 4.1.2(vue@3.4.21) - less: - specifier: ^4.2.0 - version: 4.2.0 vite: specifier: ^5.2.7 - version: 5.2.7(@types/node@20.12.2)(less@4.2.0) + version: 5.2.7(@types/node@20.12.2) vue: specifier: ^3.4.21 version: 3.4.21 @@ -841,16 +832,6 @@ packages: resolution: {integrity: sha512-yg6E+u0/+Zjva+buc3EIb+29XEg4wltq7cSmd4Uc2EE/1nUVmxyzpX6gUXD0V8jIrG0r7YeOGVIbYRkxeooCtw==} dev: false - /@types/lodash-es@4.17.12: - resolution: {integrity: sha512-0NgftHUcV4v34VhXm8QBSftKVXtbkBG3ViCjs6+eJ5a6y6Mi/jiFGPc1sC7QK+9BFhWrURE3EOggmWaSxL9OzQ==} - dependencies: - '@types/lodash': 4.17.0 - dev: true - - /@types/lodash@4.17.0: - resolution: {integrity: sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA==} - dev: true - /@types/markdown-it@13.0.7: resolution: {integrity: sha512-U/CBi2YUUcTHBt5tjO2r5QV/x0Po6nsYwQU4Y04fBS6vfoImaiZ6f8bi3CjTCxBPQSO1LMyUqkByzi8AidyxfA==} dependencies: @@ -881,7 +862,7 @@ packages: '@babel/core': 7.24.3 '@babel/plugin-transform-typescript': 7.24.1(@babel/core@7.24.3) '@vue/babel-plugin-jsx': 1.2.2(@babel/core@7.24.3) - vite: 5.2.7(@types/node@20.12.2)(less@4.2.0) + vite: 5.2.7(@types/node@20.12.2) vue: 3.4.21 transitivePeerDependencies: - supports-color @@ -894,7 +875,7 @@ packages: vite: ^5.0.0 vue: ^3.2.25 dependencies: - vite: 5.2.7(@types/node@20.12.2)(less@4.2.0) + vite: 5.2.7(@types/node@20.12.2) vue: 3.4.21 dev: false @@ -1218,11 +1199,6 @@ packages: resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==} dev: true - /copy-anything@2.0.6: - resolution: {integrity: sha512-1j20GZTsvKNkc4BY3NpMOM8tt///wY3FpIzozTOFO2ffuZcV61nojHXVKIy3WM+7ADCy5FVhdZYHYDdgTU0yJw==} - dependencies: - is-what: 3.14.1 - /core-js@3.36.1: resolution: {integrity: sha512-BTvUrwxVBezj5SZ3f10ImnX2oRByMxql3EimVqMysepbC9EeMUOpLwdy6Eoili2x6E4kf+ZUB5k/+Jv55alPfA==} requiresBuild: true @@ -1263,14 +1239,6 @@ packages: resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==} engines: {node: '>=0.12'} - /errno@0.1.8: - resolution: {integrity: sha512-dJ6oBr5SQ1VSd9qkk7ByRgb/1SH4JZjCHSW/mr63/QcXO9zLVxvJ6Oy13nio03rxpSnVDDjFor75SjVeZWPW/A==} - hasBin: true - requiresBuild: true - dependencies: - prr: 1.0.1 - optional: true - /esbuild@0.20.2: resolution: {integrity: sha512-WdOOppmUNU+IbZ0PaDiTst80zjnrOkyJNHoKupIcVyU8Lvla3Ugx94VzkQ32Ijqd7UhHJy75gNWDMUekcrSJ6g==} engines: {node: '>=12'} @@ -1336,11 +1304,6 @@ packages: engines: {node: '>=4'} dev: true - /graceful-fs@4.2.11: - resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==} - requiresBuild: true - optional: true - /has-flag@3.0.0: resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==} engines: {node: '>=4'} @@ -1355,29 +1318,11 @@ packages: engines: {node: '>=8'} dev: true - /iconv-lite@0.6.3: - resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} - engines: {node: '>=0.10.0'} - requiresBuild: true - dependencies: - safer-buffer: 2.1.2 - optional: true - - /image-size@0.5.5: - resolution: {integrity: sha512-6TDAlDPZxUFCv+fuOkIoXT/V/f3Qbq8e37p+YOiYrUv3v9cc3/6x78VdfPgFVaB9dZYeLUfKgHRebpkm/oP2VQ==} - engines: {node: '>=0.10.0'} - hasBin: true - requiresBuild: true - optional: true - /is-plain-object@3.0.1: resolution: {integrity: sha512-Xnpx182SBMrr/aBik8y+GuR4U1L9FqMSojwDQwPMmxyC6bvEqly9UBCxhauBF5vNh2gwWJNX6oDV7O+OM4z34g==} engines: {node: '>=0.10.0'} dev: true - /is-what@3.14.1: - resolution: {integrity: sha512-sNxgpk9793nzSs7bA6JQJGeIuRBQhAaNGG77kzYQgMkrID+lS6SlK07K5LaptscDlSaIgH+GPFzf+d75FVxozA==} - /js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} dev: true @@ -1394,25 +1339,9 @@ packages: hasBin: true dev: true - /less@4.2.0: - resolution: {integrity: sha512-P3b3HJDBtSzsXUl0im2L7gTO5Ubg8mEN6G8qoTS77iXxXX4Hvu4Qj540PZDvQ8V6DmX6iXo98k7Md0Cm1PrLaA==} - engines: {node: '>=6'} - hasBin: true - dependencies: - copy-anything: 2.0.6 - parse-node-version: 1.0.1 - tslib: 2.6.2 - optionalDependencies: - errno: 0.1.8 - graceful-fs: 4.2.11 - image-size: 0.5.5 - make-dir: 2.1.0 - mime: 1.6.0 - needle: 3.3.1 - source-map: 0.6.1 - /lodash-es@4.17.21: resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} + dev: true /lodash@4.17.21: resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} @@ -1437,15 +1366,6 @@ packages: dependencies: '@jridgewell/sourcemap-codec': 1.4.15 - /make-dir@2.1.0: - resolution: {integrity: sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==} - engines: {node: '>=6'} - requiresBuild: true - dependencies: - pify: 4.0.1 - semver: 5.7.2 - optional: true - /mark.js@8.11.1: resolution: {integrity: sha512-1I+1qpDt4idfgLQG+BNWmrqku+7/2bi5nLf4YwF8y8zXvmfiTBY3PV3ZibfrjBueCByROpuBjLLFCajqkgYoLQ==} dev: false @@ -1454,13 +1374,6 @@ packages: resolution: {integrity: sha512-ewyDsp7k4InCUp3jRmwHBRFGyjBimKps/AJLjRSox+2q/2H4p/PNpQf+pwONWlJiOudkBXtbdmVbFjqyybfTmQ==} dev: false - /mime@1.6.0: - resolution: {integrity: sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==} - engines: {node: '>=4'} - hasBin: true - requiresBuild: true - optional: true - /minisearch@6.3.0: resolution: {integrity: sha512-ihFnidEeU8iXzcVHy74dhkxh/dn8Dc08ERl0xwoMMGqp4+LvRSCgicb+zGqWthVokQKvCSxITlh3P08OzdTYCQ==} dev: false @@ -1482,24 +1395,10 @@ packages: resolution: {integrity: sha512-NzOgmMQ+elxxHeIha+OG/Pv3Oc3p4RU2aBhwWwAqDpXrdTbtRylbRLQztLy8dMMwfl6pclznBdfUhccEn9ZIzw==} dev: true - /needle@3.3.1: - resolution: {integrity: sha512-6k0YULvhpw+RoLNiQCRKOl09Rv1dPLr8hHnVjHqdolKwDrdNyk+Hmrthi4lIGPPz3r39dLx0hsF5s40sZ3Us4Q==} - engines: {node: '>= 4.4.x'} - hasBin: true - requiresBuild: true - dependencies: - iconv-lite: 0.6.3 - sax: 1.3.0 - optional: true - /node-releases@2.0.14: resolution: {integrity: sha512-y10wOWt8yZpqXmOgRo77WaHEmhYQYGNA6y421PKsKYWEK8aW+cqAphborZDhqfyKrbZEN92CN1X2KbafY2s7Yw==} dev: true - /parse-node-version@1.0.1: - resolution: {integrity: sha512-3YHlOa/JgH6Mnpr05jP9eDG254US9ek25LyIxZlDItp2iJtwyaXQb57lBYLdT3MowkUFYEV2XXNAYIPlESvJlA==} - engines: {node: '>= 0.10'} - /perfect-debounce@1.0.0: resolution: {integrity: sha512-xCy9V055GLEqoFaHoC1SoLIaLmWctgCUaBaWxDZ7/Zx4CTyX7cJQLJOok/orfjZAh9kEYpjJa4d0KcJmCbctZA==} dev: false @@ -1507,12 +1406,6 @@ packages: /picocolors@1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} - /pify@4.0.1: - resolution: {integrity: sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==} - engines: {node: '>=6'} - requiresBuild: true - optional: true - /postcss@8.4.38: resolution: {integrity: sha512-Wglpdk03BSfXkHoQa3b/oulrotAkwrlLDRSOb9D0bN86FdRyE9lppSp33aHNPgBa0JKCoB+drFLZkQoRRYae5A==} engines: {node: ^10 || ^12 || >=14} @@ -1525,11 +1418,6 @@ packages: resolution: {integrity: sha512-JIFjgFg9B2qnOoGiYMVBtrcFxHqn+dNXbq76bVmcaHYJFYR4lW67AOcXgAYQQTDYXDOg/kTZrKPNCdRgJ2UJmw==} dev: false - /prr@1.0.1: - resolution: {integrity: sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==} - requiresBuild: true - optional: true - /regenerator-runtime@0.14.1: resolution: {integrity: sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==} dev: true @@ -1566,16 +1454,6 @@ packages: '@rollup/rollup-win32-x64-msvc': 4.13.2 fsevents: 2.3.3 - /safer-buffer@2.1.2: - resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==} - requiresBuild: true - optional: true - - /sax@1.3.0: - resolution: {integrity: sha512-0s+oAmw9zLl1V1cS9BtZN7JAd0cW5e0QH4W3LWEK6a4LaLEA2OTpGYWDY+6XasBLtz6wkm3u1xRw95mRuJ59WA==} - requiresBuild: true - optional: true - /scroll-into-view-if-needed@2.2.31: resolution: {integrity: sha512-dGCXy99wZQivjmjIqihaBQNjryrz5rueJY7eHfTdyWEiR4ttYpsajb14rn9s5d4DY4EcY6+4+U/maARBXJedkA==} dependencies: @@ -1586,12 +1464,6 @@ packages: resolution: {integrity: sha512-Orrsjf9trHHxFRuo9/rzm0KIWmgzE8RMlZMzuhZOJ01Rnz3D0YBAe+V6473t6/H6c7irs6Lt48brULAiRWb3Vw==} dev: false - /semver@5.7.2: - resolution: {integrity: sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==} - hasBin: true - requiresBuild: true - optional: true - /semver@6.3.1: resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==} hasBin: true @@ -1611,12 +1483,6 @@ packages: resolution: {integrity: sha512-itJW8lvSA0TXEphiRoawsCksnlf8SyvmFzIhltqAHluXd88pkCd+cXJVHTDwdCr0IzwptSm035IHQktUu1QUMg==} engines: {node: '>=0.10.0'} - /source-map@0.6.1: - resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} - engines: {node: '>=0.10.0'} - requiresBuild: true - optional: true - /speakingurl@14.0.1: resolution: {integrity: sha512-1POYv7uv2gXoyGFpBCmpDVSNV74IfsWlDW216UPjbWufNf+bSU6GdbDsxdcxtfwb4xlI3yxzOTKClUosxARYrQ==} engines: {node: '>=0.10.0'} @@ -1655,9 +1521,6 @@ packages: resolution: {integrity: sha512-/OaKK0xYrs3DmxRYqL/yDc+FxFUVYhDlXMhRmv3z915w2HF1tnN1omB354j8VUGO/hbRzyD6Y3sA7v7GS/ceog==} engines: {node: '>=4'} - /tslib@2.6.2: - resolution: {integrity: sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==} - /undici-types@5.26.5: resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==} @@ -1672,7 +1535,7 @@ packages: picocolors: 1.0.0 dev: true - /vite@5.2.7(@types/node@20.12.2)(less@4.2.0): + /vite@5.2.7(@types/node@20.12.2): resolution: {integrity: sha512-k14PWOKLI6pMaSzAuGtT+Cf0YmIx12z9YGon39onaJNy8DLBfBJrzg9FQEmkAM5lpHBZs9wksWAsyF/HkpEwJA==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true @@ -1702,13 +1565,12 @@ packages: dependencies: '@types/node': 20.12.2 esbuild: 0.20.2 - less: 4.2.0 postcss: 8.4.38 rollup: 4.13.2 optionalDependencies: fsevents: 2.3.3 - /vitepress@1.0.2(@algolia/client-search@4.23.2)(@types/node@20.12.2)(less@4.2.0)(search-insights@2.13.0): + /vitepress@1.0.2(@algolia/client-search@4.23.2)(@types/node@20.12.2)(search-insights@2.13.0): resolution: {integrity: sha512-bEj9yTEdWyewJFOhEREZF+mXuAgOq27etuJZT6DZSp+J3XpQstXMJc5piSVwhZBtuj8OfA0iXy+jdP1c71KMYQ==} hasBin: true peerDependencies: @@ -1733,7 +1595,7 @@ packages: mark.js: 8.11.1 minisearch: 6.3.0 shiki: 1.2.3 - vite: 5.2.7(@types/node@20.12.2)(less@4.2.0) + vite: 5.2.7(@types/node@20.12.2) vue: 3.4.21 transitivePeerDependencies: - '@algolia/client-search'