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