diff options
| author | かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> | 2026-01-02 21:34:43 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-02 21:34:43 +0900 |
| commit | 443e1ed29e11dfed85a7a40c58ac2901c0183f88 (patch) | |
| tree | 1f417f5ae40506c390ebb247ed3d4ad1b700e043 /packages/frontend/src/preferences | |
| parent | fix(frontend): 登録日によるソートの場合はpaginator側のソー... (diff) | |
| download | misskey-443e1ed29e11dfed85a7a40c58ac2901c0183f88.tar.gz misskey-443e1ed29e11dfed85a7a40c58ac2901c0183f88.tar.bz2 misskey-443e1ed29e11dfed85a7a40c58ac2901c0183f88.zip | |
refactor(frontend): prefer.model, store.modelではcustomRefを使用するように (#17058)
* refactor(frontend): prefer.model, store.modelではcustomRefを使用するように
* fix: watchの解除に失敗してもエラーで落ちないように
* Update packages/frontend/src/lib/pizzax.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Diffstat (limited to 'packages/frontend/src/preferences')
| -rw-r--r-- | packages/frontend/src/preferences/manager.ts | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/packages/frontend/src/preferences/manager.ts b/packages/frontend/src/preferences/manager.ts index 13ba0000e4..1a1ec2b345 100644 --- a/packages/frontend/src/preferences/manager.ts +++ b/packages/frontend/src/preferences/manager.ts @@ -3,11 +3,11 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { computed, onUnmounted, ref, watch } from 'vue'; +import { customRef, ref, watch, onScopeDispose } from 'vue'; import { EventEmitter } from 'eventemitter3'; import { host, version } from '@@/js/config.js'; import { PREF_DEF } from './def.js'; -import type { Ref, WritableComputedRef } from 'vue'; +import type { Ref } from 'vue'; import type { MenuItem } from '@/types/menu.js'; import { genId } from '@/utility/id.js'; import { copyToClipboard } from '@/utility/copy-to-clipboard.js'; @@ -299,36 +299,39 @@ export class PreferencesManager extends EventEmitter<PreferencesManagerEvents> { * 特定のキーの、簡易的なcomputed refを作ります * 主にvue上で設定コントロールのmodelとして使う用 */ - public model<K extends keyof PREF, V extends ValueOf<K> = ValueOf<K>>( + public model<K extends keyof PREF, V = ValueOf<K>>( + key: K, + ): Ref<V>; + public model<K extends keyof PREF, V extends Exclude<any, ValueOf<K>>>( + key: K, + getter: (v: ValueOf<K>) => V, + setter: (v: V) => ValueOf<K>, + ): Ref<V>; + + public model<K extends keyof PREF, V>( key: K, getter?: (v: ValueOf<K>) => V, setter?: (v: V) => ValueOf<K>, - ): WritableComputedRef<V> { - const valueRef = ref(this.s[key]); - - const stop = watch(this.r[key], val => { - valueRef.value = val; - }); + ): Ref<V> { + return customRef<V>((track, trigger) => { + const watchStop = watch(this.r[key], () => { + trigger(); + }); - // NOTE: vueコンポーネント内で呼ばれない限りは、onUnmounted は無意味なのでメモリリークする - onUnmounted(() => { - stop(); - }); + onScopeDispose(() => { + watchStop(); + }, true); - // TODO: VueのcustomRef使うと良い感じになるかも - return computed({ - get: () => { - if (getter) { - return getter(valueRef.value); - } else { - return valueRef.value; - } - }, - set: (value) => { - const val = setter ? setter(value) : value; - this.commit(key, val); - valueRef.value = val; - }, + return { + get: () => { + track(); + return (getter != null ? getter(this.s[key]) : this.s[key]) as V; + }, + set: (value) => { + const val = setter != null ? setter(value) : value; + this.commit(key, val as ValueOf<K>); + }, + }; }); } |