mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Remove getOrDefault from ExtendedObservableMap (#2836)
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
353a166b1e
commit
45d74b8c3a
@ -19,7 +19,7 @@
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx";
|
||||
import { action, ObservableMap } from "mobx";
|
||||
|
||||
export class ExtendedMap<K, V> extends Map<K, V> {
|
||||
static new<K, V>(entries?: readonly (readonly [K, V])[] | null): ExtendedMap<K, V> {
|
||||
@ -67,34 +67,12 @@ export class ExtendedMap<K, V> extends Map<K, V> {
|
||||
}
|
||||
|
||||
export class ExtendedObservableMap<K, V> extends ObservableMap<K, V> {
|
||||
constructor(protected getDefault: () => V, initialData?: IObservableMapInitialValues<K, V>, enhancer?: IEnhancer<V>, name?: string) {
|
||||
super(initialData, enhancer, name);
|
||||
}
|
||||
|
||||
@action
|
||||
getOrInsert(key: K, val: V): V {
|
||||
if (this.has(key)) {
|
||||
return this.get(key);
|
||||
}
|
||||
|
||||
return this.set(key, val).get(key);
|
||||
}
|
||||
|
||||
@action
|
||||
getOrInsertWith(key: K, getVal: () => V): V {
|
||||
getOrInsert(key: K, getVal: () => V): V {
|
||||
if (this.has(key)) {
|
||||
return this.get(key);
|
||||
}
|
||||
|
||||
return this.set(key, getVal()).get(key);
|
||||
}
|
||||
|
||||
@action
|
||||
getOrDefault(key: K): V {
|
||||
if (this.has(key)) {
|
||||
return this.get(key);
|
||||
}
|
||||
|
||||
return this.set(key, this.getDefault()).get(key);
|
||||
}
|
||||
}
|
||||
|
||||
@ -255,17 +255,17 @@ async function watchFileChanges(filePath: string): Promise<[IComputedValue<Catal
|
||||
depth: stat.isDirectory() ? 0 : 1, // DIRs works with 0 but files need 1 (bug: https://github.com/paulmillr/chokidar/issues/1095)
|
||||
disableGlobbing: true,
|
||||
});
|
||||
const rootSource = new ExtendedObservableMap<string, ObservableMap<string, RootSourceValue>>(observable.map);
|
||||
const rootSource = new ExtendedObservableMap<string, ObservableMap<string, RootSourceValue>>();
|
||||
const derivedSource = computed(() => Array.from(iter.flatMap(rootSource.values(), from => iter.map(from.values(), child => child[1]))));
|
||||
const stoppers = new Map<string, Disposer>();
|
||||
|
||||
watcher
|
||||
.on("change", (childFilePath) => {
|
||||
stoppers.get(childFilePath)();
|
||||
stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrDefault(childFilePath)));
|
||||
stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrInsert(childFilePath, observable.map)));
|
||||
})
|
||||
.on("add", (childFilePath) => {
|
||||
stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrDefault(childFilePath)));
|
||||
stoppers.set(childFilePath, diffChangedConfig(childFilePath, rootSource.getOrInsert(childFilePath, observable.map)));
|
||||
})
|
||||
.on("unlink", (childFilePath) => {
|
||||
stoppers.get(childFilePath)();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user