Skip to content

checkpoints ​

This package provides the mechanism allowing to save the user's contents at various points in time, for any kernel.

  • Data are stored in the browser's IndexedDB (in the basthon database).
  • All actions are async.
  • Before the underlying store is ready, reads give nothing and writes are ignored (see Storage._ready). Needed because everything is async.

The Storage class is a wrapper around localForage objects, with readiness awareness. Data are stored as key, value pairs. For checkpoints, key is the timestamp of the backup, and value is usually a string (JSON stringified if needed).

The CheckpointsManager class actually manages two stores in parallel: one for the user contents to archive (of type T, string in GUIBase), and another one for the tags (string[]) that can be used to describe the corresponding user content.

The CheckpointsManager can store at most maxItems elements (constructor's argument, 0 disables the limit), extra (oldest) elements being removed on the fly when needed.

Readiness & synchronization mechanisms ​

All operations being async, the objects using Storage and CheckpointsManager objects need to wait for those to be ready, so that they can properly apply their own initialization steps.

This is accomplished with the ready methods of Storage (() => Promise<void>) and CheckpointsManager (() => Promise<[void, void]>) instances: they return promises that are resolved once the related object is ready, allowing to transfer the readiness information to parent objects (e.g. GUIBase, which uses a CheckpointsManager for the script's backups and a Storage for its state).

Classes ​

CheckpointsManager ​

Checkpoints: successive backups of a content (e.g. a script), stored in the browser (IndexedDB through localForage), each identified by its timestamp and with tags (e.g. "approved"). The oldest checkpoints are removed beyond a maximum number. Stored data is usually a string (JSON conversion if needed).

Each instance has its own stores, named after its name (e.g. one per language), so the checkpoints of different kernels are not mixed.

Type Parameters ​

Type ParameterDescription
TThe type of the backed up content.

Constructors ​

Constructor ​
ts
new CheckpointsManager<T>(name, maxItems?): CheckpointsManager<T>;
Parameters ​
ParameterTypeDefault valueDescription
namestringundefinedThe checkpoints' name (e.g. "console.python3").
maxItems?number0The maximum number of checkpoints kept (0 disables the limit).
Returns ​

CheckpointsManager<T>

Properties ​

PropertyModifierTypeDescription
readypublic() => Promise<[void, void]>Method transferring the readiness promise/logic: returns the promise that resolves once all internal Storage objects are ready.

Methods ​

clear() ​
ts
clear(): Promise<void>;

Remove all the checkpoints.

Returns ​

Promise<void>

get() ​
ts
get(timestamp): Promise<T | null | undefined>;

A checkpoint's content.

Parameters ​
ParameterTypeDescription
timestampstringThe checkpoint's timestamp.
Returns ​

Promise<T | null | undefined>

getIndex() ​
ts
getIndex(index): Promise<T | null | undefined>;

The content of a checkpoint by its index.

Parameters ​
ParameterTypeDescription
indexnumberThe index (0 is the most recent).
Returns ​

Promise<T | null | undefined>

getIndexTags() ​
ts
getIndexTags(index): Promise<string[] | undefined>;

The tags of a checkpoint by its index.

Parameters ​
ParameterTypeDescription
indexnumberThe index (0 is the most recent).
Returns ​

Promise<string[] | undefined>

getIndexTimestamp() ​
ts
getIndexTimestamp(index): Promise<string | undefined>;

The timestamp of a checkpoint by its index.

Parameters ​
ParameterTypeDescription
indexnumberThe index (0 is the most recent).
Returns ​

Promise<string | undefined>

getLast() ​
ts
getLast(): Promise<T | null | undefined>;

The most recent checkpoint's content (as getIndex(0)).

Returns ​

Promise<T | null | undefined>

getLastTags() ​
ts
getLastTags(): Promise<string[] | undefined>;

The most recent checkpoint's tags (as getIndexTags(0)).

Returns ​

Promise<string[] | undefined>

getLastTimestamp() ​
ts
getLastTimestamp(): Promise<string | undefined>;

The most recent timestamp (as getIndexTimestamp(0)).

Returns ​

Promise<string | undefined>

getTags() ​
ts
getTags(timestamp): Promise<string[] | undefined>;

A checkpoint's tags.

Parameters ​
ParameterTypeDescription
timestampstringThe checkpoint's timestamp.
Returns ​

Promise<string[] | undefined>

hasTag() ​
ts
hasTag(timestamp, tag): Promise<boolean | null>;

Does a checkpoint have a tag (null if the checkpoint doesn't exist)?

Parameters ​
ParameterTypeDescription
timestampstringThe checkpoint's timestamp.
tagstringThe tag.
Returns ​

Promise<boolean | null>

lastHasTag() ​
ts
lastHasTag(tag): Promise<boolean | null>;

Does the most recent checkpoint have a tag (null if there is no checkpoint)?

Parameters ​
ParameterTypeDescription
tagstringThe tag.
Returns ​

Promise<boolean | null>

length() ​
ts
length(): Promise<number>;

The number of checkpoints.

Returns ​

Promise<number>

push() ​
ts
push(data, tags?): Promise<string>;

Add a checkpoint (the oldest ones are removed if needed).

Parameters ​
ParameterTypeDefault valueDescription
dataTundefinedThe content.
tags?string[] | nullnullIts tags.
Returns ​

Promise<string>

Its timestamp.

remove() ​
ts
remove(timestamp): Promise<void>;

Remove a checkpoint.

Parameters ​
ParameterTypeDescription
timestampstringThe checkpoint's timestamp.
Returns ​

Promise<void>

removeOldest() ​
ts
removeOldest(): Promise<void>;

Remove the oldest checkpoint.

Returns ​

Promise<void>

tag() ​
ts
tag(timestamp, tag): Promise<void>;

Add a tag to a checkpoint.

Parameters ​
ParameterTypeDescription
timestampstringThe checkpoint's timestamp.
tagstringThe tag.
Returns ​

Promise<void>

tagLast() ​
ts
tagLast(tag): Promise<void>;

Add a tag to the most recent checkpoint.

Parameters ​
ParameterTypeDescription
tagstringThe tag.
Returns ​

Promise<void>

times() ​
ts
times(): Promise<string[] | undefined>;

The checkpoints' timestamps, the most recent first.

Returns ​

Promise<string[] | undefined>

untag() ​
ts
untag(timestamp, tag): Promise<void>;

Remove a tag from a checkpoint.

Parameters ​
ParameterTypeDescription
timestampstringThe checkpoint's timestamp.
tagstringThe tag.
Returns ​

Promise<void>

untagLast() ​
ts
untagLast(tag): Promise<void>;

Remove a tag from the most recent checkpoint.

Parameters ​
ParameterTypeDescription
tagstringThe tag.
Returns ​

Promise<void>

drop() ​
ts
static drop(name): Promise<void>;

Delete the stores of checkpoints.

Parameters ​
ParameterTypeDescription
namestringThe checkpoints' name.
Returns ​

Promise<void>

toHumanDate() ​
ts
static toHumanDate(timestamp): string;

Format a timestamp as a French date: DD/MM/YYYY hh:mm:ss.

Parameters ​
ParameterTypeDescription
timestampstring | numberThe timestamp (milliseconds).
Returns ​

string

The date.


Storage ​

A key-value storage of the browser (IndexedDB through localForage, in the basthon database). Before it is ready (see ready), reads give nothing and writes are ignored.

Type Parameters ​

Type ParameterDescription
TThe type of the values.

Constructors ​

Constructor ​
ts
new Storage<T>(name): Storage<T>;
Parameters ​
ParameterTypeDescription
namestringThe store's name.
Returns ​

Storage<T>

Properties ​

PropertyModifierTypeDescription
readypublic() => Promise<void>Method transferring the readiness promise/logic: returns the promise that will resolve once the Storage object is ready (operations happening before that are ignored).

Methods ​

clear() ​
ts
clear(): Promise<void>;

Delete all the values.

Returns ​

Promise<void>

get() ​
ts
get(key): Promise<T | null | undefined>;

Read a value.

Parameters ​
ParameterTypeDescription
keystringThe value's key.
Returns ​

Promise<T | null | undefined>

The value (null if there is none, undefined before ready).

iterate() ​
ts
iterate(callback): Promise<void>;

Call a function on each value.

Parameters ​
ParameterTypeDescription
callback(a) => voidThe function.
Returns ​

Promise<void>

keys() ​
ts
keys(): Promise<string[] | undefined>;

The keys of the values.

Returns ​

Promise<string[] | undefined>

length() ​
ts
length(): Promise<number>;

The number of values.

Returns ​

Promise<number>

remove() ​
ts
remove(key): Promise<void>;

Delete a value.

Parameters ​
ParameterTypeDescription
keystringThe value's key.
Returns ​

Promise<void>

set() ​
ts
set(key, value): Promise<void>;

Write a value.

Parameters ​
ParameterTypeDescription
keystringThe value's key.
valueTThe value.
Returns ​

Promise<void>

drop() ​
ts
static drop(name): Promise<void>;

Delete a store.

Parameters ​
ParameterTypeDescription
namestringThe store's name.
Returns ​

Promise<void>