From e50eb84926a34e98a6332c1c26621697909eeeb8 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:32:11 +0800 Subject: [PATCH] Update product data only when necessary This is to save API usage. --- scripts/product.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/product.js b/scripts/product.js index 4bfd664..b0f20ee 100644 --- a/scripts/product.js +++ b/scripts/product.js @@ -40,22 +40,30 @@ export default class product { /* Attach the product data to the storage. */ async attach() { // 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. 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() { // Stop when not attached (basically, not entirely initialized). if (!this.#snip) {throw new ReferenceError((new texts(`error_msg_notattached`)).localized)}; - // Save the data to the storage. - await secretariat.write([`sites`, this.URL, `snip`], this.#snip, 1); + // Write the data to the session storage, indicating that it is the last edited. + 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() {