vanilladocs
vanillapunch.com
API / misc/persistedstore

misc/persistedstore

지속 저장소.

Persistent store.

PersistedStoreextends Object

짝Counterpart: vanilla-engine PersistedStore

지속 저장소.

  • 하나의 키에 상태 객체를 JSON 으로 저장하고, 바뀔 때 구독자에게 알린다.
  • 판 번호(version)가 다르거나 파싱에 실패하면 기본값으로 되돌아간다.
  • 점수판 / 사용자 설정처럼 "localStorage + JSON + 리스너" 골격이 반복되던 것을 표준화했다.

Persistent store.

  • Stores a state object as JSON under one key, and notifies subscribers when it changes.
  • Falls back to the default value if the version number (version) differs or parsing fails.
  • Standardizes the repeated "localStorage + JSON + listener" skeleton for things like leaderboards / user settings.
예제Example설정을 저장하고 바뀔 때 알림 받기Save settings and get notified on change
import { PersistedStore } from "./build/vanilla.js";

const store = new PersistedStore("example.settings", { volume: 1, language: "en" }, 1);
const subscription = store.subscribe((state) => {
	console.log("changed", state.volume);
});
store.set({ volume: 0.5 });
const state = store.get();
console.log(state.volume, state.language);
store.unsubscribe(subscription);
store.reset();
생성자constructorconstructor(storageKey, defaultState = {}, version = 1)#0.3.0 부터since 0.3.0소스source

생성.

Constructor.

storageKeystring
상태를 둘 저장소 키.Storage key that holds the state.
defaultStateobject
기본 상태. (저장된 것이 없거나 판이 다르면 이것을 복사해 쓴다)Default state. (a copy is used when nothing is stored or the version differs)
versionnumber
저장 꼴의 판 번호. (저장된 판과 다르면 기본 상태로 되돌아간다)Version number of the saved format. (falls back to the default state if it differs from the stored version)

짝Counterpart: vanilla-engine PersistedStore.new

메서드methodget()#0.3.0 부터since 0.3.0소스source

상태 반환. (복사본이 아니므로 직접 고치지 말고 set() 을 쓴다)

Returns the state. (not a copy, so do not modify it directly, use set())

반환Returnsobject 지금 상태.The current state.

짝Counterpart: vanilla-engine PersistedStore.get

메서드methodset(partialState)#0.3.0 부터since 0.3.0소스source

상태 일부 갱신. (얕은 병합 후 저장하고 구독자에게 알린다)

Updates part of the state. (saves after a shallow merge and notifies subscribers)

partialStateobject
바꿀 키와 값만 담은 객체.Object with only the keys and values to change.

짝Counterpart: vanilla-engine PersistedStore.set

메서드methodreplace(nextState)#0.3.0 부터since 0.3.0소스source

상태 전체 교체.

Replaces the whole state.

nextStateobject
새 상태. (얕게 복사해 쓴다)The new state. (shallow copied)

짝Counterpart: vanilla-engine PersistedStore.replace

메서드methodreset()#0.3.0 부터since 0.3.0소스source

기본값으로 초기화.

Resets to the default value.

짝Counterpart: vanilla-engine PersistedStore.reset

메서드methodsubscribe(subscriber)#0.3.0 부터since 0.3.0소스source

구독. (해지 함수를 반환)

Subscribes. (returns an unsubscribe function)

subscriberFunction
(state) => void

반환ReturnsFunction 해지 함수. (부르면 구독을 푼다)Unsubscribe function. (calling it ends the subscription)

짝Counterpart: vanilla-engine PersistedStore.subscribe

메서드methodunsubscribe(subscription)#0.4.0 부터since 0.4.0소스source

구독 해지. (subscribe 가 돌려준 것을 넘긴다, 돌려받은 해지 함수를 부르는 것과 같다)

Unsubscribes. (pass what subscribe returned, same as calling the returned unsubscribe function)

subscriptionFunction
subscribe 가 돌려준 해지 함수. (모르는 것이면 아무것도 하지 않는다)Unsubscribe function returned by subscribe. (does nothing if unknown)

짝Counterpart: vanilla-engine PersistedStore.unsubscribe

메서드methodnotify()#0.3.0 부터since 0.3.0소스source

구독자 알림.

Notifies subscribers.

짝Counterpart: vanilla-engine PersistedStore.notify

메서드methodreadFromStorage()#0.3.0 부터since 0.3.0소스source

저장소에서 읽기.

Reads from the storage.

반환Returnsobject 저장된 상태를 기본 상태 위에 덮은 새 객체. (없거나, 판이 다르거나, 풀 수 없으면 기본 상태의 복사본)A new object with the stored state laid over the default state. (a copy of the default state if missing, a different version or unparsable)

짝Counterpart: vanilla-engine PersistedStore.read_from_storage

메서드methodwriteToStorage()#0.3.0 부터since 0.3.0소스source

저장소에 쓰기.

Writes to the storage.

짝Counterpart: vanilla-engine PersistedStore.write_to_storage

메서드methodreadStoredText()#0.4.0 부터since 0.4.0소스source

저장된 글 읽기. (없거나 못 읽으면 빈 글)

Reads the stored text. (empty text if missing or unreadable)

반환Returnsstring 저장된 JSON 글. 없거나 못 읽으면 빈 글.The stored JSON text. An empty string if missing or unreadable.

짝Counterpart: vanilla-engine PersistedStore.read_stored_text

메서드methodsetStorageBackend(storageBackend)#0.4.0 부터since 0.4.0소스source

이 저장소만 쓰는 곳 갈아 끼우기. (null 이면 LocalStorage, 플랫폼의 저장소처럼 getItem, setItem, removeItem 을 가진 객체)

  • 갈아 끼운 곳에서 상태를 다시 읽고 구독자에게 알린다. 저장된 것이 없으면 기본값이다.

Swaps the storage used only by this store. (null means LocalStorage, an object with getItem, setItem, removeItem like a platform storage)

  • Re-reads the state from the swapped-in storage and notifies subscribers. If nothing is stored, it is the default value.
storageBackend{ getItem: Function, setItem: Function, removeItem: Function } | null
이 저장소만 쓸 곳. (null 이나 undefined 면 LocalStorage)Storage used only by this store. (LocalStorage if null or undefined)

짝Counterpart: vanilla-engine PersistedStore.set_storage_backend

예제Example플랫폼 저장소처럼 따로 둔 곳에 쓰기Write to a separate storage such as a platform storage
import { PersistedStore } from "./build/vanilla.js";

const itemMap = new Map();
const store = new PersistedStore("example.progress", { stage: 1 }, 1);
store.setStorageBackend({
	getItem: (key) => {
		return itemMap.has(key) ? itemMap.get(key) : null;
	},
	setItem: (key, value) => {
		itemMap.set(key, value);
	},
	removeItem: (key) => {
		itemMap.delete(key);
	}
});
store.set({ stage: 3 });
console.log(itemMap.get("example.progress"));
메서드methodgetStorageBackend()#0.4.0 부터since 0.4.0소스source

이 저장소만 쓰는 곳 반환. (없으면 null)

Returns the storage used only by this store. (null if none)

반환Returnsobject | null 이 저장소만 쓰는 곳. 없으면 null.Storage used only by this store. null if none.

짝Counterpart: vanilla-engine PersistedStore.get_storage_backend

메서드methodgetStorageKey()#0.3.0 부터since 0.3.0소스source

저장 키 반환.

Returns the storage key.

반환Returnsstring 저장소 키.Storage key.

짝Counterpart: vanilla-engine PersistedStore.get_storage_key