immstruct2.0.0 • Public • Published
This documentation is for the current unreleased
v2.0.0. See the
v1.4.1 docs in the git history
See the API References for more documentation and usage.
// someFile.jsvar immstruct = ;var structure = ;// Use event `swap` or `next-animation-frame`structure;var cursor = structure;// Update the value at the cursor. As cursors are immutable,// this returns a new cursor that points to the new datavar newCursor = cursor;// We unwrap the cursor, by getting the data it is pointing at using deref// and see that the value of the old `cursor` to is still `1`console; //=> 1// `newCursor` points to the new dataconsole; //=> 2
Note: The cursors you see here are cursors from Facebooks Immutable.js library. Read the complete API in their repo.
// anotherFile.jsvar immstruct = ;var structure = ;var cursor = structure;var updatedCursor = cursor;// Unwrap the valueconsole; //=> 3
While Immutable.js cursors are immutable, Immstruct lets you create references to a piece of data from where cursors will always be fresh. The cursors you create are still immutable, but you have the ability to retrieve the newest and latest cursor pointing to a specific part of your immutable structure.
var structure = ;var ref = structure;console //=> 'bar'var oldCursor = structure;console //=> 'bar'var newCursor = structure;console //=> 'updated';// You don't need to manage and track fresh/stale cursors.// A reference cursor will do it for you.console //=> 'updated'
Updating a cursor created from a reference will also update the underlying structure.
This offers benefits similar to that of Om's
reference cursors, where
React.js or Omniscient components can observe pieces of application
state without it being passed as cursors in props from their parent components.
References also allow for listeners that fire when their path or the path of sub-cursors change:
var structure =;var ref = structure;var unobserve = ref;// Update the data using the refref;// Update the data using the initial structurestructure;// Remove the listener;
Parents' change listeners are also called when sub-cursors are changed.
Cursors created from references are still immutable. If you keep a cursor from
var cursor = reference.cursor() around, the
cursor will still point to the data
at time of cursor creation. Updating it may rewrite newer information.
// optionalKey and/or optionalLimit can be omitted from the callvar optionalLimit = 10; // only keep last 10 of history, default Infinityvar structure = immstruct;console; //=> 'bar'structure;console; //=> 'hello'structure;console; //=> 'bar'structure;console; //=> 'hello'
Creates or retrieves structures.
var structure =// Creates new structure with someKey
var structure =// Get's the structure named `someKey`.
Note: if someKey doesn't exist, an empty structure is created
var structure =var randomGeneratedKey = structurekey;// Creates a new structure with random key// Used if key is not necessary
var structure =var randomGeneratedKey = structurekey;// Create new empty structure with random key
You can also create your own instance of Immstruct, isolating the different instances of structures:
var localImmstruct =var structure = localImmstruct;
See API Reference.
A Structure object is an event emitter and emits the following events:
swap: Emitted when cursor is updated (new information is set). Is emitted on all types of changes, additions and deletions. The passed structures are always the root structure. One use case for this is to re-render design components. Callback is passed arguments:
next-animation-frame: Same as
swap, but only emitted on animation frame. Could use with many render updates and better performance. Callback is passed arguments:
change: Emitted when data/value is updated and it existed before. Emits values:
delete: Emitted when data/value is removed. Emits value:
add: Emitted when new data/value is added. Emits value:
any: With the same semantics as
anyis triggered for all types of changes. Differs from swap in the arguments that it is passed. Is passed
oldValue(or undefined) and full
keyPath. New and old value are the changed value, not relative/scoped to the reference path as with
NOTE: If you update cursors via
Cursor.set, and if the
underlying Immutable collection is not inherently changed,
events will not be emitted, neither will the history (if any) be applied.