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

Unify performanceIsometric implementation, simplify typing, and enable sha.js typing in client-utils and common-utils #18253

Merged
merged 5 commits into from Nov 14, 2023

Conversation

ChumpChief
Copy link
Contributor

Last year I fixed performanceBrowser to use globalThis to make it work in worker threads (#12081), but I really should have done the same thing in Node and unified the two. This change completes that unification. As a result, we can also eliminate some atypical tsconfig settings. Did this for both client-utils and common-utils even though it's deprecated in common-utils, because why not :)

Secondly, updated sha.js types to latest (and removed the unused dep from odsp-driver). Converted usage of sha.js to not use internal modules, which was defeating strong typing there.

In the process, all of this uncovered some typing errors and quirks that are also adjusted for in this change.

@ChumpChief ChumpChief requested review from msfluid-bot and a team as code owners November 10, 2023 19:18
@github-actions github-actions bot added area: driver Driver related issues area: odsp-driver dependencies Pull requests that update a dependency file public api change Changes to a public API base: main PRs targeted against main branch labels Nov 10, 2023
@@ -132,7 +132,7 @@ export class DocumentDeltaConnection
logger.sendErrorEvent(
{
eventName: "DeltaConnection:EventException",
name,
name: name as string,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

EventEmitterEventType explicitly permits name to be string | symbol, but sendErrorEvent definitely wants a string.

Probably the best option is to restrict EventEmitterEventType to just string for simplicity, but I don't do that in this change as it would be a public API change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what this is accomplishing. At runtime if this was a Symbol it'll still be one, and I don't understand how the cast would help TS at development time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The typing changes suddenly made TS realize this should fail type checking, so the cast allows it to build again.

If a Symbol were being passed it would throw later down the line (e.g. in something like convertToBasePropertyTypeUntagged) since there's not really a reasonable way to convert a Symbol to something that can be serialized/logged. So this cast just makes that assumption explicit; that although name technically could be a Symbol here, we're really assuming it's not if we're willing to pass it into sendErrorEvent.

My comment above is basically saying "why risk it, it would make more sense to just ban Symbol as eventName throughout the repo and force everyone to use strings, no one like Symbols anyway". Then this cast could go away. But doing so would technically be a breaking change.

@msfluid-bot
Copy link
Collaborator

msfluid-bot commented Nov 10, 2023

@fluid-example/bundle-size-tests: +6.87 KB
Metric NameBaseline SizeCompare SizeSize Diff
aqueduct.js 447.51 KB 447.51 KB No change
connectionState.js 680 Bytes 680 Bytes No change
containerRuntime.js 240.61 KB 240.61 KB No change
loader.js 178.94 KB 178.94 KB No change
map.js 48.06 KB 48.06 KB No change
matrix.js 141.84 KB 141.84 KB No change
odspDriver.js 90.27 KB 90.27 KB No change
odspPrefetchSnapshot.js 41.9 KB 41.9 KB No change
sharedString.js 162.75 KB 162.75 KB No change
sharedTree2.js 277.06 KB 277.06 KB No change
Total Size 1.74 MB 1.74 MB +6.87 KB

Baseline commit: 3392bac

Generated by 🚫 dangerJS against 2477794

"paths": {
"perf_hooks": ["types/perf_hooks.d.ts"],
},
"types": ["events"],
Copy link
Member

Choose a reason for hiding this comment

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

Why isn't this needed? Is this what's causing the node types to be referenced in the api docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the node types being referenced in the api docs is just because we're finally actually using sha.js types (which themselves reference node types). The api report changed not from the tsconfig change (first commit in this PR), but when I actually started using the typed objects (second commit in this PR). I can revert the "events" change here if you like (doesn't seem to make a difference really), but it doesn't change the api docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Confirmed with a messy hack that the node types do indeed source from the usage of @types/sha.js, specifically used to avoid explicitly typing Hash (guessing this was just a shortcut).

Since Hash doesn't actually show up on our public API surface, seems like api-extractor shouldn't strictly need to include the reference? Though I don't know much about api-extractor internals.

@@ -4,6 +4,8 @@

```ts

/// <reference types="node" />
Copy link
Member

Choose a reason for hiding this comment

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

This seems wrong...

@@ -11,3 +11,8 @@
*/
export type IsomorphicPerformance = Partial<Performance> &
Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;

/**
* @internal
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some docs while we're here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in latest!

@@ -132,7 +132,7 @@ export class DocumentDeltaConnection
logger.sendErrorEvent(
{
eventName: "DeltaConnection:EventException",
name,
name: name as string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure what this is accomplishing. At runtime if this was a Symbol it'll still be one, and I don't understand how the cast would help TS at development time.

@@ -74,7 +74,7 @@ export class DocumentDeltaConnection

private _details: IConnected | undefined;

private trackLatencyTimeout: number | undefined;
private trackLatencyTimeout: ReturnType<typeof setTimeout> | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, how was this passing type checks before when we were assigning to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure - same as the change below, the typing changes tickled TS in some way that made it realize there was bad typing in this file. Kind of spooky that they weren't caught earlier, but after investigating I think both were correctly identified as errors.

@ChumpChief ChumpChief merged commit cb8243e into microsoft:main Nov 14, 2023
26 checks passed
@ChumpChief ChumpChief deleted the ShaTypes branch November 14, 2023 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: driver Driver related issues area: odsp-driver base: main PRs targeted against main branch dependencies Pull requests that update a dependency file public api change Changes to a public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants