-
Notifications
You must be signed in to change notification settings - Fork 70
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 } = {}; | ||
|
||
/** | ||
* @param config - the configuration for the SDK client | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the client type is 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); | ||
} | ||
} |
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 need to create a type-safe client to avoid using
any
. For example, define client-specific types: