Skip to content

Commit

Permalink
refactor to use feature store event wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
brooswit committed Oct 1, 2017
1 parent 932e220 commit ea63eab
Show file tree
Hide file tree
Showing 6 changed files with 630 additions and 33 deletions.
42 changes: 11 additions & 31 deletions feature_store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
var deepEqual = require('deep-equal');
// An in-memory feature store with an async interface.
// It's async as other implementations (e.g. the RedisFeatureStore)
// may be async, and we want to retain interface compatibility.
var noop = function(){};
function InMemoryFeatureStore(emitter) {
function InMemoryFeatureStore() {
var store = {flags:{}};

store.get = function(key, cb) {
Expand Down Expand Up @@ -40,64 +39,45 @@ function InMemoryFeatureStore(emitter) {

store.init = function(flags, cb) {
cb = cb || noop;

var oldFlags = this.flags;
this.flags = flags;

for (var key in oldFlags) {
if (oldFlags.hasOwnProperty(key)) {
differ(key, oldFlags[key], flags[key]);
}
}

this.init_called = true;
cb();
}

store.delete = function(key, version, cb) {
cb = cb || noop;

var oldFlag = this.flags[key];

if (this.flags.hasOwnProperty(key)) {
if (oldFlag && oldFlag.version < version) {
oldFlag.deleted = true;
oldFlag.version = version;
this.flags[key] = oldFlag;
var old = this.flags[key];
if (old && old.version < version) {
old.deleted = true;
old.version = version;
this.flags[key] = old;
}
} else {
this.flags[key] = oldFlag;
this.flags[key] = old;
}

differ(key, oldFlag, {});

cb();
}

store.upsert = function(key, flag, cb) {
cb = cb || noop;

var oldFlag = this.flags[key];
cb = cb || noop;
var old = this.flags[key];

if (this.flags.hasOwnProperty(key)) {
if (oldFlag && oldFlag.version < flag.version) {
var old = this.flags[key];
if (old && old.version < flag.version) {
this.flags[key] = flag;
}
} else {
this.flags[key] = flag;
}

differ(key, oldFlag, flag);

cb();
}

function differ(key, oldValue, newValue) {
if(deepEqual(oldValue, newValue)) return;
emitter.emit("update", newValue);
emitter.emit(`update:${key}`, oldValue, newValue);
}

store.initialized = function() {
return this.init_called === true;
}
Expand Down
52 changes: 52 additions & 0 deletions feature_store_event_wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function FeatureStoreEventWrapper(featureStore, emitter) {
function differ(key, oldValue, newValue) {
if(newValue.version < oldValue.version) return;
emitter.emit("update", newValue);
emitter.emit(`update:${key}`, oldValue, newValue);
}

return {
get: featureStore.get,
all: featureStore.all,
initialized: featureStore.initialized,
close: featureStore.close,

init: function(newFlags, callback) {
featureStore.all(function(oldFlags){
featureStore.init(newFlags, function(){
var allFlags = newFlags.concat(oldFlags);
var handledFlags = {};

for (var key in allFlags) {
if(handledFlags[key]) continue;
differ(key, oldFlags[key], flags[key]);
handledFlags[key] = true;
;
}

callback && callback.apply(null, arguments);
});
});
},

delete: function(key, version, callback) {
featureStore.get(function(oldFlag) {
featureStore.delete(key, version, function() {
differ(key, oldFlag, {});
callback && callback.apply(null, arguments);
});
});
},

upsert: function(key, flag, callback) {
featureStore.get(function(oldFlag) {
featureStore.upsert(key, newFlag, function() {
differ(key, oldFlag, newFlag);
callback && callback.apply(null, arguments);
});
});
}
}
}

module.exports = FeatureStoreEventWrapper;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var requestify = require('requestify');
var FeatureStoreEventWrapper = require('./feature_store_event_wrapper');
var InMemoryFeatureStore = require('./feature_store');
var RedisFeatureStore = require('./redis_feature_store');
var Requestor = require('./requestor');
Expand Down Expand Up @@ -87,7 +88,9 @@ var new_client = function(sdk_key, config) {
]
})
);
config.feature_store = config.feature_store || InMemoryFeatureStore(client);

var featureStore = config.feature_store || InMemoryFeatureStore();
config.feature_store = FeatureStoreEventWrapper(featureStore, client);

var maybeReportError = createErrorReporter(client, config.logger);

Expand Down
Loading

0 comments on commit ea63eab

Please sign in to comment.