This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathstop.js
114 lines (104 loc) · 2.39 KB
/
stop.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
105
106
107
108
109
110
111
112
113
114
'use strict'
const defer = require('p-defer')
const { NotStartedError, AlreadyInitializedError } = require('../errors')
const Components = require('./')
module.exports = ({
apiManager,
options: constructorOptions,
bitswap,
blockService,
gcLock,
initOptions,
ipld,
ipns,
keychain,
libp2p,
peerInfo,
pinManager,
preload,
print,
repo
}) => async function stop () {
const stopPromise = defer()
const { cancel } = apiManager.update({ stop: () => stopPromise.promise })
try {
blockService.unsetExchange()
bitswap.stop()
preload.stop()
await Promise.all([
ipns.republisher.stop(),
// mfsPreload.stop(),
libp2p.stop(),
repo.close()
])
const api = createApi({
apiManager,
constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
keychain,
peerInfo,
pinManager,
preload,
print,
repo
})
apiManager.update(api, () => { throw new NotStartedError() })
} catch (err) {
cancel()
stopPromise.reject(err)
throw err
}
stopPromise.resolve(apiManager.api)
return apiManager.api
}
function createApi ({
apiManager,
constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
keychain,
peerInfo,
pinManager,
preload,
print,
repo
}) {
const dag = Components.legacy.dag({ _ipld: ipld, _preload: preload })
const object = Components.legacy.object({ _ipld: ipld, _preload: preload, dag, _gcLock: gcLock })
const pin = Components.legacy.pin({ _ipld: ipld, _preload: preload, object, _repo: repo, _pinManager: pinManager })
const add = Components.add({ ipld, dag, preload, pin, gcLock, options: constructorOptions })
const refs = () => { throw new NotStartedError() }
refs.local = Components.refs.local({ repo })
const api = {
add,
cat: Components.cat({ ipld, preload }),
config: Components.config({ repo }),
dns: Components.dns(),
get: Components.get({ ipld, preload }),
init: () => { throw new AlreadyInitializedError() },
isOnline: Components.isOnline({}),
ls: Components.ls({ ipld, preload }),
refs,
start: Components.start({
apiManager,
options: constructorOptions,
blockService,
gcLock,
initOptions,
ipld,
keychain,
peerInfo,
pinManager,
preload,
print,
repo
}),
stop: () => apiManager.api
}
return api
}