Skip to content

Commit

Permalink
Fix async worker return type (#7249)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmconne authored Oct 9, 2024
1 parent 3269edd commit e8139a9
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions common/api/core-bentley.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,9 @@ export class LRUMap<K, V> extends LRUCache<K, V> {
// @public
export type MarkRequired<T, K extends keyof T> = Pick<Required<T>, K> & Omit<T, K>;

// @public
export type MaybePromise<T> = T | Promise<T>;

// @public
export type Mutable<T> = {
-readonly [K in keyof T]: T[K];
Expand Down
7 changes: 4 additions & 3 deletions common/api/core-frontend.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ import { MassPropertiesRequestProps } from '@itwin/core-common';
import { MassPropertiesResponseProps } from '@itwin/core-common';
import { Matrix3d } from '@itwin/core-geometry';
import { Matrix4d } from '@itwin/core-geometry';
import { MaybePromise } from '@itwin/core-bentley';
import { MeshEdges } from '@itwin/core-common';
import { MeshPolyline } from '@itwin/core-common';
import { MeshPolylineList } from '@itwin/core-common';
Expand Down Expand Up @@ -15710,10 +15711,10 @@ export type WorkerProxy<T> = WorkerInterface<T> & {
};

// @beta
export type WorkerReturnType<T extends (...args: any) => any> = ReturnType<T> | {
result: ReturnType<T>;
export type WorkerReturnType<T extends (...args: any) => any> = MaybePromise<ReturnType<T> | {
result: Awaited<ReturnType<T>>;
transfer: Transferable[];
};
}>;

// @beta
export interface WorkerTextureParams {
Expand Down
1 change: 1 addition & 0 deletions common/api/summary/core-bentley.exports.csv
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public;class;LRUCache
public;class;LRUDictionary
public;class;LRUMap
public;type;MarkRequired
public;type;MaybePromise
public;type;Mutable
public;class;MutableCompressedId64Set
public;type;NonFunctionPropertiesOf
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-bentley",
"comment": "Add MaybePromise utility type.",
"type": "none"
}
],
"packageName": "@itwin/core-bentley"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-frontend",
"comment": "Fix WorkerReturnType for async implementations.",
"type": "none"
}
],
"packageName": "@itwin/core-frontend"
}
5 changes: 5 additions & 0 deletions core/bentley/src/UtilityTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export type PickAsyncMethods<T> = { [P in keyof T]: T[P] extends AsyncFunction ?
*/
export type AsyncMethodsOf<T> = { [P in keyof T]: T[P] extends AsyncFunction ? P : never }[keyof T];

/** A type that is either `T` or `Promise<T>`.
* @public
*/
export type MaybePromise<T> = T | Promise<T>;

/** Extracts the type to which the Promise returned by an async function resolves.
* @public
*/
Expand Down
6 changes: 3 additions & 3 deletions core/frontend/src/common/WorkerProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/** @packageDocumentation
* @module Utils
*/
import { assert } from "@itwin/core-bentley";
import { assert, MaybePromise } from "@itwin/core-bentley";

/** Holds callbacks for a Promise produced for a particular call to postMessage. */
interface Task {
Expand Down Expand Up @@ -48,11 +48,11 @@ export type WorkerInterface<T> = {
};

/** Augments each method of `T` with the ability to specify values to be transferred from the worker thread to the main thread.
* Each return type `R` is replaced with `R | { result: R; transfer: Transferable[]; }`.
* Each return type `R` is replaced with `R | { result: R; transfer: Transferable[]; }`, or a promise that resolves to such a type.
* @see [[WorkerImplementation]].
* @beta
*/
export type WorkerReturnType<T extends (...args: any) => any> = ReturnType<T> | { result: ReturnType<T>, transfer: Transferable[] };
export type WorkerReturnType<T extends (...args: any) => any> = MaybePromise<ReturnType<T> | { result: Awaited<ReturnType<T>>, transfer: Transferable[] }>;

/** Given an interface T that defines the operations provided by a worker, produce an interface to which the implementation of those operations must conform.
* The return type of each function is enhanced to permit supplying a list of values to be transferred from the worker to the main thread.
Expand Down
2 changes: 1 addition & 1 deletion core/frontend/src/test/worker/test-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ registerWorker<TestWorker>({

someVeryLongRunningAsyncOperation: async () => {
await waitNTicks(10);
return ++globalTickCounter;
return { result: ++globalTickCounter, transfer: [] };
},
someLongRunningAsyncOperation: async () => {
await waitNTicks(5);
Expand Down

0 comments on commit e8139a9

Please sign in to comment.