Skip to content

Commit e2fb313

Browse files
committed
fix: dedupe when adding new object to instance local state
1 parent a091aca commit e2fb313

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

libs/angular-three/src/lib/utils/instance.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ export function prepare<TInstance extends object = NgtAnyRecord>(
3838
objects,
3939
nonObjects,
4040
add: (object, type) => {
41-
instance.__ngt__[type].next([...instance.__ngt__[type].value, object]);
41+
const current = instance.__ngt__[type].value;
42+
const foundIndex = current.indexOf((obj: NgtInstanceNode) => obj === object);
43+
if (foundIndex > -1) {
44+
current.splice(foundIndex, 1, object);
45+
instance.__ngt__[type].next(current);
46+
} else {
47+
instance.__ngt__[type].next([...instance.__ngt__[type].value, object]);
48+
}
4249
notifyAncestors(instance.__ngt__.parent);
4350
},
4451
remove: (object, type) => {

0 commit comments

Comments
 (0)