Comparing version 0.0.5 to 0.1.0
var _ = require('underscore') | ||
, events = require('eventemitter2') | ||
, keys = require('./bjorling-keys') | ||
, http = require('./http') | ||
, projections = {} | ||
, dataUrl | ||
@@ -20,7 +22,6 @@ emitter = new events.EventEmitter2() | ||
var result = _.filter(projection, function(state, key) { | ||
if(keys.isKey(key)) return false | ||
return fn(state) | ||
}) | ||
})[0] | ||
process.nextTick(function() { | ||
cb(null, result[0]) | ||
cb(null, result) | ||
}) | ||
@@ -40,2 +41,28 @@ } | ||
function getByKeySync(projectionName, key) { | ||
return getProjection(projectionName)[key] | ||
} | ||
function load(projectionName) { | ||
function handleResponse(res) { | ||
var resData = '' | ||
res.on('data', function(data) { | ||
resData += data | ||
}) | ||
res.on('error', function(err) { | ||
console.log('error', err) | ||
}) | ||
res.on('end', function() { | ||
projections[projectionName] = resData && JSON.parse(resData) | ||
}) | ||
} | ||
process.nextTick(function() { | ||
http.get(dataUrl, { projectionName: projectionName }, handleResponse) | ||
}) | ||
} | ||
function save(projectionName, state, cb) { | ||
@@ -49,7 +76,5 @@ var key = keys(projectionName, state) | ||
function setData(projectionName, data) { | ||
projections[projectionName] = data | ||
} | ||
function update(projectionName, state) { | ||
if(!state) return | ||
function update(projectionName, state) { | ||
var key = keys(projectionName, state) | ||
@@ -64,4 +89,8 @@ , projection = getProjection(projectionName) | ||
module.exports.getByKey = getByKey | ||
module.exports.getByKeySync = getByKeySync | ||
module.exports.load = load | ||
module.exports.save = save | ||
module.exports.setData = setData | ||
module.exports.update = update | ||
module.exports.setDataLocation = function(url) { | ||
dataUrl = url | ||
} |
var _ = require('underscore') | ||
, path = require('path') | ||
, projections = {} | ||
, keys = require('./bjorling-keys') | ||
, dataDir | ||
@@ -27,2 +29,10 @@ function filter(projectionName, fn, cb) { | ||
function getByKeySync(projectionName, key) { | ||
return getProjection(projectionName)[key] | ||
} | ||
function load(projectionName) { | ||
projections[projectionName] = require(path.resolve(dataDir, projectionName)) | ||
} | ||
function save(projectionName, state, cb) { | ||
@@ -35,11 +45,10 @@ var key = keys(projectionName, state) | ||
function setData(projectionName, data) { | ||
projections[projectionName] = data | ||
module.exports.filter = filter | ||
module.exports.getByKey = getByKey | ||
module.exports.getByKeySync = getByKeySync | ||
module.exports.getProjection = getProjection | ||
module.exports.load = load | ||
module.exports.save = save | ||
module.exports.setDataLocation = function(dir) { | ||
dataDir = dir | ||
} | ||
module.exports = { | ||
filter: filter | ||
, getByKey: getByKey | ||
, save: save | ||
, setData: setData | ||
} |
@@ -5,4 +5,9 @@ var path = require('path') | ||
, keys = require('./bjorling-keys') | ||
, filters = require('./filters') | ||
, storage = require('./bjorling-storage') | ||
function join(projectionName, key) { | ||
return storage.getByKeySync(projectionName, key) | ||
} | ||
function handleEvent(eventName, eventData) { | ||
@@ -15,9 +20,32 @@ var matches = handlers[eventName] | ||
, key = keys(projectionName, eventData) | ||
storage.getByKey(projectionName, key, function(err, state) { | ||
function executeHandler(projectionName, state) { | ||
state = state || {} | ||
match.fn(state, eventData) | ||
match.fn.call(join, state, eventData) | ||
storage.save(projectionName, state, function(err) { | ||
console.log(err) | ||
if(err) console.log(err) | ||
}) | ||
}) | ||
} | ||
function processByFilter(projectionName, filter) { | ||
storage.filter(projectionName, filter, function(err, state) { | ||
if(err) return err | ||
executeHandler(projectionName, state) | ||
}) | ||
} | ||
function processByKey(projectionName, key) { | ||
storage.getByKey(projectionName, key, function(err, state) { | ||
if(err) return err | ||
executeHandler(projectionName, state) | ||
}) | ||
} | ||
if(key) { | ||
return processByKey(projectionName, key, eventData) | ||
} | ||
var filter = filters(projectionName, eventData) | ||
if(filter) { | ||
return processByFilter(projectionName, filter) | ||
} | ||
}) | ||
@@ -30,6 +58,2 @@ } | ||
function addHandler(fn, name) { | ||
if(keys.isKey(name)) { | ||
keys.add(projectionName, fn) | ||
return | ||
} | ||
var handler = handlers[name] = handlers[name] || [] | ||
@@ -46,2 +70,6 @@ handler.push({ | ||
function load() { | ||
storage.load(projectionName) | ||
} | ||
function when(handlerObj) { | ||
@@ -55,4 +83,4 @@ _.forEach(handlerObj, addHandler) | ||
function setData(data) { | ||
storage.setData(projectionName, data) | ||
function setFilter(key, filter) { | ||
filters.add(projectionName, key, filter) | ||
} | ||
@@ -66,6 +94,7 @@ | ||
getByKey: getByKey | ||
, load: load | ||
, when: when | ||
, where: where | ||
, setData: setData | ||
, setKey: setKey | ||
, setFilter: setFilter | ||
} | ||
@@ -87,2 +116,16 @@ } | ||
storage.update.apply(storage, args) | ||
} | ||
} | ||
module.exports.setDataLocation = function() { | ||
var args = [].slice.call(arguments, 0) | ||
storage.setDataLocation.apply(storage, args) | ||
} | ||
module.exports.getProjection = function(projectionName, cb) { | ||
process.nextTick(function() { | ||
cb(null, storage.getProjection(projectionName)) | ||
}) | ||
} | ||
module.exports.get = function(projectionName, key, cb) { | ||
process.nextTick(function() { | ||
storage.getByKey(projectionName, key, cb) | ||
}) | ||
} |
@@ -31,3 +31,3 @@ { | ||
, "main": "./bjorling.js" | ||
, "version": "0.0.5" | ||
, "version": "0.1.0" | ||
} |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
10847
11
300
1
1