This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from yahoo/factoryClass
[resolves #53] Change API to factory methods rather than exporting class
- Loading branch information
Showing
17 changed files
with
616 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2014, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
'use strict'; | ||
|
||
var util = require('util'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var CHANGE_EVENT = 'change'; | ||
|
||
/** | ||
* @class BaseStore | ||
* @extends EventEmitter | ||
* @param dispatcher The dispatcher interface | ||
* @constructor | ||
*/ | ||
function BaseStore(dispatcher) { | ||
this.dispatcher = dispatcher; | ||
this._hasChanged = false; | ||
if (this.initialize) { | ||
this.initialize(); | ||
} | ||
} | ||
|
||
util.inherits(BaseStore, EventEmitter); | ||
|
||
/** | ||
* Convenience method for getting the store context object. | ||
* @method getContext | ||
* @return {Object} Returns the store context object. | ||
*/ | ||
BaseStore.prototype.getContext = function getContext() { | ||
return this.dispatcher.getContext(); | ||
}; | ||
|
||
/** | ||
* Add a listener for the change event | ||
* @method addChangeListener | ||
* @param {Function} callback | ||
*/ | ||
BaseStore.prototype.addChangeListener = function addChangeListener(callback) { | ||
this.on(CHANGE_EVENT, callback); | ||
}; | ||
|
||
/** | ||
* Remove a listener for the change event | ||
* @method removeChangeListener | ||
* @param {Function} callback | ||
*/ | ||
BaseStore.prototype.removeChangeListener = function removeChangeListener(callback) { | ||
this.removeListener(CHANGE_EVENT, callback); | ||
}; | ||
|
||
/** | ||
* Determines whether the store should dehydrate or not. By default, only dehydrates | ||
* if the store has emitted an update event. If no update has been emitted, it is assumed | ||
* that the store is in its default state and therefore does not need to dehydrate. | ||
* @method shouldDehydrate | ||
* @returns {boolean} | ||
*/ | ||
BaseStore.prototype.shouldDehydrate = function shouldDehydrate() { | ||
return this._hasChanged; | ||
}; | ||
|
||
/** | ||
* Emit a change event | ||
* @method emitChange | ||
* @param {*} param=this | ||
*/ | ||
BaseStore.prototype.emitChange = function emitChange(param) { | ||
this._hasChanged = true; | ||
this.emit(CHANGE_EVENT, param || this); | ||
}; | ||
|
||
module.exports = BaseStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2014, Yahoo! Inc. | ||
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. | ||
*/ | ||
'use strict'; | ||
|
||
var util = require('util'), | ||
BaseStore = require('./BaseStore'), | ||
IGNORE_ON_PROTOTYPE = ['statics', 'storeName', 'handlers', 'mixins']; | ||
|
||
function createChainedFunction(one, two) { | ||
return function chainedFunction() { | ||
one.apply(this, arguments); | ||
two.apply(this, arguments); | ||
}; | ||
} | ||
|
||
function mixInto(dest, src) { | ||
Object.keys(src).forEach(function (prop) { | ||
if (-1 !== IGNORE_ON_PROTOTYPE.indexOf(prop)) { | ||
return; | ||
} | ||
if ('initialize' === prop) { | ||
if (!dest[prop]) { | ||
dest[prop] = src[prop]; | ||
} else { | ||
dest[prop] = createChainedFunction(dest[prop], src[prop]); | ||
} | ||
} else { | ||
if (dest.hasOwnProperty(prop)) { | ||
throw new Error('Mixin property collision for property "' + prop + '"'); | ||
} | ||
dest[prop] = src[prop]; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Helper for creating a store class | ||
* @method createStore | ||
* @param {Object} spec | ||
* @param {String} spec.storeName The name of the store | ||
* @param {Object} spec.handlers Hash of action name to method name of action handlers | ||
* @param {Function} spec.initialize Function called during construction for setting the default state | ||
* @param {Function} spec.dehydrate Function that returns serializable data to send to the client | ||
* @param {Function} spec.rehydrate Function that takes in serializable data to rehydrate the store | ||
*/ | ||
module.exports = function createStore(spec) { | ||
spec.statics = spec.statics || {}; | ||
if (!spec.storeName && !spec.statics.storeName) { | ||
throw new Error('createStore called without a storeName'); | ||
} | ||
var Store = function (dispatcher) { | ||
BaseStore.call(this, dispatcher); | ||
}; | ||
|
||
util.inherits(Store, BaseStore); | ||
|
||
Object.keys(spec.statics).forEach(function (prop) { | ||
Store[prop] = spec.statics[prop]; | ||
}); | ||
|
||
Store.storeName = spec.storeName || Store.storeName; | ||
Store.handlers = spec.handlers || Store.handlers; | ||
Store.mixins = spec.mixins || Store.mixins; | ||
|
||
if (Store.mixins) { | ||
Store.mixins.forEach(function(mixin) { | ||
mixInto(Store.prototype, mixin); | ||
}); | ||
} | ||
mixInto(Store.prototype, spec); | ||
|
||
return Store; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
BaseStore: require('./BaseStore'), | ||
createStore: require('./createStore') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.