Mirror: 馃帺 A tiny but capable push & pull stream library for TypeScript and Flow
at v2.0.2 1.3 kB view raw
1open Wonka_types; 2 3let debounce = f => 4 curry(source => 5 curry(sink => { 6 let gotEndSignal = ref(false); 7 let id: ref(option(Js.Global.timeoutId)) = ref(None); 8 9 let clearTimeout = () => 10 switch (id^) { 11 | Some(timeoutId) => 12 id := None; 13 Js.Global.clearTimeout(timeoutId); 14 | None => () 15 }; 16 17 source((. signal) => 18 switch (signal) { 19 | Start(tb) => 20 sink(. 21 Start( 22 (. signal) => 23 switch (signal) { 24 | Close => 25 clearTimeout(); 26 tb(. Close); 27 | _ => tb(. signal) 28 }, 29 ), 30 ) 31 | Push(x) => 32 clearTimeout(); 33 id := 34 Some( 35 Js.Global.setTimeout( 36 () => { 37 id := None; 38 sink(. signal); 39 if (gotEndSignal^) { 40 sink(. End); 41 }; 42 }, 43 f(. x), 44 ), 45 ); 46 | End => 47 gotEndSignal := true; 48 49 switch (id^) { 50 | None => sink(. End) 51 | _ => () 52 }; 53 } 54 ); 55 }) 56 );