From f7ce1945b83fa5604773abdc01628cacb55b888c Mon Sep 17 00:00:00 2001 From: baofeng Date: Wed, 12 Jun 2024 18:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8A=A0=E8=BD=BD=E5=87=BA=E7=8E=B0=E7=A9=BA?= =?UTF-8?q?=E7=99=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: baofeng --- CHANGELOG.md | 1 + .../components/cache/CustomSendableMap.ets | 21 ++++++++--- .../imageknife/utils/base/AsyncLock.ts | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 library/src/main/ets/components/imageknife/utils/base/AsyncLock.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c0421e5..25f0f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - 支持多种组合变换 - 提供清理缓存能力 - 修复preLoad接口失效 +- 修复多线程图片加载出现空白问题 ## 2.2.0-rc.2 - ImageKnife支持下采样 diff --git a/library/src/main/ets/components/cache/CustomSendableMap.ets b/library/src/main/ets/components/cache/CustomSendableMap.ets index bb33b65..c8e6a0b 100644 --- a/library/src/main/ets/components/cache/CustomSendableMap.ets +++ b/library/src/main/ets/components/cache/CustomSendableMap.ets @@ -13,6 +13,7 @@ * limitations under the License. */ import { collections } from '@kit.ArkTS' +import { AsyncLock } from '../imageknife/utils/base/AsyncLock'; import { DiskCacheEntry } from './DiskCacheEntry'; @Sendable @@ -38,16 +39,24 @@ export class CustomSendableMap { } // 添加键值对 - put(key: string, value: DiskCacheEntry): DiskCacheEntry | undefined{ + async put(key: string, value: DiskCacheEntry): Promise{ if (key == null || value == null) { throw new Error('key or value is invalid,checking the parameter'); } - let pre = this.map.get(key) as DiskCacheEntry | undefined; - if (this.hasKey(key)) { - this.map.delete(key) + const lock = new AsyncLock(); + await lock.acquire() + try { + let pre = this.map.get(key) as DiskCacheEntry | undefined; + if (this.hasKey(key)) { + this.map.delete(key) + } + this.map.set(key, value); + return pre; + } catch (e) { + throw new Error('put value error'); + } finally { + lock.release(); } - this.map.set(key, value); - return pre } // 去除键值,(去除键数据中的键名及对应的值) diff --git a/library/src/main/ets/components/imageknife/utils/base/AsyncLock.ts b/library/src/main/ets/components/imageknife/utils/base/AsyncLock.ts new file mode 100644 index 0000000..936c0e0 --- /dev/null +++ b/library/src/main/ets/components/imageknife/utils/base/AsyncLock.ts @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export class AsyncLock { + private locked: boolean = false; + private waitingList: Array = []; + + + async acquire() { + if (this.locked) { + await new Promise((resolve) => { + this.waitingList.push(resolve); + }); + } + this.locked = true; + } + + release() { + this.locked = false; + if (this.waitingList.length > 0) { + let resolve = this.waitingList.shift(); + resolve && resolve(); + } + } +} \ No newline at end of file