localstorage key via query string
This commit is contained in:
@@ -1,15 +1,20 @@
|
|||||||
import { defaultGame } from "./prefabs/game";
|
import { defaultGame } from "./prefabs/game";
|
||||||
import { Game, serializeGameToString, deserializeGame, deserializeGameFromString } from '@incgame/engine';
|
import { Game, serializeGameToString, deserializeGame, deserializeGameFromString } from '@incgame/engine';
|
||||||
|
|
||||||
const KEY_GAME = 'incgame';
|
const DEFAULT_KEY_GAME = 'incgame';
|
||||||
|
|
||||||
|
function getLocalStorageKey(): string {
|
||||||
|
const params = new URLSearchParams(window.location.search);
|
||||||
|
return params.get('game') || DEFAULT_KEY_GAME;
|
||||||
|
}
|
||||||
|
|
||||||
export function save(game: Game) {
|
export function save(game: Game) {
|
||||||
localStorage.setItem(KEY_GAME, serializeGameToString(game));
|
localStorage.setItem(getLocalStorageKey(), serializeGameToString(game));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function load(): Game {
|
export function load(): Game {
|
||||||
try {
|
try {
|
||||||
const raw = localStorage.getItem(KEY_GAME);
|
const raw = localStorage.getItem(getLocalStorageKey());
|
||||||
if (raw)
|
if (raw)
|
||||||
return deserializeGameFromString(raw);
|
return deserializeGameFromString(raw);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user