As mentioned before, each line in RxLine is a stream of objects and a transform that is to be applied on those objects. The core of any transform is a function that returns one or more values based on given value either synchronously or asynchronously, i.e.:
1export type Function<I, O> = (i: I) => O | Promise<O> | Observable<O>;
Working with these functions can be inconvenient due to different possible return types.
RxLine provides the Transform
class and transform()
function to address that inconvenience:
1import { transform } from 'rxline';
2import { from } from 'rxjs';
3
4
5const X = transform(async x => await somefunc(x));
6const Y = transform(x => from([x, x + 1]));
7const Z = X.combine(Y);
8Z.apply(42).subscribe(console.log);
9
10// Result:
11// > somefunc(42)
12// > somefunc(42) + 1
.combine()
can also be provided a Function
directly:
1const Z = transform(async x => await somefunc(x))
2 .combine(x => from([x, x + 1]));
Transforms
are the objects that you should provide to a line's .pipe()
method.
For convenience, if you provide it with any Function
, it will automatically wrap it with the transform()
method:
1const l = line(...); // --> or files(...) or any other line generator
2
3// These two are equivalent
4l.pipe(x => x + 2); // --> These two are equivalent
5l.pipe(transform(x => x + 2)); // --> These two are equivalent
Additionally, chaining pipe on a line (or providing multiple arguments to it) is equivalent
of calling .combine()
on the line's transform:
1const l = line(...);
2
3// These three are equivalent:
4l.pipe(transformA).pipe(transformB).pipe(transformC); // --> These three are equivalent
5l.pipe(transformA, transformB, transformC); // --> These three are equivalent
6l.pipe(transformA.combine(transformB).combine(transformC)); // --> These three are equivalent
Which can be helpful in writing your own custom transforms:
1import { transpile } from 'typescript';
2import { mapExt, mapContent } from 'rxline';
3
4export const compileTS =
5 transform(mapContent(content => transpile(content))) // --> so will transpile the typescript
6 .combine(mapExt(() => '.js')); // --> and change the extension to `.js`