Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out some code from nstore.js.
We're getting ready for an even bigger refactor.
- Loading branch information
1 parent
58118cd
commit eca6711
Showing
6 changed files
with
222 additions
and
169 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
// This is my proto library but without changing Object.prototype | |||
// Then only sub-objects of Class have the special properties. | |||
var Class = module.exports = Object.create(Object.prototype, { | |||
// Implements a forEach much like the one for Array.prototype.forEach, but for | |||
// any object. | |||
forEach: {value: function forEach(callback, thisObject) { | |||
var keys = Object.keys(this); | |||
var length = keys.length; | |||
for (var i = 0; i < length; i++) { | |||
var key = keys[i]; | |||
callback.call(thisObject, this[key], key, this); | |||
} | |||
}}, | |||
// Implements a map much like the one for Array.prototype.map, but for any | |||
// object. Returns an array, not a generic object. | |||
map: {value: function map(callback, thisObject) { | |||
var accum = []; | |||
var keys = Object.keys(this); | |||
var length = keys.length; | |||
for (var i = 0; i < length; i++) { | |||
var key = keys[i]; | |||
accum[i] = callback.call(thisObject, this[key], key, this); | |||
} | |||
return accum; | |||
}}, | |||
// Implement extend for easy prototypal inheritance | |||
extend: {value: function extend(obj) { | |||
obj.__proto__ = this; | |||
return obj; | |||
}}, | |||
// Implement new for easy self-initializing objects | |||
new: {value: function () { | |||
var obj = Object.create(this); | |||
if (obj.initialize) obj.initialize.apply(obj, arguments); | |||
return obj; | |||
}} | |||
}); |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
var Class = require('./class'); | |||
|
|||
// If a large number of writes gets queued up, the shift call normally | |||
// eats all the CPU. This implementes a faster queue. | |||
var Queue = module.exports = Class.extend({ | |||
initialize: function initialize() { | |||
if (this === Queue) throw new Error("Cann't use Queue directly"); | |||
this.tail = []; | |||
this.head = Array.prototype.slice.call(arguments); | |||
this.offset = 0; | |||
// Lock the object down | |||
Object.seal(this); | |||
return this; | |||
}, | |||
shift: function shift() { | |||
if (this.offset === this.head.length) { | |||
var tmp = this.head; | |||
tmp.length = 0; | |||
this.head = this.tail; | |||
this.tail = tmp; | |||
this.offset = 0; | |||
if (this.head.length === 0) return; | |||
} | |||
return this.head[this.offset++]; | |||
}, | |||
push: function push(item) { | |||
return this.tail.push(item); | |||
}, | |||
get length() { | |||
return this.head.length - this.offset + this.tail.length; | |||
} | |||
}); |
Oops, something went wrong.