Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/pause-methods
Pause/resume execution of an object's methods
https://github.com/shinnn/pause-methods
Last synced: 27 days ago
JSON representation
Pause/resume execution of an object's methods
- Host: GitHub
- URL: https://github.com/shinnn/pause-methods
- Owner: shinnn
- License: isc
- Created: 2019-02-08T09:47:38.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-05-26T13:19:55.000Z (over 5 years ago)
- Last Synced: 2024-09-15T07:49:49.137Z (2 months ago)
- Language: JavaScript
- Size: 110 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pause-methods
[![npm version](https://img.shields.io/npm/v/pause-methods.svg)](https://www.npmjs.com/package/pause-methods)
[![Build Status](https://travis-ci.com/shinnn/pause-methods.svg?branch=master)](https://travis-ci.com/shinnn/pause-methods)
[![codecov](https://codecov.io/gh/shinnn/pause-methods/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/pause-methods)A [Node.js](https://nodejs.org/) module to pause/resume execution of the object's methods
```javascript
const {pause, resume} = require('pause-methods');const obj = {
methodA() {
process.stdout.write('hello');
},
methodB() {
process.stdout.write('world');
}
};
const pausedObj = pause(obj);pausedObj.methodA(); // prints nothing
pausedObj.methodB(); // prints nothingresume(pausedObj); // prints 'hello' and 'world'
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install pause-methods
```## API
```javascript
const pauseMethods = require('pause-methods');
```### pauseMethods(*obj*)
*obj*: `Object`
Return: `Object` (a reference to *obj*)It makes *obj*'s [enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties) `Function` properties do nothing and return `undefined`.
```javascript
let num = 0;
const obj = {countUp: () => ++num};obj.countUp(); //=> 1
num; //=> 1const pausedObj = pauseFn(obj);
pausedObj.countUp(); //=> undefined, not 2
num; //=> 1, not 2pausedObj.countUp(); //=> undefined
pausedObj.countUp(); //=> undefined
pausedObj.countUp(); //=> undefined
// ...
num; //=> 1
```### pauseMethods.pause(*obj*)
An alias of [`pauseMethods()`](#pausemethodsobj).
### pauseMethods.resume(*obj*)
*obj*: `Object` returned by [`pauseMethods()`](#pausemethodsobj)
Return: `Object` (a reference to *obj*)It restores the original functionality of the paused methods, calls it with each arguments passed while paused.
```javascript
const pausedFs = pauseMethods(fs.promises);(async () => {
await pausedFs.writeFile('./a', 'foo'); // ./a is not created here
await pausedFs.mkdir('./b'); // ./b is not created herepauseFs.resume(); // ./a and ./b are created here
})();
```## License
[ISC License](./LICENSE) © 2019 Shinnosuke Watanabe