Module composition streategy proposal #643
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR intended to show proof-of-concept of proposal, neither mature or intended to be actually merged.
While seeing issues like #635, #642, came to feel if there's way to create module met requirements like
This proposal is based on Mixin of typescript, compose class implementations to extend modules if necessary.
How composition works
Attached PR illustrates rough mechanisms of composition. First, there is
Core
implementation of observable having bare minimum set inCoreObservable.ts
. In this PR it implements interface of CoreOperator, this could be ambient merging introduced in typescript nightly. (There's one caveat of ambient merging, mixin class should declare all interfaces even if it's not implemented in class)Rx.Core.ts
does exports it directly without any extension.ExtendedObservable.ts
represents fully features set of Observable module, corresponds to current 'KitchenSink'. it does implements additional operator.Rx.Extended.ts
is place where actual composition occurs, it declares mixinObservable<T>
which implements Core & Extended both, then callapplyMixin
to compose mixin class at runtime. This runtime composition occurs very first time moduleExtendedObservable
is imported.Rx.My.ts
is representing cases where any consumer would like to create custom module for own flavor. In this PR, it utilizes core observable along with adding new operator implementation.testIndex.ts
shows usage of each module, simply import it and type definition starts to work.Declaring Mixin
When declaraing mixin, it does do some specifics
of
,lift
, etcs: Actual implementation of factory functions are injected while applying mixin (line 18 in
CoreObservable
) it is important to declare signature in each mixin explicitly. This prevents issues like Type definition for extended operator is not published #642, extended observale generator to return extended type to allow correct type inference.: For example.
CoreObservable::filter():Operators
should be declared in mixin asMyObservable::filter():MyObservable
. This is for type inference as same as 1.Possible pros
Cons