Skip to content

Commit

Permalink
Merge pull request #310 from BackburnerJS/fail-on-type-failures
Browse files Browse the repository at this point in the history
Fail the CI build on type errors.
  • Loading branch information
stefanpenner authored Jan 19, 2018
2 parents e620fc1 + 7221b26 commit fd24f18
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 131 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cache:
directories:
- node_modules
script:
- npm run problems
- npm run lint
- npm test
after_success:
Expand Down
29 changes: 4 additions & 25 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ const fs = require('fs');

const SOURCE_MAPPING_DATA_URL = '//# sourceMap' + 'pingURL=data:application/json;base64,';

module.exports = function () {
module.exports = function (app) {
const src = new MergeTrees([
new Funnel(path.dirname(require.resolve('@types/qunit/package')), {
destDir: 'qunit',
include: ['index.d.ts']
}),
new Funnel(__dirname + '/lib', {
destDir: 'lib'
}),
Expand All @@ -25,21 +21,7 @@ module.exports = function () {
]);

const compiled = typescript(src, {
tsconfig: {
compilerOptions: {
baseUrl: '.',
inlineSourceMap: true,
inlineSources: true,
module: 'es2015',
moduleResolution: 'node',
paths: {
backburner: ['lib/index.ts']
},
strictNullChecks: true,
target: 'es2015'
},
files: ['qunit/index.d.ts', 'lib/index.ts', 'tests/index.ts']
}
throwOnError: process.env.EMBER_ENV === 'production',
});

const backburner = new Rollup(compiled, {
Expand Down Expand Up @@ -70,7 +52,7 @@ module.exports = function () {
file: 'named-amd/backburner.js',
exports: 'named',
format: 'amd',
moduleId: 'backburner',
amd: { id: 'backburner' },
sourcemap: true
}, {
file: 'backburner.js',
Expand All @@ -91,7 +73,7 @@ module.exports = function () {
output: [{
file: 'named-amd/tests.js',
format: 'amd',
moduleId: 'backburner-tests',
amd: { id: 'backburner-tests' },
sourcemap: true
}]
}
Expand Down Expand Up @@ -130,9 +112,6 @@ function loadWithInlineMap() {
result.code = code.slice(0, index);
result.map = parseSourceMap(code.slice(index + SOURCE_MAPPING_DATA_URL.length));
result.file = id;
console.log(id);
console.log(result.map.sources);
console.log(result.map.sourcesContent.map((c) => !!c));
return result;
}
};
Expand Down
6 changes: 5 additions & 1 deletion lib/backburner/iterator-drain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// accepts a function that when invoked will return an iterator
// iterator will drain until completion
export default function(fn: () => { next: () => { done: boolean, value?: any}}) {
export interface Iteratable {
next: () => { done: boolean, value?: any };
}

export default function(fn: () => Iteratable) {
let iterator = fn();
let result = iterator.next();

Expand Down
11 changes: 11 additions & 0 deletions lib/backburner/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export default class Queue {
this.globalOptions = globalOptions;
}

public stackFor(index) {
if (index < this._queue.length) {
let entry = this._queue[index * 3 + 4];
if (entry) {
return entry.stack;
} else {
return null;
}
}
}

public flush(sync?) {
let { before, after } = this.options;
let target;
Expand Down
131 changes: 65 additions & 66 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

import searchTimer from './backburner/binary-search';
import DeferredActionQueues from './backburner/deferred-action-queues';
import iteratorDrain from './backburner/iterator-drain';
import iteratorDrain, { Iteratable } from './backburner/iterator-drain';

import Queue, { QUEUE_STATE } from './backburner/queue';

Expand All @@ -29,7 +29,7 @@ function parseArgs() {
target = arguments[0];
method = arguments[1];
if (typeof method === 'string') {
method = target[method] as () => any;
method = target[method];
}

if (length > 2) {
Expand All @@ -52,24 +52,27 @@ export default class Backburner {

public options: any;

private _onBegin: () => void;
private _onEnd: () => void;
private _onBegin: (currentInstance: DeferredActionQueues, previousInstance: DeferredActionQueues | null) => void;
private _onEnd: (currentInstance: DeferredActionQueues, nextInstance: DeferredActionQueues | null) => void;
private queueNames: string[];
private instanceStack: DeferredActionQueues[];
private _debouncees: any[];
private _throttlers: any[];
private instanceStack: DeferredActionQueues[] = [];
private _debouncees: any[] = [];
private _throttlers: any[] = [];
private _eventCallbacks: {
end: () => [];
begin: () => [];
end: Function[];
begin: Function[];
} = {
end: [],
begin: []
};

private _timerTimeoutId: number | null = null;
private _timers: any[];
private _timers: any[] = [];
private _platform: {
setTimeout(fn: () => void, ms: number): number;
setTimeout(fn: Function, ms: number): number;
clearTimeout(id: number): void;
next(fn: () => void): number;
clearNext(fn): void;
next(fn: Function): number;
clearNext(id: any): void;
now(): number;
};

Expand All @@ -85,20 +88,12 @@ export default class Backburner {
this.options.defaultQueue = queueNames[0];
}

this.instanceStack = [];
this._timers = [];
this._debouncees = [];
this._throttlers = [];
this._eventCallbacks = {
end: [],
begin: []
};

this._onBegin = this.options.onBegin || noop;
this._onEnd = this.options.onEnd || noop;

let _platform = this.options._platform || {};
let platform = Object.create(null);

platform.setTimeout = _platform.setTimeout || ((fn, ms) => setTimeout(fn, ms));
platform.clearTimeout = _platform.clearTimeout || ((id) => clearTimeout(id));
platform.next = _platform.next || ((fn) => SET_TIMEOUT(fn, 0));
Expand Down Expand Up @@ -209,9 +204,9 @@ export default class Backburner {
}
}

public run(method: () => any);
public run(target: () => any | any | null, method?: () => any | string, ...args);
public run(target: any | null | undefined, method?: any, ...args: any[]);
public run(target: Function);
public run(target: Function | any | null, method?: Function | string, ...args);
public run(target: any | null | undefined, method?: Function, ...args: any[]);
public run() {
let [target, method, args] = parseArgs(...arguments);
return this._run(target, method, args);
Expand All @@ -231,6 +226,9 @@ export default class Backburner {
@param {any} args The method arguments
@return method result
*/
public join(target: Function);
public join(target: Function | any | null, method?: Function | string, ...args);
public join(target: any | null | undefined, method?: Function, ...args: any[]);
public join() {
let [target, method, args] = parseArgs(...arguments);
return this._join(target, method, args);
Expand All @@ -239,17 +237,16 @@ export default class Backburner {
/**
* @deprecated please use schedule instead.
*/
public defer(queueName: string, ...args);
public defer() {
return this.schedule(...arguments);
public defer(queueName, targetOrMethod, ..._args) {
return this.schedule(queueName, targetOrMethod, ..._args);
}

/**
* Schedule the passed function to run inside the specified queue.
*/
public schedule(queueName: string, method: () => any);
public schedule(queueName: string, method: Function);
public schedule<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
public schedule(queueName: string, target: any | null, method: any | (() => any), ...args);
public schedule(queueName: string, target: any, method: any | Function, ...args);
public schedule(queueName, ..._args) {
let [target, method, args] = parseArgs(..._args);
let stack = this.DEBUG ? new Error() : undefined;
Expand All @@ -264,25 +261,24 @@ export default class Backburner {
@param {Iterable} an iterable of functions to execute
@return method result
*/
public scheduleIterable(queueName: string, iterable: () => any) {
public scheduleIterable(queueName: string, iterable: () => Iteratable) {
let stack = this.DEBUG ? new Error() : undefined;
return this._ensureInstance().schedule(queueName, null, iteratorDrain, [iterable], false, stack);
}

/**
* @deprecated please use scheduleOnce instead.
*/
public deferOnce(queueName: string, ...args);
public deferOnce() {
return this.scheduleOnce(...arguments);
public deferOnce(queueName, targetOrMethod, ...args) {
return this.scheduleOnce(queueName, targetOrMethod, ...args);
}

/**
* Schedule the passed function to run once inside the specified queue.
*/
public scheduleOnce(queueName: string, method: () => any);
public scheduleOnce(queueName: string, method: Function);
public scheduleOnce<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
public scheduleOnce(queueName: string, target: any | null, method: any | (() => any), ...args);
public scheduleOnce(queueName: string, target: any | null, method: any | Function, ...args);
public scheduleOnce(queueName, ..._args) {
let [target, method, args] = parseArgs(..._args);
let stack = this.DEBUG ? new Error() : undefined;
Expand Down Expand Up @@ -321,7 +317,7 @@ export default class Backburner {
method = args.shift();
} else if (methodOrTarget !== null && type === 'string' && methodOrWait in methodOrTarget) {
target = args.shift();
method = target[args.shift()] as () => any;
method = target[args.shift()];
} else if (isCoercableNumber(methodOrWait)) {
method = args.shift();
wait = parseInt(args.shift(), 10);
Expand Down Expand Up @@ -370,23 +366,23 @@ export default class Backburner {
return this._setTimeout(fn, executeAt);
}

public throttle<T>(target: T, methodName: keyof T, wait: number, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, arg1: any, wait: number, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait: number, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait: number, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, wait?: number | string, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, arg1: any, wait?: number | string, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait?: number | string, immediate?: boolean): Timer;
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait?: number | string, immediate?: boolean): Timer;

// with target, with immediate
public throttle(thisArg: any, method: () => void, wait: number, immediate?: boolean): Timer;
public throttle<A>(thisArg: any, method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
public throttle<A, B>(thisArg: any, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
public throttle<A, B, C>(thisArg: any, method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number, immediate?: boolean): Timer;
public throttle(thisArg: any | null, method: () => void, wait?: number | string, immediate?: boolean): Timer;
public throttle<A>(thisArg: any | null, method: (arg1: A) => void, arg1: A, wait?: number | string, immediate?: boolean): Timer;
public throttle<A, B>(thisArg: any | null, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer;
public throttle<A, B, C>(thisArg: any | null, method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait?: number | string, immediate?: boolean): Timer;

// without target, with immediate
public throttle(method: () => void, wait: number, immediate?: boolean): Timer;
public throttle<A>(method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
public throttle<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
public throttle<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number, immediate?: boolean): Timer;
public throttle(targetOrThisArgOrMethod: object | (() => any), ...args): Timer {
public throttle(method: () => void, wait?: number | string, immediate?: boolean): Timer;
public throttle<A>(method: (arg1: A) => void, arg1: A, wait?: number | string, immediate?: boolean): Timer;
public throttle<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer;
public throttle<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait?: number | string, immediate?: boolean): Timer;
public throttle(targetOrThisArgOrMethod: object | Function, ...args): Timer {
let target;
let method;
let immediate;
Expand All @@ -404,7 +400,7 @@ export default class Backburner {
immediate = args.pop();
let type = typeof method;
if (type === 'string') {
method = target[method] as () => any;
method = target[method];
} else if (type !== 'function') {
args.unshift(method);
method = target;
Expand Down Expand Up @@ -446,23 +442,23 @@ export default class Backburner {
}

// with target, with method name, with optional immediate
public debounce<T>(target: T, methodName: keyof T, wait: number, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, arg1: any, wait: number, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait: number, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait: number, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, wait: number | string, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, arg1: any, wait: number | string, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait: number | string, immediate?: boolean): Timer;
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait: number | string, immediate?: boolean): Timer;

// with target, with optional immediate
public debounce(thisArg: any, method: () => void, wait: number, immediate?: boolean): Timer;
public debounce<A>(thisArg: any, method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
public debounce<A, B>(thisArg: any, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
public debounce<A, B, C>(thisArg: any, method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number, immediate?: boolean): Timer;
public debounce(thisArg: any | null, method: () => void, wait: number | string, immediate?: boolean): Timer;
public debounce<A>(thisArg: any | null, method: (arg1: A) => void, arg1: A, wait: number | string, immediate?: boolean): Timer;
public debounce<A, B>(thisArg: any | null, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer;
public debounce<A, B, C>(thisArg: any | null, method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number | string, immediate?: boolean): Timer;

// without target, with optional immediate
public debounce(method: () => void, wait: number, immediate?: boolean): Timer;
public debounce<A>(method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
public debounce<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
public debounce<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number, immediate?: boolean): Timer;
public debounce(targetOrThisArgOrMethod: object | (() => any), ...args): Timer {
public debounce(method: () => void, wait: number | string, immediate?: boolean): Timer;
public debounce<A>(method: (arg1: A) => void, arg1: A, wait: number | string, immediate?: boolean): Timer;
public debounce<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer;
public debounce<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number | string, immediate?: boolean): Timer;
public debounce(targetOrThisArgOrMethod: object | Function, ...args): Timer {
let target;
let method;
let immediate;
Expand All @@ -481,7 +477,7 @@ export default class Backburner {

let type = typeof method;
if (type === 'string') {
method = target[method] as () => any;
method = target[method];
} else if (type !== 'function') {
args.unshift(method);
method = target;
Expand Down Expand Up @@ -542,7 +538,10 @@ export default class Backburner {
}

public hasTimers() {
return this._timers.length > 0 || this._debouncees.length > 0 || this._throttlers.length > 0 || this._autorun !== null;
return this._timers.length > 0 ||
this._debouncees.length > 0 ||
this._throttlers.length > 0 ||
this._autorun !== null;
}

public cancel(timer?) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test:server": "ember test --server",
"serve": "ember serve",
"lint": "tslint --project tsconfig.json",
"problems": "tsc -p tsconfig.json --noEmit",
"bench": "ember build && node ./bench/index.js"
},
"author": "Erik Bryn",
Expand Down
4 changes: 2 additions & 2 deletions tests/debounce-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ QUnit.test('debounce without a target, with args', function(assert) {
let bb = new Backburner(['batman']);
let calledCount = 0;
let calledWith: string[] = [];
function debouncee(first: string) {
function debouncee(first) {
calledCount++;
calledWith.push(first);
}
Expand All @@ -435,7 +435,7 @@ QUnit.test('debounce without a target, with args - can be canceled', function(as
let bb = new Backburner(['batman']);
let calledCount = 0;
let calledWith: string[] = [];
function debouncee(first: string) {
function debouncee(first) {
calledCount++;
calledWith.push(first);
}
Expand Down
Loading

0 comments on commit fd24f18

Please sign in to comment.