Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the window operator object into the Temporal functions. #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/eep.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ Temporal.make = function(initialValue, instant) {
};

// A (degenerated) temporal aggregate function. Useful for temporal stream operators
var TemporalFunction = function(aggFn,instant) {
var self = this; var inner = aggFn.make(); var t;
var TemporalFunction = function(aggFn,instant, win) {
var self = this; var inner = aggFn.make(win); var t;
self.init = function() { inner.init(); t = Temporal.make(inner.emit(), instant); };
self.accumulate = function(value) { t = t.update(inner.accumulate(value.value())); };
self.emit = function() { return Temporal.make(inner.emit()); };
self.at = function() { return t.at(); };
};

// Factory function for turning regular aggregate functions into temporal ones
TemporalFunction.make = function(aggFn, at) {
return new TemporalFunction(aggFn, at);
TemporalFunction.make = function(aggFn, at, win) {
return new TemporalFunction(aggFn, at, win);
};

// Exports
Expand Down
2 changes: 1 addition & 1 deletion lib/eep.window_monotonic.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var TemporalFunction = require('./eep.core').TemporalFunction;
var MonotonicWindow = function(aggFn, clock) {
events.EventEmitter.call(this);
var self = this;
self.fn = TemporalFunction.make(aggFn,clock.init());
self.fn = TemporalFunction.make(aggFn,clock.init(),self);
var idx = 0;

self.fn.init();
Expand Down
2 changes: 1 addition & 1 deletion lib/eep.window_periodic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var TemporalFunction = require('./eep.core').TemporalFunction;
//
var PeriodicWindow = function(aggFn, intervalMillis) {
events.EventEmitter.call(this);
var self = this, clock = new WallClock(intervalMillis), fn = TemporalFunction.make(aggFn,clock.init());
var self = this, clock = new WallClock(intervalMillis), fn = TemporalFunction.make(aggFn,clock.init(),self);
var idx = 0;

fn.init();
Expand Down
97 changes: 97 additions & 0 deletions test/test-windows.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var eep = require("eep");
var events = require('events');
var testCase = require('nodeunit').testCase;
var util = require('util');

var floatEquals = function (a,b) {
var a1 = Math.round(parseFloat(a)*100)/100;
Expand Down Expand Up @@ -172,6 +173,7 @@ exports.read = testCase({

enqueueEvent();
setTimeout(enqueueEvent, 20);
setTimeout(triggerTick, 50);
setTimeout(enqueueEvent, 80);
setTimeout(triggerTick, 100); //3
setTimeout(triggerTick, 200); //empty window should be produced here
Expand All @@ -187,5 +189,100 @@ exports.read = testCase({
setTimeout(function() {
assert.done();
}, 450);
},
'monotonic window counter': function (assert) {
var monotonic = eep.EventWorld.make().windows().monotonic(new CountFunction(), new eep.CountingClock());

assert.ok(monotonic != null);

var results = [];
monotonic.on('emit', function(v) {
results.push(v);
});

monotonic.enqueue(5);
assert.same([], results);

monotonic.tick();
assert.same([1], results);

monotonic.enqueue(4);
monotonic.enqueue(10);
monotonic.tick();
assert.same([1, 2], results);

setTimeout(function() {
monotonic.enqueue(3);
monotonic.enqueue(9);
monotonic.enqueue(81);
}, 50);

setTimeout(function() {
monotonic.tick();
assert.same([1, 2, 3], results);
}, 60);

setTimeout(function() {
monotonic.tick();
assert.same([1, 2, 3, 0], results);
}, 80);

setTimeout(function() {
assert.done();
}, 100);
},
'monotonic window when right': function (assert) {

function EmitWhenConditionsAreRight(optionsIn, win) {
const options = optionsIn;
this.win = win;

this.init = function() {
options.whatsUp = false;
};

this.accumulate = function(whatsUpNew) {
this.whatsUp = whatsUpNew;

if(this.whatsUp) {
win.tick();
}
};

this.compensate = function() {

};

this.emit = function() {
return !!this.whatsUp? 1:0;
};

this.make = function(win) { return new EmitWhenConditionsAreRight(options, win); };
}


var monotonic = eep.EventWorld.make().windows().monotonic(new EmitWhenConditionsAreRight({}), new eep.CountingClock());

assert.ok(monotonic != null);

var results = [];
monotonic.on('emit', function(v) {
results.push(v);
});


setTimeout(() => {monotonic.enqueue(false);}, 10);
setTimeout(() => {monotonic.enqueue(true);}, 20);
setTimeout(() => {monotonic.tick();}, 100);
setTimeout(() => {monotonic.enqueue(false);}, 120);
setTimeout(() => {monotonic.tick();}, 200);
setTimeout(() => {monotonic.enqueue(true);}, 220);

setTimeout(() => {assert.same([1,1,0,1], results);}, 300);


setTimeout(function() {
assert.done();
}, 310);
}
});