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