Skip to content

Commit

Permalink
Resolve/reject once in getWsData for typegen (#5557)
Browse files Browse the repository at this point in the history
* Resolve/reject once in getWsData for typegen

* Add onmessage rejections

* CHANGELOG
  • Loading branch information
jacogr authored Mar 23, 2023
1 parent 3b39de3 commit a79e9f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Contributed:

Changes:

- Use Promise resolve/reject to track state in typegen on-chain metadata retrievals
- Basic support for markdown generation for different chains (only Kusama & Polkadot currently added)


Expand Down
29 changes: 14 additions & 15 deletions packages/typegen/src/util/wsMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@

import type { HexString } from '@polkadot/util/types';

import { promiseTracker } from '@polkadot/api/promise/decorateMethod';
import { WebSocket } from '@polkadot/x-ws';

async function getWsData <T> (endpoint: string, method: 'rpc_methods' | 'state_getMetadata' | 'state_getRuntimeVersion'): Promise<T> {
return new Promise((resolve): void => {
return new Promise((resolve, reject): void => {
const tracker = promiseTracker<T>(resolve, reject);

try {
const websocket = new WebSocket(endpoint);

websocket.onclose = (event: { code: number; reason: string }): void => {
const msg = `disconnected, code: '${event.code}' reason: '${event.reason}'`;

if (event.code === 1000) {
console.log(msg);
} else {
console.error(msg);
process.exit(1);
if (event.code !== 1000) {
tracker.reject(new Error(`disconnected, code: '${event.code}' reason: '${event.reason}'`));
}
};

websocket.onerror = (event: unknown): void => {
console.error(event);
process.exit(1);
tracker.reject(new Error(`WebSocket error:: ${JSON.stringify(event)}`));
};

websocket.onopen = (): void => {
Expand All @@ -32,14 +29,16 @@ async function getWsData <T> (endpoint: string, method: 'rpc_methods' | 'state_g
};

websocket.onmessage = (message: { data: string }): void => {
resolve((JSON.parse(message.data) as { result: T }).result);
try {
tracker.resolve((JSON.parse(message.data) as { result: T }).result);
} catch (error) {
tracker.reject(error as Error);
}

websocket.close();
};
} catch (error) {
console.error();
console.error(error);
console.error();
process.exit(1);
tracker.reject(error as Error);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@polkadot/api": ["api/src/index.ts"],
"@polkadot/api/base": ["api/src/base/index.ts"],
"@polkadot/api/packageInfo": ["api/src/packageInfo.ts"],
"@polkadot/api/promise/decorateMethod": ["api/src/promise/decorateMethod.ts"],
"@polkadot/api/submittable/*": ["api/src/submittable/*.ts"],
"@polkadot/api/types": ["api/src/types/index.ts"],
"@polkadot/api/types/*": ["api/src/types/*.ts"],
Expand Down

0 comments on commit a79e9f5

Please sign in to comment.