!304 修复多线程图片加载出现空白问题

Merge pull request !304 from baofeng/master
This commit is contained in:
Madi 2024-06-13 06:20:16 +00:00 committed by Gitee
commit d4a87f0c5e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 53 additions and 6 deletions

View File

@ -5,6 +5,7 @@
- 支持多种组合变换 - 支持多种组合变换
- 提供清理缓存能力 - 提供清理缓存能力
- 修复preLoad接口失效 - 修复preLoad接口失效
- 修复多线程图片加载出现空白问题
## 2.2.0-rc.2 ## 2.2.0-rc.2
- ImageKnife支持下采样 - ImageKnife支持下采样

View File

@ -13,6 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { collections } from '@kit.ArkTS' import { collections } from '@kit.ArkTS'
import { AsyncLock } from '../imageknife/utils/base/AsyncLock';
import { DiskCacheEntry } from './DiskCacheEntry'; import { DiskCacheEntry } from './DiskCacheEntry';
@Sendable @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) { if (key == null || value == null) {
throw new Error('key or value is invalid,checking the parameter'); throw new Error('key or value is invalid,checking the parameter');
} }
let pre = this.map.get(key) as DiskCacheEntry | undefined; const lock = new AsyncLock();
if (this.hasKey(key)) { await lock.acquire()
this.map.delete(key) 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
} }
// 去除键值,(去除键数据中的键名及对应的值) // 去除键值,(去除键数据中的键名及对应的值)

View File

@ -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();
}
}
}