69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
// if node
|
|
var BinaryPack = require('binarypack');
|
|
// end node
|
|
|
|
var util = {
|
|
inherits: function(ctor, superCtor) {
|
|
ctor.super_ = superCtor;
|
|
ctor.prototype = Object.create(superCtor.prototype, {
|
|
constructor: {
|
|
value: ctor,
|
|
enumerable: false,
|
|
writable: true,
|
|
configurable: true
|
|
}
|
|
});
|
|
},
|
|
extend: function(dest, source) {
|
|
for(var key in source) {
|
|
if(source.hasOwnProperty(key)) {
|
|
dest[key] = source[key];
|
|
}
|
|
}
|
|
return dest;
|
|
},
|
|
pack: BinaryPack.pack,
|
|
unpack: BinaryPack.unpack,
|
|
// if node
|
|
isNode: true,
|
|
// end node
|
|
setZeroTimeout: (function(global) {
|
|
// if node
|
|
|
|
return process.nextTick;
|
|
|
|
// end node
|
|
var timeouts = [];
|
|
var messageName = 'zero-timeout-message';
|
|
|
|
// Like setTimeout, but only takes a function argument. There's
|
|
// no time argument (always zero) and no arguments (you have to
|
|
// use a closure).
|
|
function setZeroTimeoutPostMessage(fn) {
|
|
timeouts.push(fn);
|
|
global.postMessage(messageName, '*');
|
|
}
|
|
|
|
function handleMessage(event) {
|
|
if (event.source == global && event.data == messageName) {
|
|
if (event.stopPropagation) {
|
|
event.stopPropagation();
|
|
}
|
|
if (timeouts.length) {
|
|
timeouts.shift()();
|
|
}
|
|
}
|
|
}
|
|
if (global.addEventListener) {
|
|
global.addEventListener('message', handleMessage, true);
|
|
} else if (global.attachEvent) {
|
|
global.attachEvent('onmessage', handleMessage);
|
|
}
|
|
return setZeroTimeoutPostMessage;
|
|
}(this))
|
|
};
|
|
|
|
// if node
|
|
module.exports = util;
|
|
// end node
|