From 443e1ed29e11dfed85a7a40c58ac2901c0183f88 Mon Sep 17 00:00:00 2001 From: かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Fri, 2 Jan 2026 21:34:43 +0900 Subject: refactor(frontend): prefer.model, store.modelではcustomRefを使用するように (#17058) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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> --- packages/frontend/src/lib/pizzax.ts | 65 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 33 deletions(-) (limited to 'packages/frontend/src/lib') diff --git a/packages/frontend/src/lib/pizzax.ts b/packages/frontend/src/lib/pizzax.ts index 80543d10e4..0dd8a82957 100644 --- a/packages/frontend/src/lib/pizzax.ts +++ b/packages/frontend/src/lib/pizzax.ts @@ -7,7 +7,7 @@ // TODO: Misskeyのドメイン知識があるのでutilityなどに移動する -import { onUnmounted, ref, watch } from 'vue'; +import { customRef, ref, watch, onScopeDispose } from 'vue'; import { BroadcastChannel } from 'broadcast-channel'; import type { Ref } from 'vue'; import { $i } from '@/i.js'; @@ -223,44 +223,43 @@ export class Pizzax { } /** - * 特定のキーの、簡易的なgetter/setterを作ります + * 特定のキーの、簡易的なcomputed refを作ります * 主にvue上で設定コントロールのmodelとして使う用 */ - // TODO: 廃止 - public makeGetterSetter( + public model( + key: K, + ): Ref; + public model>( + key: K, + getter: (v: T[K]['default']) => R, + setter: (v: R) => T[K]['default'], + ): Ref; + + public model( key: K, getter?: (v: T[K]['default']) => R, setter?: (v: R) => T[K]['default'], - ): { - get: () => R; - set: (value: R) => void; - } { - const valueRef = ref(this.s[key]); - - const stop = watch(this.r[key], val => { - valueRef.value = val; + ): Ref { + return customRef((track, trigger) => { + const watchStop = watch(this.r[key], () => { + trigger(); + }); + + onScopeDispose(() => { + watchStop(); + }, true); + + return { + get: () => { + track(); + return (getter != null ? getter(this.s[key]) : this.s[key]) as R; + }, + set: (value) => { + const val = setter != null ? setter(value) : value; + this.set(key, val as T[K]['default']); + }, + }; }); - - // NOTE: vueコンポーネント内で呼ばれない限りは、onUnmounted は無意味なのでメモリリークする - onUnmounted(() => { - stop(); - }); - - // TODO: VueのcustomRef使うと良い感じになるかも - return { - get: () => { - if (getter) { - return getter(valueRef.value); - } else { - return valueRef.value; - } - }, - set: (value) => { - const val = setter ? setter(value) : value; - this.set(key, val); - valueRef.value = val; - }, - }; } // localStorage => indexedDBのマイグレーション -- cgit v1.2.3-freya