-
Notifications
You must be signed in to change notification settings - Fork 24
/
preloader.js
44 lines (38 loc) · 1.19 KB
/
preloader.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
const _ = require('lodash')
const EventEmitter = require('events')
const once = require('once') // eslint-disable-line no-unused-vars
const Worker = require('./worker')
const md5 = require('md5')
const Logger = new (require('./logger'))()
class Preloader extends EventEmitter {
constructor () {
super()
// Declares
this.workers = {}
// Bindings
this.preload = this.preload.bind(this)
this.removeWorker = this.removeWorker.bind(this)
}
preload (line) {
let guid = md5(line.url)
let worker = _.get(this.workers, `['${guid}']`, false)
if (worker === false) {
worker = new Worker(guid, line)
_.set(this.workers, `['${guid}']`, worker)
worker.once('end', (guid) => { this.removeWorker(guid) })
Logger.verbose(`Create preload for: ${worker.line.internalUrl}`)
} else {
Logger.verbose(`Get existing preload for: ${worker.line.internalUrl}`)
}
return worker
}
removeWorker (guid) {
let worker = _.get(this.workers, `['${guid}']`, false)
if (worker !== false) {
Logger.verbose(`Remove preload for: ${worker.line.internalUrl}`)
delete this.workers[`${guid}`]
worker = null
}
}
}
module.exports = Preloader