1.0.0 • Published 5 years ago

pause-methods v1.0.0

Weekly downloads
255
License
ISC
Repository
github
Last release
5 years ago

pause-methods

npm version Build Status codecov

A Node.js module to pause/resume execution of the object's methods

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 nothing

resume(pausedObj); // prints 'hello' and 'world'

Installation

Use npm.

npm install pause-methods

API

const pauseMethods = require('pause-methods');

pauseMethods(obj)

obj: Object
Return: Object (a reference to obj)

It makes obj's enumerable Function properties do nothing and return undefined.

let num = 0;
const obj = {countUp: () => ++num};

obj.countUp(); //=> 1
num; //=> 1

const pausedObj = pauseFn(obj);

pausedObj.countUp(); //=> undefined, not 2
num; //=> 1, not 2

pausedObj.countUp(); //=> undefined
pausedObj.countUp(); //=> undefined
pausedObj.countUp(); //=> undefined
// ...
num; //=> 1

pauseMethods.pause(obj)

An alias of pauseMethods().

pauseMethods.resume(obj)

obj: Object returned by pauseMethods()
Return: Object (a reference to obj)

It restores the original functionality of the paused methods, calls it with each arguments passed while paused.

const pausedFs = pauseMethods(fs.promises);

(async () => {
  await pausedFs.writeFile('./a', 'foo'); // ./a is not created here
  await pausedFs.mkdir('./b'); // ./b is not created here

  pauseFs.resume(); // ./a and ./b are created here
})();

License

ISC License © 2019 Shinnosuke Watanabe