-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Implement upsertOne() and upsertMany() for entity adapters #550
Changes from all commits
46b62b9
6d2999c
ee9067c
8dcb54c
8efba70
2b9cc50
848e94f
ba2eaad
f06e744
0673d12
1bc563e
cb0bee9
9826793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,13 +112,41 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any { | |
return didMutate; | ||
} | ||
|
||
function upsertOneMutably(update: Update<T>, state: R): boolean; | ||
function upsertOneMutably(update: any, state: any): boolean { | ||
return upsertManyMutably([update], state); | ||
} | ||
|
||
function upsertManyMutably(updates: Update<T>[], state: R): boolean; | ||
function upsertManyMutably(updates: any[], state: any): boolean { | ||
const added: T[] = []; | ||
const updated: Update<T>[] = []; | ||
|
||
for (let index in updates) { | ||
const update = updates[index]; | ||
if (update.id in state.entities) { | ||
updated.push(update); | ||
} else { | ||
added.push({ | ||
...update.changes, | ||
id: update.id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is problematic: if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make this change. |
||
}); | ||
} | ||
} | ||
|
||
const didMutate = updateManyMutably(updated, state); | ||
return addManyMutably(added, state) || didMutate; | ||
} | ||
|
||
return { | ||
removeAll, | ||
addOne: createStateOperator(addOneMutably), | ||
addMany: createStateOperator(addManyMutably), | ||
addAll: createStateOperator(addAllMutably), | ||
updateOne: createStateOperator(updateOneMutably), | ||
updateMany: createStateOperator(updateManyMutably), | ||
upsertOne: createStateOperator(upsertOneMutably), | ||
upsertMany: createStateOperator(upsertManyMutably), | ||
removeOne: createStateOperator(removeOneMutably), | ||
removeMany: createStateOperator(removeManyMutably), | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in upsert we pass list of entities to be added if missing or updates if exists.
in your implementation - if an entity already exists will it get updated even if it is the same as the original element?
if the answer is yes - this raises a performance question, as entities that have no changes would get new references and reload in components with OnPush change detection strategy.
anyway whatever the answer is - perhaps a test that clarifies it would be a good idea:
so if an existing entity gets an update with no changes -
a) if the functionality is to recreate it the test will show that a new entity was created.
b) if the functionality is to ignore it the test will show that no new entity was created.