-
Notifications
You must be signed in to change notification settings - Fork 1k
[labs/task] Add pluggable task args equality functions #4013
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4debd41
Add pluggable args equality functions and deepArrayEquals
justinfagnani 99b5e3a
Add Map and Set support
justinfagnani a168818
Address feedback
justinfagnani a475b1e
Merge branch 'main' into task-args-equality
justinfagnani 89e82a6
Make separate deep-equals.js module
justinfagnani a3a1c9e
Update copyright
justinfagnani 313019f
Fix import in test
justinfagnani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@lit-labs/task': major | ||
| --- | ||
|
|
||
| Add pluggable task args equality functions and deepArrayEquals. Breaking: this removes performTask() and shouldRun() protected methods. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /development/ | ||
| /test/ | ||
| /node_modules/ | ||
| /deep-equals.* | ||
| /index.* | ||
| /task.* | ||
| /task.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2023 Google LLC | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| export const deepArrayEquals = <T extends ReadonlyArray<unknown>>( | ||
| oldArgs: T, | ||
| newArgs: T | ||
| ) => | ||
| oldArgs === newArgs || | ||
| (oldArgs.length === newArgs.length && | ||
| oldArgs.every((v, i) => deepEquals(v, newArgs[i]))); | ||
|
|
||
| const objectValueOf = Object.prototype.valueOf; | ||
| const objectToString = Object.prototype.toString; | ||
| const {keys: objectKeys} = Object; | ||
| const {isArray} = Array; | ||
|
|
||
| /** | ||
| * Recursively checks two objects for equality. | ||
| * | ||
| * This function handles the following cases: | ||
| * - Primitives: primitives compared with Object.is() | ||
| * - Objects: to be equal, two objects must: | ||
| * - have the same constructor | ||
| * - have same set of own property names | ||
| * - have each own property be deeply equal | ||
| * - Arrays, Maps, Sets, and RegExps | ||
| * - Objects with custom valueOf() (ex: Date) | ||
| * - Objects with custom toString() (ex: URL) | ||
| * | ||
| * Important: Objects must be free of cycles, otherwise this function will | ||
| * run infinitely! | ||
| */ | ||
| export const deepEquals = (a: unknown, b: unknown): boolean => { | ||
| if (Object.is(a, b)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ( | ||
| a !== null && | ||
| b !== null && | ||
| typeof a === 'object' && | ||
| typeof b === 'object' | ||
| ) { | ||
| // Object must have the same prototype / constructor | ||
| if (a.constructor !== b.constructor) { | ||
| return false; | ||
| } | ||
|
|
||
| // Arrays must have the same length and recursively equal items | ||
| if (isArray(a)) { | ||
| if (a.length !== (b as Array<unknown>).length) { | ||
| return false; | ||
| } | ||
| return a.every((v, i) => deepEquals(v, (b as Array<unknown>)[i])); | ||
| } | ||
|
|
||
| // Defer to custom valueOf implementations. This handles Dates which return | ||
| // ms since epoch: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf | ||
| if (a.valueOf !== objectValueOf) { | ||
| return a.valueOf() === b.valueOf(); | ||
| } | ||
|
|
||
| // Defer to custom toString implementations. This should handle | ||
| // TrustedTypes, URLs, and such. This might be a bit risky, but | ||
| // fast-deep-equals does it. | ||
| if (a.toString !== objectToString) { | ||
| return a.toString() === b.toString(); | ||
| } | ||
|
|
||
| if (a instanceof Map && b instanceof Map) { | ||
| if (a.size !== b.size) { | ||
| return false; | ||
| } | ||
| for (const [k, v] of a.entries()) { | ||
| if ( | ||
| deepEquals(v, b.get(k)) === false || | ||
| (v === undefined && b.has(k) === false) | ||
| ) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| if (a instanceof Set && b instanceof Set) { | ||
| if (a.size !== b.size) { | ||
| return false; | ||
| } | ||
| for (const k of a.keys()) { | ||
| if (b.has(k) === false) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| if (a instanceof RegExp) { | ||
| return ( | ||
| a.source === (b as RegExp).source && a.flags === (b as RegExp).flags | ||
| ); | ||
| } | ||
|
|
||
| // We have two objects, check every key | ||
| const keys = objectKeys(a) as Array<keyof typeof a>; | ||
|
|
||
| if (keys.length !== objectKeys(b).length) { | ||
| return false; | ||
| } | ||
|
|
||
| for (const key of keys) { | ||
| if (!b.hasOwnProperty(key) || !deepEquals(a[key], b[key])) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // All keys in the two objects have been compared! | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.