shelving
Version:
Toolkit for using data in JavaScript.
74 lines (73 loc) • 2.96 kB
JavaScript
import { AsyncThroughProvider, ThroughProvider } from "./ThroughProvider.js";
/** Synchronous provider that keeps a log of any written changes to its `.changes` property. */
export class ChangesProvider extends ThroughProvider {
get changes() {
return this._changes;
}
_changes = [];
addItem(collection, data) {
const id = super.addItem(collection, data);
this._changes.push({ action: "set", collection, id, data });
return id;
}
setItem(collection, id, data) {
super.setItem(collection, id, data);
this._changes.push({ action: "set", collection, id, data });
}
updateItem(collection, id, updates) {
super.updateItem(collection, id, updates);
this._changes.push({ action: "update", collection, id, updates });
}
deleteItem(collection, id) {
super.deleteItem(collection, id);
this._changes.push({ action: "delete", collection, id });
}
setQuery(collection, query, data) {
super.setQuery(collection, query, data);
this._changes.push({ action: "set", collection, query, data });
}
updateQuery(collection, query, updates) {
super.updateQuery(collection, query, updates);
this._changes.push({ action: "update", collection, query, updates });
}
deleteQuery(collection, query) {
super.deleteQuery(collection, query);
this._changes.push({ action: "delete", collection, query });
}
}
/** Asynchronous provider that keeps a log of any written changes to its `.written` property. */
export class AsyncLoggedProvider extends AsyncThroughProvider {
get written() {
return this._written;
}
_written = [];
async addItem(collection, data) {
const id = await super.addItem(collection, data);
this._written.push({ action: "set", collection, id, data });
return id;
}
async setItem(collection, id, data) {
await super.setItem(collection, id, data);
this._written.push({ action: "set", collection, id, data });
}
async updateItem(collection, id, updates) {
await super.updateItem(collection, id, updates);
this._written.push({ action: "update", collection, id, updates });
}
async deleteItem(collection, id) {
await super.deleteItem(collection, id);
this._written.push({ action: "delete", collection, id });
}
async setQuery(collection, query, data) {
await super.setQuery(collection, query, data);
this._written.push({ action: "set", collection, query, data });
}
async updateQuery(collection, query, updates) {
await super.updateQuery(collection, query, updates);
this._written.push({ action: "update", collection, query, updates });
}
async deleteQuery(collection, query) {
await super.deleteQuery(collection, query);
this._written.push({ action: "delete", collection, query });
}
}