Update product data only when necessary

This is to save API usage.
This commit is contained in:
buzz-lightsnack-2007 2024-04-29 09:32:11 +08:00
parent 9d0707982c
commit e50eb84926

View file

@ -40,22 +40,30 @@ export default class product {
/* Attach the product data to the storage. */ /* Attach the product data to the storage. */
async attach() { async attach() {
// Add the data digest. // Add the data digest.
this.#snip = (await hash.digest(this.details, {"output": "Number"})); this.#snip = (await hash.digest(this.details, {"output": "Array"}));
// Add the status about this data. // Add the status about this data.
this.status = {}; this.status = {};
this.status[`update`] = !secretariat.compare([`sites`, this.URL, `snip`], this.#snip); this.status[`update`] = !(await (secretariat.compare([`sites`, this.URL, `snip`], this.#snip)));
} }
async save() { async save() {
// Stop when not attached (basically, not entirely initialized). // Stop when not attached (basically, not entirely initialized).
if (!this.#snip) {throw new ReferenceError((new texts(`error_msg_notattached`)).localized)}; if (!this.#snip) {throw new ReferenceError((new texts(`error_msg_notattached`)).localized)};
// Save the data to the storage. // Write the data to the session storage, indicating that it is the last edited.
await secretariat.write([`sites`, this.URL, `snip`], this.#snip, 1); await secretariat.session.write([`sites`, this.URL, `snip`], this.#snip, 1);
await secretariat.session.write([`last`], this.URL);
// There is only a need to save the data if an update is needed.
if (this.status[`update`]) {
// Save the data to the storage.
await secretariat.write([`sites`, this.URL, `snip`], this.#snip, 1);
// Write the analysis data to the storage.
(this[`analysis`]) ? secretariat.write([`sites`, this.URL, `analysis`], this.analysis, 1): false;
}
// Write the analysis data to the storage.
(this[`analysis`]) ? secretariat.write([`sites`, this.URL, `analysis`], this.analysis, 1): false;
}; };
async analyze() { async analyze() {