Node.js
Full API doc is available.
Server example
Add a dependency towards the library:
{
"dependencies": {
"mathsync": "0.6.2"
}
}
Create a endpoint fetching your items, serializing them and sending the summary over the wire (here done using Koa):
var ms = require('mathsync');
var data = [/* where do your items come from? */];
var serialize = ms.string.newSerializer(function (item) {
/* how to serialize your item to string? */
});
var summarizer = ms.array.newSummarizer(data, serialize);
var app = require('koa')();
var route = require('koa-route');
app.use(route.get('/summary/:level', function* (level) {
this.body = yield summarizer(level | 0);
}));
The endpoint can be extended to expose session-specific summaries. It is possible to build custom serializers not going through a string.
Streams
The library supports the use of Node readable streams emitting all items. Read the specific API doc.
var ms = require('mathsync');
var serialize = ms.string.newSerializer(function (item) {
/* how to serialize your item to string? */
});
var local = ms.stream.newSummarizer(function () {
return new MyDataStream(/* where do your items come from? */);
}, serialize);
Generator
The library supports the use of generator functions as iterator where one is expected to and yield
all items. Read the specific API doc.
var ms = require('mathsync');
var data = {};
var serialize = ms.string.newSerializer();
var local = ms.generator.newSummarizer(function* () {
for (var k in data) {
yield (k + ':' + data[k]);
}
}, serialize);
Client example
Maybe less common, but it can act as a client to another server.
var ms = require('mathsync');
var http = require('http');
var Promise = require('mathsync/src/promise');
var data = [/* where do your items come from? */];
var serialize = ms.string.newSerializer(function (item) {
/* how to serialize your item? */
});
var local = ms.array.newSummarizer(data, serialize);
function fetchSummary(level) {
return new Promise(function (resolve, reject) {
http.get('http://localhost:8080/summary/' + level, function (res) {
var chunks = [];
res.on('data', function(chunk) {
chunks.push(chunk);
});
res.on('end', function() {
resolve(chunks);
});
}).on('error', reject);
}).then(Buffer.concat).then(JSON.parse);
}
var remote = ms.json.newSummarizer(fetchSummary);
var serialize = ms.string.newSerializer(function (item) {
/* how to deserialize your item? */
});
var resolve = ms.array.newSummarizer(data, remote, serialize, deserialize);
and then call it whenever you want to synchronize:
resolve().then(function (difference) {
difference.removed.forEach(function (i) {
/* remove deleted item locally! */
});
difference.added.forEach(function (i) {
/* add new item locally! */
});
});