Skip to content

Commit

Permalink
fix: allow setData undefined (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Aug 4, 2023
1 parent 76595a3 commit 0699e00
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
38 changes: 21 additions & 17 deletions glass-easel/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export class Component<
const builderContext: BuilderContext<any, any, any> = {
self: methodCaller,
data,
setData: comp.setData.bind(comp) as (newData: { [x: string]: unknown }) => void,
setData: comp.setData.bind(comp) as (newData?: { [x: string]: unknown }) => void,
implement: <TIn extends { [x: string]: unknown }>(
traitBehavior: TraitBehavior<TIn, { [x: string]: unknown }>,
impl: TIn,
Expand Down Expand Up @@ -1310,18 +1310,20 @@ export class Component<
* All data observers will not be triggered immediately before applied.
* Reads of the data will get the unchanged value before applied.
*/
updateData(newData: Partial<SetDataSetter<DataWithPropertyValues<TData, TProperty>>>): void
updateData(newData: Record<string, any>): void {
updateData(newData?: Partial<SetDataSetter<DataWithPropertyValues<TData, TProperty>>>): void
updateData(newData?: Record<string, any>): void {
const dataProxy = this._$dataGroup
if (dataProxy === undefined) {
throw new Error('Cannot update data before component created')
}
const keys = Object.keys(newData)
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i]!
const p = parseSinglePath(key)
if (p) {
dataProxy.replaceDataOnPath(p, newData[key])
if (typeof newData === 'object' && newData !== null) {
const keys = Object.keys(newData)
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i]!
const p = parseSinglePath(key)
if (p) {
dataProxy.replaceDataOnPath(p, newData[key])
}
}
}
}
Expand All @@ -1333,18 +1335,20 @@ export class Component<
* When called inside observers, the data update will not be applied to templates.
* Inside observers, it is recommended to use `updateData` instead.
*/
setData(newData: Partial<SetDataSetter<DataWithPropertyValues<TData, TProperty>>>): void
setData(newData: Record<string, any>): void {
setData(newData?: Partial<SetDataSetter<DataWithPropertyValues<TData, TProperty>>>): void
setData(newData?: Record<string, any> | undefined): void {
const dataProxy = this._$dataGroup
if (dataProxy === undefined) {
throw new Error('Cannot update data before component created')
}
const keys = Object.keys(newData)
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i]!
const p = parseSinglePath(key)
if (p) {
dataProxy.replaceDataOnPath(p, newData[key])
if (typeof newData === 'object' && newData !== null) {
const keys = Object.keys(newData)
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i]!
const p = parseSinglePath(key)
if (p) {
dataProxy.replaceDataOnPath(p, newData[key])
}
}
}
dataProxy.applyDataUpdates()
Expand Down
15 changes: 15 additions & 0 deletions glass-easel/tests/core/data_update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,4 +617,19 @@ describe('partial update', () => {
const comp = glassEasel.Component.createWithContext('root', compDef, domBackend)
expect(comp.data).toStrictEqual({ a: 123, b: ['a'] })
})

test('should allow set empty data', () => {
const compDef = componentSpace
.define()
.data(() => ({
a: 123,
b: 'abc',
}))
.registerComponent()
const comp = glassEasel.Component.createWithContext('root', compDef, domBackend)
comp.updateData({ a: 456 })
expect(comp.data.a).toBe(123)
comp.setData(undefined)
expect(comp.data.a).toBe(456)
})
})

0 comments on commit 0699e00

Please sign in to comment.