Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Stop using the js-sdk's compare function #12782

Merged
merged 7 commits into from
Jul 17, 2024
Merged

Conversation

dbkr
Copy link
Member

@dbkr dbkr commented Jul 16, 2024

The file is supposed to be a js-sdk internal module so we shouldn't have been using it, and now it uses the native collator, it's completely trivial. It was also causing Intl.Collator to be accessed at the module scope which risked it beating the modernizr check.

We just need to instantiate a collator each time, which also potentially allows us to use different collations as appropriate in the future.

Checklist

  • Tests written for new code (and old code if feasible).
  • New or updated public/exported symbols have accurate TSDoc documentation.
  • Linter and other CI checks pass.
  • Sign-off given on the changes (see CONTRIBUTING.md).

The file is supposed to be a js-sdk internal module so we shouldn't
have been using it, and now it uses the native collator, it's completely
trivial. It was also causing Intl.Collator to be accessed at the module
scope which risked it beating the modernizr check.
@dbkr dbkr added the T-Task Refactoring, enabling or disabling functionality, other engineering tasks label Jul 16, 2024
dbkr added a commit to matrix-org/matrix-js-sdk that referenced this pull request Jul 16, 2024
and change the one use of it to just intantiate a collator and use
it.

This was marked as internal module so this shouldn't be a breaking change.
Of course, react-sdk was using it.

Requires: matrix-org/matrix-react-sdk#12782
@t3chguy
Copy link
Member

t3chguy commented Jul 16, 2024

It was also causing Intl.Collator to be accessed at the module scope which risked it beating the modernizr check.

The Modernizr check should be updated to not load any unsafe code, by importing as little as possible, maybe nothing.

@@ -17,7 +17,6 @@ limitations under the License.

import React from "react";
import { Room, RoomEvent, RoomMember, RoomMemberEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { compare } from "matrix-js-sdk/src/utils";
Copy link
Member

Choose a reason for hiding this comment

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

matrix-js-sdk/src/utils is intended to be reusable though not necessarily stable. Given we use a load of utils from that export why are you picking on this one in particular?

image

Copy link
Member Author

Choose a reason for hiding this comment

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

Since it was instantiated at the module level, it was running before modernizr when I was trying to write the test. I figure rather than rearranging stuff to try to get the execution order right, the best way to fix this was to just not arbitrarily instantiate a static collator and instead make one when we need it, at which point the compare function is adding no value at all.

Copy link
Member

Choose a reason for hiding this comment

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

But element-web can't assume any of its dependencies don't do that. Sure we can fix that in the js-sdk but not in other dependencies. So instead we should write our code to defensively handle that situation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't disagree, although I'd need to dig more into exactly what order things get run in to do so. This felt like a thing that was still worth doing anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Although that said, if you strongly think that's a better thing to do than this then I'll stop killing myself trying to get the test coverage on this PR up to the bar.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is a band-aid for a single cause, which is quite esoteric given that https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator#browser_compatibility has been supported for years, so instead making the "bootloader" resilient to any such calls for more recent APIs in dependencies we control and those we do not would be more sane IMO

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh well, test didn't involve fixing other app bugs like the other one did, so this may as well live as tidy-up fix anyway. We might want to consider just loading a small stub from the HTML and loading the full app as an async import, which would give us more flexibility to do loading screens and such as well as have complete control over what executes in the stub.

Move the restoreAllMocks to prevent mock leakage and also add
some custom themes to test the ordering of those.
@dbkr dbkr added this pull request to the merge queue Jul 17, 2024
Merged via the queue into develop with commit 39d453a Jul 17, 2024
29 checks passed
@dbkr dbkr deleted the dbkr/stop_using_js_sdk_compare branch July 17, 2024 14:06
github-merge-queue bot pushed a commit to matrix-org/matrix-js-sdk that referenced this pull request Jul 17, 2024
* Remove the compare function from utils

and change the one use of it to just intantiate a collator and use
it.

This was marked as internal module so this shouldn't be a breaking change.
Of course, react-sdk was using it.

Requires: matrix-org/matrix-react-sdk#12782

* Add simple not-a-perf-test test

* recalculate repeatedly

otherwise we aren't testing anything different

* Use fewer members as it was making the test take a bit too long
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Task Refactoring, enabling or disabling functionality, other engineering tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants