$ cnpm install shared
Kevin Jones (https://twitter.com/hutkev)
Shared is an interface for managing shared objects. In this version the only supported store is MongoDB. The primary interface uses direct object manipulation to make it trivially easy to use.
store.apply(function(db) {
if (db.savings > amount) {
db.saving -= amount;
db.current += amount;
if (db.history === undefined)
db.history = [];
db.history.push({transfer: amount, from: 'saving', to: 'current' });
}
}, function (err) {
// Error handling
});
To make changes to shared objects you call apply on a store. Your callback is passed a db object which is the (initially empty) root object of a graph of nodes that you can create to store whatever data.
When apply invokes the callbacks your changes will have been committed to the store (and in this version to MongoDB) to allow others to see them. Applying changes is atomic, either they are all applied or none are.
var shared = require('shared');
var store = shared.createStore(options);
The options are:
The collection does not have to be for the exclusive use of shared but for safety it may be better to use a dedicated collection.
store.close();
If you don't close the stores then your process will not exit.
Shared performs updates optimistically on a locally cached version of the shared objects. If it is later determined that the cache is out of date with respect to the data stored in MongoDB your callback will be re-run.
This means you should be careful to avoid writing code that has side effects in a callback, such as logging to the console. The way to handle this is by returning a value from the callback which will be passed to the second argument of the error callback.
store.apply(function(db) {
return db.current;
}, function(err, ret) {
console.log('Balance is ' + ret);
)};
Exceptions thrown within the apply are caught automatically and passed to the error callback, if one has been provided. Any changes made prior to an exception been thrown will not be committed and so exceptions provide a way to terminate an operation abnormally.
The distribution includes some examples you may want to review:
The code is licensed under the Apache License 2.0.
The original source is written in TypeScript. The distribution you receive may only contain JavaScript derived from this while the library is being developed.
The version of rsvp in node_modules has been modified slightly for better exception handling. All other dependancies are unchanged.
Copyright 2013 - present © cnpmjs.org | Home |