修复多线程图片加载出现空白问题
Signed-off-by: baofeng <baofeng6@h-partners.com>
This commit is contained in:
parent
ee980fe094
commit
f7ce1945b8
|
@ -5,6 +5,7 @@
|
|||
- 支持多种组合变换
|
||||
- 提供清理缓存能力
|
||||
- 修复preLoad接口失效
|
||||
- 修复多线程图片加载出现空白问题
|
||||
|
||||
## 2.2.0-rc.2
|
||||
- ImageKnife支持下采样
|
||||
|
|
|
@ -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<DiskCacheEntry | undefined>{
|
||||
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
|
||||
}
|
||||
|
||||
// 去除键值,(去除键数据中的键名及对应的值)
|
||||
|
|
|
@ -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<Function> = [];
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue