Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generated typings for bindCallback.create #1087

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
},
"scripts": {
"build_all": "npm run build_es6 && npm run build_amd && npm run build_cjs && npm run build_global && npm run generate_packages",
"build_amd": "rm -rf dist/amd && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts -m amd --outDir dist/amd --sourcemap --target ES5 --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"build_cjs": "rm -rf dist/cjs && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts src/Rx.KitchenSink.ts -m commonjs --outDir dist/cjs --sourcemap --target ES5 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"build_es6": "rm -rf dist/es6 && tsc src/Rx.ts src/Rx.KitchenSink.ts --outDir dist/es6 --sourceMap --target ES6 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"build_amd": "npm run build_operators && rm -rf dist/amd && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts -m amd --outDir dist/amd --sourcemap --target ES5 --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"build_cjs": "npm run build_operators && rm -rf dist/cjs && tsc typings/es6-shim/es6-shim.d.ts src/Rx.ts src/Rx.KitchenSink.ts -m commonjs --outDir dist/cjs --sourcemap --target ES5 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"build_es6": "npm run build_operators && rm -rf dist/es6 && tsc src/Rx.ts src/Rx.KitchenSink.ts --outDir dist/es6 --sourceMap --target ES6 -d --diagnostics --pretty --noImplicitAny --suppressImplicitAnyIndexErrors",
"build_closure": "java -jar ./node_modules/google-closure-compiler/compiler.jar ./dist/global/Rx.umd.js --language_in ECMASCRIPT5 --create_source_map ./dist/global/Rx.umd.min.js.map --js_output_file ./dist/global/Rx.umd.min.js",
"build_global": "rm -rf dist/global && mkdir \"dist/global\" && node tools/make-umd-bundle.js && node tools/make-system-bundle.js && npm run build_closure",
"build_perf": "npm run build_cjs && npm run build_global && webdriver-manager update && npm run perf",
"build_test": "rm -rf dist/ && npm run lint && npm run build_cjs && jasmine",
"build_cover": "rm -rf dist/ && npm run lint && npm run build_cjs && npm run cover",
"build_docs": "./docgen.sh",
"build_operators": "node tools/typing-generator.js",
"lint_perf": "eslint perf/",
"lint_spec": "eslint spec/**/*.js",
"lint_src": "tslint -c tslint.json src/*.ts src/**/*.ts src/**/**/*.ts",
Expand Down
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 @@ -32,6 +31,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 @@ -161,7 +162,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
18 changes: 18 additions & 0 deletions src/observable/bindCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ 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: 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> {
Expand Down
45 changes: 45 additions & 0 deletions src/typings-generated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {Observable} from './Observable';
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 ||| */
182 changes: 182 additions & 0 deletions tools/typing-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
var fs = require('fs');
var regex = /export (interface|class) .*?Operators<T>\s*\{([\S|\s]*)\}/;

var core = fs.readFileSync('./src/Observable.ts').toString();
var kitchenSink = fs.readFileSync('./src/Rx.KitchenSink.ts').toString();
var combinedMethods = core.match(regex)[2].trim() + '\n' + kitchenSink.match(regex)[2].trim();
var contents = combinedMethods.split('\n');

var hasOperators = {};
var observables = {};
var operators = {};
var fileResult = '';

for (var i = 0; i < contents.length; i++) {
var item = contents[i].trim();
if (item) {
var file = item.match(/(.*?)\: (operator|observable)\.(proto|static|create)\.(.*?)(<T>)?;/);
if (!file) {
continue;
}

var _observable = file[2] === "observable";
var _static = file[3] === "static" || _observable;
var name = file[4].trim();
var filename = file[4].trim() + (!_observable && _static ? '-static' : '');
var fileContent;

if (_observable) {
fileContent = fs.readFileSync('./src/observable/' + filename + '.ts').toString('utf8');
} else {
if (fs.existsSync('./src/operator/' + filename + '.ts')) {
fileContent = fs.readFileSync('./src/operator/' + filename + '.ts').toString('utf8');
}
}

fileContent = computeTypingsFor(fileContent);

var methods = [];

var r;
if (_observable) {
r = new RegExp('static [_]?' + file[3] + '([\\s|\\S]*?[\\;\\{])', 'g');
} else {
r = new RegExp('export function [_]?' + name + '([\\s|\\S]*?[\\;\\{])', 'g');
}

do {
var result = r.exec(fileContent);
if (result) {
var method = result[1].trim();
if (methods.length > 0 && method.indexOf('{') > -1) {
continue;
}

method = method.split(/\n/g)
.filter(function(x) {
return !!x;
})
.map(function(x) {
return ('' + x).trim();
})
.join(' ')
.replace(/([\w|\d]*?)\: (\w*) = [\w|\d|\.|\-]*/g, '$1?: $2');

if (method[method.length - 1] === ';' || method[method.length - 1] === '{') {
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);

if (!hasOperators[filename]) {
hasOperators[filename] = true;
if (_observable) {
observables[file[2]] = {
name: name,
type: file[3],
methods: methods
};
} else {
operators[file[2]] = {
name: name,
type: file[3],
methods: methods
};
}
}
}
}

fileResult += 'export module observable {\n';
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 += ' }\n';
fileResult += ' }\n';
}
fileResult += '}\n';

fileResult += 'export module operator {\n';
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 += ' }\n';
fileResult += ' }\n';
}
fileResult += '}\n';

var typingsContent = fs.readFileSync('./src/typings-generated.ts').toString();
fileResult = '/* ||| MARKER ||| */\n' + fileResult + '/* ||| MARKER ||| */';
typingsContent = typingsContent.replace(/(\/\* \|\|\| MARKER \|\|\| \*\/[\s|\S]*?\/\* \|\|\| MARKER \|\|\| \*\/)/, fileResult);
fs.writeFileSync('./src/typings-generated.ts', typingsContent);


function computeTypingsFor(s) {
var captureRegex = /\/\*\-\-([\s|\S]*?)-\-\*\//g;
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);
if (compute) {
compute = compute[1] || '6';
} else {
compute = '6';
}
var range = compute.split('-');
if (range.length === 1) {
start = 1;
end = +range[0];
} else {
start = +range[0];
end = +range[1];
}

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

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('$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 ? ' | ' : ', ');
});
results.push(res);
}

return results.join('\n');
});

return s;
}