vanilladocs
vanillapunch.com
API / misc/localstorage

misc/localstorage

로컬 스토리지.

Local storage.

LocalStorageextends Object

짝Counterpart: vanilla-engine LocalStorage

로컬 스토리지.

  • 기본 저장소는 브라우저의 window.localStorage 다. setBackend 로 다른 저장소(플랫폼 SDK 의 저장소 같은 것)로 갈아 끼울 수 있다.
  • 모든 접근을 try 로 감싼다. 저장소가 막혀 있거나(사생활 보호 창, 막은 쿠키) 가득 차서 못 쓰면 예외를 내지 않고 이 페이지가 살아 있는 동안 메모리에 대신 둔다. (그 값은 다시 켜면 없다, isMemoryFallback)
  • 읽기(getString, getBoolean, getNumber, getJson)는 없는 키에 기본값을 써 넣지 않는다.

Local storage.

  • The default storage is the browser's window.localStorage. setBackend can swap in another storage (such as a platform SDK's storage).
  • Wraps every access in try. If the storage is blocked (private window, blocked cookies) or full and unusable, it throws no exception and keeps the value in memory instead while this page is alive. (that value is gone after a restart, isMemoryFallback)
  • Reads (getString, getBoolean, getNumber, getJson) do not write the default value for missing keys.
예제Example점수와 설정 저장하고 읽기Save and read a score and settings
import { LocalStorage } from "./build/vanilla.js";

LocalStorage.setNumber("example.bestScore", 1200);
LocalStorage.setBoolean("example.isMuted", true);
LocalStorage.setJson("example.settings", { volume: 0.8, language: "en" });
const bestScore = LocalStorage.getNumber("example.bestScore", 0);
const isMuted = LocalStorage.getBoolean("example.isMuted", false);
const settings = LocalStorage.getJson("example.settings", {});
console.log(bestScore, isMuted, settings.volume, LocalStorage.containsKey("example.bestScore"));
const removedCount = LocalStorage.clearByPrefix("example.");
console.log(removedCount, LocalStorage.isMemoryFallback());
생성자constructorconstructor()#0.1.0 부터since 0.1.0소스source

생성.

Constructor.

정적staticstatic setBackend(backend)#0.4.0 부터since 0.4.0소스source

저장소 갈아 끼우기. (null 이면 브라우저 window.localStorage 로 돌아간다)

  • backend 는 getItem(key) → string | null, setItem(key, value), removeItem(key) 를 가진 객체다. (Storage 와 같은 꼴)
  • 키 목록(getKeys, clearByPrefix)과 전체 제거(clear)를 쓰려면 getKeys() → string[] 를, 한 번에 지우려면 clear() 를 더 둔다.
  • 갈아 끼우면 메모리에 대신 둔 값은 버린다.

Swaps the storage. (null returns to the browser window.localStorage)

  • backend is an object with getItem(key) → string | null, setItem(key, value), removeItem(key). (same shape as Storage)
  • To use the key list (getKeys, clearByPrefix) and full removal (clear), also provide getKeys() → string[], and clear() to delete everything at once.
  • Swapping discards the values kept in memory instead.
backend{ getItem: Function, setItem: Function, removeItem: Function, getKeys?: Function, clear?: Function } | null
갈아 끼울 저장소. (null 이나 undefined 면 window.localStorage)Storage to swap in. (window.localStorage if null or undefined)

짝Counterpart: vanilla-engine LocalStorage.set_backend

예제Example내 저장소로 갈아 끼우기Swap in your own storage
import { LocalStorage } from "./build/vanilla.js";

const itemMap = new Map();
LocalStorage.setBackend({
	getItem: (key) => {
		return itemMap.has(key) ? itemMap.get(key) : null;
	},
	setItem: (key, value) => {
		itemMap.set(key, value);
	},
	removeItem: (key) => {
		itemMap.delete(key);
	},
	getKeys: () => {
		return Array.from(itemMap.keys());
	}
});
LocalStorage.setString("profile.name", "Player");
console.log(itemMap.get("profile.name"), LocalStorage.getKeys());
LocalStorage.setBackend(null);
정적staticstatic getBackend()#0.4.0 부터since 0.4.0소스source

갈아 끼운 저장소 반환. (없으면 null)

Returns the swapped-in storage. (null if none)

반환Returnsobject | null 갈아 끼운 저장소. 없으면 null.The swapped-in storage. null if none.

짝Counterpart: vanilla-engine LocalStorage.get_backend

정적staticstatic setStorageDirectory(storageDirectoryPath)#0.4.0 부터since 0.4.0소스source

저장 폴더 직접 설정. (러스트 판 데스크탑이 localstorage.json 을 둘 폴더, 원본은 브라우저가 페이지 주소마다 정해 아무것도 하지 않는다)

  • 같은 게임 코드가 두 엔진에서 돌게 두는 자리다. 러스트 판 웹도 아무것도 하지 않는다.

Sets the save folder directly. (the folder where the Rust version on desktop keeps localstorage.json, this version does nothing because the browser decides it per page address)

  • Kept so the same game code runs on both engines. The Rust version on the web does nothing either.
storageDirectoryPathstring | null
localstorage.json 을 둘 폴더. (null 이면 앱 이름으로 정한 기본 폴더)Folder to keep localstorage.json in. (null means the default folder chosen from the app name)

짝Counterpart: vanilla-engine LocalStorage.set_storage_directory

예제Example저장 폴더를 정해도 원본 저장은 그대로 브라우저 저장소에 간다Saving still goes to the browser storage even with a save folder set
import { LocalStorage } from "./build/vanilla.js";

LocalStorage.setStorageDirectory("saves");
LocalStorage.setString("profile.name", "Player");
console.log(LocalStorage.getString("profile.name"));
정적staticstatic isMemoryFallback()#0.4.0 부터since 0.4.0소스source

저장소를 못 써서 메모리로 대신한 적이 있는지.

Whether it has ever fallen back to memory because the storage was unusable.

반환Returnsboolean 메모리로 대신한 적이 있으면 참. (setBackend 를 부르면 거짓으로 돌아간다)True if memory has ever been used instead. (returns to false when setBackend is called)

짝Counterpart: vanilla-engine LocalStorage.is_memory_fallback

정적staticstatic getItem(key)#0.4.0 부터since 0.4.0소스source

값 하나 읽기. (없으면 null, 메모리에 대신 둔 값이 앞선다)

Reads one value. (null if missing, values kept in memory instead take precedence)

keystring
키.Key.

반환Returnsstring | null 저장된 글. 없거나 못 읽으면 null.The stored text. null if missing or unreadable.

짝Counterpart: vanilla-engine LocalStorage.get_item

정적staticstatic setItem(key, value)#0.4.0 부터since 0.4.0소스source

값 하나 쓰기. (저장소에 썼으면 true, 못 써서 메모리에 대신 뒀으면 false)

Writes one value. (true if written to the storage, false if it could not and was kept in memory instead)

keystring
키.Key.
valuestring
쓸 글.Text to write.

반환Returnsboolean 저장소에 썼으면 참. 메모리에 대신 뒀으면 거짓.True if written to the storage. False if kept in memory instead.

짝Counterpart: vanilla-engine LocalStorage.set_item

정적staticstatic removeItem(key)#0.4.0 부터since 0.4.0소스source

값 하나 지우기. (메모리에 대신 둔 값도 지운다)

Removes one value. (also removes the value kept in memory instead)

keystring
지울 키.Key to remove.

짝Counterpart: vanilla-engine LocalStorage.remove_item

정적staticstatic clear()#0.1.0 부터since 0.1.0소스source

전체 제거.

Removes all.

짝Counterpart: vanilla-engine LocalStorage.clear

정적staticstatic setString(key, stringValue)#0.1.0 부터since 0.1.0소스source

문자열 값 설정.

Sets a string value.

keystring
키.Key.
stringValuestring
쓸 글.Text to write.

짝Counterpart: vanilla-engine LocalStorage.set_string

정적staticstatic setBoolean(key, booleanValue)#0.1.0 부터since 0.1.0소스source

논리 값 설정.

Sets a boolean value.

keystring
키.Key.
booleanValueboolean
쓸 값. ("true" 나 "false" 글로 쓴다)Value to write. (written as the text "true" or "false")

짝Counterpart: vanilla-engine LocalStorage.set_boolean

정적staticstatic setNumber(key, numberValue)#0.1.0 부터since 0.1.0소스source

숫자 값 설정.

Sets a number value.

keystring
키.Key.
numberValuenumber
쓸 값. (String 으로 바꿔 쓴다)Value to write. (converted with String)

짝Counterpart: vanilla-engine LocalStorage.set_number

정적staticstatic remove(key)#0.1.0 부터since 0.1.0소스source

값 제거.

Removes a value.

keystring
지울 키.Key to remove.

짝Counterpart: vanilla-engine LocalStorage.remove

정적staticstatic getString(key, defaultStringValue = "")#0.1.0 부터since 0.1.0소스source

문자열 값 반환. (없으면 기본값, 저장소에는 쓰지 않는다)

Returns a string value. (default value if missing, not written to the storage)

keystring
키.Key.
defaultStringValuestring
없을 때 돌려줄 값.Value returned when missing.

반환Returnsstring 저장된 글. 없으면 기본값.The stored text. The default value if missing.

짝Counterpart: vanilla-engine LocalStorage.get_string

정적staticstatic getBoolean(key, defaultBooleanValue = false)#0.1.0 부터since 0.1.0소스source

논리 값 반환.

Returns a boolean value.

keystring
키.Key.
defaultBooleanValueboolean
없거나 빈 글일 때 돌려줄 값.Value returned when missing or empty.

반환Returnsboolean 저장된 글이 "true" 면 참. 없거나 빈 글이면 기본값.True if the stored text is "true". The default value if missing or empty.

짝Counterpart: vanilla-engine LocalStorage.get_boolean

정적staticstatic getNumber(key, defaultNumberValue = 0)#0.1.0 부터since 0.1.0소스source

숫자 값 반환.

Returns a number value.

keystring
키.Key.
defaultNumberValuenumber
없거나 빈 글일 때 돌려줄 값.Value returned when missing or empty.

반환Returnsnumber 저장된 글을 Number 로 바꾼 값. (없거나 빈 글이면 기본값, 숫자가 아니면 NaN)The stored text converted with Number. (the default value if missing or empty, NaN if not a number)

짝Counterpart: vanilla-engine LocalStorage.get_number

정적staticstatic clearByPrefix(prefix)#0.3.0 부터since 0.3.0소스source

접두사로 시작하는 키 일괄 삭제. (정적)

  • 게임 네임스페이스의 저장 데이터를 통째로 지울 때 쓴다.

Deletes all keys starting with the prefix at once. (static)

  • Used to wipe a game namespace's saved data entirely.
prefixstring
지울 키의 접두사. (빈 글이면 아무것도 지우지 않는다)Prefix of the keys to remove. (removes nothing if empty)

반환Returnsnumber 지운 키 수.Number of removed keys.

짝Counterpart: vanilla-engine LocalStorage.clear_by_prefix

정적staticstatic containsKey(key)#0.1.0 부터since 0.1.0소스source

키가 있는지 반환. (메모리에 대신 둔 값도 본다)

Returns whether the key exists. (also checks values kept in memory instead)

keystring
찾을 키.Key to look for.

반환Returnsboolean 값이 있으면 참.True if a value exists.

짝Counterpart: vanilla-engine LocalStorage.contains_key

정적staticstatic getKeys()#0.1.0 부터since 0.1.0소스source

모든 키 반환.

Returns all keys.

반환Returnsstring[] 저장소와 메모리의 키를 합쳐 정렬한 배열.Sorted array of the keys in the storage and in memory combined.

짝Counterpart: vanilla-engine LocalStorage.get_keys

정적staticstatic getJson(key, defaultValue = null)#0.4.0 부터since 0.4.0소스source

JSON 값 반환. (없거나 풀 수 없으면 기본값, 저장소에는 쓰지 않는다)

Returns a JSON value. (default value if missing or unparsable, not written to the storage)

keystring
키.Key.
defaultValueany
없거나 빈 글이거나 풀 수 없을 때 돌려줄 값.Value returned when missing, empty or unparsable.

반환Returnsany 풀어 낸 값. 없거나 풀 수 없으면 기본값.The parsed value. The default value if missing or unparsable.

짝Counterpart: vanilla-engine LocalStorage.get_json

정적staticstatic setJson(key, value)#0.4.0 부터since 0.4.0소스source

JSON 값 설정. (JSON 글로 바꿔 쓴다. 바꿀 수 없는 값(순환 참조 등)이면 쓰지 않고 false)

Sets a JSON value. (converts to JSON text and writes it. If the value cannot be converted (circular references, etc.), does not write and returns false)

keystring
키.Key.
valueany
JSON 으로 바꿔 쓸 값.Value to write as JSON.

반환Returnsboolean 저장소에 썼는지. (메모리에 대신 뒀거나 바꿀 수 없으면 false)Whether it was written to the storage. (false if kept in memory instead or not convertible)

짝Counterpart: vanilla-engine LocalStorage.set_json