git ssb

16+

Dominic / patchbay



Commit d5d67784390b67e685e8675d95cabae952961b35

wip asyn router

mix irving committed on 2/7/2018, 9:40:14 PM
Parent: ab0cfd9162ed08e4f61975edf5f604162dcd8b32

Files changed

app/html/app.jschanged
app/sync/goTo.jschanged
router/async/normalise.jsadded
router/async/router.jsadded
app/html/app.jsView
@@ -14,9 +14,10 @@
1414 'app.sync.initialise': 'first',
1515 'app.sync.window': 'reduce',
1616 'history.obs.location': 'first',
1717 'history.sync.push': 'first',
18- 'router.sync.router': 'first',
18 + 'router.async.router': 'first',
19 + 'router.sync.router': 'first', // TODO rm
1920 'styles.css': 'reduce',
2021 'settings.sync.get': 'first',
2122 'settings.sync.set': 'first'
2223 })
@@ -46,8 +47,10 @@
4647
4748 api.history.obs.location()(loc => api.app.sync.goTo(loc || {}))
4849
4950 // Catch errors
51 + // TODO - change this error handler error page annomaly
52 + // Note I'm still using a sync router just to ge this working (mix)
5053 var { container: errorPage, addError } = api.router.sync.router('/errors')
5154 window.addEventListener('error', ev => {
5255 if (!tabs.has('/errors')) tabs.add(errorPage, true)
5356
app/sync/goTo.jsView
@@ -5,10 +5,10 @@
55 exports.needs = nest({
66 'app.html.tabs': 'first',
77 'history.obs.store': 'first',
88 'history.sync.push': 'first',
9- 'router.sync.normalise': 'first',
10- 'router.sync.router': 'first'
9 + 'router.async.normalise': 'first',
10 + 'router.async.router': 'first'
1111 })
1212
1313 exports.create = function (api) {
1414 return nest('app.sync.goTo', goTo)
@@ -21,33 +21,40 @@
2121 // - extracts scrollToMessage into app.page.thread
2222 // - router.sync.router would take (location, { position }) ?
2323
2424 function goTo (location, openBackground = false, split = false) {
25- location = api.router.sync.normalise(location)
26- const locationId = JSON.stringify(location)
25 + api.router.async.normalise(location, (err, location) => {
26 + if (err) throw err
27 +
28 + const locationId = JSON.stringify(location)
2729
28- const tabs = api.app.html.tabs()
29- if (tabs.has(locationId)) {
30- tabs.select(locationId)
31- api.history.sync.push(location)
32- return true
33- }
30 + const tabs = api.app.html.tabs()
31 + if (tabs.has(locationId)) {
32 + tabs.select(locationId)
33 + api.history.sync.push(location)
34 + return true
35 + }
3436
35- const page = api.router.sync.router(location)
36- if (!page) return
37 + api.router.async.router(location, (err, page) => {
38 + debugger
39 + if (err) throw err
3740
38- page.id = page.id || locationId
39- tabs.add(page, !openBackground, split)
41 + if (!page) return
4042
41- if (openBackground) {
42- const history = api.history.obs.store()
43- var _history = history()
44- var current = _history.pop()
43 + page.id = page.id || locationId
44 + tabs.add(page, !openBackground, split)
4545
46- history.set([ ..._history, location, current ])
47- } else {
48- api.history.sync.push(location)
49- }
46 + if (openBackground) {
47 + const history = api.history.obs.store()
48 + var _history = history()
49 + var current = _history.pop()
5050
51- return openBackground
51 + history.set([ ..._history, location, current ])
52 + } else {
53 + api.history.sync.push(location)
54 + }
55 + })
56 +
57 + // return openBackground // might not be needed?
58 + })
5259 }
5360 }
router/async/normalise.jsView
@@ -1,0 +1,28 @@
1 +const nest = require('depnest')
2 +const { isBlob, isFeed, isMsg } = require('ssb-ref')
3 +
4 +exports.gives = nest('router.async.normalise')
5 +
6 +exports.needs = nest({'sbot.async.get': 'first'})
7 +
8 +exports.create = (api) => nest('router.async.normalise', normalise)
9 +
10 +function normalise (location, cb) {
11 + if (typeof location === 'object') return location
12 +
13 + if (isMsg(location)) api.sbot.async.get(location, cb)
14 +
15 + if (isBlob(location)) cb(null, { blob: location })
16 + if (isChannel(location)) cb(null, { channel: location })
17 + if (isFeed(location)) cb(null, { feed: location })
18 + if (isPage(location)) cb(null, { page: location.substring(1) })
19 +}
20 +
21 +function isChannel (str) {
22 + return typeof str === 'string' && str[0] === '#' && str.length > 1
23 +}
24 +
25 +function isPage (str) {
26 + return typeof str === 'string' && str[0] === '/' && str.length > 1
27 +}
28 +
router/async/router.jsView
@@ -1,0 +1,40 @@
1 +const nest = require('depnest')
2 +
3 +exports.gives = nest('router.async.router')
4 +
5 +exports.needs = nest({
6 + 'router.async.normalise': 'first',
7 + 'router.sync.routes': 'reduce'
8 +})
9 +
10 +exports.create = (api) => {
11 + var router = null
12 +
13 + return nest('router.async.router', (location, cb) => {
14 + if (!router) {
15 + router = Router(api.router.sync.routes())
16 + }
17 +
18 + api.router.async.normalise(location, (err, normLocation) => {
19 + if (err) return cb(err)
20 +
21 + debugger
22 +
23 + router(normLocation, cb)
24 + })
25 +
26 + // return true
27 + })
28 +}
29 +
30 +function Router (routes) {
31 + return (location, cb) => {
32 + debugger
33 + const route = routes.find(([validator]) => validator(location))
34 + // signature of a route is [ routeValidator, routeFunction ]
35 +
36 + if (route) cb(null, route[1](location))
37 + }
38 +}
39 +
40 +

Built with git-ssb-web