-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner-source.js
64 lines (62 loc) · 2.02 KB
/
scanner-source.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
'use strict'
const db = require('./scanner-db.js')
const validate = require('aproba')
const { Fic } = require('@fanfic/parser')
class ScannerSource {
constructor (source) {
this.source = source
this.sourceid = null
}
async init () {
this.sourceid = await db.addSource(this.source)
}
setLastSeen (lastseen) {
validate('N', arguments)
if (!this.sourceid) return Promise.reject(new Error('setLastSeen called without init()'))
return db.setLastSeen(this.sourceid, lastseen)
}
setLastScan (lastscan) {
validate('N', arguments)
if (!this.sourceid) return Promise.reject(new Error('setLastScan called without init()'))
return db.setLastScan(this.sourceid, lastscan)
}
replace (fic) {
validate('O', arguments)
if (!this.sourceid) return Promise.reject(new Error('replace called without init()'))
if (!(fic instanceof Fic)) return Promise.reject(new Error('replace called with non-Fic object'))
return db.replace(this.sourceid, fic)
}
/* not in use
get (match) {
validate('O', arguments)
if (match.site && match.siteId) {
return db.getdById(match.site, match.siteId)
} else {
return Promise.reject(new Error('No index available for getting fics by ' + JSON.stringify(match)))
}
}
*/
getByIds (ids) {
validate('A', arguments)
if (!this.sourceid) return Promise.reject(new Error('getByIds called without init()'))
return db.getByIds(this.sourceid, ids)
}
lastSeen () {
if (!this.sourceid) return Promise.reject(new Error('lastSeen called without init()'))
return db.lastSeen(this.sourceid)
}
lastScan () {
if (!this.sourceid) return Promise.reject(new Error('lastScan called without init()'))
return db.lastScan(this.sourceid)
}
serialize () {
if (!this.sourceid) return Promise.reject(new Error('serialize called without init()'))
return db.serialize(this.sourceid)
}
}
module.exports = async (source) => {
const scan = new ScannerSource(source)
await scan.init()
return scan
}
module.exports.Class = ScannerSource