Skip to content

Commit

Permalink
JSDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpetro committed Apr 16, 2024
1 parent e02cd22 commit bf6d272
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions smart-collections/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ class Collection {
const existing = this.find_by(data);
const item = existing ? existing : new this.item_type(this.env);
item.is_new = !!!existing;
const changed = item.update_data(data);
if (existing && !changed) return existing;
if (item.validate_save()) this.set(item);
if (item.init instanceof AsyncFunction) {
return new Promise((resolve, reject) => {
item.init(data).then(() => resolve(item));
});
}
item.init(data);
const changed = item.update_data(data); // handles this.data
if (existing && !changed) return existing; // if existing item and no changes, return existing item (no need to save)
if (item.validate_save()) this.set(item); // make it available in collection (if valid)

// dynamically handle async init functions
if (item.init instanceof AsyncFunction) return new Promise((resolve, reject) => { item.init(data).then(() => resolve(item)); });
item.init(data); // handles functions that involve other items
return item;
}

Expand Down Expand Up @@ -201,8 +199,8 @@ class Collection {
* Gets or sets the collection name. If a name is set, it overrides the default name.
* @param {String} name - The new collection name.
*/
set collection_name(name) { this._collection_name = name; }
get collection_name() { return (this._collection_name) ? this._collection_name : this.constructor.collection_name; }
set collection_name(name) { this._collection_name = name; }
/**
* Gets the keys of the items in the collection.
* @return {String[]} The keys of the items.
Expand Down

0 comments on commit bf6d272

Please sign in to comment.