Skip to content

Commit

Permalink
style(typings): added enhanced typings for bindCallback.create
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Jan 9, 2016
1 parent bb8cec5 commit 64ff686
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {combineLatest as combineLatestStatic} from './operator/combineLatest-sta
import {concat as concatStatic} from './operator/concat-static';
import {merge as mergeStatic} from './operator/merge-static';
import {zip as zipStatic} from './operator/zip-static';
import {BoundCallbackObservable} from './observable/bindCallback';
import {DeferObservable} from './observable/defer';
import {EmptyObservable} from './observable/empty';
import {ForkJoinObservable} from './observable/forkJoin';
Expand All @@ -31,6 +30,8 @@ import {RangeObservable} from './observable/range';
import {InfiniteObservable} from './observable/never';
import {ErrorObservable} from './observable/throw';

import {observable} from './typings-generated';

/**
* A representation of any set of values over any amount of time. This the most basic building block
* of RxJS.
Expand Down Expand Up @@ -160,7 +161,7 @@ export class Observable<T> implements CoreOperators<T> {
}

// static method stubs
static bindCallback: typeof BoundCallbackObservable.create;
static bindCallback: observable.create.bindCallback;
static combineLatest: typeof combineLatestStatic;
static concat: typeof concatStatic;
static defer: typeof DeferObservable.create;
Expand Down
22 changes: 20 additions & 2 deletions src/observable/bindCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@ import {AsyncSubject} from '../subject/AsyncSubject';
export class BoundCallbackObservable<T> extends Observable<T> {
subject: AsyncSubject<T>;

/*-- *compute 0-6* static create<{|X|, }TResult>(
callbackFunc: ({|x|: |X|, }callback: (result: TResult) => any) => any
): ({|x|: |X|}) => Observable<TResult>; --*/
/*-- *compute 0-6* static create<{|X|, }TResult>(callbackFunc: ({|x|: |X|, }callback: (result: TResult) => any) => any,
selector: any,
scheduler: Scheduler): ({|x|: |X|}) => Observable<TResult>; --*/
/*-- *compute 0-6* static create<{|X|}>(callbackFunc: ({|x|: |X|, }
callback: (...args: any[]) => any) => any): ({|x|: |X|}) => Observable<any[]>; --*/
/*-- *compute 0-6* static create<{|X|, }TResult>(callbackFunc: ({|x|: |X|, }callback: (...args: any[]) => any) => any,
selector: (...args: any[]) => TResult,
scheduler?: Scheduler): ({|x|: |X|}) => Observable<TResult>; --*/
static create<T>(callbackFunc: Function): (...args: any[]) => Observable<T>;
static create<T>(callbackFunc: Function,
selector: Function = undefined,
scheduler?: Scheduler): Function {
selector: void,
scheduler: Scheduler): (...args: any[]) => Observable<T>;
static create<T>(callbackFunc: Function,
selector?: (...args: any[]) => T,
scheduler?: Scheduler): (...args: any[]) => Observable<T>;
static create<T>(callbackFunc: Function,
selector: Function | void = undefined,
scheduler?: Scheduler): (...args: any[]) => Observable<T> {
return (...args): Observable<T> => {
return new BoundCallbackObservable(callbackFunc, selector, args, scheduler);
};
Expand Down
39 changes: 39 additions & 0 deletions src/typings-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,43 @@ import {Scheduler} from './Scheduler';

/* tslint:disable:class-name *//* tslint:disable:max-line-length */
/* ||| MARKER ||| */
export module observable {
export module create {
export interface bindCallback {
<TResult>( callbackFunc: (callback: (result: TResult) => any) => any ): () => Observable<TResult>;
<T, TResult>( callbackFunc: (x1: T, callback: (result: TResult) => any) => any ): (x1: T) => Observable<TResult>;
<T, T2, TResult>( callbackFunc: (x1: T, x2: T2, callback: (result: TResult) => any) => any ): (x1: T, x2: T2) => Observable<TResult>;
<T, T2, T3, TResult>( callbackFunc: (x1: T, x2: T2, x3: T3, callback: (result: TResult) => any) => any ): (x1: T, x2: T2, x3: T3) => Observable<TResult>;
<T, T2, T3, T4, TResult>( callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, callback: (result: TResult) => any) => any ): (x1: T, x2: T2, x3: T3, x4: T4) => Observable<TResult>;
<T, T2, T3, T4, T5, TResult>( callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, callback: (result: TResult) => any) => any ): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5) => Observable<TResult>;
<T, T2, T3, T4, T5, T6, TResult>( callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, callback: (result: TResult) => any) => any ): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => Observable<TResult>;
<TResult>(callbackFunc: (callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): () => Observable<TResult>;
<T, TResult>(callbackFunc: (x1: T, callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): (x1: T) => Observable<TResult>;
<T, T2, TResult>(callbackFunc: (x1: T, x2: T2, callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): (x1: T, x2: T2) => Observable<TResult>;
<T, T2, T3, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): (x1: T, x2: T2, x3: T3) => Observable<TResult>;
<T, T2, T3, T4, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): (x1: T, x2: T2, x3: T3, x4: T4) => Observable<TResult>;
<T, T2, T3, T4, T5, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5) => Observable<TResult>;
<T, T2, T3, T4, T5, T6, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, callback: (result: TResult) => any) => any, selector: any, scheduler: Scheduler): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => Observable<TResult>;
(callbackFunc: ( callback: (...args: any[]) => any) => any): () => Observable<any[]>;
<T>(callbackFunc: (x1: T, callback: (...args: any[]) => any) => any): (x1: T) => Observable<any[]>;
<T, T2>(callbackFunc: (x1: T, x2: T2, callback: (...args: any[]) => any) => any): (x1: T, x2: T2) => Observable<any[]>;
<T, T2, T3>(callbackFunc: (x1: T, x2: T2, x3: T3, callback: (...args: any[]) => any) => any): (x1: T, x2: T2, x3: T3) => Observable<any[]>;
<T, T2, T3, T4>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, callback: (...args: any[]) => any) => any): (x1: T, x2: T2, x3: T3, x4: T4) => Observable<any[]>;
<T, T2, T3, T4, T5>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, callback: (...args: any[]) => any) => any): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5) => Observable<any[]>;
<T, T2, T3, T4, T5, T6>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, callback: (...args: any[]) => any) => any): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => Observable<any[]>;
<TResult>(callbackFunc: (callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): () => Observable<TResult>;
<T, TResult>(callbackFunc: (x1: T, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): (x1: T) => Observable<TResult>;
<T, T2, TResult>(callbackFunc: (x1: T, x2: T2, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): (x1: T, x2: T2) => Observable<TResult>;
<T, T2, T3, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): (x1: T, x2: T2, x3: T3) => Observable<TResult>;
<T, T2, T3, T4, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): (x1: T, x2: T2, x3: T3, x4: T4) => Observable<TResult>;
<T, T2, T3, T4, T5, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5) => Observable<TResult>;
<T, T2, T3, T4, T5, T6, TResult>(callbackFunc: (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, callback: (...args: any[]) => any) => any, selector: (...args: any[]) => TResult, scheduler?: Scheduler): (x1: T, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => Observable<TResult>;
<T>(callbackFunc: Function): (...args: any[]) => Observable<T>;
<T>(callbackFunc: Function, selector: void, scheduler: Scheduler): (...args: any[]) => Observable<T>;
<T>(callbackFunc: Function, selector?: (...args: any[]) => T, scheduler?: Scheduler): (...args: any[]) => Observable<T>;
}
}
}
export module operator {
}
/* ||| MARKER ||| */
47 changes: 27 additions & 20 deletions tools/typing-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ for (var i = 0; i < contents.length; i++) {
} else {
if (fs.existsSync('./src/operator/' + filename + '.ts')) {
fileContent = fs.readFileSync('./src/operator/' + filename + '.ts').toString('utf8');
} else {
fileContent = fs.readFileSync('./src/operator/extended/' + filename + '.ts').toString('utf8');
}
}

Expand Down Expand Up @@ -65,12 +63,16 @@ for (var i = 0; i < contents.length; i++) {
.replace(/([\w|\d]*?)\: (\w*) = [\w|\d|\.|\-]*/g, '$1?: $2');

if (method[method.length - 1] === ';' || method[method.length - 1] === '{') {
method = method.substr(0, method.length - 1).trim();
method = method.replace(/(,\s){2}/g, ', ').substr(0, method.length - 1).trim();
}

if (!_static) {
method = method.replace(/^<T>/, '').replace(/^<T, /, '<');
}
method = method.replace(/^<>/, '');
if (method.indexOf(';') === -1) {
method += ';';
}
methods.push(method);
}
} while (result);
Expand Down Expand Up @@ -99,7 +101,7 @@ for (var i in observables) {
var value = observables[i];
fileResult += ' export module ' + value.type + ' {\n';
fileResult += ' export interface ' + value.name + ' {\n';
fileResult += ' ' + value.methods.join(';\n ') + ';\n';
fileResult += ' ' + value.methods.join('\n ') + '\n';
fileResult += ' }\n';
fileResult += ' }\n';
}
Expand All @@ -110,7 +112,7 @@ for (var i in operators) {
var value = operators[i];
fileResult += ' export module ' + value.type + ' {\n';
fileResult += ' export interface ' + value.name + ' {\n';
fileResult += ' ' + value.methods.join(';\n ') + ';\n';
fileResult += ' ' + value.methods.join('\n ') + '\n';
fileResult += ' }\n';
fileResult += ' }\n';
}
Expand All @@ -124,10 +126,11 @@ fs.writeFileSync('./src/typings-generated.ts', typingsContent);

function computeTypingsFor(s) {
var captureRegex = /\/\*\-\-([\s|\S]*?)-\-\*\//g;
var computeNumberRegex = /\*compute (\d.*?)?\*/;
var computeNumberRegex = /\*compute (\d.*?)?(x\d.*?)?\*/;
var tokenRegex = /\{.*?\}/g;

s = s.replace(captureRegex, function(capture) {
var start, end;
capture = capture.trim();
capture = capture.substr(3, capture.length - 3 * 2);
var compute = computeNumberRegex.exec(capture);
Expand All @@ -138,30 +141,34 @@ function computeTypingsFor(s) {
}
var range = compute.split('-');
if (range.length === 1) {
var start = 1;
var end = range[0];
start = 1;
end = +range[0];
} else {
var start = range[0];
var end = range[1];
start = +range[0];
end = +range[1];
}

capture = capture.replace(computeNumberRegex, '').trim();

var tokenResult;
var results = [];
for (var number = start; number <= end; number++) {
var res = capture.replace(tokenRegex, function(capture, index, str) {
var items = [];
capture = capture.substr(1, capture.length - 2);
var union = capture.indexOf('|U|') > -1;
for (var i = start; i <= number; i++) {
var typeName = 'T' + (i === 1 ? '' : i);
items.push(capture
.replace(/\|U\|/g, typeName)
.replace(/\|X\|/g, typeName)
.replace(/\|v\|/g, 'v' + i)
);
}
var union = capture.indexOf('$X$') > -1;

for (var x = start; x <= number; x++) {
if (x === 0) {
continue;
}
var xTypeName = 'T' + (x === 1 ? '' : x);
var r = capture
.replace(/\|X\|/g, xTypeName)
.replace(/\$X\$/g, xTypeName)
.replace(/\|x\|/g, 'x' + x);

items.push(r);
}

return items.join(union ? ' | ' : ', ');
});
Expand Down

0 comments on commit 64ff686

Please sign in to comment.