Module: mathsync/json

mathsync/json

Summarizer deserializing JSON content.

When synchroniwing a client and a server, the client asks a JSON summary to the server and then has to deserialize it.

Source:

Methods

<static> newSummarizer(producer, digester, selector) → {summarizer}

Deserializes JSON views of summaries, likely obtained throught the network.
Parameters:
Name Type Argument Description
producer function the producer of JSON summaries, returns promises resolving to JSON content.
digester Digest~Digester <optional>
the digester to use, defaults to SHA-1.
selector BucketSelector~Selector <optional>
how to place items in IBF buckets, uses 3 buckets by default.
Source:
Returns:
a summarizer returning deserialized summaries.
Type
summarizer
Examples

From an HTTP endpoint using XMLHttpRequest

var Promise = require('mathsync/promise'); // polyfill
function fetchSummary(level) {
 var p = new Promise(function (resolve, reject) {
   var req, url = 'http://localhost:4000/api/summary/' + level;
   function ready() {
     if (req.status === 200) {
       resolve(req.responseText);
     } else {
       reject(new Error('Failed to get summary from ' + url));
     }
   }
   function stateChange() {
     if (req.readyState === 4) {
       ready();
     }
   }
   req = new XMLHttpRequest();
   req.onreadystatechange = stateChange;
   req.open('GET', url);
   req.send(null);
 });
 return p.then(JSON.parse);
}
var summarizer = require('mathsync/json').newSummarizer(fetchSummary);

From an HTTP endpoint using jQuery

var Promise = require('mathsync/src/promise'); // polyfill
function fetchSummary(level) {
 return Promise.resolve($.getJSON('http://localhost:4000/api/summary/' + level));
}
var summarizer = require('mathsync/json').newSummarizer(fetchSummary);

From an HTTP endpoint using Node's http

var Promise = require('mathsync/src/promise'); // polyfill
var http = require('http');
function fetchSummary(level) {
 var p = new Promise(function (resolve, reject) {
   http.get('http://localhost:4000/api/summary/' + level, function (res) {
     var chunks = [];
     res.on('data', function(chunk) {
       chunks.push(chunk);
     });
     res.on('end', function() {
       resolve(chunks);
     });
   }).on('error', reject);
 });
 return p.then(Buffer.concat).then(JSON.parse);
}
var summarizer = require('mathsync/json').newSummarizer(fetchSummary);