-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathindex.ts
935 lines (797 loc) · 28.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
import { assert } from '@ember/debug';
import { onErrorTarget } from '@ember/-internals/error-handling';
import { flushAsyncObservers } from '@ember/-internals/metal';
import Backburner, { DeferredActionQueues, Timer } from 'backburner';
export { Timer };
// Partial types from https://medium.com/codex/currying-in-typescript-ca5226c85b85
type PartialParams<P extends any[]> = P extends [infer First, ...infer Rest]
? [] | [First] | [First, ...PartialParams<Rest>]
: // This is necessary to handle optional tuple values
Required<P> extends [infer First, ...infer Rest]
? [] | [First | undefined] | [First | undefined, ...PartialParams<Partial<Rest>>]
: never;
type RemainingParams<PartialParams extends any[], All extends any[]> = PartialParams extends [
infer First,
...infer Rest
]
? All extends [infer AllFirst, ...infer AllRest]
? First extends AllFirst
? RemainingParams<Rest, AllRest>
: never
: // This is necessary to handle optional tuple values
Required<All> extends [infer AllFirst, ...infer AllRest]
? First extends AllFirst | undefined
? Partial<RemainingParams<Rest, AllRest>>
: never
: never
: PartialParams extends []
? All
: never;
let currentRunLoop: DeferredActionQueues | null = null;
export function _getCurrentRunLoop() {
return currentRunLoop;
}
function onBegin(current: DeferredActionQueues) {
currentRunLoop = current;
}
function onEnd(_current: DeferredActionQueues, next: DeferredActionQueues) {
currentRunLoop = next;
flushAsyncObservers();
}
function flush(queueName: string, next: () => void) {
if (queueName === 'render' || queueName === _rsvpErrorQueue) {
flushAsyncObservers();
}
next();
}
export const _rsvpErrorQueue = `${Math.random()}${Date.now()}`.replace('.', '');
/**
Array of named queues. This array determines the order in which queues
are flushed at the end of the RunLoop. You can define your own queues by
simply adding the queue name to this array. Normally you should not need
to inspect or modify this property.
@property queues
@type Array
@default ['actions', 'destroy']
@private
*/
export const _queues = [
'actions',
// used in router transitions to prevent unnecessary loading state entry
// if all context promises resolve on the 'actions' queue first
'routerTransitions',
'render',
'afterRender',
'destroy',
// used to re-throw unhandled RSVP rejection errors specifically in this
// position to avoid breaking anything rendered in the other sections
_rsvpErrorQueue,
];
export const _backburner = new Backburner(_queues, {
defaultQueue: 'actions',
onBegin,
onEnd,
onErrorTarget,
onErrorMethod: 'onerror',
flush,
});
/**
@module @ember/runloop
*/
// ..........................................................
// run - this is ideally the only public API the dev sees
//
/**
Runs the passed target and method inside of a RunLoop, ensuring any
deferred actions including bindings and views updates are flushed at the
end.
Normally you should not need to invoke this method yourself. However if
you are implementing raw event handlers when interfacing with other
libraries or plugins, you should probably wrap all of your code inside this
call.
```javascript
import { run } from '@ember/runloop';
run(function() {
// code to be executed within a RunLoop
});
```
@method run
@for @ember/runloop
@static
@param {Object} [target] target of method to call
@param {Function|String} method Method to invoke.
May be a function or a string. If you pass a string
then it will be looked up on the passed target.
@param {Object} [args*] Any additional arguments you wish to pass to the method.
@return {Object} return value from invoking the passed function.
@public
*/
export function run<F extends (...args: any[]) => any>(method: F, ...args: Parameters<F>): void;
export function run<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: Parameters<F>
): void;
export function run<T, U extends keyof T>(
target: T,
method: U,
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : []
): void;
export function run(...args: any[]): void {
// @ts-expect-error TS doesn't like our spread args
return _backburner.run(...args);
}
/**
If no run-loop is present, it creates a new one. If a run loop is
present it will queue itself to run on the existing run-loops action
queue.
Please note: This is not for normal usage, and should be used sparingly.
If invoked when not within a run loop:
```javascript
import { join } from '@ember/runloop';
join(function() {
// creates a new run-loop
});
```
Alternatively, if called within an existing run loop:
```javascript
import { run, join } from '@ember/runloop';
run(function() {
// creates a new run-loop
join(function() {
// joins with the existing run-loop, and queues for invocation on
// the existing run-loops action queue.
});
});
```
@method join
@static
@for @ember/runloop
@param {Object} [target] target of method to call
@param {Function|String} method Method to invoke.
May be a function or a string. If you pass a string
then it will be looked up on the passed target.
@param {Object} [args*] Any additional arguments you wish to pass to the method.
@return {Object} Return value from invoking the passed function. Please note,
when called within an existing loop, no return value is possible.
@public
*/
export function join<F extends (...args: any[]) => any>(
method: F,
...args: Parameters<F>
): ReturnType<F> | void;
export function join<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: Parameters<F>
): ReturnType<F> | void;
export function join<T, U extends keyof T>(
target: T,
method: U,
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : []
): T[U] extends (...args: any[]) => any ? ReturnType<T[U]> | void : void;
export function join(methodOrTarget: any, methodOrArg?: any, ...additionalArgs: any[]): any {
return _backburner.join(methodOrTarget, methodOrArg, ...additionalArgs);
}
/**
Allows you to specify which context to call the specified function in while
adding the execution of that function to the Ember run loop. This ability
makes this method a great way to asynchronously integrate third-party libraries
into your Ember application.
`bind` takes two main arguments, the desired context and the function to
invoke in that context. Any additional arguments will be supplied as arguments
to the function that is passed in.
Let's use the creation of a TinyMCE component as an example. Currently,
TinyMCE provides a setup configuration option we can use to do some processing
after the TinyMCE instance is initialized but before it is actually rendered.
We can use that setup option to do some additional setup for our component.
The component itself could look something like the following:
```app/components/rich-text-editor.js
import Component from '@ember/component';
import { on } from '@ember/object/evented';
import { bind } from '@ember/runloop';
export default Component.extend({
initializeTinyMCE: on('didInsertElement', function() {
tinymce.init({
selector: '#' + this.$().prop('id'),
setup: bind(this, this.setupEditor)
});
}),
didInsertElement() {
tinymce.init({
selector: '#' + this.$().prop('id'),
setup: bind(this, this.setupEditor)
});
}
setupEditor(editor) {
this.set('editor', editor);
editor.on('change', function() {
console.log('content changed!');
});
}
});
```
In this example, we use `bind` to bind the setupEditor method to the
context of the RichTextEditor component and to have the invocation of that
method be safely handled and executed by the Ember run loop.
@method bind
@static
@for @ember/runloop
@param {Object} [target] target of method to call
@param {Function|String} method Method to invoke.
May be a function or a string. If you pass a string
then it will be looked up on the passed target.
@param {Object} [args*] Any additional arguments you wish to pass to the method.
@return {Function} returns a new function that will always have a particular context
@since 1.4.0
@public
*/
export function bind<
T,
F extends (this: T, ...args: any[]) => any,
A extends PartialParams<Parameters<F>>
>(
target: T,
method: F,
...args: A
): (...args: RemainingParams<A, Parameters<F>>) => ReturnType<F> | void;
export function bind<F extends (...args: any[]) => any, A extends PartialParams<Parameters<F>>>(
method: F,
...args: A
): (...args: RemainingParams<A, Parameters<F>>) => ReturnType<F> | void;
export function bind<
T,
U extends keyof T,
A extends T[U] extends (...args: any[]) => any ? PartialParams<Parameters<T[U]>> : []
>(
target: T,
method: U,
...args: A
): T[U] extends (...args: any[]) => any
? (...args: RemainingParams<A, Parameters<T[U]>>) => ReturnType<T[U]> | void
: never;
export function bind(...curried: any[]): any {
assert(
'could not find a suitable method to bind',
(function (methodOrTarget, methodOrArg) {
// Applies the same logic as backburner parseArgs for detecting if a method
// is actually being passed.
let length = arguments.length;
if (length === 0) {
return false;
} else if (length === 1) {
return typeof methodOrTarget === 'function';
} else {
return (
typeof methodOrArg === 'function' || // second argument is a function
(methodOrTarget !== null &&
typeof methodOrArg === 'string' &&
methodOrArg in methodOrTarget) || // second argument is the name of a method in first argument
typeof methodOrTarget === 'function' //first argument is a function
);
}
// @ts-expect-error TS doesn't like our spread args
})(...curried)
);
// @ts-expect-error TS doesn't like our spread args
return (...args: any[]) => join(...curried.concat(args));
}
/**
Begins a new RunLoop. Any deferred actions invoked after the begin will
be buffered until you invoke a matching call to `end()`. This is
a lower-level way to use a RunLoop instead of using `run()`.
```javascript
import { begin, end } from '@ember/runloop';
begin();
// code to be executed within a RunLoop
end();
```
@method begin
@static
@for @ember/runloop
@return {void}
@public
*/
export function begin() {
_backburner.begin();
}
/**
Ends a RunLoop. This must be called sometime after you call
`begin()` to flush any deferred actions. This is a lower-level way
to use a RunLoop instead of using `run()`.
```javascript
import { begin, end } from '@ember/runloop';
begin();
// code to be executed within a RunLoop
end();
```
@method end
@static
@for @ember/runloop
@return {void}
@public
*/
export function end() {
_backburner.end();
}
/**
Adds the passed target/method and any optional arguments to the named
queue to be executed at the end of the RunLoop. If you have not already
started a RunLoop when calling this method one will be started for you
automatically.
At the end of a RunLoop, any methods scheduled in this way will be invoked.
Methods will be invoked in an order matching the named queues defined in
the `queues` property.
```javascript
import { schedule } from '@ember/runloop';
schedule('afterRender', this, function() {
// this will be executed in the 'afterRender' queue
console.log('scheduled on afterRender queue');
});
schedule('actions', this, function() {
// this will be executed in the 'actions' queue
console.log('scheduled on actions queue');
});
// Note the functions will be run in order based on the run queues order.
// Output would be:
// scheduled on actions queue
// scheduled on afterRender queue
```
@method schedule
@static
@for @ember/runloop
@param {String} queue The name of the queue to schedule against. Default queues is 'actions'
@param {Object} [target] target object to use as the context when invoking a method.
@param {String|Function} method The method to invoke. If you pass a string it
will be resolved on the target object at the time the scheduled item is
invoked allowing you to change the target function.
@param {Object} [arguments*] Optional arguments to be passed to the queued method.
@return {*} Timer information for use in canceling, see `cancel`.
@public
*/
export function schedule<F extends (...args: any[]) => any>(
queueName: string,
method: F,
...args: Parameters<F>
): Timer;
export function schedule<T, F extends (this: T, ...args: any[]) => any>(
queueName: string,
target: T,
method: F,
...args: Parameters<F>
): Timer;
export function schedule<T, U extends keyof T>(
queueName: string,
target: T,
method: U,
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : []
): Timer;
export function schedule(...args: any[]): Timer {
// @ts-expect-error TS doesn't like the rest args here
return _backburner.schedule(...args);
}
// Used by global test teardown
export function _hasScheduledTimers() {
return _backburner.hasTimers();
}
// Used by global test teardown
export function _cancelTimers() {
_backburner.cancelTimers();
}
/**
Invokes the passed target/method and optional arguments after a specified
period of time. The last parameter of this method must always be a number
of milliseconds.
You should use this method whenever you need to run some action after a
period of time instead of using `setTimeout()`. This method will ensure that
items that expire during the same script execution cycle all execute
together, which is often more efficient than using a real setTimeout.
```javascript
import { later } from '@ember/runloop';
later(myContext, function() {
// code here will execute within a RunLoop in about 500ms with this == myContext
}, 500);
```
@method later
@static
@for @ember/runloop
@param {Object} [target] target of method to invoke
@param {Function|String} method The method to invoke.
If you pass a string it will be resolved on the
target at the time the method is invoked.
@param {Object} [args*] Optional arguments to pass to the timeout.
@param {Number} wait Number of milliseconds to wait.
@return {*} Timer information for use in canceling, see `cancel`.
@public
*/
export function later<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: [...args: Parameters<F>, wait: string | number]
): Timer;
export function later<F extends (...args: any[]) => any>(
method: F,
...args: [...args: Parameters<F>, wait: string | number]
): Timer;
export function later<T, U extends keyof T>(
target: T,
method: U,
...args: [
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : [],
wait: string | number
]
): Timer;
export function later(...args: any): Timer {
return _backburner.later(...args);
}
/**
Schedule a function to run one time during the current RunLoop. This is equivalent
to calling `scheduleOnce` with the "actions" queue.
@method once
@static
@for @ember/runloop
@param {Object} [target] The target of the method to invoke.
@param {Function|String} method The method to invoke.
If you pass a string it will be resolved on the
target at the time the method is invoked.
@param {Object} [args*] Optional arguments to pass to the timeout.
@return {Object} Timer information for use in canceling, see `cancel`.
@public
*/
export function once<F extends (...args: any[]) => any>(method: F, ...args: Parameters<F>): Timer;
export function once<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: Parameters<F>
): Timer;
export function once<T, U extends keyof T>(
target: T,
method: U,
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : []
): Timer;
export function once(...args: any[]): Timer {
// @ts-expect-error TS doesn't like the rest args here
return _backburner.scheduleOnce('actions', ...args);
}
/**
Schedules a function to run one time in a given queue of the current RunLoop.
Calling this method with the same queue/target/method combination will have
no effect (past the initial call).
Note that although you can pass optional arguments these will not be
considered when looking for duplicates. New arguments will replace previous
calls.
```javascript
import { run, scheduleOnce } from '@ember/runloop';
function sayHi() {
console.log('hi');
}
run(function() {
scheduleOnce('afterRender', myContext, sayHi);
scheduleOnce('afterRender', myContext, sayHi);
// sayHi will only be executed once, in the afterRender queue of the RunLoop
});
```
Also note that for `scheduleOnce` to prevent additional calls, you need to
pass the same function instance. The following case works as expected:
```javascript
function log() {
console.log('Logging only once');
}
function scheduleIt() {
scheduleOnce('actions', myContext, log);
}
scheduleIt();
scheduleIt();
```
But this other case will schedule the function multiple times:
```javascript
import { scheduleOnce } from '@ember/runloop';
function scheduleIt() {
scheduleOnce('actions', myContext, function() {
console.log('Closure');
});
}
scheduleIt();
scheduleIt();
// "Closure" will print twice, even though we're using `scheduleOnce`,
// because the function we pass to it won't match the
// previously scheduled operation.
```
Available queues, and their order, can be found at `queues`
@method scheduleOnce
@static
@for @ember/runloop
@param {String} [queue] The name of the queue to schedule against. Default queues is 'actions'.
@param {Object} [target] The target of the method to invoke.
@param {Function|String} method The method to invoke.
If you pass a string it will be resolved on the
target at the time the method is invoked.
@param {Object} [args*] Optional arguments to pass to the timeout.
@return {Object} Timer information for use in canceling, see `cancel`.
@public
*/
export function scheduleOnce<F extends (...args: any[]) => any>(
queueName: string,
method: F,
...args: Parameters<F>
): Timer;
export function scheduleOnce<T, F extends (this: T, ...args: any[]) => any>(
queueName: string,
target: T,
method: F,
...args: Parameters<F>
): Timer;
export function scheduleOnce<T, U extends keyof T>(
queueName: string,
target: T,
method: U,
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : []
): Timer;
export function scheduleOnce(...args: any[]): Timer {
// @ts-expect-error TS doesn't like the rest args here
return _backburner.scheduleOnce(...args);
}
/**
Schedules an item to run from within a separate run loop, after
control has been returned to the system. This is equivalent to calling
`later` with a wait time of 1ms.
```javascript
import { next } from '@ember/runloop';
next(myContext, function() {
// code to be executed in the next run loop,
// which will be scheduled after the current one
});
```
Multiple operations scheduled with `next` will coalesce
into the same later run loop, along with any other operations
scheduled by `later` that expire right around the same
time that `next` operations will fire.
Note that there are often alternatives to using `next`.
For instance, if you'd like to schedule an operation to happen
after all DOM element operations have completed within the current
run loop, you can make use of the `afterRender` run loop queue (added
by the `ember-views` package, along with the preceding `render` queue
where all the DOM element operations happen).
Example:
```app/components/my-component.js
import Component from '@ember/component';
import { scheduleOnce } from '@ember/runloop';
export Component.extend({
didInsertElement() {
this._super(...arguments);
scheduleOnce('afterRender', this, 'processChildElements');
},
processChildElements() {
// ... do something with component's child component
// elements after they've finished rendering, which
// can't be done within this component's
// `didInsertElement` hook because that gets run
// before the child elements have been added to the DOM.
}
});
```
One benefit of the above approach compared to using `next` is
that you will be able to perform DOM/CSS operations before unprocessed
elements are rendered to the screen, which may prevent flickering or
other artifacts caused by delaying processing until after rendering.
The other major benefit to the above approach is that `next`
introduces an element of non-determinism, which can make things much
harder to test, due to its reliance on `setTimeout`; it's much harder
to guarantee the order of scheduled operations when they are scheduled
outside of the current run loop, i.e. with `next`.
@method next
@static
@for @ember/runloop
@param {Object} [target] target of method to invoke
@param {Function|String} method The method to invoke.
If you pass a string it will be resolved on the
target at the time the method is invoked.
@param {Object} [args*] Optional arguments to pass to the timeout.
@return {Object} Timer information for use in canceling, see `cancel`.
@public
*/
export function next<F extends (...args: any[]) => any>(method: F, ...args: Parameters<F>): Timer;
export function next<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: Parameters<F>
): Timer;
export function next<T, U extends keyof T>(
target: T,
method: U,
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : []
): Timer;
export function next(...args: any[]) {
return _backburner.later(...args, 1);
}
/**
Cancels a scheduled item. Must be a value returned by `later()`,
`once()`, `scheduleOnce()`, `next()`, `debounce()`, or
`throttle()`.
```javascript
import {
next,
cancel,
later,
scheduleOnce,
once,
throttle,
debounce
} from '@ember/runloop';
let runNext = next(myContext, function() {
// will not be executed
});
cancel(runNext);
let runLater = later(myContext, function() {
// will not be executed
}, 500);
cancel(runLater);
let runScheduleOnce = scheduleOnce('afterRender', myContext, function() {
// will not be executed
});
cancel(runScheduleOnce);
let runOnce = once(myContext, function() {
// will not be executed
});
cancel(runOnce);
let throttle = throttle(myContext, function() {
// will not be executed
}, 1, false);
cancel(throttle);
let debounce = debounce(myContext, function() {
// will not be executed
}, 1);
cancel(debounce);
let debounceImmediate = debounce(myContext, function() {
// will be executed since we passed in true (immediate)
}, 100, true);
// the 100ms delay until this method can be called again will be canceled
cancel(debounceImmediate);
```
@method cancel
@static
@for @ember/runloop
@param {Object} timer Timer object to cancel
@return {Boolean} true if canceled or false/undefined if it wasn't found
@public
*/
export function cancel(timer: Timer): boolean {
return _backburner.cancel(timer);
}
/**
Delay calling the target method until the debounce period has elapsed
with no additional debounce calls. If `debounce` is called again before
the specified time has elapsed, the timer is reset and the entire period
must pass again before the target method is called.
This method should be used when an event may be called multiple times
but the action should only be called once when the event is done firing.
A common example is for scroll events where you only want updates to
happen once scrolling has ceased.
```javascript
import { debounce } from '@ember/runloop';
function whoRan() {
console.log(this.name + ' ran.');
}
let myContext = { name: 'debounce' };
debounce(myContext, whoRan, 150);
// less than 150ms passes
debounce(myContext, whoRan, 150);
// 150ms passes
// whoRan is invoked with context myContext
// console logs 'debounce ran.' one time.
```
Immediate allows you to run the function immediately, but debounce
other calls for this function until the wait time has elapsed. If
`debounce` is called again before the specified time has elapsed,
the timer is reset and the entire period must pass again before
the method can be called again.
```javascript
import { debounce } from '@ember/runloop';
function whoRan() {
console.log(this.name + ' ran.');
}
let myContext = { name: 'debounce' };
debounce(myContext, whoRan, 150, true);
// console logs 'debounce ran.' one time immediately.
// 100ms passes
debounce(myContext, whoRan, 150, true);
// 150ms passes and nothing else is logged to the console and
// the debouncee is no longer being watched
debounce(myContext, whoRan, 150, true);
// console logs 'debounce ran.' one time immediately.
// 150ms passes and nothing else is logged to the console and
// the debouncee is no longer being watched
```
@method debounce
@static
@for @ember/runloop
@param {Object} [target] target of method to invoke
@param {Function|String} method The method to invoke.
May be a function or a string. If you pass a string
then it will be looked up on the passed target.
@param {Object} [args*] Optional arguments to pass to the timeout.
@param {Number} wait Number of milliseconds to wait.
@param {Boolean} immediate Trigger the function on the leading instead
of the trailing edge of the wait interval. Defaults to false.
@return {Array} Timer information for use in canceling, see `cancel`.
@public
*/
export function debounce<F extends (...args: any[]) => any>(
method: F,
...args: [...args: Parameters<F>, wait: string | number, immediate?: boolean]
): Timer;
export function debounce<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: [...args: Parameters<F>, wait: string | number, immediate?: boolean]
): Timer;
export function debounce<T, U extends keyof T>(
target: T,
method: U,
...args: [
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : [],
wait: string | number,
immediate?: boolean
]
): Timer;
export function debounce(...args: any[]) {
// @ts-expect-error TS doesn't like the rest args here
return _backburner.debounce(...args);
}
/**
Ensure that the target method is never called more frequently than
the specified spacing period. The target method is called immediately.
```javascript
import { throttle } from '@ember/runloop';
function whoRan() {
console.log(this.name + ' ran.');
}
let myContext = { name: 'throttle' };
throttle(myContext, whoRan, 150);
// whoRan is invoked with context myContext
// console logs 'throttle ran.'
// 50ms passes
throttle(myContext, whoRan, 150);
// 50ms passes
throttle(myContext, whoRan, 150);
// 150ms passes
throttle(myContext, whoRan, 150);
// whoRan is invoked with context myContext
// console logs 'throttle ran.'
```
@method throttle
@static
@for @ember/runloop
@param {Object} [target] target of method to invoke
@param {Function|String} method The method to invoke.
May be a function or a string. If you pass a string
then it will be looked up on the passed target.
@param {Object} [args*] Optional arguments to pass to the timeout.
@param {Number} spacing Number of milliseconds to space out requests.
@param {Boolean} immediate Trigger the function on the leading instead
of the trailing edge of the wait interval. Defaults to true.
@return {Array} Timer information for use in canceling, see `cancel`.
@public
*/
export function throttle<F extends (...args: any[]) => any>(
method: F,
...args: [...args: Parameters<F>, wait?: string | number, immediate?: boolean]
): Timer;
export function throttle<T, F extends (this: T, ...args: any[]) => any>(
target: T,
method: F,
...args: [...args: Parameters<F>, wait?: string | number, immediate?: boolean]
): Timer;
export function throttle<T, U extends keyof T>(
target: T,
method: U,
...args: [
...args: T[U] extends (...args: any[]) => any ? Parameters<T[U]> : [],
wait?: string | number,
immediate?: boolean
]
): Timer;
export function throttle(...args: any[]): Timer {
// @ts-expect-error TS doesn't like the rest args here
return _backburner.throttle(...args);
}