-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: Introduce
@dedupeTracked
(#27084)
Same as `@tracked`, but skips notifying consumers if the value is unchanged. This introduces some performance overhead, so should only be used where excessive downstream re-evaluations are a problem. This is loosely based on `@dedupeTracked` in the `tracked-toolbox` package, but without the added complexity of a customizable 'comparator'. Implementing ourselves also avoids the need for pulling in the entire package, which contains some tools which we don't want, or which are now implemented in Ember/Glimmer (e.g. `@cached`).
- Loading branch information
1 parent
32aaf2e
commit 23b02a3
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
48 changes: 48 additions & 0 deletions
48
app/assets/javascripts/discourse/tests/unit/lib/tracked-tools-test.gjs
This file contains 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,48 @@ | ||
import { cached } from "@glimmer/tracking"; | ||
import { module, test } from "qunit"; | ||
import { dedupeTracked } from "discourse/lib/tracked-tools"; | ||
|
||
module("Unit | tracked-tools", function () { | ||
test("@dedupeTracked", async function (assert) { | ||
class Pet { | ||
initialsEvaluatedCount = 0; | ||
|
||
@dedupeTracked name; | ||
|
||
@cached | ||
get initials() { | ||
this.initialsEvaluatedCount++; | ||
return this.name | ||
?.split(" ") | ||
.map((n) => n[0]) | ||
.join(""); | ||
} | ||
} | ||
|
||
const pet = new Pet(); | ||
pet.name = "Scooby Doo"; | ||
|
||
assert.strictEqual(pet.initials, "SD", "Initials are correct"); | ||
assert.strictEqual( | ||
pet.initialsEvaluatedCount, | ||
1, | ||
"Initials getter evaluated once" | ||
); | ||
|
||
pet.name = "Scooby Doo"; | ||
assert.strictEqual(pet.initials, "SD", "Initials are correct"); | ||
assert.strictEqual( | ||
pet.initialsEvaluatedCount, | ||
1, | ||
"Initials getter not re-evaluated" | ||
); | ||
|
||
pet.name = "Fluffy"; | ||
assert.strictEqual(pet.initials, "F", "Initials are correct"); | ||
assert.strictEqual( | ||
pet.initialsEvaluatedCount, | ||
2, | ||
"Initials getter re-evaluated" | ||
); | ||
}); | ||
}); |