Comparing version 0.4.6 to 0.5.0
178
bjorling.js
var path = require('path') | ||
, _ = require('underscore') | ||
, handlers = {} | ||
, keys = require('./bjorling-keys') | ||
, filters = require('./filters') | ||
, storage = require('./bjorling-storage') | ||
, dataDir | ||
, subscriptionFactory | ||
, xtend = require('xtend') | ||
function join(projectionName, key) { | ||
return storage.getByKeySync(projectionName, key) | ||
} | ||
function Bjorling(filename) { | ||
function Bjorling(filename, opts) { | ||
if(!(this instanceof Bjorling)) { | ||
return new Bjorling(filename) | ||
return new Bjorling(filename, opts) | ||
} | ||
this._handlers = {} | ||
this._key = opts.key | ||
this._projectionName = path.basename(filename, path.extname(filename)) | ||
this._handlers = {} | ||
this._subscription = subscriptionFactory() | ||
this._storage = opts.storage(this._projectionName, opts.key) | ||
this._transformers = {} | ||
} | ||
Bjorling.prototype.execute = function(handler, eventData, position) { | ||
var projectionName = this._projectionName | ||
, key = keys(projectionName, eventData) | ||
, filter | ||
function executeHandler(projectionName, state) { | ||
var context = { | ||
join: join | ||
, remove: remove | ||
} | ||
, initialState = {} | ||
, result | ||
function remove() { | ||
storage.remove(projectionName, state) | ||
} | ||
state = state || initialState | ||
try { | ||
result = handler.call(context, state, eventData) | ||
if(!result && state === initialState) { | ||
return | ||
} | ||
result = result || state | ||
storage.eventResult(projectionName, position, result, function(err) { | ||
if(err) console.log(err) | ||
}) | ||
} | ||
catch(ex) { | ||
console.error('Error in bjorling handler: ', { | ||
name: projectionName | ||
, key: key | ||
, filter: filter | ||
, state: state | ||
, eventData: eventData | ||
}) | ||
} | ||
} | ||
try { | ||
if(key) { | ||
return executeHandler(projectionName, storage.getState(projectionName, key)) | ||
} | ||
filter = filters(projectionName, eventData) | ||
if(filter) { | ||
return executeHandler(projectionName, storage.getState(projectionName, filter)) | ||
} | ||
} | ||
catch(ex) { | ||
console.log(key, filter, ex.stack) | ||
} | ||
Bjorling.prototype.addIndex = function(index, cb) { | ||
this._storage.addIndex(index, cb) | ||
} | ||
Bjorling.prototype.getAll = function(cb) { | ||
storage.getAll(this._projectionName, cb) | ||
Bjorling.prototype.when = function(handlers) { | ||
this._handlers = handlers//xtend(this._handlers, handlers) | ||
} | ||
Bjorling.prototype.getByKey = function(key, cb) { | ||
storage.getByKey(this._projectionName, key, cb) | ||
Bjorling.prototype.transform = function(transformers) { | ||
this._transformers = transformers | ||
} | ||
Bjorling.prototype.getIndex = function(index, key) { | ||
return storage.getIndex(this._projectionName, index, key) | ||
} | ||
Bjorling.prototype.processEvent = function(anEvent, cb) { | ||
var eventType = anEvent.__type | ||
, handlers = this._handlers | ||
, handler = handlers[eventType] | ||
, transformers = this._transformers | ||
, transformer = transformers[eventType] | ||
, storage = this._storage | ||
, eventData = anEvent.data | ||
if(!handler) return cb && cb() | ||
if(transformer) { | ||
eventData = transformer(eventData) | ||
} | ||
Bjorling.prototype.load = function() { | ||
storage.load(this._projectionName) | ||
} | ||
storage.get(eventData, function(err, state) { | ||
if(!state) { | ||
var keyVal = storage.getKeyValue(eventData) | ||
if(!keyVal) return cb && cb() | ||
Bjorling.prototype.when = function(handlerObj) { | ||
this._handlers = handlerObj | ||
} | ||
var $new = handlers['$new'] | ||
if($new) { | ||
state = $new(eventData) | ||
} | ||
} | ||
state = state || {} | ||
Bjorling.prototype.where = function(filterObj, cb) { | ||
storage.filter(this._projectionName, filterObj, cb) | ||
var stateToSave = handler.call(null, state, eventData) | ||
stateToSave = stateToSave || state | ||
storage.save(stateToSave, cb) | ||
}) | ||
} | ||
Bjorling.prototype.setFilter = function(filter) { | ||
filters.add(this._projectionName, filter) | ||
function isObject(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Object]' | ||
} | ||
Bjorling.prototype.setIndex = function(index, key, val) { | ||
return storage.setIndex(this._projectionName, index, key, val) | ||
} | ||
Bjorling.prototype.get = function(queryObj, cb) { | ||
var query | ||
if(isObject(queryObj)) { | ||
query = queryObj | ||
} else { | ||
query = {} | ||
query[this._key] = queryObj | ||
} | ||
Bjorling.prototype.setKey = function(key) { | ||
keys.add(this._projectionName, key) | ||
this._storage.get(query, cb) | ||
} | ||
Bjorling.prototype.start = function() { | ||
var me = this | ||
storage.initialLoad(this._projectionName, function(err, lastProcessedPosition) { | ||
var sub = me._subscription.replay(lastProcessedPosition + 1) | ||
sub.on('event', function(evt) { | ||
var handler = me._handlers[evt.__type] | ||
if(handler) { | ||
me.execute(handler, evt.evt, evt.position) | ||
} | ||
}) | ||
}) | ||
} | ||
module.exports = Bjorling | ||
module.exports.on = function() { | ||
var args = [].slice.call(arguments, 0) | ||
storage.on.apply(storage, args) | ||
} | ||
module.exports.init = function(opts) { | ||
subscriptionFactory = opts.subscriptionFactory | ||
if(opts.http) { | ||
storage.setHttp(opts.http) | ||
} | ||
} | ||
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) | ||
}) | ||
} | ||
module.exports.storage = storage |
{ | ||
"name": "bjorling" | ||
, "author": "Brian Mavity <brian@brianmavity.com> (http://www.brianmavity.com)" | ||
, "repository": { | ||
"name": "bjorling", | ||
"author": "Brian Mavity <brian@brianmavity.com> (http://www.brianmavity.com)", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bmavity/bjorling.git" | ||
} | ||
, "bugs": { | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/bmavity/bjorling/issues" | ||
} | ||
, "contributors": [ | ||
}, | ||
"contributors": [ | ||
"Brian Mavity <brian@brianmavity.com> (http://www.brianmavity.com)" | ||
] | ||
, "description": "Projection builder" | ||
, "dependencies": { | ||
"atomic-write": "0.1.x" | ||
, "eventemitter2": "0.4.x" | ||
, "sarastro": "0.0.x" | ||
, "underscore": "1.4.x" | ||
], | ||
"description": "Projection builder", | ||
"dependencies": { | ||
"argp": "0.1.x", | ||
"atomic-write": "0.1.x", | ||
"eventemitter2": "0.4.x", | ||
"sarastro": "0.0.x", | ||
"underscore": "1.4.x" | ||
}, | ||
"devDependencies": { | ||
"karma-script-launcher": "~0.1.0", | ||
"karma-chrome-launcher": "~0.1.0", | ||
"karma-firefox-launcher": "~0.1.0", | ||
"karma-html2js-preprocessor": "~0.1.0", | ||
"karma-jasmine": "~0.1.3", | ||
"karma-requirejs": "~0.1.0", | ||
"karma-coffee-preprocessor": "~0.1.0", | ||
"karma-phantomjs-launcher": "~0.1.0", | ||
"karma": "~0.10.2", | ||
"mocha": "~1.12.1", | ||
"should": "1.x.x", | ||
"karma-mocha": "~0.1.0" | ||
}, | ||
"engines": { | ||
"node": "0.8.x, 0.10.x" | ||
} | ||
, "devDependencies": { | ||
, "scripts": { | ||
"test": "mocha -r should -R tap test/**/*.js" | ||
, "watch": "mocha -w -r should -R spec test/**/*.js" | ||
} | ||
, "engines": { | ||
"node": "0.8.x, 0.10.x" | ||
} | ||
, "keywords": [ | ||
] | ||
, "browser": { | ||
, "keywords": [], | ||
"browser": { | ||
"./bjorling-storage.js": "./bjorling-storage-browser.js" | ||
} | ||
, "license": "mit +no-false-attribs, +no-advertising license" | ||
, "main": "./bjorling.js" | ||
, "version": "0.4.6" | ||
} | ||
}, | ||
"license": "mit +no-false-attribs, +no-advertising license", | ||
"main": "./bjorling.js", | ||
"version": "0.5.0" | ||
} |
# bjorling | ||
## A Blog Engine Targeting node.js | ||
## A Projection Library for node.js and the browser |
35483
17
1055
5
12
+ Addedargp@0.1.x
+ Addedargp@0.1.3(transitive)