Creates a new instance of Immstruct, having it's own list of Structure instances.
var ImmstructInstance = require('immstruct').Immstruct;
var immstruct = new ImmstructInstance();
var structure = immstruct.get({ data: });
property | type | description |
---|---|---|
instances |
Array. | Array of Structure instances. |
Returns Immstruct
,
Gets or creates a new instance of {Structure}. Provide optional key to be able to retrieve it from list of instances. If no key is provided, a random key will be generated.
var immstruct = require('immstruct');
var structure = immstruct.get('myStruct', { foo: 'Hello' });
param | type | description |
---|---|---|
[key] |
string | optional: - defaults to random string |
[data] |
Object,Immutable | optional: - defaults to empty data |
Returns Structure
,
Get list of all instances created.
param | type | description |
---|---|---|
[name] |
string | optional: - Name of the instance to get. If undefined get all instances |
Returns Structure,Object.<String, Structure>
,
Clear the entire list of Structure
instances from the Immstruct
instance. You would do this to start from scratch, freeing up memory.
var immstruct = require('immstruct');
immstruct.clear();
Remove one Structure
instance from the Immstruct instances list.
Provided by key
var immstruct = require('immstruct');
immstruct('myKey', { foo: 'hello' });
immstruct.remove('myKey');
param | type | description |
---|---|---|
key |
string |
Returns boolean
,
Gets or creates a new instance of Structure
with history (undo/redo)
activated per default. Same usage and signature as regular Immstruct.get
.
Provide optional key to be able to retrieve it from list of instances. If no key is provided, a random key will be generated.
Provide optional limit to cap the last number of history references that will be kept. Once limit is reached, a new history record shifts off the oldest record. The default if omitted is Infinity. Setting to 0 is the as not having history enabled in the first place.
var immstruct = require('immstruct');
var structure = immstruct.withHistory('myStruct', 10, { foo: 'Hello' });
var structure = immstruct.withHistory(10, { foo: 'Hello' });
var structure = immstruct.withHistory('myStruct', { foo: 'Hello' });
var structure = immstruct.withHistory({ foo: 'Hello' });
param | type | description |
---|---|---|
[key] |
string | optional: - defaults to random string |
[limit] |
number | optional: - defaults to Infinity |
[data] |
Object,Immutable | optional: - defaults to empty data |
Returns Structure
,
This is a default instance of Immstruct
as well as a shortcut for
creating Structure
instances (See Immstruct.get
and Immstruct
).
This is what is returned from require('immstruct')
.
From Immstruct.get
:
Gets or creates a new instance of {Structure} in the default Immstruct
instance. A link to immstruct.get()
. Provide optional
key to be able to retrieve it from list of instances. If no key
is provided, a random key will be generated.
var immstruct = require('immstruct');
var structure = immstruct('myStruct', { foo: 'Hello' });
var structure2 = immstruct.withHistory({ bar: 'Bye' });
immstruct.remove('myStruct');
// ...
param | type | description |
---|---|---|
[key] |
string | optional: - defaults to random string |
[data] |
Object,Immutable | optional: - defaults to empty data |
Returns Structure,Function
,
Creates a new Structure
instance. Also accessible through
Immstruct.Structre
.
A structure is also an EventEmitter object, so it has methods as
.on
, .off
, and all other EventEmitter methods.
For the swap
event, the root structure (see structure.current
) is passed
as arguments, but for type specific events (add
, change
and delete
), the
actual changed value is passed.
For instance:
var structure = new Structure({ 'data': { 'foo': { 'bar': 'hello' } } });
structure.on('swap', function (newData, oldData, keyPath) {
keyPath.should.eql(['foo', 'bar']);
newData.toJS().should.eql({ 'foo': { 'bar': 'bye' } });
oldData.toJS().should.eql({ 'foo': { 'bar': 'hello' } });
});
structure.cursor(['foo', 'bar']).update(function () {
return 'bye';
});
But for change
var structure = new Structure({ 'data': { 'foo': { 'bar': 'hello' } } });
structure.on('change', function (newData, oldData, keyPath) {
keyPath.should.eql(['foo', 'bar']);
newData.should.eql('bye');
oldData.should.eql('hello');
});
structure.cursor(['foo', 'bar']).update(function () {
return 'bye';
});
All keyPath
s passed to listeners are the full path to where the actual
change happened
var Structure = require('immstruct/structure');
var s = new Structure({ data: { foo: 'bar' }});
// Or:
// var Structure = require('immstruct').Structure;
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:newStructure
,oldStructure
,keyPath
.next-animation-frame
: Same asswap
, but only emitted on animation frame. Could use with many render updates and better performance. Callback is passed arguments:newStructure
,oldStructure
,keyPath
.change
: Emitted when data/value is updated and it existed before. Emits values:newValue
,oldValue
andpath
.delete
: Emitted when data/value is removed. Emits value:removedValue
andpath
.add
: Emitted when new data/value is added. Emits value:newValue
andpath
.any
: With the same semantics asadd
,change
ordelete
,any
is triggered for all types of changes. Differs from swap in the arguments that it is passed. Is passednewValue
(or undefined),oldValue
(or undefined) and fullkeyPath
. New and old value are the changed value, not relative/scoped to the reference path as withswap
.
{
key: string, // Defaults to random string
data: Object|Immutable, // defaults to empty Map
history: boolean, // Defaults to false
historyLimit: number, // If history enabled, Defaults to Infinity
}
param | type | description |
---|---|---|
[options] |
{ key: string , data: Object , history: boolean } |
optional: - defaults to random key and empty data (immutable structure). No history |
|
property | type | description |
---|---|---|
history |
Immutable.List | Immutable.List with history. |
current |
Object,Immutable | Provided data as immutable data |
key |
string | Generated or provided key. |
|
Returns Structure
,
Create a Immutable.js Cursor for a given path
on the current
structure (see Structure.current
).
Changes made through created cursor will cause a swap
event to happen (see Events
).
This method returns a Immutable.js Cursor. See the Immutable.js docs for more info on how to use cursors.
var Structure = require('immstruct/structure');
var s = new Structure({ data: { foo: 'bar', a: { b: 'foo' } }});
s.cursor().set('foo', 'hello');
s.cursor('foo').update(function () { return 'Changed'; });
s.cursor(['a', 'b']).update(function () { return 'bar'; });
See more examples in the tests
param | type | description |
---|---|---|
[path] |
string,Array. | optional: - defaults to empty string. Can be array for path. See Immutable.js Cursors |
Returns Cursor
, Gives a Cursor from Immutable.js
Creates a reference. A reference can be a pointer to a cursor, allowing you to create cursors for a specific path any time. This is essentially a way to have "always updated cursors" or Reference Cursors. See example for better understanding the concept.
References also allow you to listen for changes specific for a path.
var structure = immstruct({
someBox: { message: 'Hello World!' }
});
var ref = structure.reference(['someBox']);
var unobserve = ref.observe(function () {
// Called when data the path 'someBox' is changed.
// Also called when the data at ['someBox', 'message'] is changed.
});
// Update the data using the ref
ref.cursor().update(function () { return 'updated'; });
// Update the data using the initial structure
structure.cursor(['someBox', 'message']).update(function () { return 'updated again'; });
// Remove the listener
unobserve();
See more examples in the readme
param | type | description |
---|---|---|
`[path | cursor]` | string,Array.,Cursor |
|
Returns Reference
,
Observe for changes on a reference. On references you can observe for changes, but a reference is not an EventEmitter it self.
The passed keyPath
for swap events are relative to the reference, but
Note: As on swap
for normal immstruct events, the passed arguments for
the event is the root, not guaranteed to be the actual changed value.
The structure is how ever scoped to the path passed in to the reference.
All values passed to the eventlistener for the swap event are relative
to the path used as key path to the reference.
For instance:
var structure = immstruct({ 'foo': { 'bar': 'hello' } });
var ref = structure.reference('foo');
ref.observe(function (newData, oldData, keyPath) {
keyPath.should.eql(['bar']);
newData.toJS().should.eql({ 'bar': 'updated' });
oldData.toJS().should.eql({ 'bar': 'hello' });
});
ref.cursor().update(['bar'], function () { return 'updated'; });
For type specific events, how ever, the actual changed value is passed, not the root data. In these cases, the full keyPath to the change is passed.
For instance:
var structure = immstruct({ 'foo': { 'bar': 'hello' } });
var ref = structure.reference('foo');
ref.observe('change', function (newValue, oldValue, keyPath) {
keyPath.should.eql(['foo', 'bar']);
newData.should.eql('updated');
oldData.should.eql('hello');
});
ref.cursor().update(['bar'], function () { return 'updated'; });
var ref = structure.reference(['someBox']);
var unobserve = ref.observe('delete', function () {
// Called when data the path 'someBox' is removed from the structure.
});
See more examples in the readme
swap
: Emitted when any cursor is updated (new information is set). Triggered in any data swap is made on the structure. One use case for this is to re-render design components. Data passed as arguments are scoped/relative to the path passed to the reference, this also goes for keyPath. Callback is passed arguments:newStructure
,oldStructure
,keyPath
.change
: Emitted when data/value is updated and it existed before. Emits values:newValue
,oldValue
andpath
.delete
: Emitted when data/value is removed. Emits value:removedValue
andpath
.add
: Emitted when new data/value is added. Emits value:newValue
andpath
.any
: With the same semantics asadd
,change
ordelete
,any
is triggered for all types of changes. Differs from swap in the arguments that it is passed. Is passednewValue
(or undefined),oldValue
(or undefined) and fullkeyPath
. New and old value are the changed value, not relative/scoped to the reference path as withswap
.
param | type | description |
---|---|---|
[eventName] |
string | optional: - Type of change |
callback |
Function | - Callback when referenced data is swapped |
Returns Function
, Function for removing observer (unobserve)
Create a new, updated, cursor from the base path provded to the reference. This returns a Immutable.js Cursor as the regular cursor method. You can also provide a sub-path to create a reference in a deeper level.
var ref = structure.reference(['someBox']);
var cursor = ref.cursor('someSubPath');
var cursor2 = ref.cursor();
See more examples in the readme
param | type | description |
---|---|---|
[subpath] |
string | optional: - Subpath to a deeper structure |
Returns Cursor
, Immutable.js cursor
Creates a reference on a lower level path. See creating normal references.
var structure = immstruct({
someBox: { message: 'Hello World!' }
});
var ref = structure.reference('someBox');
var newReference = ref.reference('message');
See more examples in the readme
param | type | description |
---|---|---|
[path] |
string,Array. | optional: - defaults to empty string. Can be array for path. See Immutable.js Cursors |
Returns Reference
,
Remove all observers from reference.
Returns Void
,
Destroy reference. Unobserve all observers, set all endpoints of reference to dead. For cleaning up memory.
Returns Void
,
Force emitting swap event. Pass on new, old and keypath passed to swap.
If newData is null
current will be used.
param | type | description |
---|---|---|
newData |
Object | - Immutable object for the new data to emit |
oldData |
Object | - Immutable object for the old data to emit |
keyPath |
string | - Structure path (in tree) to where the changes occured. |
Returns Void
,
Undo IFF history is activated and there are steps to undo. Returns new current immutable structure.
Will NOT emit swap when redo. You have to do this yourself.
Define number of steps to undo in param.
param | type | description |
---|---|---|
steps |
number | - Number of steps to undo |
Returns Object
, New Immutable structure after undo
Redo IFF history is activated and you can redo. Returns new current immutable structure. Define number of steps to redo in param. Will NOT emit swap when redo. You have to do this yourself.
param | type | description |
---|---|---|
head |
number | - Number of steps to head to in redo |
Returns Object
, New Immutable structure after redo
Undo IFF history is activated and passed structure
exists in history.
Returns the same immutable structure as passed as argument.
Will NOT emit swap after undo. You have to do this yourself.
param | type | description |
---|---|---|
structure |
Object | - Immutable structure to redo until |
Returns Object
, New Immutable structure after undo