Skip to content

Commit acdf2c2

Browse files
committed
add initial changes to queries converting PromiseLike -> Promise
1 parent 3545d75 commit acdf2c2

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

packages/data-connect/src/api/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface QueryResult<Data, Variables>
6767
* Promise returned from `executeQuery`
6868
*/
6969
export interface QueryPromise<Data, Variables>
70-
extends PromiseLike<QueryResult<Data, Variables>> {
70+
extends Promise<QueryResult<Data, Variables>> {
7171
// reserved for special actions like cancellation
7272
}
7373

packages/data-connect/src/network/transport/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface DataConnectTransport {
2626
invokeQuery<T, U>(
2727
queryName: string,
2828
body?: U
29-
): PromiseLike<{ data: T; errors: Error[] }>;
29+
): Promise<{ data: T; errors: Error[] }>;
3030
invokeMutation<T, U>(
3131
queryName: string,
3232
body?: U
@@ -35,7 +35,12 @@ export interface DataConnectTransport {
3535
onTokenChanged: (token: string | null) => void;
3636
}
3737

38-
export interface CancellableOperation<T> extends PromiseLike<{ data: T }> {
38+
// QUESTION:
39+
// is this the class we should be using instead of replacing "PromiseLike" with "Promise" ?
40+
// - it adds "cancel" in there for us
41+
// - it extends promise so it's not really a "frankenstein" promise - no need for Symbol.toStringTag
42+
// - what about binding though?
43+
export interface CancellableOperation<T> extends Promise<{ data: T }> {
3944
cancel: () => void;
4045
}
4146

packages/data-connect/src/network/transport/rest.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class RESTTransport implements DataConnectTransport {
161161
invokeQuery: <T, U>(
162162
queryName: string,
163163
body?: U
164-
) => PromiseLike<{ data: T; errors: Error[] }> = <T, U = unknown>(
164+
) => Promise<{ data: T; errors: Error[] }> = <T, U = unknown>(
165165
queryName: string,
166166
body: U
167167
) => {
@@ -183,9 +183,22 @@ export class RESTTransport implements DataConnectTransport {
183183
)
184184
);
185185

186+
// QUESTION:
187+
// my understanding is that the code below does the following:
188+
// - return an object with properties which are methods (the same methods required for Promise, plus one more, "cancel")
189+
// - each of these methods should be bound to the corresponding method from the withAuth promise
190+
// - why not just return withAuth? is this so we can add the "cancel" method in there?
191+
// ANOTHER QUESTION:
192+
// - do we want queries to be cancellable? there was no "cancel" method written here, i added it
186193
return {
187194
then: withAuth.then.bind(withAuth),
188-
catch: withAuth.catch.bind(withAuth)
195+
catch: withAuth.catch.bind(withAuth),
196+
finally: withAuth.finally.bind(withAuth),
197+
cancel: () => abortController.abort(),
198+
get [Symbol.toStringTag]() {
199+
return 'Promise';
200+
}
201+
189202
};
190203
};
191204
invokeMutation: <T, U>(
@@ -213,8 +226,8 @@ export class RESTTransport implements DataConnectTransport {
213226
});
214227

215228

216-
// QUESTIONS:
217-
// 1) my understanding is that the code below does the following:
229+
// QUESTION:
230+
// my understanding is that the code below does the following:
218231
// - return an object with properties which are methods (the same methods required for Promise, plus one more, "cancel")
219232
// - each of these methods should be bound to the corresponding method from the taskResult promise
220233
// - why not just return taskResult? is this so we can add the "cancel" method in there?

0 commit comments

Comments
 (0)