File tree Expand file tree Collapse file tree 2 files changed +19
-9
lines changed
Expand file tree Collapse file tree 2 files changed +19
-9
lines changed Original file line number Diff line number Diff line change 22
33// Wrapper will prevent calls > n
44
5- const once = ( f ) => ( ...args ) => {
6- if ( ! f ) return ;
7- const res = f ( ...args ) ;
8- f = null ;
9- return res ;
5+ const emptiness = ( ) => { } ;
6+
7+ const once = ( fn ) => {
8+ if ( ! fn ) return emptiness ;
9+ let finished = false ;
10+ const wrapped = ( ...args ) => {
11+ if ( finished ) return ;
12+ finished = true ;
13+ fn ( ...args ) ;
14+ } ;
15+ return wrapped ;
1016} ;
1117
1218// Usage
Original file line number Diff line number Diff line change 22
33// Wrapper will prevent calls > n
44
5- const limit = ( count , f ) => {
5+ const emptiness = ( ) => { } ;
6+
7+ const limit = ( count , fn ) => {
68 let counter = 0 ;
7- return ( ...args ) => {
8- if ( counter === count ) return ;
9+ if ( ! fn ) return emptiness ;
10+ const wrapped = ( ...args ) => {
11+ if ( counter === count ) return null ;
912 counter ++ ;
10- return f ( ...args ) ;
13+ return fn ( ...args ) ;
1114 } ;
15+ return wrapped ;
1216} ;
1317
1418// Usage
You can’t perform that action at this time.
0 commit comments