Sha256: a5729bb7f94c9cb5210715d91982e49587d4ffc71abdb83efe75f59f53c2ada5

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

/*global define: true EventSource: true */
define(['riffle'], function (riffle) {
  'use strict';

  return function streams(ajaxUri) {
    var stream = riffle.stream
      , each
      , jsonEvents
      ;

    function ajaxStream(url, args) {
      return stream(function (o) {
        $.get(url, args, function (d) {
          o(JSON.parse(d));
        });
      });
    }

    each = stream(function (o, i) {
      i.forEach(function (item) { o(item); });
    });

    function batched(delay, maxSize) {
      var batch = []
        , timer
        ;

      delay = delay || 100;
      maxSize = maxSize || 100;

      function clear(o) {
        if (batch.length > 0) {
          o(batch.splice(0, maxSize));
        }

        if (batch.length < 1) {
          clearInterval(timer);
          timer = null;
        }
      }

      return stream(function (o, i) {
        batch = batch.concat(i);

        if (!timer) { timer = setInterval(function () { clear(o); }, delay); }
      });
    }

    jsonEvents = each.input(ajaxStream(ajaxUri).invoke());

    return { example: batched(10, 1000).input(jsonEvents) };
  };
});

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rspectacles-0.5.0 lib/rspectacles/app/public/js/exampleStream.js
rspectacles-0.4.2 lib/rspectacles/app/public/js/exampleStream.js
rspectacles-0.4.1 lib/rspectacles/app/public/js/exampleStream.js
rspectacles-0.4.0 lib/rspectacles/app/public/js/exampleStream.js