Skip to content

Commit

Permalink
[release] 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jordangarcia committed Jul 23, 2015
1 parent 6685c12 commit 5d2fd58
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 1.1.0 (proposed)
## 1.1.0

- **[NEW]** added `Reactor#serialize`, `Reactor#loadState`, `Store#serialize` and `Store#deserialize` methods
- **[NEW]** added `Reactor#batch` to allow batch dispatches before notify observers
- **[NEW]** throw error when trying to dispatch within a dispatch
- **[FIXED]** fix Evaluator locking if getter evaluation errors

### API Additions
Expand Down
123 changes: 85 additions & 38 deletions dist/nuclear.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ return /******/ (function(modules) { // webpackBootstrap

/**
* Returns true if the value is an ImmutableJS data structure
* or a javascript primitive that is immutable (stirng, number, etc)
* or a JavaScript primitive that is immutable (string, number, etc)
* @param {*} obj
* @return {boolean}
*/
Expand All @@ -124,7 +124,7 @@ return /******/ (function(modules) { // webpackBootstrap
* Can be called on any type
*/
function toJS(arg) {
// arg instanceof Immutable.Sequence is unreleable
// arg instanceof Immutable.Sequence is unreliable
return (isImmutable(arg))
? arg.toJS()
: arg
Expand Down Expand Up @@ -5100,7 +5100,7 @@ return /******/ (function(modules) { // webpackBootstrap
return objectToString(val) === '[object Array]'
}

// taken from underscore source to account for browser descrepency
// taken from underscore source to account for browser discrepancy
/* istanbul ignore if */
if (typeof /./ !== 'function' && typeof Int8Array !== 'object') {
/**
Expand All @@ -5123,7 +5123,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

/**
* Checks if the passed in value is af type Object
* Checks if the passed in value is of type Object
* @param {*} val
* @return {boolean}
*/
Expand Down Expand Up @@ -5274,7 +5274,7 @@ return /******/ (function(modules) { // webpackBootstrap


/**
* In Nuclear Reactors are where state is stored. Reactors
* State is stored in NuclearJS Reactors. Reactors
* contain a 'state' object which is an Immutable.Map
*
* The only way Reactors can change state is by reacting to
Expand Down Expand Up @@ -5308,8 +5308,13 @@ return /******/ (function(modules) { // webpackBootstrap
*/
this.__changeObserver = new ChangeObserver(this.state, this.__evaluator)

this.__isBatching = false;
this.__batchDispatchCount = 0;
// keep track of the depth of batch nesting
this.__batchDepth = 0
// number of dispatches in the top most batch cycle
this.__batchDispatchCount = 0

// keep track if we are currently dispatching
this.__isDispatching = false
}

/**
Expand Down Expand Up @@ -5363,13 +5368,36 @@ return /******/ (function(modules) { // webpackBootstrap
* @param {object|undefined} payload
*/
Object.defineProperty(Reactor.prototype,"dispatch",{writable:true,configurable:true,value:function(actionType, payload) {"use strict";
if (this.__batchDepth === 0) {
if (this.__isDispatching) {
this.__isDispatching = false
throw new Error('Dispatch may not be called while a dispatch is in progress')
}
this.__isDispatching = true
}

var prevState = this.state
this.state = this.__handleAction(prevState, actionType, payload)

if (this.__isBatching) {
try {
this.state = this.__handleAction(prevState, actionType, payload)
} catch (e) {
this.__isDispatching = false
throw e
}


if (this.__batchDepth > 0) {
this.__batchDispatchCount++
} else if (this.state !== prevState) {
this.__notify()
} else {
if (this.state !== prevState) {
try {
this.__notify()
} catch (e) {
this.__isDispatching = false
throw e
}
}
this.__isDispatching = false
}
}});

Expand Down Expand Up @@ -5429,7 +5457,10 @@ return /******/ (function(modules) { // webpackBootstrap
var serialized = {}
this.__stores.forEach(function(store, id) {
var storeState = this.state.get(id)
serialized[id] = store.serialize(storeState)
var serializedState = store.serialize(storeState)
if (serializedState !== undefined) {
serialized[id] = serializedState
}
}.bind(this))
return serialized
}});
Expand All @@ -5442,7 +5473,10 @@ return /******/ (function(modules) { // webpackBootstrap
each(state, function(serializedStoreState, storeId) {
var store = this.__stores.get(storeId)
if (store) {
stateToLoad.set(storeId, store.deserialize(serializedStoreState))
var storeState = store.deserialize(serializedStoreState)
if (storeState !== undefined) {
stateToLoad.set(storeId, storeState)
}
}
}.bind(this))
}.bind(this))
Expand Down Expand Up @@ -5484,7 +5518,6 @@ return /******/ (function(modules) { // webpackBootstrap
this.__changeObserver.notifyObservers(this.state)
}});


/**
* Reduces the current state to the new state given actionType / message
* @param {string} actionType
Expand All @@ -5497,15 +5530,23 @@ return /******/ (function(modules) { // webpackBootstrap
logging.dispatchStart(actionType, payload)
}

// let each core handle the message
// let each store handle the message
this.__stores.forEach(function(store, id) {
var currState = state.get(id)
var newState = store.handle(currState, actionType, payload)
var newState

try {
newState = store.handle(currState, actionType, payload)
} catch(e) {
// ensure console.group is properly closed
logging.dispatchError(e.message)
throw e
}

if (this.debug && newState === undefined) {
var error = 'Store handler must return a value, did you forget a return statement'
logging.dispatchError(error)
throw new Error(error)
var errorMsg = 'Store handler must return a value, did you forget a return statement'
logging.dispatchError(errorMsg)
throw new Error(errorMsg)
}

state.set(id, newState)
Expand All @@ -5522,19 +5563,24 @@ return /******/ (function(modules) { // webpackBootstrap
}});

Object.defineProperty(Reactor.prototype,"__batchStart",{writable:true,configurable:true,value:function() {"use strict";
if (this.__isBatching) {
throw new Error('Reactor already in batch mode')
}
this.__isBatching = true
this.__batchDepth++
}});

Object.defineProperty(Reactor.prototype,"__batchEnd",{writable:true,configurable:true,value:function() {"use strict";
if (!this.__isBatching) {
throw new Error('Reactor is not in batch mode')
}

if (this.__batchDispatchCount > 0) {
this.__notify()
this.__batchDepth--

if (this.__batchDepth <= 0) {
if (this.__batchDispatchCount > 0) {
// set to true to catch if dispatch called from observer
this.__isDispatching = true
try {
this.__notify()
} catch (e) {
this.__isDispatching = false
throw e
}
this.__isDispatching = false
}
this.__batchDispatchCount = 0
}
}});
Expand Down Expand Up @@ -5644,7 +5690,7 @@ return /******/ (function(modules) { // webpackBootstrap
}});

/**
* Specify an getter and a change handler fn
* Specify a getter and a change handler function
* Handler function is called whenever the value of the getter changes
* @param {Getter} getter
* @param {function} handler
Expand Down Expand Up @@ -5900,9 +5946,10 @@ return /******/ (function(modules) { // webpackBootstrap
throw new Error('Evaluate may not be called within a Getters computeFn')
}

var evaluatedValue
__applyingComputeFn = true
try {
var evaluatedValue = getComputeFn(keyPathOrGetter).apply(null, args)
evaluatedValue = getComputeFn(keyPathOrGetter).apply(null, args)
__applyingComputeFn = false
} catch (e) {
__applyingComputeFn = false
Expand Down Expand Up @@ -5962,7 +6009,7 @@ return /******/ (function(modules) { // webpackBootstrap
* @param {Getter}
*/
Object.defineProperty(Evaluator.prototype,"untrack",{writable:true,configurable:true,value:function(getter) {"use strict";
// TODO: untrack all depedencies
// TODO: untrack all dependencies
}});

Object.defineProperty(Evaluator.prototype,"reset",{writable:true,configurable:true,value:function() {"use strict";
Expand Down Expand Up @@ -6053,7 +6100,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

/**
* This method is overriden by extending classses to setup message handlers
* This method is overridden by extending classes to setup message handlers
* via `this.on` and to set up the initial state
*
* Anything returned from this function will be coerced into an ImmutableJS value
Expand Down Expand Up @@ -6086,7 +6133,7 @@ return /******/ (function(modules) { // webpackBootstrap

/**
* Pure function taking the current state of store and returning
* the new state after a Nuclear reactor has been reset
* the new state after a NuclearJS reactor has been reset
*
* Overridable
*/
Expand All @@ -6102,23 +6149,23 @@ return /******/ (function(modules) { // webpackBootstrap
}});

/**
* Serializes store state to plain JSON serializable javascript
* Serializes store state to plain JSON serializable JavaScript
* Overridable
* @param {*}
* @return {*}
*/
Object.defineProperty(Store.prototype,"serialize",{writable:true,configurable:true,value:function(state) {"use strict";
return toJS(state);
return toJS(state)
}});

/**
* Deserializes plain javascript to store state
* Deserializes plain JavaScript to store state
* Overridable
* @param {*}
* @return {*}
*/
Object.defineProperty(Store.prototype,"deserialize",{writable:true,configurable:true,value:function(state) {"use strict";
return toImmutable(state);
return toImmutable(state)
}});


Expand Down
6 changes: 3 additions & 3 deletions dist/nuclear.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuclear-js",
"version": "1.0.5",
"version": "1.1.0",
"description": "Immutable, reactive Flux architecture. UI Agnostic.",
"main": "dist/nuclear.js",
"scripts": {
Expand Down

0 comments on commit 5d2fd58

Please sign in to comment.