You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/throttle/src/Shared/ThrottledFunction.lua
+86-26Lines changed: 86 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,17 @@
1
1
--!strict
2
2
--[=[
3
-
Throttles execution of a functon. Does both leading, and following
3
+
Throttles execution of a function with configurable leading and trailing behavior.
4
4
@class ThrottledFunction
5
5
]=]
6
6
7
+
--[=[
8
+
@interface ThrottleConfig
9
+
.leading boolean? -- If true, will dispatch immediately after creating this ThrottledFunction.
10
+
.trailing boolean? -- If true, will dispatch after the timeout with the latest-called args.
11
+
.leadingFirstTimeOnly boolean? -- If true, will dispatch immediately after creating this ThrottledFunction, but from then on, will begin the <timeout> window upon manual call
12
+
and delay dispatch until <timeout> seconds have passed (with latest-called args).
13
+
@within ThrottledFunction
14
+
]=]
7
15
exporttypeThrottleConfig= {
8
16
leading: boolean?,
9
17
trailing: boolean?,
@@ -25,14 +33,23 @@ export type ThrottledFunction<T...> = typeof(setmetatable(
25
33
_callLeading: boolean,
26
34
_callTrailing: boolean,
27
35
_callLeadingFirstTime: boolean?,
36
+
_delayedDispatchThread: thread?,
28
37
},
29
38
{} :: typeof({ __index=ThrottledFunction })
30
39
))
31
40
41
+
--[=[
42
+
@function new
43
+
@within ThrottledFunction
44
+
@param timeoutInSeconds number -- The (minimum) time in seconds to wait between each function dispatch; the "cooldown".
45
+
@param func function -- The actual function whose calls will be throttled.
46
+
@param config? ThrottleConfig -- The configuration for how throttling will behave.
0 commit comments