ImportWatcher -> BackgroundTask -> _processImportables

Continuing the push to remove explicit events for db changes
This commit is contained in:
Timothy Farrell 2017-10-27 09:20:49 -05:00
parent a6b6d00f49
commit ead1c5766a

View File

@ -27,6 +27,7 @@ export const removed = new Event('Image.removed');
// Watchers // Watchers
export const watcher = Watcher(db, SELECTOR); export const watcher = Watcher(db, SELECTOR);
export const importWatcher = Watcher(db, IMPORT_SELECTOR);
// Methods // Methods
const getId = id => (id.startsWith(PREFIX) ? id : `${PREFIX}_${id}`); const getId = id => (id.startsWith(PREFIX) ? id : `${PREFIX}_${id}`);
@ -61,9 +62,6 @@ export async function add(imageFileList) {
} }
})); }));
const results = await db.bulkDocs(docs); const results = await db.bulkDocs(docs);
processImportables();
return docs.filter((d, i) => results[i].ok); return docs.filter((d, i) => results[i].ok);
} }
@ -90,9 +88,14 @@ export async function addAttachment(doc, key, blob) {
} }
// Internal Functions // Internal Functions
const processImportables = backgroundTask(async function _processImportables() { importWatcher(
backgroundTask(async function _processImportables(changeId, deleted) {
if (deleted) {
return;
}
const selector = changeId ? { _id: changeId } : IMPORT_SELECTOR;
const result = await db.find({ const result = await db.find({
selector: IMPORT_SELECTOR, selector,
limit: 1 limit: 1
}); });
@ -162,5 +165,5 @@ const processImportables = backgroundTask(async function _processImportables() {
} }
await db.remove({ _id, _rev }); await db.remove({ _id, _rev });
processImportables(); })
}); );