Skip to content

Module composition streategy proposal #643

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

Closed
wants to merge 1 commit into from
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
31 changes: 31 additions & 0 deletions src/CoreObservable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//Represents current 'Observable'

export interface Operators<T> {
map?(): Operators<any>;
filter?(): Operators<any>;
}

export class CoreObservable<T> implements Operators<T> {
filter(): Operators<any> {
console.log('filter');
return this;
}

static of: () => CoreObservable<any>;
}

export function applyMixins(derivedCtor: any, baseCtors: any[]) {
derivedCtor.of = function () {
return new derivedCtor();
};

console.log('mixin called');

baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
})
});
}

export default CoreObservable;
8 changes: 8 additions & 0 deletions src/ExtendedObservable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Represents current 'KitchenSink'

export default class ExtendedObservable<T> {
isEmpty(): ExtendedObservable<any> {
console.log('isEmpty');
return this;
}
}
6 changes: 6 additions & 0 deletions src/Rx.Core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import CoreObservable from './CoreObservable';

//core rx does not have anything to add
export {
CoreObservable as Observable
}
17 changes: 17 additions & 0 deletions src/Rx.Extended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import CoreObservable from './CoreObservable';
import {applyMixins} from './CoreObservable';
import ExtendedObservable from './ExtendedObservable';

//'KitchenSink' includes Core operator with additional 'isEmpty'
class Observable<T> implements CoreObservable<T>, ExtendedObservable<T> {
filter: () => Observable<any>;
isEmpty: () => Observable<any>;

static of: () => Observable<any>;
}

applyMixins(Observable, [CoreObservable, ExtendedObservable])

export {
Observable
};
22 changes: 22 additions & 0 deletions src/Rx.My.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import CoreObservable from './CoreObservable';
import {applyMixins} from './CoreObservable';

//Creating new module based on flavor, adding custom operator
export class CustomObservable<T> {
awesome(): CustomObservable<T>{
return this;
}
}

export class MyObservable<T> implements CoreObservable<T>, CustomObservable<T> {
filter: () => MyObservable<any>;
awesome: () => MyObservable<any>;

static of: () => MyObservable<any>;
}

applyMixins(MyObservable, [CoreObservable, CustomObservable])

export {
MyObservable as Observable
};
16 changes: 16 additions & 0 deletions src/testIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Rx from './Rx.Core';
import * as MyRx from './Rx.My';
import * as RxExtended from './Rx.Extended';

var rx = Rx.Observable.of();
rx.filter(); //works
//rx.isEmpty(); TS will complain

var myRx = MyRx.Observable.of();
myRx.awesome(); //new operator
myRx.filter(); //works from core
//myRx.isEmpty(); TS will complain

var extended = RxExtended.Observable.of();
extended.filter();
extended.isEmpty(); //works