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