From b5ba6f6ccda14f7c41202a44a7606134729b3f86 Mon Sep 17 00:00:00 2001 From: unadlib Date: Sun, 14 Aug 2022 19:10:38 +0800 Subject: [PATCH] fix(reactant-share): fix lock issue --- packages/reactant-router/src/router.tsx | 2 +- packages/reactant-share/src/lock.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/reactant-router/src/router.tsx b/packages/reactant-router/src/router.tsx index 40e03fe33..6289847dc 100644 --- a/packages/reactant-router/src/router.tsx +++ b/packages/reactant-router/src/router.tsx @@ -1,7 +1,7 @@ /* eslint-disable consistent-return */ import React, { PropsWithChildren, FunctionComponent } from 'react'; import { PluginModule, injectable, inject, storeKey } from 'reactant-module'; -import { ReducersMapObject, Store } from 'redux'; +import type { ReducersMapObject, Store } from 'redux'; import { connectRouter, ConnectedRouter, diff --git a/packages/reactant-share/src/lock.ts b/packages/reactant-share/src/lock.ts index 054498408..c97821ac8 100644 --- a/packages/reactant-share/src/lock.ts +++ b/packages/reactant-share/src/lock.ts @@ -8,7 +8,7 @@ type LockCallBack = (lock: { }) => Promise; type LockQueue = { tabId: string; lockId: string }[]; -const lockMap: Record> = {}; +const lockMap: Map> = new Map(); const tabId = createId(); const lockStorageKey = 'reactant:lock'; const tabStorageKey = 'reactant:tab'; @@ -97,8 +97,8 @@ const simpleLock = (name: LockName, callback: LockCallBack) => { return new Promise((resolve, reject) => { const lockId = createId(); - lockMap[name] ??= {}; - lockMap[name][lockId] = callback; + lockMap.set(name, lockMap.get(name) ?? new Map()); + lockMap.get(name)!.set(lockId, callback); const storageKey = `${lockStorageKey}:${name}`; const oldStorageValue = localStorage.getItem(storageKey); const lockQueue: LockQueue = JSON.parse(oldStorageValue ?? '[]'); @@ -109,7 +109,7 @@ const simpleLock = (name: LockName, callback: LockCallBack) => { if (lock?.tabId === tabId && lock?.lockId === lockId) { window.removeEventListener('storage', listener); try { - const result = await lockMap[name][lockId]({ + const result = await lockMap.get(name)!.get(lockId)!({ name, mode: 'exclusive', }); @@ -117,7 +117,7 @@ const simpleLock = (name: LockName, callback: LockCallBack) => { } catch (e) { reject(e); } - delete lockMap[name][lockId]; + lockMap.get(name)!.delete(lockId); const currentLockQueue: LockQueue = JSON.parse( localStorage.getItem(storageKey) ?? '[]' );