Open CMCDragonkai opened 2 years ago
This could be applied to GRPCClient
and its child classes as well. Right now we had to separate the creation functions with GRPCClient.createClient
needing to be called by the child classes. If GRPCClient
could have a static method that constructs all child classes as well, this would be nice.
However as I said it's difficult to override the parent static method with this technique, and child classes of grpc client may need to do it differently.
If https://github.com/MatrixAI/js-polykey/issues/333 is resolved though, there won't be a need for this issue to be applied to the grpc domain.
Specification
It would be nice for users to be able to extend
IdInternal
which can allow us to specialise the encoding functions into the child classes.However this is not really possible easily due to the fact that
Id
type is a union ofIdInternal
andnumber
.There's a potential way to do this though with "polymorphic static this" https://github.com/MatrixAI/js-polykey/pull/321#issuecomment-1030748675
This makes
Id
itself a generic type, and it allows all downstream classes to override instance encoding methods liketoMultibase
or otherwise, and also mean thatNodeIdInternal.create
would createId<NodeIdInternal>
, which you can match withtype NodeId = Id<NodeIdInternal>
.One tradeoff is that's complex for child classes to extend any static methods of
IdInternal
, but that's unlikely to be needed at all. The only methods that child classes may want to extend are all the encoding methods, and potentially adding new features to their ID objects.Note that this means right now where alot of functions hand off their decoding to
idUtils
, they would need to be changed to refer to the current constructor. So likefromBuffer
cannot just callidUtils.fromBuffer
since that's hardcoded to callIdInternal
, but instead refer tothis
.Additional context
Tasks