patternjavascriptMinor
JavascriptObservable Storage
Viewed 0 times
storagejavascriptobservablestackoverflow
Problem
I created this observable storage thing in JavaScript (partly for fun, but also because it came in handy and got bigger without me noticing it).
The repository can be found here.
```
(function(window, undefined){
"use strict";
var eagle = window.eagle = (function() {
var storage = {
feathers : {},
observers : {}
}
function storeFeather(name, data) {
var action;
if ( (name in storage.feathers) ) {
action = 'update';
} else {
action = 'insert';
}
//for now, copletly overwrite the shit out of it
storage.feathers[name] = data;
notifyObservers(name, action);
}
function retrieveFeather(name) {
return storage.feathers[name];
}
function destroyFeather(name) {
delete storage.feathers[name];
notifyObservers(name, 'destroy');
}
function notifyObservers(observerTarget, action) {
if ( !(observerTarget in storage.observers) ) {
return;
}
if ( !(action in storage.observers[observerTarget]) ) {
return;
}
var observers = storage.observers[observerTarget][action];
for ( var i=0, len=observers.length; i<len; i++ ) {
observersi;
}
}
function registerObserver(observerTarget, action, task) {
if ( !(observerTarget in storage.observers) ) {
storage.observers[observerTarget] = [];
}
if ( action instanceof Array ) {
for ( var i=0, len=action.length; i<len; i++ ) {
registerObserverForAction(observerTarget, action[i], task);
}
} else if ( typeof action == 'string' ) {
registerObserverForAction(observerTarget, a
The repository can be found here.
```
(function(window, undefined){
"use strict";
var eagle = window.eagle = (function() {
var storage = {
feathers : {},
observers : {}
}
function storeFeather(name, data) {
var action;
if ( (name in storage.feathers) ) {
action = 'update';
} else {
action = 'insert';
}
//for now, copletly overwrite the shit out of it
storage.feathers[name] = data;
notifyObservers(name, action);
}
function retrieveFeather(name) {
return storage.feathers[name];
}
function destroyFeather(name) {
delete storage.feathers[name];
notifyObservers(name, 'destroy');
}
function notifyObservers(observerTarget, action) {
if ( !(observerTarget in storage.observers) ) {
return;
}
if ( !(action in storage.observers[observerTarget]) ) {
return;
}
var observers = storage.observers[observerTarget][action];
for ( var i=0, len=observers.length; i<len; i++ ) {
observersi;
}
}
function registerObserver(observerTarget, action, task) {
if ( !(observerTarget in storage.observers) ) {
storage.observers[observerTarget] = [];
}
if ( action instanceof Array ) {
for ( var i=0, len=action.length; i<len; i++ ) {
registerObserverForAction(observerTarget, action[i], task);
}
} else if ( typeof action == 'string' ) {
registerObserverForAction(observerTarget, a
Solution
Pretty nice! I have only a few comments:
-
You have a typo: "arguemnts" in your
-
I'd avoid all the extra logic in the returned object. I'd prefer the functions to be doing all the argument handling themselves. I do understand why you'd split up the logic, but at the same time you're, well, splitting up the logic.
-
There should probably be a way to remove observers...
-
You could consider more conventional naming, like
If I do:
It's a minor thing, and as much a question of personal preference, but I find the word "observe" to imply more than what's actually happening here.
-
You have a typo: "arguemnts" in your
throw statements :)-
I'd avoid all the extra logic in the returned object. I'd prefer the functions to be doing all the argument handling themselves. I do understand why you'd split up the logic, but at the same time you're, well, splitting up the logic.
-
There should probably be a way to remove observers...
-
You could consider more conventional naming, like
on or addEventListener for observers. You say "observer", but I'd rather say "event handler", since it's not total value observing.If I do:
var data = { foo: "bar" };
eagle.observe("data", dataChanged);
eagle.store("data", data); // will call dataChanged
data.foo = "baz"; // will *not* call dataChangedIt's a minor thing, and as much a question of personal preference, but I find the word "observe" to imply more than what's actually happening here.
Code Snippets
var data = { foo: "bar" };
eagle.observe("data", dataChanged);
eagle.store("data", data); // will call dataChanged
data.foo = "baz"; // will *not* call dataChangedContext
StackExchange Code Review Q#58515, answer score: 4
Revisions (0)
No revisions yet.