FunctionHelper
internal
Provides functionality for working with functions
Functions
Functions are methods available for calling on the class-
Creates an abortable promise and returns an array containing the promise, the abort signal, and the abort controller. When AbortController is invoked, promise gets rejected.
-
Inserts a function after the specified
methodis called on anobject. To remove this hook, invoke the function returned by this method.class A { method() { console.log('method'); } } let instance = new A(); let detach = FunctionHelper.after(instance, 'method', () => { console.log('after') }); instance.method(); > method > after detach(); instance.method(); > methodThe value returned by the original method is passed as the first argument to
fnfollowed by all the arguments passed by the caller.If
fnreturns a value (notundefined), that value is returned from the method call instead of the value returned by the original method.class A { method(x) { console.log('method', x); return x * 2 } } let instance = new A(); let detach = FunctionHelper.after(instance, 'method', (ret, x) => { console.log('after', ret, x); return x / 2; }); console.log(instance.method(50)); > method 50 > after 100 50 > 25 detach(); console.log(instance.method(50)); > method 50 > 100 -
Calls the passed function on every animation frame while animating the
progressvalue from 0 to 1 over the specified duration.The function is called with the current
progressvalue as the first argument. That value changes from0to1over the duration of the animation at a rate determined by theeasingfunction.The called function may change the value of any property or perform some other action based on the current
progressvalue.The function should return
falseto indicate that the animation should be stopped immediately.// Increase hourHeight by 20 pixels in a visually appealing way const { activeView } = this, { hourHeight } = activeView; FunctionHelper.animate( 1000, progress => activeView.hourHeight = hourHeight * progress * 20, null, 'bounce' ); -
Inserts a function before the specified
methodis called on anobject. To remove this hook, invoke the function returned by this method.class A { method() { console.log('method'); } } let instance = new A(); let detach = FunctionHelper.before(instance, 'method', () => { console.log('before') }); instance.method(); > before > method detach(); instance.method(); > methodIf
fnreturnsfalse, the original method is not invoked andfalseis returned to the caller.class A { method(x) { console.log('method', x); return x * 2; } } let instance = new A(); let detach = FunctionHelper.before(instance, 'method', x => { console.log('before', x); return false; }); console.log(instance.method(50)); > before 50 > false detach(); console.log(instance.method(50)); > method 50 > 100 -
Combines all the passed filtering functions into a single function which returns
falseif any of the passed functions returnsfalse, otherwise it returnstrue.If only one function is passed, it is returned without being wrapped in another function.
If no functions are passed,
undefinedis returned - this is useful for cases where a filter function is optional and we don't want to add the overhead of an extra function call when there is no filter. -
Create a "debounced" function which will call on the "trailing edge" of a timer period. When first invoked will wait until the buffer period has expired to call the function, and more calls within that time will restart the timer.
This is useful for responding to keystrokes, but deferring action until the user pauses typing.
-
Returns a function which calls the passed
interceptorfunction first, and the passedoriginalafter as long as theinterceptordoes not returnfalse. -
Returns a function which calls the passed
sequencefunction after calling the passedoriginal. -
Create a "debounced" function which will call on the "leading edge" of a timer period. When first invoked will call immediately, but invocations after that inside its buffer period will be rejected, and one invocation will be made after the buffer period has expired.
This is useful for responding immediately to a first mousemove, but from then on, only calling the action function on a regular timer while the mouse continues to move.
-
Protects the specified
methodon a givenobjectsuch that calling it will not throw exceptions.
Type definitions
-
AbortablePromiseMetainternal
Properties
- promise : Promise
Actual promise
- controller : AbortController
Abort controller which could be used to abort the promise
- promise : Promise