Permalink
Browse files

Notifications performance tweaks/optimizations

  • Loading branch information...
1 parent c0b998f commit 0138f96a2c16b9d3e3dd5fd87708c7feb92920f3 Taka Kojima committed Feb 11, 2012
Showing with 136 additions and 211 deletions.
  1. +23 −36 lib/minion.class.js
  2. +30 −33 lib/minion.main.js
  3. +83 −142 lib/minion.notifications.js
View
@@ -1,4 +1,4 @@
-(function(){
+(function() {
"use strict";
@@ -11,58 +11,50 @@
__static : {
__isDefined: true
},
-
+
/**
* The base minion Class. All Classes are required to be descendants
* of this class, either directly, or indirectly.
*/
- init: function(){
- if(!this._interestHandlers){
- this._interestHandlers = [];
- }
+ init: function() {
+
},
/**
* Local version of window.setTimeout that keeps scope of <i>this</i>.<br>
*/
- setTimeout : function(func, delay){
+ setTimeout : function(func, delay) {
return setTimeout(this.proxy(func), delay);
},
/**
* Local version of window.setInterval that keeps scope of <i>this</i>.<br>
*/
- setInterval : function(func, delay){
+ setInterval : function(func, delay) {
return setInterval(this.proxy(func), delay);
},
/**
* Shorthand for func.bind(this)
* or rather, $.proxy(func, this) in jQuery terms
*/
- proxy : function(func){
- var bind = function (context) {
- if (!context) {return this;}
- var this_ = this;
- return function() {
- return this_.apply(context, Array.prototype.slice.call(arguments));
- };
- };
-
- return bind.call(func, this);
+ proxy : function(fn) {
+ return minion.proxy(fn, this);
},
/**
* Subscribes to a notification.
*/
- subscribe : function(name, handler, priority){
- if(!this._interestHandlers){
+ subscribe : function(name, handler, priority) {
+ if(!this._interestHandlers) {
this._interestHandlers = [];
}
- if(handler && !this._interestHandlers[name]){
- minion.subscribe(this, name, priority);
+
+ if(handler && !this._interestHandlers[name]) {
+ handler = this.proxy(handler);
+ minion.subscribe(handler, name, priority);
this._interestHandlers[name] = handler;
}
},
@@ -71,21 +63,22 @@
* Unsubscribes from a notification.
*/
- unsubscribe : function(name){
- if(this._interestHandlers && this._interestHandlers[name]){
+ unsubscribe : function(name) {
+ if(this._interestHandlers && this._interestHandlers[name]) {
+ var handler = this._interestHandlers[name];
this._interestHandlers[name] = null;
delete this._interestHandlers[name];
}
- minion.unsubscribe(this, name);
+ minion.unsubscribe(handler, name);
},
/**
* Unsubscribes from all notifications registered via this.subscribe();
*/
- unsubscribeAll : function(){
- for(var interest in this._interestHandlers){
- if(this._interestHandlers.hasOwnProperty(interest)){
+ unsubscribeAll : function() {
+ for(var interest in this._interestHandlers) {
+ if(this._interestHandlers.hasOwnProperty(interest)) {
this.unsubscribe(interest);
}
}
@@ -96,16 +89,10 @@
* Publishes a notification with the specified data.
*/
- publish : function(name, data, callback){
+ publish : function(name, data, callback) {
minion.publish(name, data, this, callback);
- },
-
- handleNotification : function(n){
- var handler = this._interestHandlers[n.name];
- if(handler){
- this.proxy(handler)(n);
- }
}
+
})
});
View
@@ -79,7 +79,7 @@ var minion = (function (root) {
var _waitID;
var _waitingForLoad = [];
var _loadedFiles = [];
- var _notificationManager;
+ var _NotificationManager;
var _waitInterval = 500;
var _isNode = (typeof window === "undefined");
@@ -653,45 +653,42 @@ var minion = (function (root) {
return scope;
};
-
- _minion.enableNotifications = function () {
- if (_minion.isDefined("minion.NotificationManager")) {
- if (!_notificationManager) {
- _notificationManager = new (minion.get("minion.NotificationManager"))();
-
-
- _minion.subscribe = function () {
- _notificationManager.subscribe.apply(_notificationManager, arguments);
+ // Like jQuery .proxy, or ES5 .bind, just a way to make sure it's there cross-browser.
+ _minion.proxy = function (fn, scope) {
+ if(fn) {
+
+ var bind = function (context) {
+ if (!context) {return this;}
+ var this_ = this;
+ return function() {
+ return this_.apply(context, Array.prototype.slice.call(arguments));
};
+ };
-
- _minion.unsubscribe = function () {
- _notificationManager.unsubscribe.apply(_notificationManager, arguments);
- };
+ if(Function.prototype.bind){
+ return fn.bind(scope);
+ }
-
- _minion.publish = function () {
- _notificationManager.publish.apply(_notificationManager, arguments);
- };
+ return bind.call(fn, scope);
+ }
+ return fn;
+ };
- _minion.publishNotification = function () {
- _notificationManager.publishNotification.apply(_notificationManager, arguments);
- };
-
- _minion.holdNotification = function () {
- _notificationManager.holdNotification.apply(_notificationManager, arguments);
- };
+ _minion.enableNotifications = function () {
+ if (_minion.isDefined("minion.NotificationManager")) {
+ if (!_NotificationManager) {
-
- _minion.releaseNotification = function () {
- _notificationManager.releaseNotification.apply(_notificationManager, arguments);
- };
+ var NM = _NotificationManager = (minion.get("minion.NotificationManager"));
+
+ _minion.subscribe = _minion.proxy(NM.subscribe, NM);
+ _minion.unsubscribe = _minion.proxy(NM.unsubscribe, NM);
+ _minion.publish = _minion.proxy(NM.publish, NM);
+ _minion.publishNotification = _minion.proxy(NM.publishNotification, NM);
+ _minion.holdNotification = _minion.proxy(NM.holdNotification, NM);
+ _minion.releaseNotification = _minion.proxy(NM.releaseNotification, NM);
+ _minion.cancelNotification = _minion.proxy(NM.cancelNotification, NM);
-
- _minion.cancelNotification = function () {
- _notificationManager.cancelNotification.apply(_notificationManager, arguments);
- };
}
}
};
Oops, something went wrong.

0 comments on commit 0138f96

Please sign in to comment.