anki/ts/sveltelib/store-subscribe.ts
Henrik Giesel 489eadb352
Fix IME input after tab (#1584)
* Avoid initial call of mirror-dom

* Disable updateFocus from OldEditorAdapter

* fixes IME input duplication bug

* Fix saving of latestLocation for ContentEditable

* Fix IME after calling placeCaretAfterContent

* Export other libraries from domlib/index.ts

* Remove dead code

+ Uncomment code which was mistakenly left commmented
2022-01-12 08:39:41 +10:00

43 lines
958 B
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { Readable, Unsubscriber } from "svelte/store";
interface StoreAccessors {
subscribe: () => void;
unsubscribe: () => void;
}
/**
* Helper function to prevent double (un)subscriptions
*/
function storeSubscribe<T>(
store: Readable<T>,
callback: (value: T) => void,
start = true,
): StoreAccessors {
function subscribe(): Unsubscriber {
return store.subscribe(callback);
}
let unsubscribe: Unsubscriber | null = start ? subscribe() : null;
function resubscribe(): void {
if (!unsubscribe) {
unsubscribe = subscribe();
}
}
function doUnsubscribe() {
unsubscribe?.();
unsubscribe = null;
}
return {
subscribe: resubscribe,
unsubscribe: doUnsubscribe,
};
}
export default storeSubscribe;