Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

state manager: Updating non-existed array element does nothing #1020

Merged
merged 2 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/DotVVM.Framework/Resources/Scripts/state-manager.ts
Expand Up @@ -326,8 +326,12 @@ function createWrappedObservable<T>(initialValue: DeepReadonly<T>, typeHint: Typ
}
const indexForClosure = index
newContents[index] = createWrappedObservable(newVal[index], Array.isArray(typeHint) ? typeHint[0] : void 0, update => updater((viewModelArray: any) => {
const newElement = update(viewModelArray![indexForClosure])
const newArray = createArray(viewModelArray!)
if (viewModelArray == null || viewModelArray.length <= indexForClosure) {
// the item or the array does not exist anymore
return viewModelArray
}
const newElement = update(viewModelArray[indexForClosure])
const newArray = createArray(viewModelArray)
newArray[indexForClosure] = newElement
return Object.freeze(newArray) as any
}))
Expand Down
Expand Up @@ -438,3 +438,21 @@ test("push on observable array - automatic coercion happens", () => {
expect(vm.Array()[2]).toEqual(oldValue);

})

test("update of element in array that stops existing", () => {

vm.Array([
{ Id: 1 },
{ Id: 3 }
])
expect(vm.Array.state.length).toEqual(2)
const a = vm.Array()[1]()
a.Id(10)
expect(vm.Array.state[1].Id).toEqual(10)
vm.Array([ { Id: 1 } ])
expect(vm.Array.state.length).toEqual(1)
a.Id(10)
expect(vm.Array.state.length).toEqual(1)
expect(vm.Array.state[0]).toEqual({ $type: "t2", Id: 1 })

})