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

[task-manager] correct the return type of TaskManagerTaskExecutor #32557

Merged
merged 2 commits into from
Nov 5, 2024

Conversation

Simek
Copy link
Collaborator

@Simek Simek commented Nov 3, 2024

Why

Should fix:

How

Add missing Promise to the TaskManagerTaskExecutor return type. We were already awaiting the response:

Test Plan

The package lints correctly. The docs changes have been reviewed by running app locally.

Preview

Screenshot 2024-11-03 at 09 32 10

Simek and others added 2 commits November 3, 2024 09:32
@Simek Simek linked an issue Nov 3, 2024 that may be closed by this pull request
@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Nov 3, 2024
@Simek Simek force-pushed the @simek/task-manager-correct-type branch from 6da2292 to 6af7b7d Compare November 3, 2024 09:33
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Nov 3, 2024
@Simek Simek marked this pull request as ready for review November 3, 2024 09:58
Copy link
Contributor

github-actions bot commented Nov 3, 2024

Subscribed to pull request

File Patterns Mentions
docs/** @amandeepmittal
packages/expo-task-manager/** @tsapeta, @byCedric

Generated by CodeMention

@tsapeta tsapeta requested a review from chrfalch November 3, 2024 22:37
@Simek Simek merged commit baf85f1 into main Nov 5, 2024
22 of 23 checks passed
@Simek Simek deleted the @simek/task-manager-correct-type branch November 5, 2024 17:31
@brentvatne brentvatne added the published Changes from the PR have been published to npm label Nov 5, 2024
@@ -86,13 +86,15 @@ export interface RegisteredTask extends TaskManagerTask {}
/**
* Type of task executor – a function that handles the task.
*/
export type TaskManagerTaskExecutor<T = unknown> = (body: TaskManagerTaskBody<T>) => void;
export type TaskManagerTaskExecutor<T = any> = (body: TaskManagerTaskBody<T>) => Promise<any>;
Copy link
Contributor

@trajano trajano Nov 6, 2024

Choose a reason for hiding this comment

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

rather than Promise<any> it may be better to have

export type TaskManagerTaskExecutor<T = unknown, R= unknown> = (body: TaskManagerTaskBody<T>) => Promise<R>;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was thinking about introducing an another generic, but if you look at the code, we already were casting the result to any, and it would be a breaking change for all of the task manager users in the TS project, and since we are close to the stable SDK release I decided not to introduce breaking change that late in the process.

However, we might revisit it this in the future, and follow up with more strict typing.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK but I don't think it should break as you added a "default". The default can be any as well to match your result.

FYI you already changed the default of the data type original from unknown to any.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm talking about the typing which we apply for the returned result in the handling process, it was any for few years already, and I have changed unknown to match that:

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok ...

I was wondering why T=unknown changed to T=any?

Also

adding a typing after so it's <T=unknown,R=unknown> would not break the type because Promise<R> which resolves to R would get matched with any

What it does allow you to do is for BackgroundFetch as an example in a future API

BackgroundFetch.defineTask(task:TaskManagerExecutor<Record<string,unknown>, BackgroundFetchResult>)

So it's a specific strongly typed version of defineTask for BackgroundFetch.

I chose Record<string,unknown> because the data varies from APNS vs FCM as I have noted in

@trajano
Copy link
Contributor

trajano commented Nov 6, 2024

also would this be backported to 51?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about published Changes from the PR have been published to npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[docs] BackgroundFetch example does not match the type
5 participants