Skip to content

Commit 04902b3

Browse files
authored
[web] Update model processor usage (#47)
1 parent c9b215d commit 04902b3

File tree

7 files changed

+35
-45
lines changed

7 files changed

+35
-45
lines changed

web/lib/src/0.8/data/model-processor.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import {
6060
export class A2UIModelProcessor {
6161
static readonly DEFAULT_SURFACE_ID = "@default";
6262

63-
#currentSurface = A2UIModelProcessor.DEFAULT_SURFACE_ID;
6463
#mapCtor: MapConstructor = Map;
6564
#arrayCtor: ArrayConstructor = Array;
6665
#setCtor: SetConstructor = Set;
@@ -118,8 +117,6 @@ export class A2UIModelProcessor {
118117
this.#handleDeleteSurface(message.deleteSurface);
119118
}
120119
}
121-
122-
this.#currentSurface = A2UIModelProcessor.DEFAULT_SURFACE_ID;
123120
}
124121

125122
/**
@@ -132,7 +129,7 @@ export class A2UIModelProcessor {
132129
relativePath: string,
133130
surfaceId = A2UIModelProcessor.DEFAULT_SURFACE_ID
134131
): DataValue | null {
135-
const surface = this.#surfaces.get(surfaceId);
132+
const surface = this.#getOrCreateSurface(surfaceId);
136133
if (!surface) return null;
137134

138135
let finalPath: string;
@@ -149,25 +146,13 @@ export class A2UIModelProcessor {
149146
return this.#getDataByPath(surface.dataModel, finalPath);
150147
}
151148

152-
getDataByPath(path: string, surfaceId: SurfaceID | null = null) {
153-
if (!surfaceId) {
154-
surfaceId = A2UIModelProcessor.DEFAULT_SURFACE_ID;
155-
}
156-
const surface = this.#getOrCreateSurface(surfaceId);
157-
if (!surface) {
158-
return null;
159-
}
160-
161-
return this.#getDataByPath(surface.dataModel, path) ?? null;
162-
}
163-
164149
setData(
165150
node: AnyComponentNode,
166151
relativePath: string,
167152
value: DataValue,
168153
surfaceId = A2UIModelProcessor.DEFAULT_SURFACE_ID
169154
): void {
170-
const surface = this.#surfaces.get(surfaceId);
155+
const surface = this.#getOrCreateSurface(surfaceId);
171156
if (!surface) return null;
172157

173158
let finalPath: string;
@@ -184,19 +169,6 @@ export class A2UIModelProcessor {
184169
this.#setDataByPath(surface.dataModel, finalPath, value);
185170
}
186171

187-
setDataByPath(
188-
path: string,
189-
value: DataValue,
190-
surfaceId = A2UIModelProcessor.DEFAULT_SURFACE_ID
191-
) {
192-
const surface = this.#getOrCreateSurface(surfaceId);
193-
if (!surface) {
194-
return null;
195-
}
196-
197-
return this.#setDataByPath(surface.dataModel, path, value);
198-
}
199-
200172
resolvePath(path: string, dataContextPath?: string): string {
201173
// If the path is absolute, it overrides any context.
202174
if (path.startsWith("/")) {

web/lib/src/0.8/events/a2ui.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { Action } from "../types/components.js";
18+
import { AnyComponentNode } from "../types/types.js";
1819
import { BaseEventDetail } from "./base.js";
1920

2021
type Namespace = "a2ui";
@@ -23,4 +24,5 @@ export interface A2UIAction extends BaseEventDetail<`${Namespace}.action`> {
2324
readonly action: Action;
2425
readonly dataContextPath: string;
2526
readonly sourceComponentId: string;
27+
readonly sourceComponent: AnyComponentNode;
2628
}

web/lib/src/0.8/ui/button.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class Button extends Root {
5757
action: this.action,
5858
dataContextPath: this.dataContextPath,
5959
sourceComponentId: this.id,
60+
sourceComponent: this.component,
6061
});
6162
this.dispatchEvent(evt);
6263
}}

web/lib/src/0.8/ui/datetime-input.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ export class DateTimeInput extends Root {
6767
return;
6868
}
6969

70-
this.processor.setDataByPath(
71-
this.processor.resolvePath(this.value.path, this.dataContextPath),
72-
value
70+
this.processor.setData(
71+
this.component,
72+
this.value.path,
73+
value,
74+
this.surfaceId ?? A2UIModelProcessor.DEFAULT_SURFACE_ID
7375
);
7476
}
7577

web/lib/src/0.8/ui/multiple-choice.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { customElement, property } from "lit/decorators.js";
1919
import { Root } from "./root.js";
2020
import { StringValue } from "../types/primitives.js";
2121
import * as Styles from "./styles/index.js";
22+
import { A2UIModelProcessor } from "../data/model-processor.js";
2223

2324
@customElement("a2ui-multiplechoice")
2425
export class MultipleChoice extends Root {
@@ -65,9 +66,11 @@ export class MultipleChoice extends Root {
6566
return;
6667
}
6768

68-
this.processor.setDataByPath(
69-
this.processor.resolvePath(this.value.path, this.dataContextPath),
70-
value
69+
this.processor.setData(
70+
this.component,
71+
this.value.path,
72+
value,
73+
this.surfaceId ?? A2UIModelProcessor.DEFAULT_SURFACE_ID
7174
);
7275
}
7376

web/restaurant/restaurant.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ export class A2UILayoutEditor extends SignalWatcher(LitElement) {
257257
item.value.path,
258258
evt.detail.dataContextPath
259259
);
260-
const value = this.#processor.getDataByPath(
260+
const value = this.#processor.getData(
261+
evt.detail.sourceComponent,
261262
path,
262263
surfaceId
263264
);

web/tests/0.8/model.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const toPlainObject = (value: unknown): ReturnType<typeof JSON.parse> => {
4545
};
4646

4747
describe("A2UIModelProcessor", () => {
48-
let processor;
48+
let processor = new v0_8.Data.A2UIModelProcessor();
4949

5050
beforeEach(() => {
5151
processor = new v0_8.Data.A2UIModelProcessor();
@@ -138,7 +138,10 @@ describe("A2UIModelProcessor", () => {
138138
},
139139
},
140140
]);
141-
const name = processor.getDataByPath("/user/name");
141+
const name = processor.getData(
142+
{ dataContextPath: "/" } as v0_8.Types.AnyComponentNode,
143+
"/user/name"
144+
);
142145
assert.strictEqual(name, "Alice");
143146
});
144147

@@ -152,13 +155,17 @@ describe("A2UIModelProcessor", () => {
152155
},
153156
},
154157
]);
155-
const user = processor.getDataByPath("/user");
158+
const user = processor.getData(
159+
{ dataContextPath: "/" } as v0_8.Types.AnyComponentNode,
160+
"/user"
161+
);
156162
assert.deepStrictEqual(toPlainObject(user), { name: "Bob" });
157163
});
158164

159165
it("should create nested structures when setting data", () => {
160-
processor.setDataByPath("/a/b/c", "value");
161-
const data = processor.getDataByPath("/a/b/c");
166+
const component = { dataContextPath: "/" } as v0_8.Types.AnyComponentNode;
167+
processor.setData(component, "/a/b/c", "value");
168+
const data = processor.getData(component, "/a/b/c");
162169
assert.strictEqual(data, "value");
163170
});
164171

@@ -494,8 +501,9 @@ describe("A2UIModelProcessor", () => {
494501

495502
processor.processMessages(messages);
496503

497-
const title = processor.getDataByPath("/title", "test-surface");
498-
const items = processor.getDataByPath("/items", "test-surface");
504+
const component = { dataContextPath: "/" } as v0_8.Types.AnyComponentNode;
505+
const title = processor.getData(component, "/title", "test-surface");
506+
const items = processor.getData(component, "/items", "test-surface");
499507

500508
assert.strictEqual(title, "My Title");
501509
assert.deepStrictEqual(toPlainObject(items), [{ id: 1 }, { id: 2 }]);
@@ -512,7 +520,8 @@ describe("A2UIModelProcessor", () => {
512520
},
513521
]);
514522

515-
const badData = processor.getDataByPath("/badData");
523+
const component = { dataContextPath: "/" } as v0_8.Types.AnyComponentNode;
524+
const badData = processor.getData(component, "/badData");
516525
assert.strictEqual(badData, invalidJSON);
517526
});
518527
});

0 commit comments

Comments
 (0)