Skip to content

Commit

Permalink
fix max call stack exceeded when setting token values from change han…
Browse files Browse the repository at this point in the history
…dler (microsoft#6499)

* fixing issue and adding regression tests

* Change files

Co-authored-by: nicholasrice <nicholasrice@users.noreply.github.com>
Co-authored-by: Chris Holt <chhol@microsoft.com>
Co-authored-by: Rob Eisenberg <EisenbergEffect@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 9, 2022
1 parent ebfe1a3 commit e4a2bfc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fixing bug in DesignToken causing RangeError",
"packageName": "@microsoft/fast-foundation",
"email": "nicholasrice@users.noreply.github.com",
"dependentChangeType": "prerelease"
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ test.describe("DesignTokenNode", () => {

expect(target.getTokenValue(token)).toEqual(14);
});

test("should not throw when setting a token value from within a change handler", () => {
const node = new DesignTokenNode();
const tokenA = { $value: undefined };
const tokenB = { $value: undefined };

node.setTokenValue(tokenA, 12);
Observable.getNotifier(tokenA).subscribe({
handleChange(source, args) {
node.setTokenValue(tokenB, 14);
},
});

expect(() => {
node.setTokenValue(tokenA, 13);
}).not.toThrow();
});
});
test.describe("setting a token to a derived value", () => {
test("should support getting and setting falsey values", () => {
Expand Down Expand Up @@ -287,6 +304,23 @@ test.describe("DesignTokenNode", () => {
});
}).toThrow();
});

test("should not throw when setting a token derived value from within a change handler", () => {
const node = new DesignTokenNode();
const tokenA = { $value: undefined };
const tokenB = { $value: undefined };

node.setTokenValue(tokenA, 12);
Observable.getNotifier(tokenA).subscribe({
handleChange(source, args) {
node.setTokenValue(tokenB, () => 12);
},
});

expect(() => {
node.setTokenValue(tokenA, 13);
}).not.toThrow();
});
});

test.describe("getting a token value", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,12 @@ export class DesignTokenNode {
* Emit all queued notifications
*/
private static notify() {
for (const record of this._notifications) {
const notifications = this._notifications;
this._notifications = [];

for (const record of notifications) {
record.notify();
}

this._notifications = [];
}

private static queueNotification(...records: DesignTokenChangeRecordImpl<any>[]) {
Expand Down

0 comments on commit e4a2bfc

Please sign in to comment.