Skip to content

Refactor client initialization logic to use a generic lazy-loaded method #265

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 18 additions & 79 deletions packages/core-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ export class StoryClient {
private readonly config: StoryConfig & { chainId: SupportedChainIds };
private readonly rpcClient: PublicClient;
private readonly wallet: SimpleWalletClient;
private _ipAsset: IPAssetClient | null = null;
private _permission: PermissionClient | null = null;
private _license: LicenseClient | null = null;
private _dispute: DisputeClient | null = null;
private _ipAccount: IPAccountClient | null = null;
private _royalty: RoyaltyClient | null = null;
private _nftClient: NftClient | null = null;
private _clients: { [key: string]: any } = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to create a type-safe client to avoid using any. For example, define client-specific types:

type ClientRegistry = {
  ipAsset: IPAssetClient;
  permission: PermissionClient;
  // ...
};


/**
* @param config - the configuration for the SDK client
Expand Down Expand Up @@ -106,100 +100,45 @@ export class StoryClient {
}

/**
* Getter for the ip asset client. The client is lazily created when
* Generic getter for clients. The client is lazily created when
* this method is called.
*
* @returns the IPAssetClient instance
* @param clientName - the name of the client
* @param ClientClass - the class of the client
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now prefer using inline annotation. Could you use it as well?

* @returns the client instance
*/
public get ipAsset(): IPAssetClient {
if (this._ipAsset === null) {
this._ipAsset = new IPAssetClient(this.rpcClient, this.wallet, this.config.chainId);
private getClient<T>(clientName: string, ClientClass: new (...args: any[]) => T): T {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the client type is ClientRegistry, you can use the following code. You also need to consider the chainId inconsistency issue.

private getClient<K extends keyof ClientRegistry>(
    clientName: K,
    ClientClass: new (
        rpcClient: PublicClient, 
        wallet: SimpleWalletClient, 
        chainId?: ChainIds
    ) => ClientRegistry[K] 
): ClientRegistry[K]

if (!this._clients[clientName]) {
this._clients[clientName] = new ClientClass(this.rpcClient, this.wallet, this.config.chainId);
}
return this._clients[clientName];
}

return this._ipAsset;
public get ipAsset(): IPAssetClient {
return this.getClient('ipAsset', IPAssetClient);
}

/**
* Getter for the permission client. The client is lazily created when
* this method is called.
*
* @returns the PermissionClient instance
*/
public get permission(): PermissionClient {
if (this._permission === null) {
this._permission = new PermissionClient(this.rpcClient, this.wallet, this.config.chainId);
}

return this._permission;
return this.getClient('permission', PermissionClient);
}

/**
* Getter for the license client. The client is lazily created when
* this method is called.
*
* @returns the LicenseClient instance
*/
public get license(): LicenseClient {
if (this._license === null) {
this._license = new LicenseClient(this.rpcClient, this.wallet);
}

return this._license;
return this.getClient('license', LicenseClient);
}

/**
* Getter for the dispute client. The client is lazily created when
* this method is called.
*
* @returns the DisputeClient instance
*/
public get dispute(): DisputeClient {
if (this._dispute === null) {
this._dispute = new DisputeClient(this.rpcClient, this.wallet);
}

return this._dispute;
return this.getClient('dispute', DisputeClient);
}

/**
* Getter for the ip account client. The client is lazily created when
* this method is called.
*
* @returns the IPAccountClient instance
*/
public get ipAccount(): IPAccountClient {
if (this._ipAccount === null) {
this._ipAccount = new IPAccountClient(this.rpcClient, this.wallet);
}

return this._ipAccount;
return this.getClient('ipAccount', IPAccountClient);
}

/**
* Getter for the royalty client. The client is lazily created when
* this method is called.
*
* @returns the RoyaltyClient instance
*/
public get royalty(): RoyaltyClient {
if (this._royalty === null) {
this._royalty = new RoyaltyClient(this.rpcClient, this.wallet);
}

return this._royalty;
return this.getClient('royalty', RoyaltyClient);
}

/**
* Getter for the NFT client. The client is lazily created when
* this method is called.
*
* @returns the NftClient instance
*/
public get nftClient(): NftClient {
if (this._nftClient === null) {
this._nftClient = new NftClient(this.rpcClient, this.wallet);
}

return this._nftClient;
return this.getClient('nftClient', NftClient);
}
}