allow override for save

This commit is contained in:
buzz-lightsnack-2007 2024-05-25 11:18:31 +08:00
parent 0a3c281a49
commit ee1559522b

View file

@ -9,7 +9,7 @@ import {URLs} from "/scripts/utils/URLs.js";
// Don't forget to set the class as export default. // Don't forget to set the class as export default.
export default class product { export default class product {
// Create private variables for explicit use for the storage. // Create private variables for explicit use for the storage.
#options; #options = {};
/* Initialize a new product with its details. /* Initialize a new product with its details.
@ -51,14 +51,22 @@ export default class product {
}; };
} }
async save() { async save(options = {}) {
// There is only a need to save the data if an update is needed. // Set the default options.
if (Object.hasOwn(this.status, `update`) ? this.status[`update`] : true) { options = Object.assign({}, this.#options, options);
// Save the snip data.
(this.snip) ? await global.write([`sites`, this.URL, `snip`], this.snip, 1) : false;
// Write the analysis data to the storage. // There is only a need to save the data if an update is needed.
return((this[`analysis`]) ? global.write([`sites`, this.URL, `analysis`], this.analysis, 1) : false); if ((Object.hasOwn(this.status, `update`) ? this.status[`update`] : true) || options[`override`]) {
} let STATUS = true;
// Save the data.
Object.keys(this).forEach(async (KEY) => {
if ((!([`#options`, `status`, `details`].includes(KEY))) && STATUS) {
STATUS = await global.write([`sites`, this.URL, KEY], this[KEY], 1, {"override": true});
};
});
return (STATUS);
};
}; };
}; };