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
basthondatabase). - 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 Parameter | Description |
|---|---|
T | The type of the backed up content. |
Constructors
Constructor
new CheckpointsManager<T>(name, maxItems?): CheckpointsManager<T>;Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
name | string | undefined | The checkpoints' name (e.g. "console.python3"). |
maxItems? | number | 0 | The maximum number of checkpoints kept (0 disables the limit). |
Returns
Properties
| Property | Modifier | Type | Description |
|---|---|---|---|
ready | public | () => Promise<[void, void]> | Method transferring the readiness promise/logic: returns the promise that resolves once all internal Storage objects are ready. |
Methods
clear()
clear(): Promise<void>;Remove all the checkpoints.
Returns
Promise<void>
get()
get(timestamp): Promise<T | null | undefined>;A checkpoint's content.
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | The checkpoint's timestamp. |
Returns
Promise<T | null | undefined>
getIndex()
getIndex(index): Promise<T | null | undefined>;The content of a checkpoint by its index.
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | The index (0 is the most recent). |
Returns
Promise<T | null | undefined>
getIndexTags()
getIndexTags(index): Promise<string[] | undefined>;The tags of a checkpoint by its index.
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | The index (0 is the most recent). |
Returns
Promise<string[] | undefined>
getIndexTimestamp()
getIndexTimestamp(index): Promise<string | undefined>;The timestamp of a checkpoint by its index.
Parameters
| Parameter | Type | Description |
|---|---|---|
index | number | The index (0 is the most recent). |
Returns
Promise<string | undefined>
getLast()
getLast(): Promise<T | null | undefined>;The most recent checkpoint's content (as getIndex(0)).
Returns
Promise<T | null | undefined>
getLastTags()
getLastTags(): Promise<string[] | undefined>;The most recent checkpoint's tags (as getIndexTags(0)).
Returns
Promise<string[] | undefined>
getLastTimestamp()
getLastTimestamp(): Promise<string | undefined>;The most recent timestamp (as getIndexTimestamp(0)).
Returns
Promise<string | undefined>
getTags()
getTags(timestamp): Promise<string[] | undefined>;A checkpoint's tags.
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | The checkpoint's timestamp. |
Returns
Promise<string[] | undefined>
hasTag()
hasTag(timestamp, tag): Promise<boolean | null>;Does a checkpoint have a tag (null if the checkpoint doesn't exist)?
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | The checkpoint's timestamp. |
tag | string | The tag. |
Returns
Promise<boolean | null>
lastHasTag()
lastHasTag(tag): Promise<boolean | null>;Does the most recent checkpoint have a tag (null if there is no checkpoint)?
Parameters
| Parameter | Type | Description |
|---|---|---|
tag | string | The tag. |
Returns
Promise<boolean | null>
length()
length(): Promise<number>;The number of checkpoints.
Returns
Promise<number>
push()
push(data, tags?): Promise<string>;Add a checkpoint (the oldest ones are removed if needed).
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
data | T | undefined | The content. |
tags? | string[] | null | null | Its tags. |
Returns
Promise<string>
Its timestamp.
remove()
remove(timestamp): Promise<void>;Remove a checkpoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | The checkpoint's timestamp. |
Returns
Promise<void>
removeOldest()
removeOldest(): Promise<void>;Remove the oldest checkpoint.
Returns
Promise<void>
tag()
tag(timestamp, tag): Promise<void>;Add a tag to a checkpoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | The checkpoint's timestamp. |
tag | string | The tag. |
Returns
Promise<void>
tagLast()
tagLast(tag): Promise<void>;Add a tag to the most recent checkpoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
tag | string | The tag. |
Returns
Promise<void>
times()
times(): Promise<string[] | undefined>;The checkpoints' timestamps, the most recent first.
Returns
Promise<string[] | undefined>
untag()
untag(timestamp, tag): Promise<void>;Remove a tag from a checkpoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | The checkpoint's timestamp. |
tag | string | The tag. |
Returns
Promise<void>
untagLast()
untagLast(tag): Promise<void>;Remove a tag from the most recent checkpoint.
Parameters
| Parameter | Type | Description |
|---|---|---|
tag | string | The tag. |
Returns
Promise<void>
drop()
static drop(name): Promise<void>;Delete the stores of checkpoints.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The checkpoints' name. |
Returns
Promise<void>
toHumanDate()
static toHumanDate(timestamp): string;Format a timestamp as a French date: DD/MM/YYYY hh:mm:ss.
Parameters
| Parameter | Type | Description |
|---|---|---|
timestamp | string | number | The 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 Parameter | Description |
|---|---|
T | The type of the values. |
Constructors
Constructor
new Storage<T>(name): Storage<T>;Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The store's name. |
Returns
Storage<T>
Properties
Methods
clear()
clear(): Promise<void>;Delete all the values.
Returns
Promise<void>
get()
get(key): Promise<T | null | undefined>;Read a value.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The value's key. |
Returns
Promise<T | null | undefined>
The value (null if there is none, undefined before ready).
iterate()
iterate(callback): Promise<void>;Call a function on each value.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | (a) => void | The function. |
Returns
Promise<void>
keys()
keys(): Promise<string[] | undefined>;The keys of the values.
Returns
Promise<string[] | undefined>
length()
length(): Promise<number>;The number of values.
Returns
Promise<number>
remove()
remove(key): Promise<void>;Delete a value.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The value's key. |
Returns
Promise<void>
set()
set(key, value): Promise<void>;Write a value.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The value's key. |
value | T | The value. |
Returns
Promise<void>
drop()
static drop(name): Promise<void>;Delete a store.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The store's name. |
Returns
Promise<void>