shelving
Version:
Toolkit for using data in JavaScript.
278 lines (277 loc) • 9.52 kB
JavaScript
import { AsyncThroughProvider, ThroughProvider } from "./ThroughProvider.js";
/** Provider that logs operations to a synchronous source provider to the console. */
export class DebugProvider extends ThroughProvider {
getItem(collection, id) {
try {
const item = super.getItem(collection, id);
console.debug("↩ GET", collection, id, item);
return item;
}
catch (reason) {
console.error("✘ GET", collection, id, reason);
throw reason;
}
}
async *getItemSequence(collection, id) {
try {
console.debug("⋯ ITERATE", collection, id);
for await (const item of super.getItemSequence(collection, id)) {
console.debug("↩ ITERATE", collection, id, item);
yield item;
}
console.debug("✔ ITERATE", collection, id);
}
catch (thrown) {
console.error("✘ ITERATE", collection, id, thrown);
}
}
addItem(collection, data) {
try {
const id = super.addItem(collection, data);
console.debug("✔ ADD", collection, data, id);
return id;
}
catch (reason) {
console.error("✘ ADD", collection, data, reason);
throw reason;
}
}
setItem(collection, id, data) {
try {
super.setItem(collection, id, data);
console.debug("✔ SET", collection, id, data);
}
catch (reason) {
console.error("✘ SET", collection, id, data, reason);
throw reason;
}
}
updateItem(collection, id, updates) {
try {
super.updateItem(collection, id, updates);
console.debug("✔ UPDATE", collection, id, updates);
}
catch (reason) {
console.error("✘ UPDATE", collection, id, updates, reason);
throw reason;
}
}
deleteItem(collection, id) {
try {
super.deleteItem(collection, id);
console.debug("✔ DELETE", collection, id);
}
catch (reason) {
console.error("✘ DELETE", collection, id, reason);
throw reason;
}
}
countQuery(collection, query) {
try {
const count = super.countQuery(collection, query);
console.debug("✔ GET", collection, query, count);
return count;
}
catch (reason) {
console.error("✘ GET", collection, query, reason);
throw reason;
}
}
getQuery(collection, query) {
try {
const items = super.getQuery(collection, query);
console.debug("✔ GET", collection, query, items);
return items;
}
catch (reason) {
console.error("✘ GET", collection, query, reason);
throw reason;
}
}
async *getQuerySequence(collection, query) {
try {
console.debug("⋯ ITERATE", collection, query);
for await (const items of super.getQuerySequence(collection, query)) {
console.debug("↩ ITERATE", collection, query, items);
yield items;
}
console.debug("✔ ITERATE", collection, query);
}
catch (thrown) {
console.error("✘ ITERATE", collection, query, thrown);
}
}
setQuery(collection, query, data) {
try {
super.setQuery(collection, query, data);
console.debug("✔ SET", collection, query, data);
}
catch (reason) {
console.error("✘ SET", collection, query, data, reason);
throw reason;
}
}
updateQuery(collection, query, updates) {
try {
super.updateQuery(collection, query, updates);
console.debug("✔ UPDATE", collection, query, updates);
}
catch (reason) {
console.error("✘ UPDATE", collection, query, updates, reason);
throw reason;
}
}
deleteQuery(collection, query) {
try {
super.deleteQuery(collection, query);
console.debug("✔ DELETE", collection, query);
}
catch (reason) {
console.error("✘ DELETE", collection, query, reason);
throw reason;
}
}
}
/** Provider that logs operations to a synchronous source provider to the console. */
export class AsyncDebugProvider extends AsyncThroughProvider {
async getItem(collection, id) {
try {
console.debug("⋯ GET", collection, id);
const item = await super.getItem(collection, id);
console.debug("↩ GET", collection, id, item);
return item;
}
catch (reason) {
console.error("✘ GET", collection, id, reason);
throw reason;
}
}
async *getItemSequence(collection, id) {
try {
console.debug("⋯ ITERATE", collection, id);
for await (const item of super.getItemSequence(collection, id)) {
console.debug("↩ ITERATE", collection, id, item);
yield item;
}
console.debug("✔ ITERATE", collection, id);
}
catch (thrown) {
console.error("✘ ITERATE", collection, id, thrown);
}
}
async addItem(collection, data) {
try {
console.debug("⋯ ADD", collection, data);
const id = await super.addItem(collection, data);
console.debug("✔ ADD", collection, id, data);
return id;
}
catch (reason) {
console.error("✘ ADD", collection, data, reason);
throw reason;
}
}
async setItem(collection, id, data) {
try {
console.debug("⋯ SET", collection, id, data);
await super.setItem(collection, id, data);
console.debug("✔ SET", collection, id, data, 1);
}
catch (reason) {
console.error("✘ SET", collection, id, data, reason);
throw reason;
}
}
async updateItem(collection, id, updates) {
try {
console.debug("⋯ UPDATE", collection, id, updates);
await super.updateItem(collection, id, updates);
console.debug("✔ UPDATE", collection, id, updates, 1);
}
catch (reason) {
console.error("✘ UPDATE", collection, id, updates, reason);
throw reason;
}
}
async deleteItem(collection, id) {
try {
console.debug("⋯ DELETE", collection, id);
await super.deleteItem(collection, id);
console.debug("✔ DELETE", collection, id, 1);
}
catch (reason) {
console.error("✘ DELETE", collection, id, reason);
throw reason;
}
}
async countQuery(collection, query) {
try {
console.debug("⋯ COUNT", collection, query);
const count = await super.countQuery(collection, query);
console.debug("✔ COUNT", collection, query, count);
return count;
}
catch (reason) {
console.error("✘ COUNT", collection, query, reason);
throw reason;
}
}
async getQuery(collection, query) {
try {
console.debug("⋯ GET", collection, query);
const items = await super.getQuery(collection, query);
console.debug("✔ GET", collection, query, items);
return items;
}
catch (reason) {
console.error("✘ GET", collection, query, reason);
throw reason;
}
}
async *getQuerySequence(collection, query) {
try {
console.debug("⋯ ITERATE", collection, query);
for await (const items of super.getQuerySequence(collection, query)) {
console.debug("↩ ITERATE", collection, query, items);
yield items;
}
console.debug("✔ ITERATE", collection, query);
}
catch (thrown) {
console.error("✘ ITERATE", collection, query, thrown);
}
}
async setQuery(collection, query, data) {
try {
console.debug("⋯ SET", collection, query, data);
await super.setQuery(collection, query, data);
console.debug("✔ SET", collection, query, data);
}
catch (reason) {
console.error("✘ SET", collection, query, data, reason);
throw reason;
}
}
async updateQuery(collection, query, updates) {
try {
console.debug("⋯ UPDATE", collection, query, updates);
await super.updateQuery(collection, query, updates);
console.debug("✔ UPDATE", collection, query, updates);
}
catch (reason) {
console.error("✘ UPDATE", collection, query, updates, reason);
throw reason;
}
}
async deleteQuery(collection, query) {
try {
console.debug("⋯ DELETE", collection, query);
await super.deleteQuery(collection, query);
console.debug("✔ DELETE", collection, query);
}
catch (reason) {
console.error("✘ DELETE", collection, query, reason);
throw reason;
}
}
}