Allow Watcher Re-use to cut down on memory pressure.

This commit is contained in:
Timothy Farrell 2017-10-30 04:59:41 -05:00
parent 55768a892a
commit 90e67b6217
3 changed files with 20 additions and 16 deletions

View File

@ -12,7 +12,7 @@ EventEmitter.defaultMaxListeners = 1000; // https://github.com/pouchdb/pouchdb/i
window.db = getDatabase(); window.db = getDatabase();
// Attach our root view to the DOM // Attach our root view to the DOM
createView(GalleryView, {}).mount(document.querySelector('#app')); createView(GalleryView, { db: getDatabase() }).mount(document.querySelector('#app'));
// Start the router // Start the router
router.start('home'); router.start('home');

View File

@ -6,23 +6,27 @@ import { ImageView } from './image.js';
import { AlbumView } from './album.js'; import { AlbumView } from './album.js';
import { router, routeChanged } from '../services/router.js'; import { router, routeChanged } from '../services/router.js';
import { LiveArray } from '../utils/livearray.js'; import { LiveArray } from '../utils/livearray.js';
import { Watcher } from '../utils/watcher.js';
const NAV_OPTIONS = {
images: {
selector: image.SELECTOR,
title: 'Images'
},
albums: {
selector: index.SELECTOR,
title: 'Albums'
}
};
function uploadImages(evt) { function uploadImages(evt) {
image.add(evt.currentTarget.files); image.add(evt.currentTarget.files);
} }
export function GalleryView(vm, model) { export function GalleryView(vm, model) {
const { db } = model;
const NAV_OPTIONS = {
images: {
selector: image.SELECTOR,
watcher: image.watcher,
title: 'Images'
},
albums: {
selector: index.SELECTOR,
watcher: Watcher(db, index.SELECTOR),
title: 'Albums'
}
};
let data = null; let data = null;
let title = ''; let title = '';
@ -31,7 +35,7 @@ export function GalleryView(vm, model) {
data.cleanup(); data.cleanup();
} }
const o = NAV_OPTIONS[route.name]; const o = NAV_OPTIONS[route.name];
data = LiveArray(db, o.selector); data = LiveArray(db, o.selector, o.watcher);
title = o.title; title = o.title;
data.subscribe(() => vm.redraw()); data.subscribe(() => vm.redraw());
}); });

View File

@ -2,8 +2,8 @@ import { observable, computed } from 'frptools';
import { group, groupEnd, log } from '../services/console.js'; import { group, groupEnd, log } from '../services/console.js';
import { Watcher } from './watcher.js'; import { Watcher } from './watcher.js';
export function LiveArray(db, selector) { export function LiveArray(db, selector, watcher) {
const watcher = Watcher(db, selector); const _watcher = watcher || Watcher(db, selector);
const data = observable({ docs: [] }); const data = observable({ docs: [] });
const docs = computed(r => r.docs, [data]); const docs = computed(r => r.docs, [data]);
let changeSub = null; let changeSub = null;
@ -28,7 +28,7 @@ export function LiveArray(db, selector) {
} }
refresh().then(() => { refresh().then(() => {
changeSub = watcher(refresh); changeSub = _watcher(refresh);
accessor.ready(true); accessor.ready(true);
}); });
return accessor; return accessor;