forked from hoodiehq/hoodie-task-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (34 loc) · 1.1 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
module.exports = Task
var EventEmitter = require('events').EventEmitter
var StoreClient = require('@hoodie/store-client')
var start = require('./lib/start.js')
var off = require('./lib/off.js')
var on = require('./lib/on.js')
var one = require('./lib/one.js')
function Task (id, options) {
if (!(this instanceof Task)) return new Task(id, options)
if (typeof id !== 'string') throw new Error('Must be a valid string.')
if (!options || !options.remoteBaseUrl) {
throw new Error('options.remoteBaseUrl is required')
}
options.remoteBaseUrl = options.remoteBaseUrl.replace(/\/$/, '')
options.remote = (options.remoteBaseUrl + '/queue/' + encodeURIComponent(id))
delete options.remoteBaseUrl
var taskStore = new StoreClient(id, options)
taskStore.on('error', function (error) {
console.error(error)
})
taskStore.connect()
var state = {
taskStore: taskStore,
emitter: new EventEmitter()
}
return function (type) {
return {
start: start.bind(null, state, type),
off: off.bind(null, state),
on: on.bind(null, state),
one: one.bind(null, state)
}
}
}