Commit 00f38454d156756289d50eaf84d0c5b276f702b3
extract history, move rendering into app.html.app
mix irving committed on 8/9/2017, 8:04:37 AMParent: e73bff8390f085ab1944c5d9ac4c514cbbacf355
Files changed
app/html/app.js | changed |
index.js | changed |
main.js | changed |
package-lock.json | changed |
package.json | changed |
history/index.js | deleted |
history/obs/store.js | deleted |
history/sync/back.js | deleted |
history/sync/push.js | deleted |
app/html/app.js | ||
---|---|---|
@@ -5,8 +5,11 @@ | ||
5 | 5 | exports.gives = nest('app.html.app') |
6 | 6 | |
7 | 7 | exports.needs = nest({ |
8 | 8 | 'history.sync.push': 'first', |
9 | + 'history.obs.location': 'first', | |
10 | + 'history.obs.store': 'first', | |
11 | + 'router.sync.router': 'first', | |
9 | 12 | 'styles.css': 'first' |
10 | 13 | }) |
11 | 14 | |
12 | 15 | exports.create = (api) => { |
@@ -15,7 +18,26 @@ | ||
15 | 18 | function app () { |
16 | 19 | const css = values(api.styles.css()).join('\n') |
17 | 20 | insertCss(css) |
18 | 21 | |
19 | - return api.history.sync.push({ page: 'home' }) | |
22 | + api.history.obs.location()(render) | |
23 | + api.history.sync.push({ page: 'home' }) | |
20 | 24 | } |
25 | + | |
26 | + function render (location) { | |
27 | + const newView = api.router.sync.router(location) | |
28 | + | |
29 | + if (!newView) { | |
30 | + api.history.obs.store().pop() // remove bogus location | |
31 | + return | |
32 | + } | |
33 | + | |
34 | + const oldView = document.body.firstChild | |
35 | + oldView | |
36 | + ? document.body.replaceChild(newView, oldView) | |
37 | + : document.body.appendChild(newView) | |
38 | + } | |
21 | 39 | } |
40 | + | |
41 | + | |
42 | + | |
43 | + |
index.js | ||
---|---|---|
@@ -1,8 +1,7 @@ | ||
1 | 1 | const ticktack = { |
2 | 2 | app: require('./app'), |
3 | 3 | blob: require('./blob'), |
4 | - history: require('./history'), | |
5 | 4 | router: require('./router'), |
6 | 5 | styles: require('./styles') |
7 | 6 | } |
8 | 7 |
main.js | ||
---|---|---|
@@ -1,22 +1,20 @@ | ||
1 | 1 | const combine = require('depject') |
2 | 2 | const entry = require('depject/entry') |
3 | 3 | const nest = require('depnest') |
4 | 4 | |
5 | -const ticktack = require('./') | |
6 | -const patchcore = require('patchcore') | |
7 | - | |
8 | 5 | // polyfills |
9 | 6 | require('setimmediate') |
10 | 7 | |
11 | 8 | // from more specialized to more general |
12 | 9 | const sockets = combine( |
13 | - ticktack, | |
14 | - patchcore | |
10 | + require('./'), | |
11 | + require('patch-history'), | |
12 | + require('patchcore') | |
15 | 13 | ) |
16 | 14 | |
17 | 15 | const api = entry(sockets, nest('app.html.app', 'first')) |
18 | 16 | |
19 | 17 | const app = api.app.html.app() |
20 | 18 | |
21 | -// TODO (mix) : once goTo/ router is swapping pages, attach the app to the page here | |
19 | +// TODO (mix) : once app has swapping pages, attach the app to the page here | |
22 | 20 | // document.body.appendChild(app) |
package-lock.json | ||
---|---|---|
@@ -1039,8 +1039,19 @@ | ||
1039 | 1039 | "pull-reader": "1.2.9", |
1040 | 1040 | "pull-through": "1.0.18" |
1041 | 1041 | } |
1042 | 1042 | }, |
1043 | + "patch-history": { | |
1044 | + "version": "1.0.0", | |
1045 | + "resolved": "https://registry.npmjs.org/patch-history/-/patch-history-1.0.0.tgz", | |
1046 | + "integrity": "sha512-lWc3U5awYkuZIBLiRcDgagtilCEQwb2IN3UAorVrIzdp4+MkyrVH3MQmVazx4YLV4w9AkVUy5Fdyz3yn8v32iw==", | |
1047 | + "requires": { | |
1048 | + "depject": "4.1.0", | |
1049 | + "depnest": "1.3.0", | |
1050 | + "lodash": "4.17.4", | |
1051 | + "mutant": "3.21.2" | |
1052 | + } | |
1053 | + }, | |
1043 | 1054 | "patchcore": { |
1044 | 1055 | "version": "1.9.0", |
1045 | 1056 | "resolved": "https://registry.npmjs.org/patchcore/-/patchcore-1.9.0.tgz", |
1046 | 1057 | "integrity": "sha512-d9bJ7oivSS9uLknOZxnpJMDWTGVSyLmUt38mOTrL0LD1YEAAmThhi+6i/+mPXhm6605DFbYzWtxBVmXQ2t2SjQ==", |
package.json | ||
---|---|---|
@@ -22,8 +22,9 @@ | ||
22 | 22 | "libnested": "^1.2.1", |
23 | 23 | "lodash": "^4.17.4", |
24 | 24 | "micro-css": "^2.0.1", |
25 | 25 | "mutant": "^3.21.2", |
26 | + "patch-history": "^1.0.0", | |
26 | 27 | "patchcore": "^1.9.0", |
27 | 28 | "pull-stream": "^3.6.0", |
28 | 29 | "read-directory": "^2.1.0", |
29 | 30 | "setimmediate": "^1.0.5" |
history/index.js | ||
---|---|---|
@@ -1,10 +1,0 @@ | ||
1 | -module.exports ={ | |
2 | - obs: { | |
3 | - store: require('./obs/store'), | |
4 | - }, | |
5 | - sync: { | |
6 | - push: require('./sync/push'), | |
7 | - back: require('./sync/back') | |
8 | - } | |
9 | -} | |
10 | - |
history/obs/store.js | ||
---|---|---|
@@ -1,11 +1,0 @@ | ||
1 | -const nest = require('depnest') | |
2 | -const { Array: MutantArray } = require('mutant') | |
3 | - | |
4 | -exports.gives = nest('history.obs.store') | |
5 | - | |
6 | -var _store = MutantArray() | |
7 | - | |
8 | -exports.create = (api) => { | |
9 | - return nest('history.obs.store', () => _store) | |
10 | -} | |
11 | - |
history/sync/back.js | ||
---|---|---|
@@ -1,31 +1,0 @@ | ||
1 | -const nest = require('depnest') | |
2 | -const last = require('lodash/last') | |
3 | - | |
4 | -exports.gives = nest('history.sync.back') | |
5 | - | |
6 | -exports.needs = nest({ | |
7 | - 'history.obs.store': 'first', | |
8 | - 'router.sync.router': 'first' | |
9 | -}) | |
10 | - | |
11 | -exports.create = (api) => { | |
12 | - return nest('history.sync.back', back) | |
13 | - | |
14 | - function back () { | |
15 | - const history = api.history.obs.store() | |
16 | - if (history().length === 1) return false | |
17 | - | |
18 | - history.pop() | |
19 | - const location = last(history()) | |
20 | - | |
21 | - const newView = api.router.sync.router(location) | |
22 | - renderPage(newView) | |
23 | - } | |
24 | -} | |
25 | - | |
26 | -function renderPage (newView) { | |
27 | - const oldView = document.body.firstChild | |
28 | - oldView | |
29 | - ? document.body.replaceChild(newView, oldView) | |
30 | - : document.body.appendChild(newView) | |
31 | -} |
history/sync/push.js | ||
---|---|---|
@@ -1,30 +1,0 @@ | ||
1 | -const nest = require('depnest') | |
2 | - | |
3 | -exports.gives = nest('history.sync.push') | |
4 | - | |
5 | -exports.needs = nest({ | |
6 | - 'history.obs.store': 'first', | |
7 | - 'router.sync.router': 'first' | |
8 | -}) | |
9 | - | |
10 | -exports.create = (api) => { | |
11 | - return nest('history.sync.push', push) | |
12 | - | |
13 | - function push (location) { | |
14 | - const newView = api.router.sync.router(location) | |
15 | - | |
16 | - if (!newView) return | |
17 | - | |
18 | - api.history.obs.store().push(location) | |
19 | - | |
20 | - renderPage(newView) | |
21 | - } | |
22 | -} | |
23 | - | |
24 | -function renderPage (newView) { | |
25 | - const oldView = document.body.firstChild | |
26 | - oldView | |
27 | - ? document.body.replaceChild(newView, oldView) | |
28 | - : document.body.appendChild(newView) | |
29 | - | |
30 | -} |
Built with git-ssb-web