Timothy Farrell e5107e72f9 So much in here
- Image.upload calls File.upload
- doc deletes are PUT instead of deleted
- generate thumbnails is called off of processImportables
- got rid of stupid context loaders
- added Type.getOrCreate()
- fix massively broken comparators
- get rid of global watcher (unnecessary with new delete method)

Still broken
- indexes (albums)
- files not deleted along with images
- some wonky jpgs
2017-11-19 21:21:39 -06:00

41 lines
1.0 KiB
JavaScript

import { prop, computed } from 'frptools';
import { Watcher } from './watcher.js';
import { pouchDocArrayComparator } from './comparators.js';
// LiveArray is a subscribable property function that always returns the db results that match the provided selector and calls subscribers when the results change.
export function LiveArray(db, selector, mapper) {
const _watcher = Watcher(db, selector);
let changeSub = null;
let _mapper = mapper || (doc => doc);
const ready = prop(false);
const data = prop({ docs: [] });
const docs = computed(r => r.docs.map(_mapper), [data], pouchDocArrayComparator);
const cleanup = () => {
docs.unsubscribeAll();
ready.unsubscribeAll();
if (changeSub) {
changeSub();
changeSub = null;
}
data({ docs: [] });
};
const refresh = async function refresh(...args) {
data(await db.find({ selector }));
};
docs.ready = ready;
docs.cleanup = cleanup;
docs.selector = selector;
docs.db = db;
refresh().then(() => {
changeSub = _watcher(refresh);
ready(true);
});
return docs;
}