Here we use lazy loading for tbtc sdk initialization in our threshold-ts lib. Instead of keeping the sdk object in _sdk property we actually store the promise itself in _sdkPromise property. This promise is set during the construction of the SDK class and is meant to be resolved once the SDK is fully initialized. The promise is used to ensure that the SDK is initialized only once and that all subsequent retrievals of the SDK instance await this promise, thereby ensuring that the SDK is ready for use before any operations are performed.
The promise also facilitates updating the SDK instance dynamically: if a new initialization is triggered (e.g., when a user logs in and provides a new signer), it ensures that the new SDK object replaces any previously pending SDK object, even if the original promise has not yet resolved.
To retrieve the SDK object, use the getSdk() method. This method will either return the already resolved SDK object or wait until the promise resolves before returning the SDK.
Additionally I've changed the getTransactionConfirmations method implementation - it does not have to use SDK. The IsSdkInitializingContext is also not neede anymore so I've removed it completely.
Instead of storing sdk object in a property and add some helping variables (like isSdkInitalized) that will tell if the sdk is initialized or not, we now store the promise itself:
/**
* Holds the promise for the asynchronously initialized SDK instance.
* This promise is set during the construction of the SDK class and is meant
* to be resolved once the SDK is fully initialized. The promise is used to
* ensure that the SDK is initialized only once and that all subsequent
* retrievals of the SDK instance await this promise, thereby ensuring that
* the SDK is ready for use before any operations are performed.
*
* The promise also facilitates updating the SDK instance dynamically: if a
* new initialization is triggered (e.g., when a user logs in and provides a
* new signer), it ensures that the new SDK object replaces any previously
* pending SDK object, even if the original promise has not yet resolved.
*
* To retrieve the SDK object, use the `getSdk()` method. This method will
* either return the already resolved SDK object or wait until the promise
* resolves before returning the SDK.
*/
private _sdkPromise: Promise<SDK | undefined>
We retrieve the SDK object by using _getSDK method which will wait for the promise mentioned above to be resolved:
a) User interacts with tbtc contract but the SDK is not initialized yet:
in such cases we use _getSDK method so the code will wait until _sdkPromise is resolved and then proceeds with the code
b) User interacts with tbtc contract and SDK is already initialied:
this means that the promise is already resolved so we will retudn the sdk object immidietaly from this_.sdkPromise (no need to wait for it being initialized because it already is, and the promise is already resolved)
Closes: #749
Here we use lazy loading for tbtc sdk initialization in our threshold-ts lib. Instead of keeping the sdk object in
_sdk
property we actually store the promise itself in_sdkPromise
property. This promise is set during the construction of the SDK class and is meant to be resolved once the SDK is fully initialized. The promise is used to ensure that the SDK is initialized only once and that all subsequent retrievals of the SDK instance await this promise, thereby ensuring that the SDK is ready for use before any operations are performed.The promise also facilitates updating the SDK instance dynamically: if a new initialization is triggered (e.g., when a user logs in and provides a new signer), it ensures that the new SDK object replaces any previously pending SDK object, even if the original promise has not yet resolved.
To retrieve the SDK object, use the
getSdk()
method. This method will either return the already resolved SDK object or wait until the promise resolves before returning the SDK.Additionally I've changed the
getTransactionConfirmations
method implementation - it does not have to use SDK. TheIsSdkInitializingContext
is also not neede anymore so I've removed it completely.Further explanation of the flow (based on https://github.com/threshold-network/token-dashboard/pull/751#discussion_r1575410420):
isSdkInitalized
) that will tell if the sdk is initialized or not, we now store the promise itself:_getSDK
method which will wait for the promise mentioned above to be resolved:So, there are few cases that can happen:
a) User interacts with tbtc contract but the SDK is not initialized yet:
_getSDK
method so the code will wait until_sdkPromise
is resolved and then proceeds with the codeb) User interacts with tbtc contract and SDK is already initialied:
this_.sdkPromise
(no need to wait for it being initialized because it already is, and the promise is already resolved)