-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isPeerId #57
isPeerId #57
Conversation
src/index.js
Outdated
@@ -219,6 +219,12 @@ exports.createFromJSON = function (obj, callback) { | |||
} | |||
} | |||
|
|||
exports.isPeerId = function (peerId) { | |||
if (peerId.constructor && peerId.constructor.name) { | |||
return peerId.constructor.name === 'PeerId' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't do this I'm afraid, see libp2p/js-libp2p#65
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, it is even weird that this has only appeared now as an issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🦆 all the way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish instanceof
would just work..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use symbols for this
const peerIdSymbol = Symbol('PeerId')
class PeerId {
constructor () {
this[peerIdSymbol] = true
}
static isPeerId (other) {
return other && other[peerIdSymbol]
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's the point of symbols, you reuse the same symbol on all instances as you can see above in my example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You won't reuse if there is a js-peer-id 0.n.x
and a 0.n+1.x
.
Issue mitigated, will move forward with this PR. Will leave libp2p/js-libp2p#65 open for ideas. |
No description provided.