Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JST-598: simplify worker type #673

Merged
merged 5 commits into from
Nov 23, 2023
Merged

Conversation

SewerynKras
Copy link
Contributor

BREAKING CHANGE: worker type no longer takes two parameters

BREAKING CHANGE: worker type no longer takes two parameters
@SewerynKras SewerynKras requested a review from a team November 21, 2023 11:11
@@ -117,7 +117,7 @@ export class TaskExecutor {
private networkService?: NetworkService;
private statsService: StatsService;
private activityReadySetupFunctions: Worker[] = [];
private taskQueue: TaskQueue<Task<unknown, unknown>>;
private taskQueue: TaskQueue<Task>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nice improvement can be to change the signature to TaskQueue<T extends QueueableTask = Task> to provide a nice default Then we could just use private taskQueue: TaskQueue; instead.

@@ -98,7 +98,7 @@ export class GolemNetwork {
* console.log(status);
* ```
*/
public async createJob<Output = unknown>(worker: Worker<unknown, Output>) {
public async createJob<Output = unknown>(worker: Worker<Output>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we could drop = unknown as the type inference would work here just perfectly.

const worker: Worker<null, Result> = async (ctx) => ctx.run("some_shell_command");
const task = new Task<null, Result>("1", worker);
const worker: Worker<Result> = async (ctx) => ctx.run("some_shell_command");
const task = new Task<Result>("1", worker);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Write code to promote type inference. The equivalent would be:

const worker = async (ctx: WorkContext) => ctx.run("some_shell_command");
const task = new Task("1", worker);

This way We don't have to specify Worker<Result> | Task<Result> and the type inference does the job.

@@ -17,7 +17,7 @@ describe("Work Context", () => {
describe("Executing", () => {
it("should execute run command", async () => {
const activity = await Activity.create("test_agreement_id", yagnaApi);
const worker: Worker<void, Result> = async (ctx) => ctx.run("some_shell_command");
const worker: Worker<Result> = async (ctx) => ctx.run("some_shell_command");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about type inference applies here. Refactored example:

const worker = async (ctx: WorkContext) => ctx.run("some_shell_command");

BREAKING CHANGE: `executor.run` always returns the `OutputType` or rejects with an Error.
`undefined` has been removed from the return type union.
@SewerynKras
Copy link
Contributor Author

With the change to executor.run's return type, we now get the correct type inference:
image

For reference, previously the type union always had | undefined
image

@SewerynKras SewerynKras requested a review from grisha87 November 22, 2023 12:43
@SewerynKras SewerynKras merged commit 8487362 into beta Nov 23, 2023
16 checks passed
@SewerynKras SewerynKras deleted the feature/JST-598/simplify-worker-type branch November 23, 2023 15:10
Copy link

🎉 This PR is included in version 1.0.0-beta.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants