-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (82 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict'
const EventEmitter = require('events').EventEmitter
const msOption = require('./lib/ms-option')
const setCorrectingInterval = require('./lib/interval')
module.exports = function plugin (options, task) {
return new SimpleSchedule(options, task)
}
class SimpleSchedule extends EventEmitter {
constructor (options, task) {
super()
if (!options) {
options = {}
}
if (!task) {
throw new TypeError('This plugin needs a "task" argument')
}
const interval = msOption(options.interval, 'interval')
if (interval <= 0) throw new RangeError('The "interval" option must be > 0')
this._unref = false
this._timer = null
this._pingInFlight = false
this._stopAfterNextPing = false
this._interval = interval
this._pingTask = this._pingTask.bind(this)
this._pingTaskCallback = this._pingTaskCallback.bind(this)
this._task = task
}
start (callback) {
this._timer = setCorrectingInterval(this._pingTask, this._interval)
this._stopAfterNextPing = false
if (this._unref) {
this._timer.unref()
}
process.nextTick(callback)
}
_pingTask () {
if (this._pingInFlight) {
let name = '<Unknown>'
// Requires telemetry >= 0.2.0
if (typeof this._task.currentPingTarget === 'function') {
name = this._task.currentPingTarget()
}
process.emitWarning(
`Skipping scheduled ping because previous ping did not complete yet. Currently pinging: ${name}`,
'TelemetryWarning'
)
return
}
if (this._stopAfterNextPing) {
this._timer.clear()
this._timer = null
}
this._pingInFlight = true
this._task.ping(this._pingTaskCallback)
}
_pingTaskCallback (err) {
this._pingInFlight = false
// TODO (later): wrap error
if (err) process.emitWarning(err)
if (this._stopAfterNextPing) {
this.emit('_stop')
} else {
this.emit('_ping')
}
}
stop (callback) {
if (this._pingInFlight) {
this.once('_ping', () => {
this.stop(callback)
})
} else {
this._stopAfterNextPing = true
this.once('_stop', callback)
}
}
unref () {
this._unref = true
if (this._timer !== null) {
this._timer.unref()
}
}
}