Last active
May 13, 2019 15:37
-
-
Save vasco-santos/a3edcf2bbb17e6ca62fff07be256e17b to your computer and use it in GitHub Desktop.
js-libp2p stream
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
const ROLE = { | |
INITIATOR: 0, | |
RESPONDER: 1 | |
} | |
class Stream { | |
constructor(iterableDuplex, connection, isInitiator = true) { | |
/** | |
* Stream identifier | |
*/ | |
this.id = (~~(Math.random() * 1e9)).toString(36) + Date.now() | |
/** | |
* Streaming iterable duplex object | |
*/ | |
this._iterableDuplex = iterableDuplex | |
/** | |
* Stream parent connection | |
*/ | |
this.connection = connection | |
/** | |
* Role in the stream, initiator or responder | |
*/ | |
this.role = isInitiator ? ROLE.INITIATOR : ROLE.RESPONDER | |
/** | |
* Stream timeline | |
*/ | |
this.timeline = { | |
openTs: Date.now(), | |
closeTs: undefined | |
} | |
/** | |
* User provided tags | |
*/ | |
this.tags = [] | |
/** | |
* Stream protocol | |
*/ | |
this._protocol = undefined | |
} | |
sink () { | |
} | |
source () { | |
} | |
close () { | |
} | |
} |
No reason, I will change to the connection reference!
@alanshaw do you envision sink
and source
as you have in js-libp2p-websockets
?
In this case, we need to pass the options to this stream class for the abortable, and in newStream
for the connection.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any reason to not just make this the actual connection reference? Just thinking about usability here. If I have a stream and want to get the connection, I'll have to take the
connectionId
and then look up the connection via the Switch. I don't think it's critical, just a potential usability improvement.