Skip to content

[vanilla-store] Remove useSyncPersistStore from useSyncStore and useSetStore - #181

Merged
yujeong-jeon merged 2 commits into
mainfrom
feature/178
Jul 14, 2025
Merged

yujeong-jeon merged 2 commits into
mainfrom
feature/178

Conversation

@yujeong-jeon

@yujeong-jeon yujeong-jeon commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor

Related Issue

Describe your changes

개선점 요약

import {useEffect, useRef, useSyncExternalStore} from 'react'

import {shallowEqual} from './shallowEqual'

import type {SetAction, VanillaSelect, VanillaStore} from './type'

function useSyncStore<State>(store: VanillaStore<State> | VanillaSelect<State>, initialValue?: State) {
	const value = useSyncExternalStore(store.subscribe, store.get, () => initialValue || store.get())
	// useSyncPersistStore(store, value) // 제거: useSyncWithInitialValue와 중복
	return value
}

export function useSetStore<State>(store: VanillaSelect<State>, initialValue?: State): never
export function useSetStore<State>(store: VanillaStore<State>, initialValue?: State): SetAction<State>
export function useSetStore<State>(store: VanillaStore<State> | VanillaSelect<State>, initialValue?: State) {

	useSyncWithInitialValue(store, initialValue)
	
	// useSyncStore(store, initialValue) // 제거: Persist 동기화는 이미 useSyncWithInitialValue에서 처리됨, useSyncExternalStore는 상태 조회용으로 useSetSore에서는 필요하지 않음
	
	return store.set

}

(1) useSyncStore 에서 useSyncPersistStore 제거

1. useSyncPersistStore의 역할

export function useSyncPersistStore<State>(store: VanillaStore<State> | VanillaSelect<State>, value: State) {
    useEffect(() => {
        if (store.persistStore?.value !== value) {
            if (store.persistStore?.value) {
                // 타입 검증 후 persist 값을 store에 설정
                const persistValue = store.persistStore.value
                store.set(() => persistValue)
            }
        }
    }, []) // 마운트 시에만 실행
}
  1. 이미 useSyncWithInitialValue에서 처리됨
function useSyncWithInitialValue<State>(store: VanillaStore<State> | VanillaSelect<State>, initialValue?: State) {
    const ref = useRef<State | null>(initialValue || null)

    useEffect(() => {
        const serialized = store.persistStore?.serialized
        const persistValue = store.persistStore?.value

        if (!isNil(serialized) && !isNil(persistValue)) {
            store.set(persistValue)  // <- 동일한 로직
            ref.current = null
            return
        }

        if (!isNil(ref.current)) {
            store.set(ref.current)
            ref.current = null
        }
    }, [store])
}

두 함수 모두

  • ✅ 동일한 목적: persist 값을 store에 동기화
  • ✅ 동일한 시점: 컴포넌트 마운트 시 실행
  • ✅ 동일한 로직: store.set(persistValue) 호출

따라서 useSyncStore 내부의 useSyncPersistStore는 제거해도� 됨

(2) useSetStore 에서 useSyncStore 제거

  1. persist 동기화는 이미 (1)에서 언급한 것처럼 useSyncWithInitialValue 에서 처리
  2. store의 set 함수가 호출될 때마다 자동으로 persist에 저장되고 있음
  3. useSyncExternalStore는 store를 구독하여 최신 상태를 반환하는 동시에 리렌더링을 트리거하는 역할

그러나 useSetStore는 쓰기 전용 함수로, 구독 불필요함

Request

@yujeong-jeon
yujeong-jeon requested a review from a team as a code owner July 10, 2025 08:21
@npayfebot

npayfebot commented Jul 10, 2025 •

Copy link
Copy Markdown
Collaborator

✅ Changeset detected

Latest commit: 99a70ac

@naverpay/vanilla-store package have detected changes.

If no version change is needed, please add skip-detect-change to the label.

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@naverpay/vanilla-store 🐛 Patch
powered by: naverpay changeset detect-add actions

@npayfebot

npayfebot commented Jul 10, 2025 •

Copy link
Copy Markdown
Collaborator

NPM Packages

📦 @naverpay/vanilla-store

Total Sizes: 7.35 kB

Total Changes: -608 B (-8%) ✅ (Size Decreased)

File Status Previous Size Updated Size Changed
/dist/esm/react.mjs 🛠️ 608 B 588 B -20 B (-3%)
/dist/esm/persist/hooks.mjs 🔥 251 B - -251 B
/dist/cjs/react.js 🛠️ 670 B 647 B -23 B (-3%)
/dist/cjs/persist/hooks.js 🔥 314 B - -314 B
ℹ️ View Unchanged Files
File Status Previous Size Updated Size Changed
/dist/esm/store.mjs - 738 B 738 B -
/dist/esm/shallowEqual.mjs - 317 B 317 B -
/dist/esm/select.mjs - 437 B 437 B -
/dist/esm/index.mjs - 217 B 217 B -
/dist/esm/applyOptions.mjs - 267 B 267 B -
/dist/esm/persist/type.mjs - 216 B 216 B -
/dist/esm/persist/SessionStoragePersist.mjs - 344 B 344 B -
/dist/esm/persist/LocalStoragePersist.mjs - 342 B 342 B -
/dist/cjs/store.js - 793 B 793 B -
/dist/cjs/shallowEqual.js - 374 B 374 B -
/dist/cjs/select.js - 497 B 497 B -
/dist/cjs/index.js - 280 B 280 B -
/dist/cjs/applyOptions.js - 323 B 323 B -
/dist/cjs/persist/type.js - 276 B 276 B -
/dist/cjs/persist/SessionStoragePersist.js - 349 B 349 B -
/dist/cjs/persist/LocalStoragePersist.js - 348 B 348 B -

powered by: naverpay size-action

@yujeong-jeon

Copy link
Copy Markdown
Contributor Author

/rc-publish

@npayfebot

Copy link
Copy Markdown
Collaborator

Published Canary Packages

@yceffort-naver yceffort-naver left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅇㅈ하는 부분입니다

@yujeong-jeon

Copy link
Copy Markdown
Contributor Author

서비스에 써보고 오겠읍니다~!

@keemhyunseok keemhyunseok left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매우 감사합니다

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants