Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

emit event once initialised #1058

Closed
pgte opened this issue Nov 3, 2017 · 8 comments
Closed

emit event once initialised #1058

pgte opened this issue Nov 3, 2017 · 8 comments
Labels
exp/wizard Extensive knowledge (implications, ramifications) required help wanted Seeking public contribution on this issue P2 Medium: Good to have, but can wait until someone steps up

Comments

@pgte
Copy link
Contributor

pgte commented Nov 3, 2017

  • Version: 0.26.0
  • Platform: All
  • Subsystem:

Type:

Enhancement

Severity:

Very Low

Description:

I want to support better offline use cases. I need the peerId to be able to start working locally, without needing for Swarm, Bitswap or Pubsub to consider themselves ready.

AFAIK, right now there is no event that tells me this. init is only emitted if the repo needs initialising.
start and ready are only emitted once the network layer is ready, which may not happen in my case.

Right now I'm polling the IPFS node for the _peerInfo property, but I would like to have a better alternative.

I would like to have an event that tells me once the peerId is known.

Steps to reproduce the error:

N/A

@victorb
Copy link
Member

victorb commented Nov 3, 2017

AFAIK, right now there is no event that tells me this. init is only emitted if the repo needs initialising.
start and ready are only emitted once the network layer is ready, which may not happen in my case.

This is incorrect regarding the init event. It's supposed to be emitted when the node is done initializing, not if it needs initializing. Things seem to work currently, check this example:

const ipfs = require('./src')

const node = new ipfs({repo: './repo-' + Math.random(), init: {bits: 32}, start: false})
node.once('ready', () => {
  console.log('ready')
  console.log(`current state is "${node.state._state}"`)
})
node.once('start', () => {
  console.log('started but it should have had started')
})
node.once('init', () => {
  console.log('initialized')
  console.log(`current state is "${node.state._state}"`)
})

This outputs the following for me:

$ node node-init-test.js
initialized
current state is "stopped"
ready
current state is "stopped"

However, I would expect node._peerInfo to actually have the peerInfo at this point, but it's undefined. So calling node.id() would not work.

@victorb
Copy link
Member

victorb commented Nov 3, 2017

Ah, I think I misunderstood a tiny bit. You're saying that if the repo is already initiralized, the init event is never emitted, correct? That seems to be true, while the ready event is still emitted in that case.

@pgte
Copy link
Contributor Author

pgte commented Nov 4, 2017

@victorbjelkholm yes, that's the problem. If the node happens to be offline, the ready event is not emitted, and I need a peerId in order to be able to work locally...

@daviddias daviddias added status/ready Ready to be worked exp/wizard Extensive knowledge (implications, ramifications) required help wanted Seeking public contribution on this issue P2 Medium: Good to have, but can wait until someone steps up labels Jan 25, 2018
@wraithgar
Copy link
Contributor

Made a PR to address this that goes down the path of correcting the init event behavior to emit when the node is done initializing, not only just when it has initialized a new repo.

@Mr0grog
Copy link
Contributor

Mr0grog commented Apr 3, 2018

@pgte I just spent a bunch of time staring at all the bootstrapping code to document the constructor options and it looks to me like ready should always emit (so long as there were no errors). I just took my computer offline and tried this; it fired the ready event (and got valid peer info):

var IPFS = require('ipfs');
var node = new IPFS();
node.on('ready', () => {
  console.log('READY!!!!!');
  node.id().then(id => console.log('Peer Info:', id));
});

Am I misunderstanding what you’re getting at? (Seems important since these new event docs would be wrong if there’s a non-error case where it doesn’t emit.)

@pgte
Copy link
Contributor Author

pgte commented Apr 4, 2018

I now see that I should have added a test case, since the behaviour I can no longer reproduce the behaviour I think was witnessing.

(I think this may related to the ws-star transport. It still doesn't allow me to start offline if I enable this transport by declaring a p2p-websocket-star address. — but this is a completely separate issue).

Anyway, the point here is:

The ready event (if the ws-star transport was able to start offline) is not really what I need, since it is emitted after the network layer is initialised (which is later than I need).
I would love to have an event which tells me once I have a PeerId and functioning repo so I can start making local changes, being it init or some other.

@Mr0grog
Copy link
Contributor

Mr0grog commented Apr 5, 2018

Looking at boot.js, some meaningful event points seem like:

  • When the repo is opened (repo or repo-open?) (still too early for peer info, though)
  • When peer info is available (peer-info/peer-id?) — this event could even be accompanied by the actual peer ID info (note that the repo might not be fully initialized yet here)
  • When the node’s state transitions to initialized, as @wraithgar started to do in fix: init event #1293, though I think it needs a new name. (Would it be confusing to have an init and initialized event? Maybe we could rename init to repo-init or repo-initialized and use init or initialized for this? Not a very fun change for existing users, though :\ )

@alanshaw
Copy link
Member

alanshaw commented Nov 1, 2018

Would passing start:false to the constructor and then waiting for the ready event be what you’re looking for?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
exp/wizard Extensive knowledge (implications, ramifications) required help wanted Seeking public contribution on this issue P2 Medium: Good to have, but can wait until someone steps up
Projects
None yet
Development

No branches or pull requests

6 participants