Skip to content
Bertrand Laporte edited this page Jul 27, 2018 · 5 revisions

Hibe - Immutable data without pain

hibe is library to create immutable data objects through a 'mutable' api

Core concepts are explained in the main readme file. The present pages give a more detailed presentation of the APIs exposed to the developers

General helper functions

function isMutating(d:any): boolean

Tell if an object is being mutated

function isImmutable(d:any): boolean

Tell if an object is immutable (i.e. if it has already been mutated and cannot be mutated again, in which case lastVersion() should be called to retrieve a mutable version)

function isDataset(d:any): boolean

Tell if an object is a dataset

function lastVersion<T>(dataNode: T): T

Return the last version of an object (note: this is not necessarily the direct next version). Last version is always mutable.

async function mutationComplete<T>(dataset: T): Promise<T>

Return a promise that will be fulfilled when the mutation of the dataset passes as argument is completed (or immediately if the dataset is not being mutated)

@Dataset
class TestNode {
    @string() value;
}
let o = new TestNode();
console.log(isMutating(o));  // print false
console.log(isDataset(o));   // print true
o.value = "foo";
console.log(isMutating(o));  // print true
console.log(isImmutable(o)); // print false

let o2 = await mutationComplete(o);
console.log(isImmutable(o)); // print true
console.log(isMutating(o));  // print false
console.log(isMutating(o2)); // print false
console.log(lastVersion(o) === o2); // print true

function watch(d: any, fn: (any) => void): ((any) => void)

Register a callback that will be called when a new version of a given dataset is spawn. The same callback will be called for any subsequent version (no need to re-register) - until unwatch() is called

function unwatch(d: any, watchFn: ((any) => void) | null)

Unregister a watch callback

let node = initNewArrTestNode();

let watchRef = watch(node, (newNode) => {
  node = newNode;
  // do something - e.g. UI refresh
});

unwatch(node, watchRef);

Clone this wiki locally