From faec6dcc694ab3a73c4abca6d1765c2f42037c41 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Thu, 19 Jul 2018 23:52:28 -0500 Subject: [PATCH] Simplify the gallery router by removing an unnecessary Event class --- packages/gallery/src/interface/gallery.js | 16 ++++----- packages/gallery/src/services/router.js | 10 +++--- packages/gallery/src/utils/event.js | 44 +---------------------- 3 files changed, 13 insertions(+), 57 deletions(-) diff --git a/packages/gallery/src/interface/gallery.js b/packages/gallery/src/interface/gallery.js index c55368f..76051cb 100644 --- a/packages/gallery/src/interface/gallery.js +++ b/packages/gallery/src/interface/gallery.js @@ -13,17 +13,17 @@ import { FocusView } from './focus.js'; import { Dropzone } from './components/dropzone.js'; import { Overlay } from './components/overlay.js'; import { Icon } from './components/icon.js'; -import { routeChanged } from '../services/router.js'; +import { currentRoute } from '../services/router.js'; import { injectStyle, styled } from '../utils/style.js'; import { FILL_STYLE } from './styles.js'; export function GalleryView(vm) { - const routeName = prop(); - const routeParams = prop(); + let routeName; + let routeParams; - routeChanged.subscribe(function onRouteChange(name, params) { - routeName(name); - routeParams(params); + currentRoute.subscribe(([name, params]) => { + routeName = name; + routeParams = params; vm.redraw(); }); @@ -33,9 +33,9 @@ export function GalleryView(vm) { renderSwitch( { photos: [AllImagesView, {}, 'allImages'], - focus: [FocusView, routeParams(), `focus_${routeParams() && routeParams().id}`] + focus: [FocusView, routeParams, `focus_${routeParams && routeParams.id}`] }, - routeName() + routeName ) ]) ]; diff --git a/packages/gallery/src/services/router.js b/packages/gallery/src/services/router.js index 753e683..de4c62d 100644 --- a/packages/gallery/src/services/router.js +++ b/packages/gallery/src/services/router.js @@ -1,15 +1,13 @@ import { Router } from 'router'; -import { Event } from '../utils/event.js'; +import { prop } from 'frptools'; -export const routeChanged = new Event('Router.newRoute'); - -const fire = routeChanged.fire.bind(routeChanged); +export const currentRoute = prop([], r => r[1] && r[1].path); export const router = Router([ { name: 'home', path: '/', - enter: (r, route) => fire('photos', route) + enter: (r, route) => currentRoute(['photos', route]) }, { name: 'focus', @@ -17,7 +15,7 @@ export const router = Router([ vars: { id: /[_A-Za-z0-9]+/ }, - enter: (r, route) => fire('focus', route) + enter: (r, route) => currentRoute(['focus', route]) }, { id: '404', diff --git a/packages/gallery/src/utils/event.js b/packages/gallery/src/utils/event.js index 850651a..d145e3d 100644 --- a/packages/gallery/src/utils/event.js +++ b/packages/gallery/src/utils/event.js @@ -1,46 +1,4 @@ -import { log, error, group, groupEnd } from '../utils/console.js'; - -export class Event { - constructor(name) { - this.name = name; - this.stages = []; - } - - async fire(...args) { - const groupName = `Feeding pipeline "${this.name}"`; - group(groupName); - log('params:', ...args); - let i = this.stages.length; - const _next = async res => { - if (!i) { - groupEnd(groupName); - return res; - } - i -= 1; - const stage = this.stages[i]; - try { - const result = stage(...args); - if (result && result.then) { - return result.then(_next); - } - return Promise.resolve(result).then(_next); - } catch (e) { - const stageName = stage.name || ''; - error(`${stageName} threw error:`, e); - } - }; - - return await _next(); - } - - subscribe(callback, position = 0) { - this.stages.splice(position, 0, callback); - } - - unsubscribe(callback) { - this.stages.splice(this.stages.indexOf(callback), 1); - } -} +import { log, group, groupEnd } from '../utils/console.js'; // requestIdleCallback sortof-polyfill if (!global.requestIdleCallback) {