1.0.6 • Published 3 years ago

pausable-setimeout v1.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

pausable-setimeout

  • setTimeout,can pause and continue, can stop
  • similiar to rxjs BehaviorSubject , but more simple
  • without any dependancy

install

  • npm install pausable-setimeout

nodejs

  • nodejs is easy, coz nodejs has process.hrtime and setTimeout has .refresh .close
  • const pausable = require('pausable-setimeout').pausable

pausable

const pausable = require('pausable-setimeout').pausable

function get_hrtime(){
    let hr = process.hrtime()
    return(hr[0]*1000+hr[1]/1000000)
}

var tmout_f2 = (y)=> {
    console.log("tmout_f2: "+y,"finished_at: "+get_hrtime())
}



var tmout = pausable(tmout_f2,15000,'y')
//...等待几秒
tmout.pause()
//paused_at: 7881666548.160006 time_past: 9543.428115844727
//...等待几秒
tmout.continue()
//continued_at: 7881669453.450948
//....
//tmout_f2: y finished_at: 7881674910.43161
//> 7881674910.43161 - 7881669453.450948 + 9543.428115844727
//15000.408778190613        ----------------- 误差1毫秒之内

usage-not-nodejs

  • const wrap = require('pausable-setimeout').setimeout
  • var f = wrap(your_functuion,delay,unit='ms')
  • default unit is ms
  • var p = f(...your_function_params)
  • p is a promise

wrap

const wrap = require('pausable-setimeout')

//
function cufunc(x) {
    var rand_err = parseInt(Math.random()*10);
    if(rand_err<5) {
        throw(new Error("some error"))
    } else {
        return(x)
    }
}

var f = wrap(cufunc,8000)
var p = f("delay msg after 8000")    //will return a promise
p.then(r=>{console.log(p)}).catch(err=>{console.log(p)})
//when finish
Promise {
  'delay msg after 8000',
  '$started_at': 1603364346282,
  '$ended_at': 1603364354292,
  '$final_costed_time': 8010,
  '$paused_at': undefined,
  '$lst_continued_at': 1603364346282,
  '$costed_times': [ 8010 ],
  '$state': 'resolved',
  '$rslt': 'delay msg after 8000',
  '$exception': undefined,
  '$stop': [Function],
  '$pause': [Function],
  '$continue': [Function]
}
>

pause

var f = wrap(cufunc,8000)
var p = f("delay msg after 8000")
//pause
var p = p.$pause()
> p
Promise {
  <pending>,
  '$started_at': 1603365345093,
  '$ended_at': undefined,
  '$final_costed_time': undefined,
  '$paused_at': 1603365348885,
  '$lst_continued_at': undefined,
  '$costed_times': [ 3792 ],
  '$state': 'paused',
  '$rslt': undefined,
  '$exception': undefined,
  '$stop': [Function],
  '$pause': [Function],
  '$continue': [Function]
}
> p.$state
'paused'
>

continue

//must use a variable p = .. ,because the old setTimeout-in-old-promise will be destroyed
var p = p.$continue()
p.then(r=>{console.log(p)})

> Promise {
  'delay msg after 8000',
  '$started_at': 1603365345093,
  '$ended_at': 1603365393531,
  '$final_costed_time': 8004,
  '$paused_at': undefined,
  '$lst_continued_at': 1603365389319,
  '$costed_times': [ 3792, 4212 ],         //each continue-pause costed_time  recorded
  '$state': 'resolved',
  '$rslt': 'delay msg after 8000',
  '$exception': undefined,
  '$stop': [Function],
  '$pause': [Function],
  '$continue': [Function]
}

>

//mutil pause continue 


var f = wrap(cufunc,16000)
var p = f("delay msg after 16 sec") 
> //
undefined
> var p = p.$pause()
undefined
> var p = p.$continue()
undefined
> var p = p.$pause()
undefined
> var p = p.$continue()
undefined
> var p = p.$pause()
p is already in rejected !!!
undefined
> p
Promise {
  <rejected> Error: some error
      at cufunc (repl:4:15)
      at Timeout._onTimeout (repl:8:36)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7),
  '$started_at': 1603365545231,
  '$ended_at': 1603365557878,
  '$final_costed_time': 4895,
  '$paused_at': undefined,
  '$lst_continued_at': 1603365557876,
  '$costed_times': [ 1337, 3556, 2 ],
  '$state': 'rejected',
  '$rslt': undefined,
  '$exception': Error: some error
      at cufunc (repl:4:15)
      at Timeout._onTimeout (repl:8:36)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7),
  '$stop': [Function],
  '$pause': [Function],
  '$continue': [Function]
}
>

stop

var f = wrap(cufunc,8000)
var p = f("delay msg after 8000")
//wait a little and stop
p.$stop()

> p.$stop()
null                     //after stop will return none
> p
Promise {
  <pending>,
  '$started_at': 1603364445989,
  '$ended_at': undefined,
  '$final_costed_time': undefined,
  '$paused_at': undefined,
  '$lst_continued_at': 1603364445989,
  '$costed_times': [],
  '$state': 'stopped',
  '$rslt': undefined,
  '$exception': undefined,
  '$stop': [Function],
  '$pause': [Function],
  '$continue': [Function]
}
> p.$state
'stopped'
>

property

$started_at          task-started-at
$ended_at            task-ended-at
$final_costed_time   task-final-costed-time
$paused_at
$lst_continued_at
$costed_times
$state               pending | resolved | rejected | paused | stopped       
$rslt
$exception  

methods

var p = p.$stop()
var p = p.$pause()
var p = p.$continue()

LICENSE

  • ISC
1.0.6

3 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago