command enabling refactor

This commit is contained in:
Jan Prochazka
2021-03-15 19:33:37 +01:00
parent dfa8ca6797
commit 3b3e81e3f7
13 changed files with 207 additions and 140 deletions

View File

@@ -0,0 +1,30 @@
import { tick } from 'svelte';
import { commands } from '../stores';
let isInvalidated = false;
export default async function invalidateCommands() {
if (isInvalidated) return;
isInvalidated = true;
await tick();
isInvalidated = false;
commands.update(dct => {
let res = null;
for (const key of Object.keys(dct)) {
const command = dct[key];
const { testEnabled } = command;
let enabled = command.enabled;
if (testEnabled) enabled = testEnabled();
if (enabled != command.enabled) {
if (!res) res = { ...dct };
res[key] = {
...command,
enabled,
};
}
}
return res || dct;
});
}