-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (72 loc) · 2.07 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
'use strict'
const { fromCallback, fromPromise } = require('catering')
const bm = require('browser-manifest')
const Nanoresource = require('nanoresource/emitter')
const kPromises = Symbol('kPromises')
const kTitle = Symbol('kTitle')
class Browser extends Nanoresource {
constructor (manifest, target) {
super()
this.manifest = bm(manifest)
this.target = target
this.supports = { ...this.supports, promises: true, callbacks: true }
this[kPromises] = false
this[kTitle] = this.manifest.title
}
open (...args) {
const callback = fromCallback(takeCallback(args))
super.open(...args, callback)
return callback.promise
}
close (...args) {
const callback = fromCallback(takeCallback(args))
super.close(...args, callback)
return callback.promise
}
get title () {
if (!this[kTitle]) {
// Should we make title mandatory?
const { provider, name, version } = this.manifest
this[kTitle] = ['(anonymous)', provider, name, version].filter(Boolean).join(' ')
}
return this[kTitle]
}
setStatus (ok, callback) {
if (this[kPromises]) {
return fromPromise(this._setStatus(ok), callback)
} else {
let sync = true
callback = fromCallback(callback)
this._setStatus(ok, function (err) {
if (sync) return process.nextTick(callback, err)
callback(err)
})
sync = false
return callback.promise
}
}
_setStatus (ok, callback) { process.nextTick(callback) }
}
Browser.promises = class BrowserPromises extends Browser {
constructor (...args) {
super(...args)
this.supports = { ...this.supports, promises: true, callbacks: true }
this[kPromises] = true
this._open = wrapAsync(this._open)
this._close = wrapAsync(this._close)
}
async _open () {}
async _close () {}
async _setStatus (ok) {}
}
module.exports = Browser
function takeCallback (args) {
if (typeof args[args.length - 1] === 'function') {
return args.pop()
}
}
function wrapAsync (fn) {
return function (callback) {
fromPromise(fn.call(this), callback)
}
}