From d5f941f7e60b0ffb6486472f72b868b28b5070fd Mon Sep 17 00:00:00 2001 From: Markus Felten Date: Thu, 2 Apr 2020 20:51:08 +0200 Subject: [PATCH] feat: reorder arguments to make name optional --- README.md | 6 +++--- src/one-time-execution-method.mjs | 9 +++++---- tests/one-time-execution-method-test.mjs | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 13ee558..41d3597 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ class MyClass { } // add initialize() method to MyClass -defineOneTimeExecutionMethod(MyClass.prototype, "initialize", function() { +defineOneTimeExecutionMethod(MyClass.prototype, function initialize() { this.executions++; return new Promise(resolve => setTimeout(resolve, 1000)); }); @@ -75,7 +75,7 @@ will be preserved and always delivered in the future. ```js class MyClass { } -defineOneTimeExecutionMethod(MyClass.prototype, "initialize", async function() { +defineOneTimeExecutionMethod(MyClass.prototype, async function initialize() { // code here will be executed only once }); @@ -86,8 +86,8 @@ object.initialize(); // body will be executed only once ### Parameters - `object` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** prototype to bind method against -- `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** of the method - `func` **[AsyncFunction](#asyncfunction)** to be executed (once) must deliver a Promise +- `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** of the method (optional, default `func.name`) ## transitionState diff --git a/src/one-time-execution-method.mjs b/src/one-time-execution-method.mjs index 5783cc9..9a745fa 100644 --- a/src/one-time-execution-method.mjs +++ b/src/one-time-execution-method.mjs @@ -10,7 +10,7 @@ * will be preserved and always delivered in the future. * ```js * class MyClass { } - * defineOneTimeExecutionMethod(MyClass.prototype, "initialize", async function() { + * defineOneTimeExecutionMethod(MyClass.prototype, async function initialize() { * // code here will be executed only once * }); * @@ -18,10 +18,11 @@ * object.initialize(); // body will be executed only once * ``` * @param {Object} object prototype to bind method against - * @param {string} name of the method - * @param {AsyncFunction} func to be executed (once) must deliver a Promise + * @param {AsyncFunction} func to be executed (once) must deliver a Promise + * @param {string} name of the method */ -export function defineOneTimeExecutionMethod(object, name, func) { +export function defineOneTimeExecutionMethod(object, func, name=func.name) { + /** * Object symbol slot holding the state of the method * * undefined -> call func and store Promise diff --git a/tests/one-time-execution-method-test.mjs b/tests/one-time-execution-method-test.mjs index 493f486..bf7588b 100644 --- a/tests/one-time-execution-method-test.mjs +++ b/tests/one-time-execution-method-test.mjs @@ -9,7 +9,7 @@ class MyClass { } } -defineOneTimeExecutionMethod(MyClass.prototype, "initialize", function () { +defineOneTimeExecutionMethod(MyClass.prototype, function initialize () { this.executions++; return new Promise(resolve => setTimeout(resolve, 200)); }); @@ -34,7 +34,7 @@ test("defineOneTimeExecutionMethod parallel", async t => { -defineOneTimeExecutionMethod(MyClass.prototype, "reentrantInitialize", async function() { +defineOneTimeExecutionMethod(MyClass.prototype, async function reentrantInitialize() { this.executions++; await this.reentrantInitialize();