Skip to content

Commit

Permalink
go
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz committed Jul 22, 2012
1 parent a9a9124 commit 56d93bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 171 deletions.
2 changes: 2 additions & 0 deletions public/fast.html
Expand Up @@ -2,6 +2,8 @@
<!-- saved from url=(0020)http://1x.com/photos -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="binarypack.js"></script>
<script src="binary.js"></script>
<script src="imgs.js"></script>
<script>

Expand Down
190 changes: 19 additions & 171 deletions public/imgs.js
Expand Up @@ -5,161 +5,6 @@
* Eric Zhang
*/

/**
* EventEmitter class
* Creates an object with event registering and firing methods
*/
function EventEmitter() {
// Initialise required storage variables
this._events = {};
}

var isArray = Array.isArray;


EventEmitter.prototype.addListener = function(type, listener, scope, once) {
if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function');
}

// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
this.emit('newListener', type, typeof listener.listener === 'function' ?
listener.listener : listener);

if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
} else if (isArray(this._events[type])) {

// If we've already got an array, just append.
this._events[type].push(listener);

} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
}

};

EventEmitter.prototype.on = EventEmitter.prototype.addListener;

EventEmitter.prototype.once = function(type, listener, scope) {
if ('function' !== typeof listener) {
throw new Error('.once only takes instances of Function');
}

var self = this;
function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
};

g.listener = listener;
self.on(type, g);

return this;
};

EventEmitter.prototype.removeListener = function(type, listener, scope) {
if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function');
}

// does not use listeners(), so no side effect of creating _events[type]
if (!this._events[type]) return this;

var list = this._events[type];

if (isArray(list)) {
var position = -1;
for (var i = 0, length = list.length; i < length; i++) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener))
{
position = i;
break;
}
}

if (position < 0) return this;
list.splice(position, 1);
if (list.length == 0)
delete this._events[type];
} else if (list === listener ||
(list.listener && list.listener === listener))
{
delete this._events[type];
}

return this;
};


EventEmitter.prototype.off = EventEmitter.prototype.removeListener;


EventEmitter.prototype.removeAllListeners = function(type) {
if (arguments.length === 0) {
this._events = {};
return this;
}

// does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null;
return this;
};

EventEmitter.prototype.listeners = function(type) {
if (!this._events[type]) this._events[type] = [];
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
}
return this._events[type];
};

EventEmitter.prototype.emit = function(type) {
var type = arguments[0];
var handler = this._events[type];
if (!handler) return false;

if (typeof handler == 'function') {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
var l = arguments.length;
var args = new Array(l - 1);
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
handler.apply(this, args);
}
return true;

} else if (isArray(handler)) {
var l = arguments.length;
var args = new Array(l - 1);
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];

var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
} else {
return false;
}
};



var util = {
inherits: function(ctor, superCtor) {
Expand Down Expand Up @@ -220,17 +65,25 @@ function ImgsClient(url, options) {

options = util.extend({}, options);

this.worker = [];
this.worker.push(new Worker('worker.js'));
//this.worker.push(new Worker('worker.js'));
this._client = new BinaryClient('ws://academyready.com:8085');

this._client.on('open', function(){
console.log('open');


});

this._client.on('stream', function(stream){

stream.on('data', function(data){
for(var i = 0, ii = data.length; i < ii; i++) {
var id = data[i].id;
self._images[id].src = self._URL.createObjectURL(new Blob([data[i].data]));
}
});

});

for(var i = 0; i < this.worker.length; i++) {
this.worker[i].onmessage = function(event){
var command = event.data;
var img = self._images[command.id];
img.src = self._cache[command.src] = command.data;
};
}

this._cache = {};

Expand All @@ -245,7 +98,6 @@ function ImgsClient(url, options) {
};


util.inherits(ImgsClient, EventEmitter);

ImgsClient.prototype._init = function(){
// Initial load
Expand All @@ -264,13 +116,9 @@ ImgsClient.prototype._init = function(){
}
}
}
this.worker[0].postMessage(data);
this._client.send(data,{type: 'request'});
};


ImgsClient.prototype._createObjectURL = function(data){
return this._URL.createObjectURL(new Blob(data));
};

exports.ImgsClient = ImgsClient;
})(this);

0 comments on commit 56d93bd

Please sign in to comment.