Reactive-Extensions/RxJS

View on GitHub
src/core/concurrency/defaultscheduler.js

Summary

Maintainability
F
1 wk
Test Coverage
var scheduleMethod, clearMethod;
 
Similar blocks of code found in 4 locations. Consider refactoring.
var localTimer = (function () {
var localSetTimeout, localClearTimeout = noop;
if (!!root.setTimeout) {
localSetTimeout = root.setTimeout;
localClearTimeout = root.clearTimeout;
} else if (!!root.WScript) {
localSetTimeout = function (fn, time) {
root.WScript.Sleep(time);
fn();
};
} else {
throw new NotSupportedError();
}
 
return {
setTimeout: localSetTimeout,
clearTimeout: localClearTimeout
};
}());
var localSetTimeout = localTimer.setTimeout,
localClearTimeout = localTimer.clearTimeout;
 
Similar blocks of code found in 4 locations. Consider refactoring.
(function () {
 
var nextHandle = 1, tasksByHandle = {}, currentlyRunning = false;
 
clearMethod = function (handle) {
delete tasksByHandle[handle];
};
 
function runTask(handle) {
if (currentlyRunning) {
localSetTimeout(function () { runTask(handle); }, 0);
} else {
var task = tasksByHandle[handle];
if (task) {
currentlyRunning = true;
var result = tryCatch(task)();
clearMethod(handle);
currentlyRunning = false;
if (result === errorObj) { thrower(result.e); }
}
}
}
 
var reNative = new RegExp('^' +
String(toString)
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
.replace(/toString| for [^\]]+/g, '.*?') + '$'
);
 
var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
!reNative.test(setImmediate) && setImmediate;
 
function postMessageSupported () {
// Ensure not in a worker
if (!root.postMessage || root.importScripts) { return false; }
var isAsync = false, oldHandler = root.onmessage;
// Test for async
root.onmessage = function () { isAsync = true; };
root.postMessage('', '*');
root.onmessage = oldHandler;
 
return isAsync;
}
 
// Use in order, setImmediate, nextTick, postMessage, MessageChannel, script readystatechanged, setTimeout
if (isFunction(setImmediate)) {
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
setImmediate(function () { runTask(id); });
 
return id;
};
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
process.nextTick(function () { runTask(id); });
 
return id;
};
} else if (postMessageSupported()) {
var MSG_PREFIX = 'ms.rx.schedule' + Math.random();
 
var onGlobalPostMessage = function (event) {
// Only if we're a match to avoid any other global events
if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
runTask(event.data.substring(MSG_PREFIX.length));
}
};
 
root.addEventListener('message', onGlobalPostMessage, false);
 
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
root.postMessage(MSG_PREFIX + id, '*');
return id;
};
} else if (!!root.MessageChannel) {
var channel = new root.MessageChannel();
 
channel.port1.onmessage = function (e) { runTask(e.data); };
 
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
channel.port2.postMessage(id);
return id;
};
} else if ('document' in root && 'onreadystatechange' in root.document.createElement('script')) {
 
scheduleMethod = function (action) {
var scriptElement = root.document.createElement('script');
var id = nextHandle++;
tasksByHandle[id] = action;
 
scriptElement.onreadystatechange = function () {
runTask(id);
scriptElement.onreadystatechange = null;
scriptElement.parentNode.removeChild(scriptElement);
scriptElement = null;
};
root.document.documentElement.appendChild(scriptElement);
return id;
};
 
} else {
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
localSetTimeout(function () {
runTask(id);
}, 0);
 
return id;
};
}
}());
 
/**
* Gets a scheduler that schedules work via a timed callback based upon platform.
*/
Function `DefaultScheduler` has 49 lines of code (exceeds 25 allowed). Consider refactoring.
Function `DefaultScheduler` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Similar blocks of code found in 4 locations. Consider refactoring.
var DefaultScheduler = (function (__super__) {
inherits(DefaultScheduler, __super__);
function DefaultScheduler() {
__super__.call(this);
}
 
function scheduleAction(disposable, action, scheduler, state) {
return function schedule() {
disposable.setDisposable(Disposable._fixup(action(scheduler, state)));
};
}
 
function ClearDisposable(id) {
this._id = id;
this.isDisposed = false;
}
 
ClearDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
clearMethod(this._id);
}
};
 
function LocalClearDisposable(id) {
this._id = id;
this.isDisposed = false;
}
 
LocalClearDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
localClearTimeout(this._id);
}
};
 
DefaultScheduler.prototype.schedule = function (state, action) {
var disposable = new SingleAssignmentDisposable(),
id = scheduleMethod(scheduleAction(disposable, action, this, state));
return new BinaryDisposable(disposable, new ClearDisposable(id));
};
 
DefaultScheduler.prototype._scheduleFuture = function (state, dueTime, action) {
if (dueTime === 0) { return this.schedule(state, action); }
var disposable = new SingleAssignmentDisposable(),
id = localSetTimeout(scheduleAction(disposable, action, this, state), dueTime);
return new BinaryDisposable(disposable, new LocalClearDisposable(id));
};
 
function scheduleLongRunning(state, action, disposable) {
return function () { action(state, disposable); };
}
 
DefaultScheduler.prototype.scheduleLongRunning = function (state, action) {
var disposable = disposableCreate(noop);
scheduleMethod(scheduleLongRunning(state, action, disposable));
return disposable;
};
 
return DefaultScheduler;
}(Scheduler));
 
var defaultScheduler = Scheduler['default'] = Scheduler.async = new DefaultScheduler();