Bump dev & re-export ScProvider (#5150)

* Bump dev & re-export ScProvider

* CHANGELOG

* Expose WellKnownChain on ScProvider

* Adjust ScProvider method formatting

* Adjust CHANGELOG
This commit is contained in:
Jaco
2022-08-11 22:10:45 +03:00
committed by GitHub
parent 3b48825fe6
commit 3e9d2d6b8f
5 changed files with 26 additions and 36 deletions
@@ -7,7 +7,7 @@ import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, Pro
import { Chain, createScClient, ScClient, WellKnownChain } from '@substrate/connect';
import EventEmitter from 'eventemitter3';
import { isError } from '@polkadot/util';
import { isError, objectSpread } from '@polkadot/util';
import { RpcCoder } from '../coder';
import { healthChecker } from './Health';
@@ -36,20 +36,19 @@ const wellKnownChains = new Set(Object.values(WellKnownChain));
const scClients = new WeakMap<ScProvider, ScClient>();
export { WellKnownChain };
export class ScProvider implements ProviderInterface {
readonly #coder: RpcCoder = new RpcCoder();
readonly #spec: string | WellKnownChain;
readonly #sharedSandbox?: ScProvider;
readonly #subscriptions: Map<
string,
[ResponseCallback, { unsubscribeMethod: string; id: string | number }]
> = new Map();
readonly #subscriptions: Map<string, [ResponseCallback, { unsubscribeMethod: string; id: string | number }]> = new Map();
readonly #requests: Map<number, ResponseCallback> = new Map();
readonly #eventemitter: EventEmitter = new EventEmitter();
#chain: Promise<Chain> | null = null;
#isChainReady = false;
static WellKnownChain = WellKnownChain;
public constructor (spec: string | WellKnownChain, sharedSandbox?: ScProvider) {
this.#spec = spec;
this.#sharedSandbox = sharedSandbox;
@@ -215,15 +214,14 @@ export class ScProvider implements ProviderInterface {
this.#eventemitter.emit(isReady ? 'connected' : 'disconnected');
});
return {
...chain,
return objectSpread({}, chain, {
remove: () => {
hc.stop();
chain.remove();
cleanup();
},
sendJsonRpc: hc.sendJsonRpc.bind(hc)
};
});
});
try {
@@ -252,10 +250,7 @@ export class ScProvider implements ProviderInterface {
this.#eventemitter.emit('disconnected');
}
public on (
type: ProviderInterfaceEmitted,
sub: ProviderInterfaceEmitCb
): () => void {
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
// It's possible. Although, quite unlikely, that by the time that polkadot
// subscribes to the `connected` event, the Provider is already connected.
// In that case, we must emit to let the consumer know that we are connected.
@@ -305,12 +300,7 @@ export class ScProvider implements ProviderInterface {
}
}
public async subscribe (
type: string,
method: string,
params: any[],
callback: ProviderInterfaceCallback
): Promise<number | string> {
public async subscribe (type: string, method: string, params: any[], callback: ProviderInterfaceCallback): Promise<number | string> {
if (!subscriptionUnsubscriptionMethods.has(method)) {
throw new Error(`Unsupported subscribe method: ${method}`);
}
@@ -337,11 +327,7 @@ export class ScProvider implements ProviderInterface {
return id;
}
public unsubscribe (
type: string,
method: string,
id: number | string
): Promise<boolean> {
public unsubscribe (type: string, method: string, id: number | string): Promise<boolean> {
if (!this.isConnected) {
throw new Error('Provider is not connected');
}