Core · class

RemoteSaveStore

Explained in The loop.

class RemoteSaveStore implements KeyValueStore
import { RemoteSaveStore } from '@driftengine/core';

Constructor

new

constructor(backend: SaveBackend, timer?: SaveTimer, options?: RemoteSaveOptions)
ParameterTypeDescription
backendSaveBackend
timer?SaveTimer
options?RemoteSaveOptions

Accessors

NameTypeDescription
loadedgetbooleanWhether load has completed, either way.
statusgetSaveStatus
pendinggetnumberHow many keys are waiting to be sent, counting a batch that failed and came back.
lastErrorgetstring | nullThe last failure's message, cleared by the next batch that lands.

Methods

load

load(): Promise<boolean>

Fill the cache from the backend. Await this before the first frame.

More

Answers whether it worked rather than rejecting, because a rejected promise nobody caught is an unhandled rejection during boot and a store that cannot load is a store that still has to serve defaults. A failure also says so on the console once: a consumer that ignores both the answer and the status boots on defaults and overwrites the player's real settings with them on the first write, which is the one outcome worse than not loading.

onStatusChange

onStatusChange(listener: (status: SaveStatus) => void): () => void

Told when the status changes, and only when it changes. Returns the unsubscribe.

ParameterTypeDescription
listener(status: SaveStatus) => void

read

read(key: string): string | null
ParameterTypeDescription
keystring

write

write(key: string, value: string): void
ParameterTypeDescription
keystring
valuestring

remove

remove(key: string): void
ParameterTypeDescription
keystring

flush

flush(): Promise<void>

Send everything queued now, and resolve when that attempt has settled.

More

For a consumer leaving the page. It never rejects: read status afterwards to find out whether it landed. A batch already in flight is awaited first, so two flushes cannot overlap.

discardPending

discardPending(): void

Throw away everything waiting to be sent.

More

The escape hatch that makes "a failed batch is kept forever" acceptable: a consumer that has decided the queue is stale — a player signing out, a save slot deleted — says so, rather than this store guessing after some number of failures. The cache is untouched, so reads still answer what the session wrote.