Comparing version 0.15.0 to 0.16.0
@@ -54,7 +54,4 @@ /** @license MIT License (c) copyright 2010-2015 original author or authors */ | ||
ScanSink.prototype.error = Pipe.prototype.error; | ||
ScanSink.prototype.end = Pipe.prototype.end; | ||
ScanSink.prototype.end = function(t) { | ||
this.sink.end(t, this.value); | ||
}; | ||
/** | ||
@@ -61,0 +58,0 @@ * Reduce a stream to produce a single result. Note that reducing an infinite |
@@ -7,2 +7,3 @@ /** @license MIT License (c) copyright 2010-2015 original author or authors */ | ||
var Observer = require('./sink/Observer'); | ||
var SettableDisposable = require('./disposable/SettableDisposable'); | ||
var defaultScheduler = require('./scheduler/defaultScheduler'); | ||
@@ -19,20 +20,7 @@ | ||
return new Promise(function (resolve, reject) { | ||
var disposable; | ||
var disposable = new SettableDisposable(); | ||
var observer = new Observer(f, resolve, reject, disposable); | ||
var observer = new Observer(f, | ||
function (x) { | ||
disposeThen(resolve, reject, disposable, x); | ||
}, function (e) { | ||
disposeThen(reject, reject, disposable, e); | ||
}); | ||
disposable = source.run(observer, scheduler); | ||
disposable.setDisposable(source.run(observer, scheduler)); | ||
}); | ||
} | ||
function disposeThen(resolve, reject, disposable, x) { | ||
Promise.resolve(disposable.dispose()).then(function () { | ||
resolve(x); | ||
}, reject); | ||
} | ||
@@ -7,4 +7,2 @@ /** @license MIT License (c) copyright 2010-2015 original author or authors */ | ||
var findIndex = base.findIndex; | ||
module.exports = Scheduler; | ||
@@ -79,6 +77,7 @@ | ||
task.active = false; | ||
var i = findIndex(task, this._tasks); | ||
var i = binarySearch(task.time, this._tasks); | ||
if(i >= 0) { | ||
this._tasks.splice(i, 1); | ||
if(i >= 0 && i < this._tasks.length) { | ||
var at = base.findIndex(task, this._tasks[i].events); | ||
this._tasks[i].events.splice(at, 1); | ||
this._reschedule(); | ||
@@ -106,51 +105,2 @@ } | ||
Scheduler.prototype._runReadyTasks = function(now) { | ||
this._timer = null; | ||
this._runTasks(this._collectReadyTasks(now)); | ||
this._scheduleNextRun(this.now()); | ||
}; | ||
Scheduler.prototype._collectReadyTasks = function(now) { | ||
var tasks = this._tasks; | ||
var l = tasks.length; | ||
var toRun = []; | ||
var task, i; | ||
// Collect all active tasks with time <= now | ||
for(i=0; i<l; ++i) { | ||
task = tasks[i]; | ||
if(task.time > now) { | ||
break; | ||
} | ||
if(task.active) { | ||
toRun.push(task); | ||
} | ||
} | ||
this._tasks = base.drop(i, tasks); | ||
return toRun; | ||
}; | ||
Scheduler.prototype._runTasks = function(tasks) { | ||
// Run all ready tasks | ||
var l = tasks.length; | ||
var task; | ||
for(var i=0; i<l; ++i) { | ||
task = tasks[i]; | ||
runTask(task); | ||
// Reschedule periodic repeating tasks | ||
// Check active again, since a task may have canceled itself | ||
if(task.period >= 0 && task.active) { | ||
task.time = task.time + task.period; | ||
insertByTime(task, this._tasks); | ||
} | ||
} | ||
}; | ||
Scheduler.prototype._scheduleNextRun = function(now) { | ||
@@ -177,19 +127,66 @@ if(this._tasks.length === 0) { | ||
function insertByTime(task, tasks) { | ||
tasks.splice(findInsertion(task, tasks), 0, task); | ||
} | ||
function findInsertion(task, tasks) { | ||
var i = binarySearch(task, tasks); | ||
Scheduler.prototype._runReadyTasks = function(now) { | ||
this._timer = null; | ||
this._findAndRunTasks(now); | ||
this._scheduleNextRun(this.now()); | ||
}; | ||
Scheduler.prototype._findAndRunTasks = function(now) { | ||
var tasks = this._tasks; | ||
var l = tasks.length; | ||
var t = task.time; | ||
var i = 0; | ||
while(i<l && t === tasks[i].time) { | ||
while(i < l && tasks[i].time <= now) { | ||
++i; | ||
} | ||
return i; | ||
this._tasks = tasks.slice(i); | ||
// Run all ready tasks | ||
for (var j = 0; j < i; ++j) { | ||
runTasks(tasks[j], this._tasks); | ||
} | ||
}; | ||
function runTasks(timeslot, tasks) { | ||
var events = timeslot.events; | ||
for(var i=0; i<events.length; ++i) { | ||
var task = events[i]; | ||
if(task.active) { | ||
runTask(task); | ||
// Reschedule periodic repeating tasks | ||
// Check active again, since a task may have canceled itself | ||
if(task.period >= 0) { | ||
task.time = task.time + task.period; | ||
insertByTime(task, tasks); | ||
} | ||
} | ||
} | ||
} | ||
function binarySearch(x, sortedArray) { | ||
function insertByTime(task, timeslots) { | ||
var l = timeslots.length; | ||
if(l === 0) { | ||
timeslots.push(newTimeslot(task.time, [task])); | ||
return; | ||
} | ||
var i = binarySearch(task.time, timeslots); | ||
if(i >= l) { | ||
timeslots.push(newTimeslot(task.time, [task])); | ||
} else if(task.time === timeslots[i].time) { | ||
timeslots[i].events.push(task); | ||
} else { | ||
timeslots.splice(i, 0, newTimeslot(task.time, [task])); | ||
} | ||
} | ||
function binarySearch(t, sortedArray) { | ||
var lo = 0; | ||
@@ -203,5 +200,5 @@ var hi = sortedArray.length; | ||
if (x.time === y.time) { | ||
if (t === y.time) { | ||
return mid; | ||
} else if (x.time < y.time) { | ||
} else if (t < y.time) { | ||
hi = mid; | ||
@@ -214,1 +211,5 @@ } else { | ||
} | ||
function newTimeslot(t, events) { | ||
return { time: t, events: events }; | ||
} |
@@ -5,2 +5,4 @@ /** @license MIT License (c) copyright 2010-2015 original author or authors */ | ||
var Promise = require('../Promise'); | ||
module.exports = Observer; | ||
@@ -11,11 +13,9 @@ | ||
* signals. | ||
* @param {function(x:*):void} event function to be applied to each event | ||
* @param {function(x:*):void} end function to apply to end signal value. | ||
* @param {function(e:Error|*):void} error function to apply to error signal value. | ||
* @constructor | ||
*/ | ||
function Observer(event, end, error) { | ||
function Observer(event, end, error, disposable) { | ||
this._event = event; | ||
this._end = end; | ||
this._error = error; | ||
this._disposable = disposable; | ||
this.active = true; | ||
@@ -36,3 +36,3 @@ } | ||
this.active = false; | ||
this._end(x); | ||
disposeThen(this._end, this._error, this._disposable, x); | ||
}; | ||
@@ -42,3 +42,9 @@ | ||
this.active = false; | ||
this._error(e); | ||
disposeThen(this._error, this._error, this._disposable, e); | ||
}; | ||
function disposeThen(end, error, disposable, x) { | ||
Promise.resolve(disposable.dispose()).then(function () { | ||
end(x); | ||
}, error); | ||
} |
{ | ||
"name": "most", | ||
"version": "0.15.0", | ||
"version": "0.16.0", | ||
"description": "Monadic streams", | ||
@@ -5,0 +5,0 @@ "main": "most.js", |
@@ -5,2 +5,4 @@ [![Build Status](https://travis-ci.org/cujojs/most.svg?branch=master)](https://travis-ci.org/cujojs/most) | ||
[![Join the chat at https://gitter.im/cujojs/most](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cujojs/most?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
Most.js is a toolkit for reactive programming. It helps you compose asynchronous operations on streams of values and events, e.g. WebSocket messages, DOM events, etc, and on time-varying values, e.g. the "current value" of an <input>, without many of the hazards of side effects and mutable shared state. | ||
@@ -7,0 +9,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
121640
67
3678
190