Mirror: 🎩 A tiny but capable push & pull stream library for TypeScript and Flow
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore: Reduce size of ported code (#118)

* Simplify Callbag talkback signal conversion

* Remove symbol fallback for observables

* Merge placeholder functions

* Remove typeof check in delay operator

* Remove rest-spread parameter transform

* Use rest-spread in pipe fn

authored by

Phil Pluckthun and committed by
GitHub
b1e1678b 38cae8e7

+22 -30
+5
rollup.config.js
··· 37 37 buble({ 38 38 transforms: { 39 39 unicodeRegExp: false, 40 + defaultParameter: false, 40 41 dangerousForOf: true, 41 42 dangerousTaggedTemplateString: true, 43 + destructuring: false, 42 44 asyncAwait: false, 43 45 arrow: false, 44 46 classes: false, 47 + computedProperty: false, 45 48 conciseMethodProperty: false, 46 49 templateString: false, 47 50 objectRestSpread: false, 51 + parameterDestructuring: false, 52 + spreadRest: false, 48 53 }, 49 54 exclude: 'node_modules/**', 50 55 }),
+4 -12
src/callbag.ts
··· 1 - import { Source, SignalKind, TalkbackKind } from './types'; 1 + import { Source, SignalKind } from './types'; 2 2 import { push, start } from './helpers'; 3 3 4 4 interface Callbag<I, O> { ··· 13 13 if (signal === 0) { 14 14 sink( 15 15 start(signal => { 16 - if (signal === TalkbackKind.Pull) { 17 - data(1); 18 - } else { 19 - data(2); 20 - } 16 + data(signal + 1); 21 17 }) 22 18 ); 23 19 } else if (signal === 1) { 24 20 sink(push(data)); 25 - } else if (signal === 2) { 21 + } else { 26 22 sink(SignalKind.End); 27 23 } 28 24 }); ··· 37 33 sink(2); 38 34 } else if (signal.tag === SignalKind.Start) { 39 35 sink(0, (num: number) => { 40 - if (num === 1) { 41 - signal[0](TalkbackKind.Pull); 42 - } else if (num === 2) { 43 - signal[0](TalkbackKind.Close); 44 - } 36 + if (num < 3) signal[0](num - 1); 45 37 }); 46 38 } else { 47 39 sink(1, signal[0]);
+1 -3
src/helpers.ts
··· 1 1 import { TalkbackFn, TeardownFn, Start, Push, SignalKind } from './types'; 2 2 3 - export const talkbackPlaceholder: TalkbackFn = _signal => { 4 - /*noop*/ 5 - }; 6 3 export const teardownPlaceholder: TeardownFn = () => { 7 4 /*noop*/ 8 5 }; 6 + export const talkbackPlaceholder: TalkbackFn = teardownPlaceholder; 9 7 10 8 export function start<T>(talkback: TalkbackFn): Start<T> { 11 9 const box: any = [talkback];
+8 -11
src/observable.ts
··· 16 16 subscribe(observer: ObservableObserver<T>): ObservableSubscription; 17 17 } 18 18 19 - const observableSymbol: unique symbol = 20 - typeof Symbol === 'function' 21 - ? (Symbol as any).observable || ((Symbol as any).observable = Symbol('observable')) 22 - : '@@observable'; 19 + const observableSymbol = (): symbol => 20 + (Symbol as any).observable || ((Symbol as any).observable = Symbol('observable')); 23 21 24 22 export function fromObservable<T>(input: Observable<T>): Source<T> { 25 - const observable: Observable<T> = input[observableSymbol] 26 - ? (input as any)[observableSymbol]() 27 - : input; 23 + input = input[observableSymbol()] ? (input as any)[observableSymbol()]() : input; 28 24 return sink => { 29 - const subscription = observable.subscribe({ 25 + const subscription = input.subscribe({ 30 26 next(value: T) { 31 27 sink(push(value)); 32 28 }, ··· 46 42 } 47 43 48 44 export function toObservable<T>(source: Source<T>): Observable<T> { 49 - const observable: Observable<T> = { 45 + return { 50 46 subscribe(observer: ObservableObserver<T>) { 51 47 let talkback = talkbackPlaceholder; 52 48 let ended = false; ··· 73 69 }; 74 70 return subscription; 75 71 }, 72 + [observableSymbol()]() { 73 + return this; 74 + }, 76 75 }; 77 - observable[observableSymbol] = () => observable; 78 - return observable; 79 76 }
+1 -1
src/operators.ts
··· 801 801 return source => sink => { 802 802 let active = 0; 803 803 source(signal => { 804 - if (typeof signal !== 'number' && signal.tag === SignalKind.Start) { 804 + if (signal !== SignalKind.End && signal.tag === SignalKind.Start) { 805 805 sink(signal); 806 806 } else { 807 807 active++;
+3 -3
src/pipe.ts
··· 151 151 consumer: UnaryFn<Source<H>, R> 152 152 ): R; 153 153 154 - function pipe() { 155 - let x = arguments[0]; 156 - for (let i = 1, l = arguments.length; i < l; i++) x = arguments[i](x); 154 + function pipe(...args: any[]) { 155 + let x = args[0]; 156 + for (let i = 1, l = args.length; i < l; i++) x = args[i](x); 157 157 return x; 158 158 } 159 159