-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlit-directive-switch-part.js
41 lines (36 loc) · 1.11 KB
/
lit-directive-switch-part.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { directive, nothing } from "lit-html";
const getProperty = (store, key) => (Object.prototype.hasOwnProperty.call(store, key) ? store[key] : nothing);
const isFn = (v) => typeof v === 'function';
function switchPartDirective(store, opt = {}){
let part;
let lastKey;
let {default: _default = 'default', resolver = getProperty, cache = true } = opt;
const kvStore = new Map();
const getRaw = (key) => {
let value = resolver(store, key);
return isFn(value) ? value(lastKey) : value;
};
const getFromCache = (key) => {
if(kvStore.has(key)){
return kvStore.get(key);
}
kvStore.set(key, getRaw(key));
return kvStore.get(key);
};
const get = cache ? getFromCache : getRaw;
const setValue = (value, key) => {
if(part){
part.setValue(value);
part.commit();
lastKey = key;
}
};
const switchPartFn = (_part) => {
part = _part;
setValue(get(_default));
};
switchPartFn.clear = (_) => setValue(nothing);
switchPartFn.case = (key) => setValue(get(key), key)
return switchPartFn;
}
export const switchPart = directive(switchPartDirective);