Commit d5d67784390b67e685e8675d95cabae952961b35
wip asyn router
mix irving committed on 2/7/2018, 9:40:14 PMParent: ab0cfd9162ed08e4f61975edf5f604162dcd8b32
Files changed
app/html/app.js | changed |
app/sync/goTo.js | changed |
router/async/normalise.js | added |
router/async/router.js | added |
app/html/app.js | |||
---|---|---|---|
@@ -14,9 +14,10 @@ | |||
14 | 14 … | 'app.sync.initialise': 'first', | |
15 | 15 … | 'app.sync.window': 'reduce', | |
16 | 16 … | 'history.obs.location': 'first', | |
17 | 17 … | 'history.sync.push': 'first', | |
18 | - 'router.sync.router': 'first', | ||
18 … | + 'router.async.router': 'first', | ||
19 … | + 'router.sync.router': 'first', // TODO rm | ||
19 | 20 … | 'styles.css': 'reduce', | |
20 | 21 … | 'settings.sync.get': 'first', | |
21 | 22 … | 'settings.sync.set': 'first' | |
22 | 23 … | }) | |
@@ -46,8 +47,10 @@ | |||
46 | 47 … | ||
47 | 48 … | api.history.obs.location()(loc => api.app.sync.goTo(loc || {})) | |
48 | 49 … | ||
49 | 50 … | // 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) | ||
50 | 53 … | var { container: errorPage, addError } = api.router.sync.router('/errors') | |
51 | 54 … | window.addEventListener('error', ev => { | |
52 | 55 … | if (!tabs.has('/errors')) tabs.add(errorPage, true) | |
53 | 56 … |
app/sync/goTo.js | ||
---|---|---|
@@ -5,10 +5,10 @@ | ||
5 | 5 … | exports.needs = nest({ |
6 | 6 … | 'app.html.tabs': 'first', |
7 | 7 … | 'history.obs.store': 'first', |
8 | 8 … | 'history.sync.push': 'first', |
9 | - 'router.sync.normalise': 'first', | |
10 | - 'router.sync.router': 'first' | |
9 … | + 'router.async.normalise': 'first', | |
10 … | + 'router.async.router': 'first' | |
11 | 11 … | }) |
12 | 12 … | |
13 | 13 … | exports.create = function (api) { |
14 | 14 … | return nest('app.sync.goTo', goTo) |
@@ -21,33 +21,40 @@ | ||
21 | 21 … | // - extracts scrollToMessage into app.page.thread |
22 | 22 … | // - router.sync.router would take (location, { position }) ? |
23 | 23 … | |
24 | 24 … | 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) | |
27 | 29 … | |
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 … | + } | |
34 | 36 … | |
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 | |
37 | 40 … | |
38 | - page.id = page.id || locationId | |
39 | - tabs.add(page, !openBackground, split) | |
41 … | + if (!page) return | |
40 | 42 … | |
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) | |
45 | 45 … | |
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() | |
50 | 50 … | |
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 … | + }) | |
52 | 59 … | } |
53 | 60 … | } |
router/async/normalise.js | ||
---|---|---|
@@ -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.js | ||
---|---|---|
@@ -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