import { computed, ref, type Ref } from "vue"; import { useSettingsStore } from "@/stores/settingsStore"; import { normalizePluginShortcutSettings, type PluginShortcutSettings } from "@/lib/plugins/pluginShortcuts"; const queues = new WeakMap; pending: Ref }>(); /** Serialize partial updates across the settings page, sorting and the resize handle. */ export function usePluginShortcutPreferences() { const settings = useSettingsStore(); let queue = queues.get(settings); if (!queue) { queue = { tail: Promise.resolve(), pending: ref(0) }; queues.set(settings, queue); } const state = queue; function update(patch: Partial | ((current: PluginShortcutSettings) => Partial)) { state.pending.value++; const operation = state.tail.then(async () => { const current = settings.editorSettings.pluginShortcuts; await settings.updateEditorSettingsAndPersist({ pluginShortcuts: normalizePluginShortcutSettings({ ...current, ...(typeof patch === "function" ? patch(current) : patch) }) }); }); state.tail = operation .catch(() => {}) .finally(() => { state.pending.value--; }); return operation; } return { update, saving: computed(() => state.pending.value > 0) }; }