one-time-execution-method
Version:
define a method that will be executed only once
21 lines (20 loc) • 727 B
text/typescript
/**
* Replace a given method with one that will only be executed once.
* For async functions the resulting Promise of the 1st. invocation
* will be preserved and always delivered in the future.
* ```js
* class MyClass {
* async initialize() {
* // code here will be executed only once
* }
* }
* replaceWithOneTimeExecutionMethod(MyClass.prototype, "initialize");
*
* const object = new MyClass();
* object.initialize(); // body will/can be executed only once
* object.initialize(); // 2nd. call immediatly returns
* ```
* @param {Object} object prototype to bind method against
* @param {string} name of the method
*/
export function replaceWithOneTimeExecutionMethod(object: any, name: string): void;