Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Skip observer map reprocessing and notification when a generated accessor is assigned the reference it already holds.",
"packageName": "@microsoft/fast-element",
"email": "144495202+AKnassa@users.noreply.github.com",
"dependentChangeType": "none"
}
2 changes: 1 addition & 1 deletion packages/fast-element/SIZES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Bundle sizes for `@microsoft/fast-element` exports.
| enableHydration (@microsoft/fast-element/hydration.js) | 46.53 KB | 13.91 KB | 12.49 KB |
| declarativeTemplate (@microsoft/fast-element/declarative.js) | 62.15 KB | 19.46 KB | 17.42 KB |
| ArrayObserver (@microsoft/fast-element/arrays.js) | 12.55 KB | 4.46 KB | 4.03 KB |
| observerMap (@microsoft/fast-element/observer-map.js) | 21.96 KB | 7.73 KB | 6.97 KB |
| observerMap (@microsoft/fast-element/observer-map.js) | 21.98 KB | 7.74 KB | 6.98 KB |
| attributeMap (@microsoft/fast-element/attribute-map.js) | 15.31 KB | 5.41 KB | 4.88 KB |
172 changes: 172 additions & 0 deletions packages/fast-element/src/declarative/extension-subpaths.pw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,178 @@ test.describe("extension subpaths", () => {
expect(result.hasObservableName).toBe(true);
});

test("observerMap skips reprocessing when the same array reference is assigned", async ({
page,
}) => {
await page.goto("/");

const result = await page.evaluate(async () => {
const { ArrayObserver, Observable, Schema, observerMap } = await import(
// @ts-expect-error: Client module.
"/extension-subpaths-main.js"
);

ArrayObserver.enable();

class ManualElement {
public someData: any;
}

const schema = new Schema("manual-same-reference");
schema.addPath({
rootPropertyName: "someData",
pathConfig: {
type: "repeat",
path: "someData.users",
currentContext: "user",
parentContext: null,
},
childrenMap: null,
});
schema.addPath({
rootPropertyName: "someData",
pathConfig: {
type: "repeat",
path: "user.orders",
currentContext: "order",
parentContext: "user",
},
childrenMap: null,
});
schema.addPath({
rootPropertyName: "someData",
pathConfig: {
type: "default",
path: "order.id",
currentContext: "order",
parentContext: "user",
},
childrenMap: null,
});

observerMap({ schema })({ type: ManualElement } as any);

const instance = new ManualElement();
instance.someData = {
users: [{ orders: [{ id: "first" }] }],
};

const user = instance.someData.users[0];
const orders = user.orders;

let notifyCount = 0;
Observable.getNotifier(user).subscribe(
{
handleChange() {
notifyCount++;
},
},
"orders",
);

user.orders = orders;

user.orders.push({ id: "second" });
await new Promise(resolve =>
requestAnimationFrame(() => requestAnimationFrame(resolve)),
);

return {
notifyCount,
preservesReference: user.orders === orders,
hasObservableIdOnPushedItem:
typeof Object.getOwnPropertyDescriptor(user.orders[1], "id")?.get ===
"function",
};
});

expect(result.notifyCount).toBe(0);
expect(result.preservesReference).toBe(true);
expect(result.hasObservableIdOnPushedItem).toBe(true);
});

test("observerMap processes a replacement array assigned to a nested property", async ({
page,
}) => {
await page.goto("/");

const result = await page.evaluate(async () => {
const { ArrayObserver, Observable, Schema, observerMap } = await import(
// @ts-expect-error: Client module.
"/extension-subpaths-main.js"
);

ArrayObserver.enable();

class ManualElement {
public someData: any;
}

const schema = new Schema("manual-replacement-array");
schema.addPath({
rootPropertyName: "someData",
pathConfig: {
type: "repeat",
path: "someData.users",
currentContext: "user",
parentContext: null,
},
childrenMap: null,
});
schema.addPath({
rootPropertyName: "someData",
pathConfig: {
type: "repeat",
path: "user.orders",
currentContext: "order",
parentContext: "user",
},
childrenMap: null,
});
schema.addPath({
rootPropertyName: "someData",
pathConfig: {
type: "default",
path: "order.id",
currentContext: "order",
parentContext: "user",
},
childrenMap: null,
});

observerMap({ schema })({ type: ManualElement } as any);

const instance = new ManualElement();
instance.someData = {
users: [{ orders: [{ id: "first" }] }],
};

const user = instance.someData.users[0];

let notifyCount = 0;
Observable.getNotifier(user).subscribe(
{
handleChange() {
notifyCount++;
},
},
"orders",
);

user.orders = [{ id: "second" }];

return {
notifyCount,
hasObservableIdOnReplacementItem:
typeof Object.getOwnPropertyDescriptor(user.orders[0], "id")?.get ===
"function",
};
});

expect(result.notifyCount).toBe(1);
expect(result.hasObservableIdOnReplacementItem).toBe(true);
});

test("observerMap reports missing schemas for non-declarative definitions", async ({
page,
}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ function defineObservableProperty(
},
setValue(source: any, value: any): void {
const oldValue = source[field];

if (value === oldValue) {
return;
}

const newValue = assignObservables(
schema,
rootSchema,
Expand Down
1 change: 1 addition & 0 deletions packages/fast-element/test/extension-subpaths-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
AttributeMap,
attributeMap,
} from "@microsoft/fast-element/attribute-map.js";
export { Observable } from "@microsoft/fast-element/observable.js";
export {
ObserverMap,
observerMap,
Expand Down
2 changes: 1 addition & 1 deletion sites/website/src/docs/3.x/resources/export-sizes.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ Bundle sizes for `@microsoft/fast-element` exports.
| enableHydration (@microsoft/fast-element/hydration.js) | 46.53 KB | 13.91 KB | 12.49 KB |
| declarativeTemplate (@microsoft/fast-element/declarative.js) | 62.15 KB | 19.46 KB | 17.42 KB |
| ArrayObserver (@microsoft/fast-element/arrays.js) | 12.55 KB | 4.46 KB | 4.03 KB |
| observerMap (@microsoft/fast-element/observer-map.js) | 21.96 KB | 7.73 KB | 6.97 KB |
| observerMap (@microsoft/fast-element/observer-map.js) | 21.98 KB | 7.74 KB | 6.98 KB |
| attributeMap (@microsoft/fast-element/attribute-map.js) | 15.31 KB | 5.41 KB | 4.88 KB |