MCPcopy Index your code
hub / github.com/material-shell/material-shell / throttle

Function throttle

src/utils/index.ts:49–93  ·  view source on GitHub ↗
(
    this: C,
    func: (...args: T) => R,
    wait: number,
    options?: Partial<TrottleParams>
)

Source from the content-addressed store, hash-verified

47// but if you'd like to disable the execution on the leading edge, pass
48// `{leading: false}`. To disable execution on the trailing edge, ditto.
49export function throttle<T extends any[], R, C>(
50 this: C,
51 func: (...args: T) => R,
52 wait: number,
53 options?: Partial<TrottleParams>
54): (...args: T) => R {
55 let call: { context: C; args: T } | null;
56 let result: R;
57 let timeout: number | null = null;
58 let previous = 0;
59 const definedOptions: TrottleParams = Object.assign(
60 {
61 trailing: true,
62 leading: true,
63 },
64 options
65 );
66
67 const later = function () {
68 previous = definedOptions.leading === false ? 0 : Date.now();
69 timeout = null;
70 assert(call !== null, 'unreachable');
71 result = func.apply(call.context, call.args);
72 if (!timeout) call = null;
73 return false;
74 };
75 return function (this: C, ...params: T) {
76 const now = Date.now();
77 if (!previous && definedOptions.leading === false) previous = now;
78 const remaining = wait - (now - previous);
79 call = { context: this, args: params };
80 if (remaining <= 0 || remaining > wait) {
81 if (timeout !== null) {
82 Async.clearTimeoutId(timeout);
83 timeout = null;
84 }
85 previous = now;
86 result = func.apply(call.context, call.args);
87 if (!timeout) call = null;
88 } else if (!timeout && definedOptions.trailing !== false) {
89 timeout = Async.addTimeout(GLib.PRIORITY_DEFAULT, remaining, later);
90 }
91 return result;
92 };
93}
94
95export const isParentOfActor = (
96 parent: Clutter.Actor | null,

Callers 5

constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90

Calls 4

clearTimeoutIdMethod · 0.80
applyMethod · 0.80
addTimeoutMethod · 0.80
assignMethod · 0.65

Tested by

no test coverage detected