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.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove proxy api object and detect initialisaion state
Ask the repo if it has been initialised, if so, allow the user to skip the `.init()` step and move on to `.start()` Removes the proxy api object in favour of vanilla functions because it was causing errors to be thrown if you even referenced properties that were from a different api state. E.g. with an unitialised repo: ```javascript const ipfs = await IPFS.create({ init: false, start: false }) // no invocation, just referencing the property causes an error to be thrown console.info(ipfs.start) ``` I'd looked at changing the proxy behaviour to return a function that throws if invoked, but at the time the proxy is called you don't know what the calling code is going to do with the return value so it's hard to know if it's accessing a function or a property - the return value is just put on the stack and interacted with so it seemed simpler to just pull it out and define the API up front. A nice future improvement might be to have `.init`, `.start` and `.stop` export functions that update the API - that way after `.stop` has been invoked, it could restore the API from the post-`.init` state, but this can come later. Also upgrades `ipfsd-ctl` to pass refs only during factory creation. Depends on: - [ ] ipfs/js-ipfsd-ctl#457 - [ ] ipfs/js-ipfs-repo#219 - [ ] ipfs-inactive/npm-go-ipfs-dep#40 fix: do not allow parallel init, start or stop If the user is calling `.init`, `.start` or `.stop` from the code in multiple places simultaneously, they probably have a bug in their code and we should let them know instead of masking it.
- Loading branch information
1 parent
60fe851
commit 7f3a2d7
Showing
12 changed files
with
496 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,128 @@ | ||
'use strict' | ||
|
||
const noop = () => {} | ||
const defaultApi = (onUndef = noop) => ({ | ||
add: onUndef, | ||
bitswap: { | ||
stat: onUndef, | ||
unwant: onUndef, | ||
wantlist: onUndef | ||
}, | ||
block: { | ||
get: onUndef, | ||
put: onUndef, | ||
rm: onUndef, | ||
stat: onUndef | ||
}, | ||
bootstrap: { | ||
add: onUndef, | ||
list: onUndef, | ||
rm: onUndef | ||
}, | ||
cat: onUndef, | ||
config: onUndef, | ||
dag: { | ||
get: onUndef, | ||
put: onUndef, | ||
resolve: onUndef, | ||
tree: onUndef | ||
}, | ||
dns: onUndef, | ||
files: { | ||
chmod: onUndef, | ||
cp: onUndef, | ||
flush: onUndef, | ||
ls: onUndef, | ||
mkdir: onUndef, | ||
mv: onUndef, | ||
read: onUndef, | ||
rm: onUndef, | ||
stat: onUndef, | ||
touch: onUndef, | ||
write: onUndef | ||
}, | ||
get: onUndef, | ||
id: onUndef, | ||
init: onUndef, | ||
isOnline: onUndef, | ||
key: { | ||
export: onUndef, | ||
gen: onUndef, | ||
import: onUndef, | ||
info: onUndef, | ||
list: onUndef, | ||
rename: onUndef, | ||
rm: onUndef | ||
}, | ||
ls: onUndef, | ||
name: { | ||
publish: onUndef, | ||
pubsub: { | ||
cancel: onUndef, | ||
state: onUndef, | ||
subs: onUndef | ||
} | ||
}, | ||
object: { | ||
data: onUndef, | ||
get: onUndef, | ||
links: onUndef, | ||
new: onUndef, | ||
patch: { | ||
addLink: onUndef, | ||
appendData: onUndef, | ||
rmLink: onUndef, | ||
setData: onUndef | ||
}, | ||
put: onUndef, | ||
stat: onUndef | ||
}, | ||
pin: onUndef, | ||
ping: onUndef, | ||
pubsub: { | ||
subscribe: onUndef, | ||
unsubscribe: onUndef, | ||
publish: onUndef, | ||
ls: onUndef, | ||
peers: onUndef | ||
}, | ||
refs: onUndef, | ||
repo: { | ||
gc: onUndef, | ||
stat: onUndef, | ||
version: onUndef | ||
}, | ||
resolve: onUndef, | ||
start: onUndef, | ||
stats: { | ||
bitswap: onUndef, | ||
bw: onUndef, | ||
repo: onUndef | ||
}, | ||
stop: onUndef, | ||
swarm: { | ||
addrs: onUndef, | ||
connect: onUndef, | ||
disconnect: onUndef, | ||
localAddrs: onUndef, | ||
peers: onUndef | ||
}, | ||
version: onUndef | ||
}) | ||
|
||
module.exports = class ApiManager { | ||
constructor () { | ||
this._api = {} | ||
this._onUndef = () => undefined | ||
this.api = new Proxy(this._api, { | ||
get: (_, prop) => { | ||
if (prop === 'then') return undefined // Not a promise! | ||
return this._api[prop] === undefined ? this._onUndef(prop) : this._api[prop] | ||
} | ||
}) | ||
this.api = { | ||
...defaultApi() | ||
} | ||
} | ||
|
||
update (nextApi, onUndef) { | ||
const prevApi = { ...this._api } | ||
const prevUndef = this._onUndef | ||
Object.keys(this._api).forEach(k => { delete this._api[k] }) | ||
Object.assign(this._api, nextApi) | ||
if (onUndef) this._onUndef = onUndef | ||
Object.keys(this.api).forEach(k => { delete this.api[k] }) | ||
Object.assign(this.api, defaultApi(onUndef), nextApi) | ||
this._onUndef = onUndef || noop | ||
return { cancel: () => this.update(prevApi, prevUndef), api: this.api } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.