Skip to content

Commit

Permalink
feat: reorder arguments to make name optional
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Apr 2, 2020
1 parent a4b23ab commit d5f941f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down Expand Up @@ -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
});

Expand All @@ -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

Expand Down
9 changes: 5 additions & 4 deletions src/one-time-execution-method.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
* 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
* });
*
* const object = new MyClass();
* 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
Expand Down
4 changes: 2 additions & 2 deletions tests/one-time-execution-method-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand All @@ -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();
Expand Down

0 comments on commit d5f941f

Please sign in to comment.