Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Sep 11, 2023
2 parents 5659597 + 64ca326 commit 2975a45
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions glass-easel/src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,10 @@ export class Behavior<
else if (type === NormalizedPropertyType.Boolean) value = false
else if (type === NormalizedPropertyType.Array) value = []
else value = null
} else if (propDef.default !== undefined) {
triggerWarning(
`the initial value of property "${name}" is not used when its default is provided (when preparing behavior "${is}").`,
)
}
let observer: ((newValue: any, oldValue: any) => void) | null
if (typeof propDef.observer === 'function') {
Expand Down
15 changes: 12 additions & 3 deletions glass-easel/src/data_proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ export class DataGroup<
data = simpleDeepCopy(newData)
}
}
if (prop.comparer && !prop.comparer(data, this.data[propName])) return false
this._$pendingChanges.push([[propName], data, undefined, undefined])
return true
}
Expand Down Expand Up @@ -418,6 +417,7 @@ export class DataGroup<
const propName = String(path[0])
const excluded = pureDataPattern ? pureDataPattern.test(propName) : false
const prop: PropertyDefinition | undefined = propFields[propName]
let changed = true
if (prop && path.length === 1) {
// do update for 1-level fields
const oldData: unknown = this.data[propName]
Expand Down Expand Up @@ -489,7 +489,16 @@ export class DataGroup<
}
}
}
if (!excluded && oldData !== filteredData) {
changed = prop.comparer
? !!safeCallback(
'Property Comparer',
prop.comparer,
comp!,
[newData, oldData],
comp?.general(),
)
: oldData !== filteredData
if (!excluded && changed) {
propChanges.push({
propName,
prop,
Expand Down Expand Up @@ -605,7 +614,7 @@ export class DataGroup<
}
}
markTriggerBitOnPath(this._$observerTree, this._$observerStatus, path)
if (!excluded) {
if (!excluded && changed) {
combinedChanges.push(change)
}
if (this._$doingUpdates) {
Expand Down
1 change: 0 additions & 1 deletion glass-easel/tests/core/behavior.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ describe('chaining-form interface', () => {
.define()
.property('propA', {
type: Number,
value: 123,
default: () => 456,
})
.observer('propA', () => {
Expand Down
12 changes: 8 additions & 4 deletions glass-easel/tests/core/data_update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ describe('partial update', () => {
comparer(newValue, oldValue) {
execArr.push('A')
if (newValue === 'abc') return false
if (newValue === 'ghi') return true
return newValue !== oldValue
},
observer() {
Expand All @@ -561,19 +562,22 @@ describe('partial update', () => {
}))
.registerComponent()
const comp = glassEasel.Component.createWithContext('root', compDef, domBackend)
expect(execArr).toStrictEqual(['A'])
expect(execArr).toStrictEqual(['A', 'B'])
execArr = []
glassEasel.Element.pretendAttached(comp)
comp.setData({ p: '' })
expect(execArr).toStrictEqual(['A', 'B', 'C'])
execArr = []
comp.setData({ p: 'abc' })
expect(execArr).toStrictEqual(['A'])
expect(execArr).toStrictEqual(['A', 'B'])
execArr = []
comp.setData({ p: '' })
expect(execArr).toStrictEqual(['A'])
expect(execArr).toStrictEqual(['A', 'B', 'C'])
execArr = []
comp.setData({ p: 'ghi' })
expect(execArr).toStrictEqual(['A', 'B', 'C'])
execArr = []
comp.setData({ p: 'def' })
comp.setData({ p: 'ghi' })
expect(execArr).toStrictEqual(['A', 'B', 'C'])
})

Expand Down
2 changes: 0 additions & 2 deletions glass-easel/tests/legacy/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,12 @@ describe('Component', function () {
properties: {
a: {
type: String,
value: 'abc',
default: function () {
return 'def'
},
},
b: {
type: Number,
value: 123,
default: function () {
return 456
},
Expand Down
17 changes: 17 additions & 0 deletions glass-easel/tests/tmpl/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,21 @@ describe('binding expression', () => {
elem.setData({ c: 0 })
expect(eA.dataset.a).toEqual(100)
})

test('function call', () => {
const def = glassEasel.registerElement({
template: tmpl(`
<div id="n1" data-a="{{ f(a, 1) }}"></div>
`),
data: {
a: 2,
f: (a: number, b: number) => a + b,
},
})
const elem = glassEasel.createElement('root', def.general())
const n1 = elem.getShadowRoot()!.getElementById('n1')!
expect(n1.dataset.a).toEqual(3)
elem.setData({ a: 10 })
expect(n1.dataset.a).toEqual(11)
})
})

0 comments on commit 2975a45

Please sign in to comment.