Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## @lock | ||
|
||
Executes the decorated function as soon as you call it for the first | ||
time, and then disables further execution. | ||
|
||
#### Example | ||
|
||
```js | ||
class Button extends React.Component { | ||
|
||
@lock | ||
onClick(e) { | ||
// supposing that a user is clicking repeatedly the button | ||
// this function executes only once, immediately, because the event handler | ||
// is then redefined to `null` | ||
|
||
console.log('Clicked!'); | ||
} | ||
|
||
render() { | ||
return ( | ||
<input | ||
type="button" | ||
onClick={this.onClick} | ||
{...this.props} | ||
/> | ||
); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { validateFunction } from '../../utils/validators'; | ||
|
||
export default function lock(target, key, descriptor) { | ||
let userFunc = descriptor.value; | ||
|
||
validateFunction(userFunc, 'lock'); | ||
|
||
return { | ||
...descriptor, | ||
value: function locker(...params) { | ||
if (userFunc) { | ||
// Call the result immediately, but then set the function to | ||
// null so it cannot be called again | ||
userFunc.apply(this, [...params]); | ||
userFunc = null; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters