Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate for clear and delete on icache #695

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/en/middleware/supplemental.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ import icache from '@dojo/framework/core/middleware/icache';
- Sets the provided `value` for the given `key`. If `value` is a function, it will be invoked in order to obtain the actual value to cache. If the function returns a promise, a 'pending' value will be cached until the final value is fully resolved. In all scenarios, once a value is available and has been stored in the cache, the widget will be marked as invalid so it can be re-rendered with the final value available.
- `icache.has(key: any): boolean`
- Returns `true` or `false` based in whether the key is set in the cache.
- `icache.delete(key: any): void`
- `icache.delete(key: any, invalidate: boolean = true): void`
- Remove the item from the cache.
- `clear()`
- `clear(invalidate: boolean = true)`
- Clears all values currently stored in the widget's local cache.

`icache` can be typed in two different ways. One approach uses generics to enable the return type to get specified at the call-site, and for `getOrSet`, the return type can get inferred from the value type. If the `value` for `getOrSet` is a function then the type will get inferred from the functions return type.
Expand Down
4 changes: 2 additions & 2 deletions docs/zh-CN/middleware/supplemental.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ import icache from '@dojo/framework/core/middleware/icache';
- 将提供的 `value` 设置给指定的 `key`。如果 `value` 是一个函数,则将调用它以获取要缓存的实际值。如果函数返回的是 promise,则会先缓存一个“pending”值,直到解析出最终的值。在所有场景中,一旦一个值可用并存储到缓存中,该部件将被标记为无效,这样就可以使用最终的值重新渲染。
- `icache.has(key: any): boolean`
- 根据 key 是否已在缓存中设置,来返回 `true` 或 `false`。
- `icache.delete(key: any): void`
- `icache.delete(key: any, invalidate: boolean = true): void`
- 从缓存中移除对应的项。
- `clear()`
- `clear(invalidate: boolean = true)`
- 清除当前在部件本地缓存中存储的所有值。

可以使用两种方式为 `icache` 设置类型。一种方式是使用泛型来在调用的地方指定返回类型,对于 `getOrSet`,可以根据值类型推断出返回的类型,如果 `getOrSet` 的 `value` 是一个函数,则使用函数返回的类型推断出值类型。
Expand Down
10 changes: 6 additions & 4 deletions src/core/middleware/icache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export interface ICacheResult<S = void> {
): void;
};
has<T extends void extends S ? any : keyof S>(key: void extends S ? any : T): boolean;
delete<T extends void extends S ? any : keyof S>(key: void extends S ? any : T): void;
clear(): void;
delete<T extends void extends S ? any : keyof S>(key: void extends S ? any : T, invalidate?: boolean): void;
clear(invalidate?: boolean): void;
}

export function createICacheMiddleware<S = void>() {
Expand Down Expand Up @@ -108,11 +108,13 @@ export function createICacheMiddleware<S = void>() {
has(key: any) {
return cacheMap.has(key);
},
delete(key: any) {
delete(key: any, invalidate = true) {
cacheMap.delete(key);
invalidate && invalidator();
},
clear(): void {
clear(invalidate = true): void {
cacheMap.clear();
invalidate && invalidator();
}
};
}
Expand Down
10 changes: 9 additions & 1 deletion tests/core/unit/middleware/icache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('icache middleware', () => {
});
icache.set('test', () => promiseOne);
assert.isUndefined(icache.get('test'));
invalidatorStub.notCalled;
assert.isTrue(invalidatorStub.notCalled);
resolverOne('value');
await promiseOne;

Expand Down Expand Up @@ -161,11 +161,15 @@ describe('icache middleware', () => {
assert.isUndefined(icache.get('test'));
icache.set('test', 'value');
icache.set('test-two', 'value');
assert.isTrue(invalidatorStub.calledTwice);
assert.strictEqual(icache.get('test'), 'value');
assert.strictEqual(icache.get('test-two'), 'value');
icache.delete('test');
assert.isTrue(invalidatorStub.calledThrice);
assert.isUndefined(icache.get('test'));
assert.strictEqual(icache.get('test-two'), 'value');
icache.delete('test', false);
assert.isTrue(invalidatorStub.calledThrice);
});

it('should be able to clear the cache', () => {
Expand All @@ -180,9 +184,13 @@ describe('icache middleware', () => {
});
assert.isUndefined(icache.get('test'));
icache.set('test', 'value');
assert.isTrue(invalidatorStub.calledOnce);
assert.strictEqual(icache.get('test'), 'value');
icache.clear();
assert.isTrue(invalidatorStub.calledTwice);
assert.isUndefined(icache.get('test'));
icache.clear(false);
assert.isTrue(invalidatorStub.calledTwice);
});

describe('icache factory', () => {
Expand Down