Skip to content

Commit

Permalink
chore!: wrap subscriptions in promise (#2964)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Aug 19, 2024
1 parent 80cb187 commit bbd794a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-seals-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": minor
---

chore!: wrap subscriptions in promise
14 changes: 4 additions & 10 deletions packages/account/src/providers/fuel-graphql-subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ type FuelGraphQLSubscriberOptions = {
};

export class FuelGraphqlSubscriber implements AsyncIterator<unknown> {
private stream!: ReadableStreamDefaultReader<Uint8Array>;
private static textDecoder = new TextDecoder();

public constructor(private options: FuelGraphQLSubscriberOptions) {}

private async setStream() {
const { url, query, variables, fetchFn } = this.options;
private constructor(private stream: ReadableStreamDefaultReader<Uint8Array>) {}

public static async create(options: FuelGraphQLSubscriberOptions) {
const { url, query, variables, fetchFn } = options;
const response = await fetchFn(`${url}-sub`, {
method: 'POST',
body: JSON.stringify({
Expand All @@ -31,17 +29,13 @@ export class FuelGraphqlSubscriber implements AsyncIterator<unknown> {
});

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.stream = response.body!.getReader();
return new FuelGraphqlSubscriber(response.body!.getReader());
}

private events: Array<{ data: unknown; errors?: { message: string }[] }> = [];
private parsingLeftover = '';

async next(): Promise<IteratorResult<unknown, unknown>> {
if (!this.stream) {
await this.setStream();
}

// eslint-disable-next-line no-constant-condition
while (true) {
if (this.events.length > 0) {
Expand Down
20 changes: 10 additions & 10 deletions packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`

await expectToThrowFuelError(
async () => {
for await (const value of provider.operations.statusChange({
for await (const value of await provider.operations.statusChange({
transactionId: 'invalid transaction id',
})) {
// shouldn't be reached and should fail if reached
Expand Down Expand Up @@ -1012,7 +1012,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`
);

const { error } = await safeExec(async () => {
for await (const iterator of provider.operations.statusChange({
for await (const iterator of await provider.operations.statusChange({
transactionId: 'doesnt matter, will be aborted',
})) {
// shouldn't be reached and should fail if reached
Expand Down Expand Up @@ -1153,7 +1153,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`
);
});

for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
expect(submitAndAwait.type).toEqual('SuccessStatus');
Expand Down Expand Up @@ -1185,7 +1185,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`

let numberOfEvents = 0;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
numberOfEvents += 1;
Expand Down Expand Up @@ -1232,7 +1232,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`

let numberOfEvents = 0;

for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
numberOfEvents += 1;
Expand Down Expand Up @@ -1280,7 +1280,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`
);
});

for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
expect(submitAndAwait.type).toEqual('SuccessStatus');
Expand Down Expand Up @@ -1328,7 +1328,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`

let numberOfEvents = 0;

for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
numberOfEvents += 1;
Expand Down Expand Up @@ -1387,7 +1387,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`

let numberOfEvents = 0;

for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
numberOfEvents += 1;
Expand Down Expand Up @@ -1425,7 +1425,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`
await expectToThrowFuelError(
async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const { submitAndAwait } of provider.operations.submitAndAwait({
for await (const { submitAndAwait } of await provider.operations.submitAndAwait({
encodedTransaction: "it's mocked so doesn't matter",
})) {
// shouldn't be reached!
Expand Down Expand Up @@ -1492,7 +1492,7 @@ Supported fuel-core version: ${mock.supportedVersion}.`
});

await safeExec(async () => {
for await (const iterator of provider.operations.statusChange({
for await (const iterator of await provider.operations.statusChange({
transactionId: 'doesnt matter, will be aborted',
})) {
// Just running a subscription to trigger the middleware
Expand Down
17 changes: 14 additions & 3 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,22 @@ type ChainInfoCache = Record<string, ChainInfo>;
*/
type NodeInfoCache = Record<string, NodeInfo>;

type Operations = ReturnType<typeof getOperationsSdk>;

type SdkOperations = Omit<Operations, 'submitAndAwait' | 'statusChange'> & {
submitAndAwait: (
...args: Parameters<Operations['submitAndAwait']>
) => Promise<ReturnType<Operations['submitAndAwait']>>;
statusChange: (
...args: Parameters<Operations['statusChange']>
) => Promise<ReturnType<Operations['statusChange']>>;
};

/**
* A provider for connecting to a node
*/
export default class Provider {
operations: ReturnType<typeof getOperationsSdk>;
operations: SdkOperations;
cache?: ResourceCache;

/** @hidden */
Expand Down Expand Up @@ -559,7 +570,7 @@ Supported fuel-core version: ${supportedVersion}.`
* @returns The operation SDK object
* @hidden
*/
private createOperations() {
private createOperations(): SdkOperations {
const fetchFn = Provider.getFetchFn(this.options);
const gqlClient = new GraphQLClient(this.url, {
fetch: (url: string, requestInit: RequestInit) => fetchFn(url, requestInit, this.options),
Expand All @@ -583,7 +594,7 @@ Supported fuel-core version: ${supportedVersion}.`
const isSubscription = opDefinition?.operation === 'subscription';

if (isSubscription) {
return new FuelGraphqlSubscriber({
return FuelGraphqlSubscriber.create({
url: this.url,
query,
fetchFn: (url, requestInit) => fetchFn(url as string, requestInit, this.options),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class TransactionResponse {
});

if (!response.transaction) {
const subscription = this.provider.operations.statusChange({
const subscription = await this.provider.operations.statusChange({
transactionId: this.id,
});

Expand Down Expand Up @@ -236,7 +236,7 @@ export class TransactionResponse {
return;
}

const subscription = this.provider.operations.statusChange({
const subscription = await this.provider.operations.statusChange({
transactionId: this.id,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/fuel-gauge/src/edge-cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Edge Cases', () => {

await response.waitForResult();

const subsciption = provider.operations.statusChange({ transactionId });
const subsciption = await provider.operations.statusChange({ transactionId });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const iterator of subsciption) {
Expand Down

0 comments on commit bbd794a

Please sign in to comment.