Skip to content

Commit

Permalink
Updated to include promise instead of promiselike
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht committed Dec 6, 2024
1 parent cb4309f commit 02bca93
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 32 deletions.
6 changes: 3 additions & 3 deletions packages/data-connect/src/api/Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function mutationRef<Data, Variables>(
* @internal
*/
export class MutationManager {
private _inflight: Array<PromiseLike<unknown>> = [];
private _inflight: Array<Promise<unknown>> = [];
constructor(private _transport: DataConnectTransport) {}
executeMutation<Data, Variables>(
mutationRef: MutationRef<Data, Variables>
Expand All @@ -95,7 +95,7 @@ export class MutationManager {
return obj;
});
this._inflight.push(result);
const removePromise = (): Array<PromiseLike<unknown>> =>
const removePromise = (): Array<Promise<unknown>> =>
(this._inflight = this._inflight.filter(promise => promise !== result));
result.then(removePromise, removePromise);
return withRefPromise;
Expand All @@ -113,7 +113,7 @@ export interface MutationResult<Data, Variables>
* Mutation return value from `executeMutation`
*/
export interface MutationPromise<Data, Variables>
extends PromiseLike<MutationResult<Data, Variables>> {
extends Promise<MutationResult<Data, Variables>> {
// reserved for special actions like cancellation
}

Expand Down
4 changes: 2 additions & 2 deletions packages/data-connect/src/api/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface QueryResult<Data, Variables>
* Promise returned from `executeQuery`
*/
export interface QueryPromise<Data, Variables>
extends PromiseLike<QueryResult<Data, Variables>> {
extends Promise<QueryResult<Data, Variables>> {
// reserved for special actions like cancellation
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export function queryRef<Data, Variables>(
dataConnect: dcInstance,
refType: QUERY_STR,
name: queryName,
variables: variables as Variables
variables: variables
};
}
/**
Expand Down
7 changes: 3 additions & 4 deletions packages/data-connect/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ export class QueryManager {
variables: queryRef.variables,
refType: QUERY_STR
});
const trackedQuery = this._queries.get(key) as TrackedQuery<
Data,
Variables
>;
const trackedQuery: TrackedQuery<Data, Variables> = this._queries.get(
key
) as TrackedQuery<Data, Variables>;
const subscription = {
userCallback: onResultCallback,
errCallback: onErrorCallback
Expand Down
9 changes: 7 additions & 2 deletions packages/data-connect/src/network/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ function getGoogApiClientValue(_isUsingGen: boolean): string {
}
return str;
}
export interface DataConnectFetchBody<T> {
name: string;
operationName: string;
variables: T;
}
export function dcFetch<T, U>(
url: string,
body: U,
body: DataConnectFetchBody<U>,
{ signal }: AbortController,
appId: string | null,
accessToken: string | null,
Expand Down Expand Up @@ -95,7 +100,7 @@ export function dcFetch<T, U>(
logError('DataConnect error while performing request: ' + stringified);
throw new DataConnectError(Code.OTHER, stringified);
}
return res as { data: T; errors: Error[] };
return res;
});
}
interface MessageObject {
Expand Down
6 changes: 3 additions & 3 deletions packages/data-connect/src/network/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export interface DataConnectTransport {
invokeQuery<T, U>(
queryName: string,
body?: U
): PromiseLike<{ data: T; errors: Error[] }>;
): Promise<{ data: T; errors: Error[] }>;
invokeMutation<T, U>(
queryName: string,
body?: U
): PromiseLike<{ data: T; errors: Error[] }>;
): Promise<{ data: T; errors: Error[] }>;
useEmulator(host: string, port?: number, sslEnabled?: boolean): void;
onTokenChanged: (token: string | null) => void;
}

export interface CancellableOperation<T> extends PromiseLike<{ data: T }> {
export interface CancellableOperation<T> extends Promise<{ data: T }> {
cancel: () => void;
}

Expand Down
22 changes: 6 additions & 16 deletions packages/data-connect/src/network/transport/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class RESTTransport implements DataConnectTransport {
invokeQuery: <T, U>(
queryName: string,
body?: U
) => PromiseLike<{ data: T; errors: Error[] }> = <T, U = unknown>(
) => Promise<{ data: T; errors: Error[] }> = <T, U = unknown>(
queryName: string,
body: U
) => {
Expand All @@ -174,24 +174,20 @@ export class RESTTransport implements DataConnectTransport {
name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,
operationName: queryName,
variables: body
} as unknown as U, // TODO(mtewani): This is a patch, fix this.
},
abortController,
this.appId,
this._accessToken,
this._appCheckToken,
this._isUsingGen
)
);

return {
then: withAuth.then.bind(withAuth),
catch: withAuth.catch.bind(withAuth)
};
return withAuth;
};
invokeMutation: <T, U>(
queryName: string,
body?: U
) => PromiseLike<{ data: T; errors: Error[] }> = <T, U = unknown>(
) => Promise<{ data: T; errors: Error[] }> = <T, U = unknown>(
mutationName: string,
body: U
) => {
Expand All @@ -203,20 +199,14 @@ export class RESTTransport implements DataConnectTransport {
name: `projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,
operationName: mutationName,
variables: body
} as unknown as U,
},
abortController,
this.appId,
this._accessToken,
this._appCheckToken,
this._isUsingGen
);
});

return {
then: taskResult.then.bind(taskResult),
// catch: taskResult.catch.bind(taskResult),
// finally: taskResult.finally.bind(taskResult),
cancel: () => abortController.abort()
};
return taskResult;
};
}
12 changes: 10 additions & 2 deletions packages/data-connect/test/unit/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ describe('fetch', () => {
await expect(
dcFetch(
'http://localhost',
{},
{
name: 'n',
operationName: 'n',
variables: {}
},
{} as AbortController,
null,
null,
Expand All @@ -61,7 +65,11 @@ describe('fetch', () => {
await expect(
dcFetch(
'http://localhost',
{},
{
name: 'n',
operationName: 'n',
variables: {}
},
{} as AbortController,
null,
null,
Expand Down

0 comments on commit 02bca93

Please sign in to comment.