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

Use comlink in workers #5915

Merged
merged 1 commit into from May 18, 2020
Merged

Use comlink in workers #5915

merged 1 commit into from May 18, 2020

Conversation

balloob
Copy link
Member

@balloob balloob commented May 17, 2020

Breaking change

Proposed change

This migrates our two worker scripts to use new Worker(…) and use worker-plugin instead of workerize-loader to automatically move this worker script into a chunk. Workerize did two things: move script + dependencies into a chunk and create an async API for it.

With worker-plugin + comlink we get the same experience except it's in two parts.

To help see what is going on, I have also extracted the code that interacts with the worker into standalone functions.

However, there is a downside to comlink: it requires ES proxies.

We have two options:

  • Add proxy-polyfill to the compatibility.ts and load it in both the web workers.
  • Do not use web workers in ES5 mode but instead dynamically import the file.

This PR implements the 1st option as it keeps our code more consistent. Easier to test.

Also, I saw that memoizeOne was used in the web worker for sorting/filtering. However, when data is copied from the UI thread to a worker thread, it's… copied :) So it can never hit the memoize fast-path of being equal to the last passed in data.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

export const filterSortData = memoizeOne(
async (
data: DataTableRowData[],
columns: DataTableColumnContainer,
Copy link
Member Author

Choose a reason for hiding this comment

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

This type was wrong, I replaced that with type SortableColumnContainer which I extracted from HaDataTable["_sortColumns"]

return filterData(data, columns, filter.toUpperCase());
}
);
const filterSortData = (
Copy link
Member Author

@balloob balloob May 17, 2020

Choose a reason for hiding this comment

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

I've removed all the memoization because of the copying.

Comment on lines +15 to +25
if (!worker) {
worker = wrap(new Worker("./sort_filter_worker", { type: "module" }));
}

return await worker.filterSortData(
data,
columns,
filter,
direction,
sortColumn
);
Copy link
Member Author

Choose a reason for hiding this comment

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

To disable the built-in worker and use the API directly:

const workerAPI = await import("./sort_filter_worker");
return workerAPI.api.filterSortData()

@bramkragten bramkragten merged commit a66d2ca into dev May 18, 2020
@bramkragten bramkragten deleted the worker-comlink branch May 18, 2020 14:51
@lock lock bot locked and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants