From f623fcd625780c69d085c66f3d39eed849516d63 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Mon, 30 Oct 2017 04:59:41 -0500 Subject: [PATCH] Allow Watcher Re-use to cut down on memory pressure. --- packages/gallery/src/app.js | 2 +- packages/gallery/src/interface/gallery.js | 28 +++++++++++++---------- packages/gallery/src/utils/livearray.js | 6 ++--- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/gallery/src/app.js b/packages/gallery/src/app.js index c3e87c5..9a54a54 100644 --- a/packages/gallery/src/app.js +++ b/packages/gallery/src/app.js @@ -12,7 +12,7 @@ EventEmitter.defaultMaxListeners = 1000; // https://github.com/pouchdb/pouchdb/i window.db = getDatabase(); // Attach our root view to the DOM -createView(GalleryView, {}).mount(document.querySelector('#app')); +createView(GalleryView, { db: getDatabase() }).mount(document.querySelector('#app')); // Start the router router.start('home'); diff --git a/packages/gallery/src/interface/gallery.js b/packages/gallery/src/interface/gallery.js index 14348bb..7bc4663 100644 --- a/packages/gallery/src/interface/gallery.js +++ b/packages/gallery/src/interface/gallery.js @@ -6,23 +6,27 @@ import { ImageView } from './image.js'; import { AlbumView } from './album.js'; import { router, routeChanged } from '../services/router.js'; import { LiveArray } from '../utils/livearray.js'; - -const NAV_OPTIONS = { - images: { - selector: image.SELECTOR, - title: 'Images' - }, - albums: { - selector: index.SELECTOR, - title: 'Albums' - } -}; +import { Watcher } from '../utils/watcher.js'; function uploadImages(evt) { image.add(evt.currentTarget.files); } 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 title = ''; @@ -31,7 +35,7 @@ export function GalleryView(vm, model) { data.cleanup(); } const o = NAV_OPTIONS[route.name]; - data = LiveArray(db, o.selector); + data = LiveArray(db, o.selector, o.watcher); title = o.title; data.subscribe(() => vm.redraw()); }); diff --git a/packages/gallery/src/utils/livearray.js b/packages/gallery/src/utils/livearray.js index d1750df..a1176f4 100644 --- a/packages/gallery/src/utils/livearray.js +++ b/packages/gallery/src/utils/livearray.js @@ -2,8 +2,8 @@ import { observable, computed } from 'frptools'; import { group, groupEnd, log } from '../services/console.js'; import { Watcher } from './watcher.js'; -export function LiveArray(db, selector) { - const watcher = Watcher(db, selector); +export function LiveArray(db, selector, watcher) { + const _watcher = watcher || Watcher(db, selector); const data = observable({ docs: [] }); const docs = computed(r => r.docs, [data]); let changeSub = null; @@ -28,7 +28,7 @@ export function LiveArray(db, selector) { } refresh().then(() => { - changeSub = watcher(refresh); + changeSub = _watcher(refresh); accessor.ready(true); }); return accessor;