Created
May 28, 2012 10:02
-
-
Save tatat/2818285 to your computer and use it in GitHub Desktop.
データをもにょもにょ
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
(function(g, undefined) { | |
var __create = Object.create ? function(o) { | |
return Object.create(o); | |
} : function (o) { | |
function F() {} | |
F.prototype = o; | |
return new F(); | |
} | |
var __next_tick = function(fn) { | |
setTimeout(fn, 0); | |
}; | |
// https://gist.github.com/1838843 | |
var EventManager = g.EventManager; | |
var DataStore = function(store) { | |
EventManager.call(this); | |
this.__store = store || '__data'; | |
this[this.__store] = {}; | |
}; | |
DataStore.prototype = __create(EventManager.prototype); | |
DataStore.prototype.get = function(key, cb) { | |
var self = this | |
, store = this[this.__store] | |
, value; | |
if (typeof store[key] !== 'undefined') { | |
value = store[key]; | |
__next_tick(function() { | |
cb.call(self, value); | |
}); | |
return this; | |
} else { | |
return this.once('--key ' + key, function(e, value) { | |
cb.call(self, value); | |
}); | |
} | |
}; | |
DataStore.prototype.get_next = function(key, cb) { | |
var self = this | |
, store = this[this.__store]; | |
return this.once('--key ' + key, function(e, value) { | |
cb.call(self, value); | |
}); | |
}; | |
DataStore.prototype.set = function(key, cb) { | |
var self = this | |
, store = this[this.__store]; | |
if (typeof store[key] !== 'undefined') | |
delete store[key]; | |
__next_tick(function() { | |
cb.call(self, function(value) { | |
store[key] = value; | |
self.emit('--key ' + key, value); | |
}); | |
}); | |
return this; | |
}; | |
DataStore.prototype.unset = function(key, cb) { | |
var self = this | |
, store = this[this.__store]; | |
__next_tick(function() { | |
var value; | |
if (typeof store[key] !== 'undefined') { | |
value = store[key]; | |
delete store[key]; | |
} else { | |
value = null; | |
} | |
cb && cb.call(self, value); | |
}); | |
return this; | |
}; | |
DataStore.prototype.get_sync = function(key, default_value) { | |
var store = this[this.__store]; | |
return typeof store[key] !== 'undefined' ? store[key] : | |
default_value != null ? default_value : | |
null; | |
}; | |
DataStore.prototype.set_sync = function(key, value) { | |
var store = this[this.__store]; | |
store[key] = value; | |
return this.emit('--key ' + key, value); | |
}; | |
DataStore.prototype.unset_sync = function(key) { | |
var store = this[this.__store] | |
, value; | |
if (typeof store[key] !== 'undefined') | |
delete store[key]; | |
return this; | |
}; | |
DataStore.prototype.get_all = function(/* [key..], cb */) { | |
var self = this | |
, store = this[this.__store] | |
, result = {} | |
, args | |
, cb; | |
if (arguments[0] && arguments[0].constructor === Array) { | |
args = arguments[0]; | |
cb = arguments[1]; | |
} else { | |
args = Array.prototype.slice.call(arguments); | |
cb = args.pop(); | |
} | |
for (var i = 0, j = args.length, count = 0; i < j; i ++) { | |
(function(key) { | |
self.get(key, function(value) { | |
result[key] = value; | |
-- count === 0 && cb.call(self, result); | |
}); | |
count ++; | |
})(args[i]); | |
} | |
if (i === 0) { | |
__next_tick(function() { | |
cb.call(self, result); | |
}); | |
} | |
return this; | |
}; | |
DataStore.prototype.get_all_new = function(/* [key..], cb */) { | |
var self = this | |
, store = this[this.__store] | |
, args | |
, cb | |
, oncomplete = function() { | |
for (var i = 0, j = args.length, result = {}, a; i < j; i ++) { | |
a = args[i]; | |
result[a] = store[a] != null ? store[a] : null; | |
} | |
cb.call(self, result); | |
}; | |
if (arguments[0] && arguments[0].constructor === Array) { | |
args = arguments[0]; | |
cb = arguments[1]; | |
} else { | |
args = Array.prototype.slice.call(arguments); | |
cb = args.pop(); | |
} | |
for (var i = 0, j = args.length, count = 0; i < j; i ++) { | |
self.get(args[i], function(value) { | |
-- count === 0 && oncomplete(); | |
}); | |
count ++; | |
} | |
if (i === 0) | |
__next_tick(oncomplete); | |
return this; | |
}; | |
DataStore.prototype.get_all_sync = function(/* [key..] */) { | |
var store = this[this.__store] | |
, args = arguments[0] && arguments[0].constructor === Array ? | |
arguments[0] : Array.prototype.slice.call(arguments) | |
, result = {}; | |
for (var i = 0, j = args.length, a; i < j; i ++) { | |
a = args[i]; | |
result[a] = store[a] != null ? store[a] : null; | |
} | |
return result; | |
}; | |
DataStore.prototype.each = function(cb) { | |
var store = this[this.__store]; | |
for (var n in store) { | |
if (false === cb.call(this, store[n], n)) | |
break; | |
} | |
return this; | |
}; | |
g.DataStore = DataStore; | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment