Skip to content

Commit

Permalink
fix(core): list diff wrong when an item moved to list front (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
LastLeaf committed Sep 20, 2023
1 parent d982338 commit 2217861
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
8 changes: 4 additions & 4 deletions glass-easel/src/tmpl/range_list_diff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Element, VirtualNode } from '..'
import { Element, VirtualNode, dumpElementToString } from '..'

Check warning on line 1 in glass-easel/src/tmpl/range_list_diff.ts

View workflow job for this annotation

GitHub Actions / Build and Test (lts/*, windows-latest)

'dumpElementToString' is defined but never used

Check warning on line 1 in glass-easel/src/tmpl/range_list_diff.ts

View workflow job for this annotation

GitHub Actions / Build and Test (lts/*, ubuntu-latest)

'dumpElementToString' is defined but never used

Check warning on line 1 in glass-easel/src/tmpl/range_list_diff.ts

View workflow job for this annotation

GitHub Actions / Build and Test (latest, windows-latest)

'dumpElementToString' is defined but never used

Check warning on line 1 in glass-easel/src/tmpl/range_list_diff.ts

View workflow job for this annotation

GitHub Actions / Build and Test (latest, ubuntu-latest)

'dumpElementToString' is defined but never used
import { DataValue } from '../data_proxy'
import { UpdatePathTreeNode, UpdatePathTreeRoot } from './proc_gen_wrapper'
import { triggerWarning } from '../func_arr'
Expand Down Expand Up @@ -327,12 +327,12 @@ export class RangeListManager {
// decide what operation to be used with each item
const enum OpKind {
Stable = 0,
ForwardMove,
BackwardMove,
ForwardMove, // move towards the array end (index increasing direction)
BackwardMove, // move towards the array start (index decreasing direction)
}
const oldListOp = new Array(oldRawKeys.length) as (OpKind | undefined)[]
const changedItems = new Array(newRawKeys.length) as (VirtualNode | undefined)[]
let prevLcsOldPos = lcsArr[curLcsArrIndex]!
let prevLcsOldPos = -1
for (let i = 0; i < oldPosList.length; i += 1) {
const oldPos = oldPosList[i]!
if (i === lcsArr[curLcsArrIndex]) {
Expand Down
55 changes: 55 additions & 0 deletions glass-easel/tests/core/data_update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,61 @@ describe('partial update', () => {
expect(execArr).toStrictEqual(['E'])
})

test('should be able to update list (full list update)', () => {
let execArr = [] as string[]
const childCompDef = componentSpace
.define()
.property('p', String)
.observer('p', function () {
execArr.push(this.data.p)
})
.registerComponent()
const compDef = componentSpace
.define()
.usingComponents({
child: childCompDef.general(),
})
.template(
tmpl(`
<child wx:for="{{list}}" wx:key="k" p="{{item.v}}" />
`),
)
.data(() => ({
list: [
{ k: 1, v: 'A' },
{ k: 2, v: 'B' },
{ k: 3, v: 'C' },
{ k: 4, v: 'D' },
],
}))
.registerComponent()
const comp = glassEasel.Component.createWithContext('root', compDef, domBackend)
glassEasel.Element.pretendAttached(comp)
const getP = () => {
const ret = [] as string[]
comp
.getShadowRoot()!
.childNodes[0]!.asElement()!
.childNodes.forEach((child) => {
ret.push(child.asElement()!.childNodes[0]!.asInstanceOf(childCompDef)!.data.p)
})
return ret
}
expect(getP()).toStrictEqual(['A', 'B', 'C', 'D'])
expect(execArr).toStrictEqual(['A', 'B', 'C', 'D'])
execArr = []
comp.setData({
list: [
{ k: 3, v: 'C' },
{ k: 5, v: 'E' },
{ k: 1, v: 'A' },
{ k: 6, v: 'F' },
],
})
expect(getP()).toStrictEqual(['C', 'E', 'A', 'F'])
expect(execArr).toStrictEqual(['E', 'F', 'C', 'A'])
})

test('should be able to update list keys', () => {
let execArr = [] as string[]
const childCompDef = componentSpace
Expand Down

0 comments on commit 2217861

Please sign in to comment.