Skip to content

Commit

Permalink
feat(TypedMessenger): Add response options (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
jespertheend authored Oct 10, 2023
1 parent 240eec2 commit 8bc2fa1
Show file tree
Hide file tree
Showing 9 changed files with 456 additions and 236 deletions.
236 changes: 155 additions & 81 deletions src/util/TypedMessenger.js

Large diffs are not rendered by default.

35 changes: 12 additions & 23 deletions studio/src/tasks/task/TaskBuildApplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class TaskBuildApplication extends Task {
},
});

/** @type {TypedMessenger<BuildApplicationMessengerResponseHandlers, import("../workers/buildApplication/mod.js").BuildApplicationMessengerResponseHandlers, true>} */
/** @type {TypedMessenger<BuildApplicationMessengerResponseHandlers, import("../workers/buildApplication/mod.js").BuildApplicationMessengerResponseHandlers>} */
#messenger;

#lastContextId = 0;
Expand All @@ -46,7 +46,7 @@ export class TaskBuildApplication extends Task {
constructor(...args) {
super(...args);

this.#messenger = new TypedMessenger({returnTransferSupport: true});
this.#messenger = new TypedMessenger();
this.#messenger.initializeWorker(this.worker, this.getResponseHandlers());
}

Expand Down Expand Up @@ -92,14 +92,9 @@ export class TaskBuildApplication extends Task {
*/
readJavaScript: async (contextId, uuid) => {
const context = this.getContext(contextId);
const javaScriptContent = await context.readAssetFromUuid(uuid, {
return await context.readAssetFromUuid(uuid, {
assertAssetType: ProjectAssetTypeJavascript,
});
/** @type {import("../../../../src/util/TypedMessenger.js").RequestHandlerReturn} */
const result = {
returnValue: javaScriptContent,
};
return result;
},
/**
* @param {number} contextId
Expand All @@ -123,10 +118,12 @@ export class TaskBuildApplication extends Task {
throw new Error("Assertion failed: unexpected asset bundle file data, not an ArrayBuffer");
}

/** @type {import("../../../../src/util/TypedMessenger.js").RequestHandlerReturn} */
/** @satisfies {import("../../../../src/util/TypedMessenger.js").TypedMessengerRequestHandlerReturn} */
const result = {
returnValue: assetBundle,
transfer: [assetBundle],
$respondOptions: {
returnValue: assetBundle,
transfer: [assetBundle],
},
};
return result;
},
Expand All @@ -148,10 +145,8 @@ export class TaskBuildApplication extends Task {
const servicesScript = this.#assertSingleStringWriteAsset(generateServicesResult);

return {
returnValue: {
servicesScript,
usedAssets: generateServicesResult.customData?.usedAssets || [],
},
servicesScript,
usedAssets: generateServicesResult.customData?.usedAssets || [],
};
},
/**
Expand All @@ -161,16 +156,13 @@ export class TaskBuildApplication extends Task {
*/
bundleScripts: async (contextId, entryPoint, servicesSource) => {
const context = this.getContext(contextId);
const bundleScriptsResult = await context.runChildTask("renda:bundleScripts", {
return await context.runChildTask("renda:bundleScripts", {
outputPath: ["js"],
entryPoints: [entryPoint],
servicesSource,
}, {
allowDiskWrites: false,
});
return {
returnValue: bundleScriptsResult,
};
},
/**
* @param {number} contextId
Expand All @@ -188,10 +180,7 @@ export class TaskBuildApplication extends Task {
}, {
allowDiskWrites: false,
});
const html = this.#assertSingleStringWriteAsset(result);
return {
returnValue: html,
};
return this.#assertSingleStringWriteAsset(result);
},
};
}
Expand Down
20 changes: 12 additions & 8 deletions studio/src/tasks/task/TaskBundleAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function getResponseHandlers(assetManager, fileStreams) {
*/
async getBundledAssetData(assetUuid) {
const asset = await assetManager.getProjectAssetFromUuid(assetUuid);
if (!asset) return {returnValue: null};
if (!asset) return null;

const assetTypeUuid = await asset.getAssetTypeUuid();
// TODO: Ideally assets without an asset type are filtered out before the initial message is sent to the worker.
Expand All @@ -60,13 +60,17 @@ function getResponseHandlers(assetManager, fileStreams) {
transfer = [bufferOrString];
}

return {
returnValue: {
assetTypeUuid,
assetData: bufferOrString,
/** @satisfies {import("../../../../src/mod.js").TypedMessengerRequestHandlerReturn} */
const result = {
$respondOptions: {
returnValue: {
assetTypeUuid,
assetData: bufferOrString,
},
transfer,
},
transfer,
};
return result;
},
};
}
Expand Down Expand Up @@ -142,7 +146,7 @@ export class TaskBundleAssets extends Task {
},
});

/** @type {TypedMessenger<BundleAssetsMessengerResponseHandlers, import("../workers/bundleAssets/mod.js").BundleAssetsMessengerResponseHandlers, true>} */
/** @type {TypedMessenger<BundleAssetsMessengerResponseHandlers, import("../workers/bundleAssets/mod.js").BundleAssetsMessengerResponseHandlers>} */
#messenger;

#lastFileStreamId = 0;
Expand All @@ -155,7 +159,7 @@ export class TaskBundleAssets extends Task {
constructor(...args) {
super(...args);

this.#messenger = new TypedMessenger({returnTransferSupport: true});
this.#messenger = new TypedMessenger();
const assetManager = this.studioInstance.projectManager.assetManager;
if (!assetManager) {
throw new Error("Failed to create Bundle Scripts task: no asset manager.");
Expand Down
10 changes: 3 additions & 7 deletions studio/src/tasks/workers/buildApplication/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {TypedMessenger} from "../../../../../src/util/TypedMessenger.js";

/** @typedef {typeof responseHandlers} BuildApplicationMessengerResponseHandlers */

/** @type {TypedMessenger<BuildApplicationMessengerResponseHandlers, import("../../task/TaskBuildApplication.js").BuildApplicationMessengerResponseHandlers, true>} */
const messenger = new TypedMessenger({returnTransferSupport: true});
/** @type {TypedMessenger<BuildApplicationMessengerResponseHandlers, import("../../task/TaskBuildApplication.js").BuildApplicationMessengerResponseHandlers>} */
const messenger = new TypedMessenger();

const responseHandlers = {
/**
Expand Down Expand Up @@ -74,11 +74,7 @@ const responseHandlers = {
/** @type {import("../../task/Task.js").RunTaskReturn} */
const taskResult = {writeAssets};

/** @type {import("../../../../../src/util/TypedMessenger.js").RequestHandlerReturn} */
const requestResult = {
returnValue: taskResult,
};
return requestResult;
return taskResult;
},
};

Expand Down
7 changes: 2 additions & 5 deletions studio/src/tasks/workers/bundleAssets/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ export async function bundle(assetUuids, fileStreamId, messenger) {
}
}

let returnValue = null;
if (useFileStream) {
await messenger.sendWithOptions.writeFile({transfer: [header]}, fileStreamId, {
type: "write",
position: 0,
data: header,
});
await messenger.send.closeFile(fileStreamId);
return null;
} else {
let totalBufferLength = header.byteLength;
for (const buffer of assetBuffers) {
Expand All @@ -97,9 +97,6 @@ export async function bundle(assetUuids, fileStreamId, messenger) {
view.set(new Uint8Array(buffer), offset);
offset += buffer.byteLength;
}
returnValue = view.buffer;
return view.buffer;
}
return {
returnValue,
};
}
4 changes: 2 additions & 2 deletions studio/src/tasks/workers/bundleAssets/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {bundle} from "./bundle.js";
/** @typedef {typeof messenger} BundleScriptsMessenger */
/** @typedef {typeof responseHandlers} BundleAssetsMessengerResponseHandlers */

/** @type {TypedMessenger<BundleAssetsMessengerResponseHandlers, import("../../task/TaskBundleAssets.js").BundleAssetsMessengerResponseHandlers, true>} */
const messenger = new TypedMessenger({returnTransferSupport: true});
/** @type {TypedMessenger<BundleAssetsMessengerResponseHandlers, import("../../task/TaskBundleAssets.js").BundleAssetsMessengerResponseHandlers>} */
const messenger = new TypedMessenger();

const responseHandlers = {
/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/shared/typeAssertions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* These is a helper function for verifying types. You can use them to verify
* This is a helper function for verifying types. You can use them to verify
* return types from functions with generics.
*
* This function checks if the second parameter 'fits' inside the first parameter.
Expand Down
Loading

0 comments on commit 8bc2fa1

Please sign in to comment.