8000 [CHORE] Guard setting weakmap key · SortableJS/ember-sortablejs@f0ede36 · GitHub
[go: up one dir, main page]

Skip to content

Commit f0ede36

Browse files
committed
[CHORE] Guard setting weakmap key
1 parent 58b988c commit f0ede36

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

addon/components/sortable-js.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default class SortableJsComponent extends Component {
7979
if (!isObject) throw new TypeError('Item is not an Object');
8080

8181
if (!this.cachedIdentity.has(item)) {
82-
this.cachedIdentity.set(item, { value: item });
82+
this.setIdentity(item);
8383
}
8484
});
8585
this.list = [...(this.args.items || [])];
@@ -107,8 +107,7 @@ export default class SortableJsComponent extends Component {
107107
oldIndex,
108108
} = evt;
109109

110-
[this.list[oldIndex], this.list[newIndex]]
111-
.forEach((item) => this.cachedIdentity.set(item, { value: item}));
110+
[this.list[oldIndex], this.list[newIndex]].forEach((item) => this.setIdentity(item));
112111

113112
this.sync(evt, move(this.list, oldIndex, newIndex));
114113
this.hasUpdatedList = true;
@@ -139,7 +138,7 @@ export default class SortableJsComponent extends Component {
139138

140139
const oldItem = this.dragStore.dragStartInstance.list[oldIndex];
141140

142-
this.cachedIdentity.set(oldItem, { value: oldItem });
141+
this.setIdentity(oldItem);
143142

144143
this.sync(evt, insertAt(this.list, newIndex, oldItem));
145144
this.args?.onAdd?.(evt);
@@ -155,8 +154,7 @@ export default class SortableJsComponent extends Component {
155154
if (!this.hasUpdatedList) {
156155
evt.item.remove();
157156
this.list = this.list.map((item) => {
158-
const newIdentity = { value: item };
159-
this.cachedIdentity.set(item, newIdentity);
157+
this.setIdentity(item);
160158
return item;
161159
});
162160

@@ -167,6 +165,12 @@ export default class SortableJsComponent extends Component {
167165
this.hasUpdatedList = false;
168166
}
169167

168+
setIdentity(obj) {
169+
if (obj && (typeof obj === 'object')) {
170+
this.cachedIdentity.set(obj, { value: obj });
171+
}
172+
}
173+
170174
sync({ item }, changedArray) {
171175
item.remove();
172176
this.list = [...changedArray];

0 commit comments

Comments
 (0)
0