Skip to content

Commit

Permalink
Added toJS method to nodes. Improved performance on refreshing parent…
Browse files Browse the repository at this point in the history
…s. Removed legacy curxor path code
  • Loading branch information
arqex committed Feb 15, 2015
1 parent 1f9e5b4 commit b0f3765
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 31 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ shouldComponentUpdate: function( nextProps ){

*Freezer* has less features than *Immutable*, but its API is simpler and it is much more lightweight (Minified, Immutable is ~56KB and Freezer ~7KB).

Instead on learning the set of methods needed to use *Immutable*, *Freezer* uses common JS objects and arrays to store the data, so you can start using it right now.
Instead on learning the set of methods needed to use *Immutable*, *Freezer* uses common JS objects and array-like objects to store the data, so you can start using it right now.

## API

Expand Down Expand Up @@ -181,7 +181,7 @@ console.log( store.get().c ); // false

#### Events

Every time that the data is updated, an `update` event is triggered on the cursor. In order to use those events use `on`, `once` and `off` methods provided by the Listener API that the curxor object implements.
Every time that the data is updated, an `update` event is triggered on the cursor. In order to use those events use `on`, `once` and `off` methods provided by the [listener API](#listener-api) that the curxor object implements.

## Update methods

Expand All @@ -207,7 +207,7 @@ Arrays and hashes can update their children using the `set` method. It accepts a
var store = new Freezer({obj: {a:'hola', b:'adios'}, arr: [1,2]});

// Updating using a hash
store.getdata().obj.set( {b:'bye', c:'ciao'} );
store.get().obj.set( {b:'bye', c:'ciao'} );

// Updating using key and value
store.get().arr.set( 0, 0 );
Expand All @@ -216,6 +216,19 @@ store.get().arr.set( 0, 0 );
console.log( store.get() )
```

#### toJS()
*Freezer* nodes are immutable, and array ones are not pure JS arrays but objects that inherit from arrays. `toJS` transform *Freezer* nodes to plain mutable JS objects in case you need them.
```js
// Require node.js assert
var assert = require('assert');

var data = {obj: {a:'hola', b:'adios'}, arr: [1,2]},
store = new Freezer( data )
;

assert.deepEqual( data, store.get().toJS ); // Ok
```

## Hash methods
#### remove( keyOrKeys )
Removes elements from a hash node. It accepts a string or an array with the names of the strings to remove.
Expand Down Expand Up @@ -287,6 +300,10 @@ Can unregister all callbacks from a listener if the `eventName` parameter is omi
Trigger an event on the listener. All the extra parameters will be passed to the registered callbacks.

## Changelog
###v0.3.3
Improved: Performance on refreshing parent nodes on update.
Added: `toJS` and `toJSON` methods on nodes to get a pure JS object from a node.
Removed: Path legacy code from curxor.js
###v0.3.2
Fixed: Chained calls should trigger update with the value of all operations. https://github.com/arqex/freezer/issues/2
###v0.3.1
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "freezer-js",
"version": "0.3.2",
"version": "0.3.3",
"homepage": "https://github.com/arqex/freezer",
"authors": [
"arqex"
Expand Down
46 changes: 34 additions & 12 deletions build/freezer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* freezer-js v0.3.2 (14-2-2015)
/* freezer-js v0.3.3 (15-2-2015)
* https://github.com/arqex/freezer
* By arqex
* License: GNU-v2
Expand Down Expand Up @@ -189,6 +189,12 @@ var emitterProto = {
// hashmaps
var Emitter = Utils.createNonEnumerable( emitterProto );


/**
* Creates non-enumerable property descriptors, to be used by Object.create.
* @param {Object} attrs Properties to create descriptors
* @return {Object} A hash with the descriptors.
*/
var createNE = function( attrs ){
var ne = {};

Expand All @@ -215,14 +221,34 @@ var commonMethods = {

return this.__.notify( 'replace', this, attrs );
},
getPaths: function( attrs ){
return this.__.notify( 'path', this );
},

getListener: function(){
return this.__.notify( 'listener', this );
},

toJS: function(){
var js;
if( this.constructor == Array ){
js = new Array( this.length );
}
else {
js = {};
}

Utils.each( this, function( child, i ){
if( child && child.__ )
js[ i ] = child.toJS();
else
js[ i ] = child;
});

return js;
}
};

// Implement toJSON in order to mimic JS objects on `JSON.stringify`
commonMethods.toJSON = commonMethods.toJS;

var FrozenArray = Object.create( Array.prototype, createNE( Utils.extend({
push: function( el ){
return this.append( [el] );
Expand Down Expand Up @@ -571,7 +597,8 @@ var Frozen = {
i
;

this.trigger( newChild, 'update', newChild );
if( __.listener )
this.trigger( newChild, 'update', newChild );

if( !__.parents.length ){
if( __.listener ){
Expand Down Expand Up @@ -621,12 +648,11 @@ var Frozen = {

trigger: function( node, eventName, param ){
var listener = node.__.listener,
ticking = listener && listener.ticking
ticking = listener.ticking
;

listener.ticking = param;

if( listener && !ticking ){
if( !ticking ){
Utils.nextTick( function(){
var updated = listener.ticking;
listener.ticking = false;
Expand Down Expand Up @@ -660,10 +686,6 @@ var Freezer = function( initialValue ) {
var frozen;

var notify = function notify( eventName, node, options ){

if( eventName == 'path' )
return Frozen.getPaths( frozen, node );

if( eventName == 'listener' )
return Frozen.createListener( node );

Expand Down
4 changes: 2 additions & 2 deletions build/freezer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "freezer-js",
"version": "0.3.2",
"version": "0.3.3",
"description": "A tree data structure that is always updated from the root, making easier to think in a reactive way.",
"main": "freezer.js",
"homepage": "https://github.com/arqex/freezer",
Expand Down
4 changes: 0 additions & 4 deletions src/freezer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ var Freezer = function( initialValue ) {
var frozen;

var notify = function notify( eventName, node, options ){

if( eventName == 'path' )
return Frozen.getPaths( frozen, node );

if( eventName == 'listener' )
return Frozen.createListener( node );

Expand Down
8 changes: 4 additions & 4 deletions src/frozen.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ var Frozen = {
i
;

this.trigger( newChild, 'update', newChild );
if( __.listener )
this.trigger( newChild, 'update', newChild );

if( !__.parents.length ){
if( __.listener ){
Expand Down Expand Up @@ -319,12 +320,11 @@ var Frozen = {

trigger: function( node, eventName, param ){
var listener = node.__.listener,
ticking = listener && listener.ticking
ticking = listener.ticking
;

listener.ticking = param;

if( listener && !ticking ){
if( !ticking ){
Utils.nextTick( function(){
var updated = listener.ticking;
listener.ticking = false;
Expand Down
Loading

0 comments on commit b0f3765

Please sign in to comment.