Skip to content

Commit 051379e

Browse files
committed
Add change files for FormatSpecHandle fix
1 parent 1ffd873 commit 051379e

4 files changed

Lines changed: 46 additions & 11 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix FormatSpecHandle stale state during onFormattingReady",
5+
"type": "none",
6+
"packageName": "@itwin/core-frontend"
7+
}
8+
],
9+
"packageName": "@itwin/core-frontend",
10+
"email": "50554904+hl662@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix FormatSpecHandle stale state during onFormattingReady",
5+
"type": "none",
6+
"packageName": "@itwin/core-quantity"
7+
}
8+
],
9+
"packageName": "@itwin/core-quantity",
10+
"email": "50554904+hl662@users.noreply.github.com"
11+
}

core/frontend/src/quantity-formatting/QuantityFormatter.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,12 +1403,19 @@ export class QuantityFormatter implements UnitsProvider, FormattingSpecProvider
14031403
formatProps,
14041404
formatName: name,
14051405
});
1406-
if (!this._formatSpecsRegistry.has(name))
1407-
this._formatSpecsRegistry.set(name, new Map());
1408-
const unitMap = this._formatSpecsRegistry.get(name)!;
1409-
if (!unitMap.has(persistenceUnitName))
1410-
unitMap.set(persistenceUnitName, new Map());
1411-
unitMap.get(persistenceUnitName)!.set(effectiveSystem, { formatterSpec, parserSpec });
1406+
let unitMap = this._formatSpecsRegistry.get(name);
1407+
if (!unitMap) {
1408+
unitMap = new Map();
1409+
this._formatSpecsRegistry.set(name, unitMap);
1410+
}
1411+
1412+
let systemMap = unitMap.get(persistenceUnitName);
1413+
if (!systemMap) {
1414+
systemMap = new Map();
1415+
unitMap.set(persistenceUnitName, systemMap);
1416+
}
1417+
1418+
systemMap.set(effectiveSystem, { formatterSpec, parserSpec });
14121419
} else {
14131420
throw new Error(`Unable to find format properties for ${name} with persistence unit ${persistenceUnitName}`);
14141421
}

core/frontend/src/test/QuantityFormatter.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,13 +1133,17 @@ describe("Multi-system KoQ registry", () => {
11331133
await qf.addFormattingSpecsToRegistry({ name: "TestKoQ.LENGTH", persistenceUnitName: "Units.M", formatProps: simpleDecimalFormat, system: "imperial" });
11341134
await qf.addFormattingSpecsToRegistry({ name: "TestKoQ.LENGTH", persistenceUnitName: "Units.M", formatProps: simpleDecimalFormat, system: "metric" });
11351135

1136-
let handle: ReturnType<typeof qf.getFormatSpecHandle>;
1137-
const observedSpecs: FormatterSpec[] = [];
1138-
qf.onFormattingReady.addListener(() => {
1139-
observedSpecs.push(handle.formatterSpec!);
1136+
const handleRef: { current: ReturnType<typeof qf.getFormatSpecHandle> | undefined } = { current: undefined };
1137+
const observedSpecs: Array<FormatterSpec | undefined> = [];
1138+
const removeReadyListener = qf.onFormattingReady.addListener(() => {
1139+
if (!handleRef.current)
1140+
throw new Error("Expected FormatSpecHandle to be created before onFormattingReady fired");
1141+
1142+
observedSpecs.push(handleRef.current.formatterSpec);
11401143
});
11411144

1142-
handle = qf.getFormatSpecHandle("TestKoQ.LENGTH", "Units.M");
1145+
const handle = qf.getFormatSpecHandle("TestKoQ.LENGTH", "Units.M");
1146+
handleRef.current = handle;
11431147
const imperialFormatterSpec = qf.getSpecsByNameAndUnit({
11441148
name: "TestKoQ.LENGTH",
11451149
persistenceUnitName: "Units.M",
@@ -1160,6 +1164,8 @@ describe("Multi-system KoQ registry", () => {
11601164
expect(observedSpecs).toHaveLength(1);
11611165
expect(observedSpecs[0]).toBe(metricFormatterSpec);
11621166
expect(observedSpecs[0]).not.toBe(imperialFormatterSpec);
1167+
1168+
removeReadyListener();
11631169
handle[Symbol.dispose]();
11641170
});
11651171
});

0 commit comments

Comments
 (0)