Skip to content

Commit fd24f18

Browse files
authored
Merge pull request #310 from BackburnerJS/fail-on-type-failures
Fail the CI build on type errors.
2 parents e620fc1 + 7221b26 commit fd24f18

14 files changed

+133
-131
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cache:
66
directories:
77
- node_modules
88
script:
9+
- npm run problems
910
- npm run lint
1011
- npm test
1112
after_success:

ember-cli-build.js

+4-25
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ const fs = require('fs');
1010

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

13-
module.exports = function () {
13+
module.exports = function (app) {
1414
const src = new MergeTrees([
15-
new Funnel(path.dirname(require.resolve('@types/qunit/package')), {
16-
destDir: 'qunit',
17-
include: ['index.d.ts']
18-
}),
1915
new Funnel(__dirname + '/lib', {
2016
destDir: 'lib'
2117
}),
@@ -25,21 +21,7 @@ module.exports = function () {
2521
]);
2622

2723
const compiled = typescript(src, {
28-
tsconfig: {
29-
compilerOptions: {
30-
baseUrl: '.',
31-
inlineSourceMap: true,
32-
inlineSources: true,
33-
module: 'es2015',
34-
moduleResolution: 'node',
35-
paths: {
36-
backburner: ['lib/index.ts']
37-
},
38-
strictNullChecks: true,
39-
target: 'es2015'
40-
},
41-
files: ['qunit/index.d.ts', 'lib/index.ts', 'tests/index.ts']
42-
}
24+
throwOnError: process.env.EMBER_ENV === 'production',
4325
});
4426

4527
const backburner = new Rollup(compiled, {
@@ -70,7 +52,7 @@ module.exports = function () {
7052
file: 'named-amd/backburner.js',
7153
exports: 'named',
7254
format: 'amd',
73-
moduleId: 'backburner',
55+
amd: { id: 'backburner' },
7456
sourcemap: true
7557
}, {
7658
file: 'backburner.js',
@@ -91,7 +73,7 @@ module.exports = function () {
9173
output: [{
9274
file: 'named-amd/tests.js',
9375
format: 'amd',
94-
moduleId: 'backburner-tests',
76+
amd: { id: 'backburner-tests' },
9577
sourcemap: true
9678
}]
9779
}
@@ -130,9 +112,6 @@ function loadWithInlineMap() {
130112
result.code = code.slice(0, index);
131113
result.map = parseSourceMap(code.slice(index + SOURCE_MAPPING_DATA_URL.length));
132114
result.file = id;
133-
console.log(id);
134-
console.log(result.map.sources);
135-
console.log(result.map.sourcesContent.map((c) => !!c));
136115
return result;
137116
}
138117
};

lib/backburner/iterator-drain.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// accepts a function that when invoked will return an iterator
22
// iterator will drain until completion
3-
export default function(fn: () => { next: () => { done: boolean, value?: any}}) {
3+
export interface Iteratable {
4+
next: () => { done: boolean, value?: any };
5+
}
6+
7+
export default function(fn: () => Iteratable) {
48
let iterator = fn();
59
let result = iterator.next();
610

lib/backburner/queue.ts

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ export default class Queue {
2222
this.globalOptions = globalOptions;
2323
}
2424

25+
public stackFor(index) {
26+
if (index < this._queue.length) {
27+
let entry = this._queue[index * 3 + 4];
28+
if (entry) {
29+
return entry.stack;
30+
} else {
31+
return null;
32+
}
33+
}
34+
}
35+
2536
public flush(sync?) {
2637
let { before, after } = this.options;
2738
let target;

lib/index.ts

+65-66
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

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

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

@@ -29,7 +29,7 @@ function parseArgs() {
2929
target = arguments[0];
3030
method = arguments[1];
3131
if (typeof method === 'string') {
32-
method = target[method] as () => any;
32+
method = target[method];
3333
}
3434

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

5353
public options: any;
5454

55-
private _onBegin: () => void;
56-
private _onEnd: () => void;
55+
private _onBegin: (currentInstance: DeferredActionQueues, previousInstance: DeferredActionQueues | null) => void;
56+
private _onEnd: (currentInstance: DeferredActionQueues, nextInstance: DeferredActionQueues | null) => void;
5757
private queueNames: string[];
58-
private instanceStack: DeferredActionQueues[];
59-
private _debouncees: any[];
60-
private _throttlers: any[];
58+
private instanceStack: DeferredActionQueues[] = [];
59+
private _debouncees: any[] = [];
60+
private _throttlers: any[] = [];
6161
private _eventCallbacks: {
62-
end: () => [];
63-
begin: () => [];
62+
end: Function[];
63+
begin: Function[];
64+
} = {
65+
end: [],
66+
begin: []
6467
};
6568

6669
private _timerTimeoutId: number | null = null;
67-
private _timers: any[];
70+
private _timers: any[] = [];
6871
private _platform: {
69-
setTimeout(fn: () => void, ms: number): number;
72+
setTimeout(fn: Function, ms: number): number;
7073
clearTimeout(id: number): void;
71-
next(fn: () => void): number;
72-
clearNext(fn): void;
74+
next(fn: Function): number;
75+
clearNext(id: any): void;
7376
now(): number;
7477
};
7578

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

88-
this.instanceStack = [];
89-
this._timers = [];
90-
this._debouncees = [];
91-
this._throttlers = [];
92-
this._eventCallbacks = {
93-
end: [],
94-
begin: []
95-
};
96-
9791
this._onBegin = this.options.onBegin || noop;
9892
this._onEnd = this.options.onEnd || noop;
9993

10094
let _platform = this.options._platform || {};
10195
let platform = Object.create(null);
96+
10297
platform.setTimeout = _platform.setTimeout || ((fn, ms) => setTimeout(fn, ms));
10398
platform.clearTimeout = _platform.clearTimeout || ((id) => clearTimeout(id));
10499
platform.next = _platform.next || ((fn) => SET_TIMEOUT(fn, 0));
@@ -209,9 +204,9 @@ export default class Backburner {
209204
}
210205
}
211206

212-
public run(method: () => any);
213-
public run(target: () => any | any | null, method?: () => any | string, ...args);
214-
public run(target: any | null | undefined, method?: any, ...args: any[]);
207+
public run(target: Function);
208+
public run(target: Function | any | null, method?: Function | string, ...args);
209+
public run(target: any | null | undefined, method?: Function, ...args: any[]);
215210
public run() {
216211
let [target, method, args] = parseArgs(...arguments);
217212
return this._run(target, method, args);
@@ -231,6 +226,9 @@ export default class Backburner {
231226
@param {any} args The method arguments
232227
@return method result
233228
*/
229+
public join(target: Function);
230+
public join(target: Function | any | null, method?: Function | string, ...args);
231+
public join(target: any | null | undefined, method?: Function, ...args: any[]);
234232
public join() {
235233
let [target, method, args] = parseArgs(...arguments);
236234
return this._join(target, method, args);
@@ -239,17 +237,16 @@ export default class Backburner {
239237
/**
240238
* @deprecated please use schedule instead.
241239
*/
242-
public defer(queueName: string, ...args);
243-
public defer() {
244-
return this.schedule(...arguments);
240+
public defer(queueName, targetOrMethod, ..._args) {
241+
return this.schedule(queueName, targetOrMethod, ..._args);
245242
}
246243

247244
/**
248245
* Schedule the passed function to run inside the specified queue.
249246
*/
250-
public schedule(queueName: string, method: () => any);
247+
public schedule(queueName: string, method: Function);
251248
public schedule<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
252-
public schedule(queueName: string, target: any | null, method: any | (() => any), ...args);
249+
public schedule(queueName: string, target: any, method: any | Function, ...args);
253250
public schedule(queueName, ..._args) {
254251
let [target, method, args] = parseArgs(..._args);
255252
let stack = this.DEBUG ? new Error() : undefined;
@@ -264,25 +261,24 @@ export default class Backburner {
264261
@param {Iterable} an iterable of functions to execute
265262
@return method result
266263
*/
267-
public scheduleIterable(queueName: string, iterable: () => any) {
264+
public scheduleIterable(queueName: string, iterable: () => Iteratable) {
268265
let stack = this.DEBUG ? new Error() : undefined;
269266
return this._ensureInstance().schedule(queueName, null, iteratorDrain, [iterable], false, stack);
270267
}
271268

272269
/**
273270
* @deprecated please use scheduleOnce instead.
274271
*/
275-
public deferOnce(queueName: string, ...args);
276-
public deferOnce() {
277-
return this.scheduleOnce(...arguments);
272+
public deferOnce(queueName, targetOrMethod, ...args) {
273+
return this.scheduleOnce(queueName, targetOrMethod, ...args);
278274
}
279275

280276
/**
281277
* Schedule the passed function to run once inside the specified queue.
282278
*/
283-
public scheduleOnce(queueName: string, method: () => any);
279+
public scheduleOnce(queueName: string, method: Function);
284280
public scheduleOnce<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
285-
public scheduleOnce(queueName: string, target: any | null, method: any | (() => any), ...args);
281+
public scheduleOnce(queueName: string, target: any | null, method: any | Function, ...args);
286282
public scheduleOnce(queueName, ..._args) {
287283
let [target, method, args] = parseArgs(..._args);
288284
let stack = this.DEBUG ? new Error() : undefined;
@@ -321,7 +317,7 @@ export default class Backburner {
321317
method = args.shift();
322318
} else if (methodOrTarget !== null && type === 'string' && methodOrWait in methodOrTarget) {
323319
target = args.shift();
324-
method = target[args.shift()] as () => any;
320+
method = target[args.shift()];
325321
} else if (isCoercableNumber(methodOrWait)) {
326322
method = args.shift();
327323
wait = parseInt(args.shift(), 10);
@@ -370,23 +366,23 @@ export default class Backburner {
370366
return this._setTimeout(fn, executeAt);
371367
}
372368

373-
public throttle<T>(target: T, methodName: keyof T, wait: number, immediate?: boolean): Timer;
374-
public throttle<T>(target: T, methodName: keyof T, arg1: any, wait: number, immediate?: boolean): Timer;
375-
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait: number, immediate?: boolean): Timer;
376-
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait: number, immediate?: boolean): Timer;
369+
public throttle<T>(target: T, methodName: keyof T, wait?: number | string, immediate?: boolean): Timer;
370+
public throttle<T>(target: T, methodName: keyof T, arg1: any, wait?: number | string, immediate?: boolean): Timer;
371+
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait?: number | string, immediate?: boolean): Timer;
372+
public throttle<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait?: number | string, immediate?: boolean): Timer;
377373

378374
// with target, with immediate
379-
public throttle(thisArg: any, method: () => void, wait: number, immediate?: boolean): Timer;
380-
public throttle<A>(thisArg: any, method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
381-
public throttle<A, B>(thisArg: any, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
382-
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;
375+
public throttle(thisArg: any | null, method: () => void, wait?: number | string, immediate?: boolean): Timer;
376+
public throttle<A>(thisArg: any | null, method: (arg1: A) => void, arg1: A, wait?: number | string, immediate?: boolean): Timer;
377+
public throttle<A, B>(thisArg: any | null, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer;
378+
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;
383379

384380
// without target, with immediate
385-
public throttle(method: () => void, wait: number, immediate?: boolean): Timer;
386-
public throttle<A>(method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
387-
public throttle<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
388-
public throttle<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number, immediate?: boolean): Timer;
389-
public throttle(targetOrThisArgOrMethod: object | (() => any), ...args): Timer {
381+
public throttle(method: () => void, wait?: number | string, immediate?: boolean): Timer;
382+
public throttle<A>(method: (arg1: A) => void, arg1: A, wait?: number | string, immediate?: boolean): Timer;
383+
public throttle<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer;
384+
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;
385+
public throttle(targetOrThisArgOrMethod: object | Function, ...args): Timer {
390386
let target;
391387
let method;
392388
let immediate;
@@ -404,7 +400,7 @@ export default class Backburner {
404400
immediate = args.pop();
405401
let type = typeof method;
406402
if (type === 'string') {
407-
method = target[method] as () => any;
403+
method = target[method];
408404
} else if (type !== 'function') {
409405
args.unshift(method);
410406
method = target;
@@ -446,23 +442,23 @@ export default class Backburner {
446442
}
447443

448444
// with target, with method name, with optional immediate
449-
public debounce<T>(target: T, methodName: keyof T, wait: number, immediate?: boolean): Timer;
450-
public debounce<T>(target: T, methodName: keyof T, arg1: any, wait: number, immediate?: boolean): Timer;
451-
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait: number, immediate?: boolean): Timer;
452-
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait: number, immediate?: boolean): Timer;
445+
public debounce<T>(target: T, methodName: keyof T, wait: number | string, immediate?: boolean): Timer;
446+
public debounce<T>(target: T, methodName: keyof T, arg1: any, wait: number | string, immediate?: boolean): Timer;
447+
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, wait: number | string, immediate?: boolean): Timer;
448+
public debounce<T>(target: T, methodName: keyof T, arg1: any, arg2: any, arg3: any, wait: number | string, immediate?: boolean): Timer;
453449

454450
// with target, with optional immediate
455-
public debounce(thisArg: any, method: () => void, wait: number, immediate?: boolean): Timer;
456-
public debounce<A>(thisArg: any, method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
457-
public debounce<A, B>(thisArg: any, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
458-
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;
451+
public debounce(thisArg: any | null, method: () => void, wait: number | string, immediate?: boolean): Timer;
452+
public debounce<A>(thisArg: any | null, method: (arg1: A) => void, arg1: A, wait: number | string, immediate?: boolean): Timer;
453+
public debounce<A, B>(thisArg: any | null, method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer;
454+
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;
459455

460456
// without target, with optional immediate
461-
public debounce(method: () => void, wait: number, immediate?: boolean): Timer;
462-
public debounce<A>(method: (arg1: A) => void, arg1: A, wait: number, immediate?: boolean): Timer;
463-
public debounce<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number, immediate?: boolean): Timer;
464-
public debounce<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number, immediate?: boolean): Timer;
465-
public debounce(targetOrThisArgOrMethod: object | (() => any), ...args): Timer {
457+
public debounce(method: () => void, wait: number | string, immediate?: boolean): Timer;
458+
public debounce<A>(method: (arg1: A) => void, arg1: A, wait: number | string, immediate?: boolean): Timer;
459+
public debounce<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer;
460+
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;
461+
public debounce(targetOrThisArgOrMethod: object | Function, ...args): Timer {
466462
let target;
467463
let method;
468464
let immediate;
@@ -481,7 +477,7 @@ export default class Backburner {
481477

482478
let type = typeof method;
483479
if (type === 'string') {
484-
method = target[method] as () => any;
480+
method = target[method];
485481
} else if (type !== 'function') {
486482
args.unshift(method);
487483
method = target;
@@ -542,7 +538,10 @@ export default class Backburner {
542538
}
543539

544540
public hasTimers() {
545-
return this._timers.length > 0 || this._debouncees.length > 0 || this._throttlers.length > 0 || this._autorun !== null;
541+
return this._timers.length > 0 ||
542+
this._debouncees.length > 0 ||
543+
this._throttlers.length > 0 ||
544+
this._autorun !== null;
546545
}
547546

548547
public cancel(timer?) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"test:server": "ember test --server",
1616
"serve": "ember serve",
1717
"lint": "tslint --project tsconfig.json",
18+
"problems": "tsc -p tsconfig.json --noEmit",
1819
"bench": "ember build && node ./bench/index.js"
1920
},
2021
"author": "Erik Bryn",

tests/debounce-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ QUnit.test('debounce without a target, with args', function(assert) {
411411
let bb = new Backburner(['batman']);
412412
let calledCount = 0;
413413
let calledWith: string[] = [];
414-
function debouncee(first: string) {
414+
function debouncee(first) {
415415
calledCount++;
416416
calledWith.push(first);
417417
}
@@ -435,7 +435,7 @@ QUnit.test('debounce without a target, with args - can be canceled', function(as
435435
let bb = new Backburner(['batman']);
436436
let calledCount = 0;
437437
let calledWith: string[] = [];
438-
function debouncee(first: string) {
438+
function debouncee(first) {
439439
calledCount++;
440440
calledWith.push(first);
441441
}

0 commit comments

Comments
 (0)