1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 09:35:00 +09:00

Atomic changes for storing slices in a state

This commit is contained in:
Sergey 2023-04-25 18:17:59 +02:00 committed by Mikhail Iudin
parent ab270aabfb
commit 6f86c196b1
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
9 changed files with 1369 additions and 166 deletions

View file

@ -292,3 +292,28 @@ func ApplyChanges[T any](origin []T, changes []Change[T], getID func(T) string)
return res
}
func UnwrapChanges[T, R any](
changes []Change[T],
add func(afterID string, items []T) R,
remove func(ids []string) R,
move func(afterID string, ids []string) R,
update func(id string, item T) R,
) []R {
res := make([]R, 0, len(changes))
for _, c := range changes {
if v := c.Add(); v != nil {
res = append(res, add(v.AfterID, v.Items))
}
if v := c.Remove(); v != nil {
res = append(res, remove(v.IDs))
}
if v := c.Move(); v != nil {
res = append(res, move(v.AfterID, v.IDs))
}
if v := c.Replace(); v != nil {
res = append(res, update(v.ID, v.Item))
}
}
return res
}