LCOV - code coverage report
Current view: top level - hooks - store.go Coverage Total Hit
Test: coverage.lcov Lines: 100.0 % 36 36
Test Date: 2026-11-19 00:25:22 Functions: - 0 0

            Line data    Source code
       1              : package hooks
       2              : 
       3              : import (
       4              :         "reflect"
       5              : 
       6              :         "github.com/kijimaD/ruins/internal/inputmapper"
       7              : )
       8              : 
       9              : // Reducer は個別の状態を更新する関数の型
      10              : type Reducer func(state any, action inputmapper.ActionID) any
      11              : 
      12              : // Store は状態とreducerを保持する
      13              : type Store struct {
      14              :         states   map[string]any
      15              :         reducers map[string]Reducer
      16              :         version  uint64 // 状態が実際に変わるたびに増える。Mount が再描画要否をこれで判定する
      17              : }
      18              : 
      19              : // NewStore は新しいStoreを生成する
      20              : func NewStore() *Store {
      21            1 :         return &Store{
      22            1 :                 states:   make(map[string]any),
      23            1 :                 reducers: make(map[string]Reducer),
      24            1 :         }
      25              : }
      26              : 
      27              : // set は状態を書き、値が実際に変わったときだけ version を上げる。states への書き込みは全て
      28              : // ここを通し、変更検知を1点へ集約する。毎フレーム同値を書く UseTabMenu では版が動かない。
      29              : func (store *Store) set(key string, val any) {
      30            1 :         if old, ok := store.states[key]; ok && reflect.DeepEqual(old, val) {
      31            1 :                 return
      32            1 :         }
      33            1 :         store.states[key] = val
      34            1 :         store.version++
      35              : }
      36              : 
      37              : // Version は現在の状態版を返す。値が実際に変わるたびに増える
      38            1 : func (store *Store) Version() uint64 { return store.version }
      39              : 
      40              : // UseState は状態を取得・登録する
      41              : // keyで状態を識別し、初回呼び出し時にinitで初期化する
      42              : // reducer関数はDispatch時に呼ばれ、状態を更新する
      43              : // reducerは毎回再登録される。これによりProps変化時に最新のクロージャが使われる
      44              : func UseState[T any](store *Store, key string, init T, reducer func(T, inputmapper.ActionID) T) T {
      45            1 :         if _, ok := store.states[key]; !ok {
      46            1 :                 store.set(key, init)
      47            1 :         }
      48              :         // 毎回reducerを再登録して最新のクロージャを反映する
      49            1 :         store.reducers[key] = func(s any, a inputmapper.ActionID) any {
      50            1 :                 typed, ok := s.(T)
      51            1 :                 if !ok {
      52            1 :                         panic("hooks: state type does not match reducer type argument: key=" + key)
      53              :                 }
      54            1 :                 return reducer(typed, a)
      55              :         }
      56            1 :         v, ok := store.states[key].(T)
      57            1 :         if !ok {
      58            1 :                 panic("hooks: state type does not match registration: key=" + key)
      59              :         }
      60            1 :         return v
      61              : }
      62              : 
      63              : // Dispatch は全てのStateにActionを送る
      64              : func (store *Store) Dispatch(action inputmapper.ActionID) {
      65            1 :         for key, reducer := range store.reducers {
      66            1 :                 store.set(key, reducer(store.states[key], action))
      67            1 :         }
      68              : }
      69              : 
      70              : // GetStoreState は指定キーの状態を取得する。Mount を経由せずに直接取得できる
      71              : func GetStoreState[T any](store *Store, key string) (T, bool) {
      72            1 :         v, ok := store.states[key]
      73            1 :         if !ok {
      74            1 :                 var zero T
      75            1 :                 return zero, false
      76            1 :         }
      77            1 :         typed, ok := v.(T)
      78            1 :         if !ok {
      79            1 :                 var zero T
      80            1 :                 return zero, false
      81            1 :         }
      82            1 :         return typed, true
      83              : }
        

Generated by: LCOV version 2.0-1