Skip to content

Commit

Permalink
_.handleDefaultCallback()
Browse files Browse the repository at this point in the history
  • Loading branch information
undoZen committed Aug 19, 2014
1 parent 4905144 commit af860bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
11 changes: 1 addition & 10 deletions src/ListenerMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ module.exports = {
var unsubscribe = listenable.listen(callback, this);
this.subscriptions.push(unsubscribe);

if (defaultCallback && _.isFunction(defaultCallback)) {
if (listenable.getDefaultData && _.isFunction(listenable.getDefaultData)) {
data = listenable.getDefaultData();
if (data && data.then && _.isFunction(data.then)) {
data.then(defaultCallback.bind(this));
} else {
defaultCallback.bind(this)(data);
}
}
}
_.handleDefaultCallback(this, listenable, defaultCallback);
},

componentWillUnmount: function() {
Expand Down
11 changes: 1 addition & 10 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ module.exports = function(definition) {
if (this.hasListener(listenable)) {
throw Error("Store cannot listen to this listenable because of circular loop");
}
if (defaultCallback && _.isFunction(defaultCallback)) {
if (listenable.getDefaultData && _.isFunction(listenable.getDefaultData)) {
data = listenable.getDefaultData();
if (data && data.then && _.isFunction(data.then)) {
data.then(defaultCallback.bind(this));
} else {
defaultCallback.bind(this)(data);
}
}
}
_.handleDefaultCallback(this, listenable, defaultCallback);
this.registered.push(listenable);
return listenable.listen(callback, this);
};
Expand Down
23 changes: 18 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* order to remove the dependency
*/

var isObject = module.exports.isObject = function(obj) {
var isObject = exports.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};

module.exports.extend = function(obj) {
exports.extend = function(obj) {
if (!isObject(obj)) {
return obj;
}
Expand All @@ -22,11 +22,24 @@ module.exports.extend = function(obj) {
return obj;
};

module.exports.isFunction = function(value) {
var isFunction = exports.isFunction = function(value) {
return typeof value === 'function';
};

module.exports.EventEmitter = require('eventemitter3');
module.exports.nextTick = function(callback) {
exports.EventEmitter = require('eventemitter3');
exports.nextTick = function(callback) {
setTimeout(callback, 0);
};

exports.handleDefaultCallback = function (listener, listenable, defaultCallback) {
if (defaultCallback && isFunction(defaultCallback)) {
if (listenable.getDefaultData && isFunction(listenable.getDefaultData)) {
data = listenable.getDefaultData();
if (data && data.then && isFunction(data.then)) {
data.then(defaultCallback.bind(listener));
} else {
defaultCallback.bind(listener)(data);
}
}
}
};

0 comments on commit af860bb

Please sign in to comment.