Skip to content

Commit 8435fb8

Browse files
committed
chore: update sc provider
1 parent b132f64 commit 8435fb8

File tree

4 files changed

+115
-155
lines changed

4 files changed

+115
-155
lines changed

packages/rpc-provider/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@
3535
"tslib": "^2.8.1"
3636
},
3737
"devDependencies": {
38-
"@substrate/connect": "0.8.11"
38+
"@substrate/connect": "^2.1.2"
3939
},
40-
"optionalDependencies": {
41-
"@substrate/connect": "0.8.11"
40+
"peerDependencies": {
41+
"@substrate/connect": "^2.1.2"
42+
},
43+
"peerDependenciesMeta": {
44+
"@substrate/connect": {
45+
"optional": true
46+
}
4247
}
4348
}

packages/rpc-provider/src/substrate-connect/index.spec.ts

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const wait = (ms: number) =>
4141
setTimeout(resolve, ms)
4242
);
4343

44-
function healthCheckerMock (): MockedHealthChecker {
44+
function healthCheckerMock(): MockedHealthChecker {
4545
let cb: (health: SmoldotHealth) => void = () => undefined;
4646
let sendJsonRpc: (request: string) => void = () => undefined;
4747
let isActive = false;
@@ -66,7 +66,7 @@ function healthCheckerMock (): MockedHealthChecker {
6666
};
6767
}
6868

69-
function healthCheckerFactory () {
69+
function healthCheckerFactory() {
7070
const _healthCheckers: MockedHealthChecker[] = [];
7171

7272
return {
@@ -82,13 +82,29 @@ function healthCheckerFactory () {
8282
};
8383
}
8484

85-
function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
85+
function getFakeChain(spec: string): MockChain {
8686
const _receivedRequests: string[] = [];
8787
let _isTerminated = false;
8888

8989
let terminateInterceptor = Function.prototype;
9090
let sendJsonRpcInterceptor = Function.prototype;
9191

92+
const responseQueue: string[] = []
93+
94+
const nextJsonRpcResponse = async () => {
95+
while (responseQueue.length === 0) {
96+
await new Promise((resolve) => setTimeout(resolve, 0));
97+
}
98+
return responseQueue.shift()!;
99+
}
100+
101+
async function* jsonRpcResponsesGenerator(): AsyncIterableIterator<string> {
102+
while (true) {
103+
const response = await nextJsonRpcResponse();
104+
yield response;
105+
}
106+
}
107+
92108
return {
93109
_getLatestRequest: () => _receivedRequests[_receivedRequests.length - 1],
94110
_isTerminated: () => _isTerminated,
@@ -101,14 +117,15 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
101117
},
102118
_spec: () => spec,
103119
_triggerCallback: (response) => {
104-
callback(
105-
typeof response === 'string'
106-
? response
107-
: stringify(response)
108-
);
120+
const message = typeof response === 'string'
121+
? response
122+
: stringify(response)
123+
responseQueue.push(message)
124+
109125
},
110-
addChain: (chainSpec, jsonRpcCallback) =>
111-
Promise.resolve(getFakeChain(chainSpec, jsonRpcCallback ?? noop)),
126+
nextJsonRpcResponse,
127+
jsonRpcResponses: jsonRpcResponsesGenerator(),
128+
addChain: (chainSpec) => Promise.resolve(getFakeChain(chainSpec)),
112129
remove: () => {
113130
terminateInterceptor();
114131
_isTerminated = true;
@@ -120,11 +137,27 @@ function getFakeChain (spec: string, callback: Sc.JsonRpcCallback): MockChain {
120137
};
121138
}
122139

123-
function getFakeClient () {
140+
function getFakeClient() {
124141
const chains: MockChain[] = [];
125142
let addChainInterceptor: Promise<void> = Promise.resolve();
126143
let addWellKnownChainInterceptor: Promise<void> = Promise.resolve();
127144

145+
const addChain: Sc.AddChain = async (chainSpec) => addChainInterceptor.then(() => {
146+
const result = getFakeChain(chainSpec);
147+
148+
chains.push(result);
149+
150+
return result;
151+
})
152+
153+
const addWellKnownChain: Sc.AddWellKnownChain = async (wellKnownChain) => addWellKnownChainInterceptor.then(() => {
154+
const result = getFakeChain(wellKnownChain);
155+
156+
chains.push(result);
157+
158+
return result;
159+
})
160+
128161
return {
129162
_chains: () => chains,
130163
_setAddChainInterceptor: (interceptor: Promise<void>) => {
@@ -133,29 +166,12 @@ function getFakeClient () {
133166
_setAddWellKnownChainInterceptor: (interceptor: Promise<void>) => {
134167
addWellKnownChainInterceptor = interceptor;
135168
},
136-
addChain: (chainSpec: string, cb: Sc.JsonRpcCallback): Promise<MockChain> =>
137-
addChainInterceptor.then(() => {
138-
const result = getFakeChain(chainSpec, cb);
139-
140-
chains.push(result);
141-
142-
return result;
143-
}),
144-
addWellKnownChain: (
145-
wellKnownChain: string,
146-
cb: Sc.JsonRpcCallback
147-
): Promise<MockChain> =>
148-
addWellKnownChainInterceptor.then(() => {
149-
const result = getFakeChain(wellKnownChain, cb);
150-
151-
chains.push(result);
152-
153-
return result;
154-
})
169+
addChain,
170+
addWellKnownChain
155171
};
156172
}
157173

158-
function connectorFactory (): MockSc {
174+
function connectorFactory(): MockSc {
159175
const clients: ReturnType<typeof getFakeClient>[] = [];
160176
const latestClient = () => clients[clients.length - 1];
161177

@@ -175,7 +191,7 @@ function connectorFactory (): MockSc {
175191
} as unknown as MockSc;
176192
}
177193

178-
function setChainSyncyingStatus (isSyncing: boolean): void {
194+
function setChainSyncyingStatus(isSyncing: boolean): void {
179195
getCurrentHealthChecker()._triggerHealthUpdate({
180196
isSyncing,
181197
peers: 1,

packages/rpc-provider/src/substrate-connect/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class ScProvider implements ProviderInterface {
6464
#chain: Promise<ScType.Chain> | null = null;
6565
#isChainReady = false;
6666

67-
public constructor (Sc: SubstrateConnect, spec: string | ScType.WellKnownChain, sharedSandbox?: ScProvider) {
67+
public constructor (Sc: SubstrateConnect, spec: ScType.WellKnownChain | (string & {}), sharedSandbox?: ScProvider) {
6868
if (!isObject(Sc) || !isObject(Sc.WellKnownChain) || !isFunction(Sc.createScClient)) {
6969
throw new Error('Expected an @substrate/connect interface as first parameter to ScProvider');
7070
}
@@ -133,13 +133,13 @@ export class ScProvider implements ProviderInterface {
133133
}
134134

135135
const response = JSON.parse(hcRes) as JsonRpcResponse<string>;
136-
let decodedResponse: string | Error;
136+
let decodedResponse: string | Error;
137137

138-
try {
139-
decodedResponse = this.#coder.decodeResponse(response);
140-
} catch (e) {
141-
decodedResponse = e as Error;
142-
}
138+
try {
139+
decodedResponse = this.#coder.decodeResponse(response);
140+
} catch (e) {
141+
decodedResponse = e as Error;
142+
}
143143

144144
// It's not a subscription message, but rather a standar RPC response
145145
if (response.params?.subscription === undefined || !response.method) {
@@ -166,7 +166,19 @@ export class ScProvider implements ProviderInterface {
166166
? client.addWellKnownChain
167167
: client.addChain;
168168

169-
this.#chain = addChain(this.#spec as ScType.WellKnownChain, onResponse).then((chain) => {
169+
this.#chain = addChain(this.#spec as ScType.WellKnownChain).then((chain) => {
170+
// Process JSON-RPC responses
171+
void (async () => {
172+
try {
173+
for await (const response of chain.jsonRpcResponses) {
174+
onResponse(response);
175+
}
176+
} catch (error) {
177+
l.error('Error processing JSON-RPC responses:', error);
178+
this.#eventemitter.emit('error', error);
179+
}
180+
})();
181+
170182
hc.setSendJsonRpc(chain.sendJsonRpc);
171183

172184
this.#isChainReady = false;

0 commit comments

Comments
 (0)