MatrixAI / js-id

ID generation for JavaScript & TypeScript Applications
https://polykey.com
Apache License 2.0
10 stars 1 forks source link

Allow generic extensions to `Id` #13

Open CMCDragonkai opened 2 years ago

CMCDragonkai commented 2 years ago

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.

class NodeIdInternal extends IdInternal {
  pubic toMultibase(): string {
    return super.toMultibase('base32hex');
  }
}

const nodeId = NodeIdInternal.create(/* ... */);
nodeId.toMultibase();
// instead of nodesUtils.encodeNodeId();

However this is not really possible easily due to the fact that Id type is a union of IdInternal and number.

There's a potential way to do this though with "polymorphic static this" https://github.com/MatrixAI/js-polykey/pull/321#issuecomment-1030748675

type Id<T = IdInternal> = T & number;

class IdInternal extends Uint8Array {
  static create<T extends typeof IdInternal>(this: T): Id<InstanceType<T>> {
    return (new this() as Id<InstanceType<T>>);
  }
  baseMethod () {
  }
}

type NodeId = Id<NodeIdInternal>;

class NodeIdInternal extends IdInternal {
  derivedMethod () { }
}

class ClaimIdInternal extends IdInternal {
  anotherMethod () {}
}

class SomeOtherIdOfId extends ClaimIdInternal {
}

const x = IdInternal.create();
const y = NodeIdInternal.create();
const z = SomeOtherIdOfId.create();

// console.log(x.baseMethod());

function doSomething(x: NodeId) {
  const pojo = { };
  pojo[x] = 123;
}

doSomething(x); // this is a type error
doSomething(y);

This makes Id itself a generic type, and it allows all downstream classes to override instance encoding methods like toMultibase or otherwise, and also mean that NodeIdInternal.create would create Id<NodeIdInternal>, which you can match with type 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 like fromBuffer cannot just call idUtils.fromBuffer since that's hardcoded to call IdInternal, but instead refer to this.

Additional context

Tasks

  1. ...
  2. ...
  3. ...
CMCDragonkai commented 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.