Skip to content

Commit

Permalink
feat: collect supports collect(app, 'path') to reset
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Mar 2, 2018
1 parent 147b725 commit 97de2d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/decorator/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ export async function fetch(source) {
return source
}

function fetchAndPush(ref, path) {
return fetch(get(ref, path))
.then(data => {
set(ref, path, data)
})
}

const symbol = typeof Symbol === 'function' ? Symbol('collect') : '--[[collect]]--'

/**
* 收集全局 Store 的数据,一般在需要修改其他页面数据的时候使用;
* 并且在使用了 webpack **异步加载VM**,搭配 `collect` 使用。
*
* 并且在使用了 webpack **异步加载VM**,搭配 `collect` 使用。
*
* 1. 支持直接调用 `const someVM = await collect(this.app.someVM)`
* 2. 支持修饰器形式调用
* @public
* @param {...string} paths
* @param {...string} paths
* @example <caption>app.js</caption>
* \@bindView(ContainerView)
* export default class App extends Root {
Expand Down Expand Up @@ -59,7 +66,7 @@ const symbol = typeof Symbol === 'function' ? Symbol('collect') : '--[[collect]]
* // this.app.editVM
* // this.app.viewVM
* }
*
*
* // 在下面的生命周期中,不能直接使用
* // 需要 await collect(this.app.editVM) 来异步使用
* constructor() {}
Expand All @@ -69,12 +76,21 @@ const symbol = typeof Symbol === 'function' ? Symbol('collect') : '--[[collect]]
*/
export default function collect(...paths) {
if (
!Array.isArray(arguments[0]) && arguments[0]
&& (
typeof arguments[0] === 'object' || typeof arguments[0] === 'function'
)
typeof paths[0] === 'object' && !Array.isArray(paths[0])
|| typeof paths[0] === 'function'
) {
return fetch(arguments[0])
const source = paths[0]
paths = paths.slice(1)
// `collect(app.pageA)`
if (!paths.length) {
return fetch(source)
}
// `collect(app, 'pageA')` // should reset
return Promise.all(
paths.map(
path => fetchAndPush(source, path)
)
).then(() => source)
}

return function collectInner(Comp) {
Expand All @@ -89,10 +105,7 @@ export default function collect(...paths) {
super(...args)
Promise.all(
paths.map(path =>
fetch(get(this.store.app, path))
.then(data => {
set(this.store.app, path, data)
})
fetchAndPush(this.store.app, path)
)
).then(() => {
this[symbol] = 'resolved'
Expand Down
12 changes: 12 additions & 0 deletions test/ssr-spec.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @file ssr-spec
* @author Cuttle Cong
* @date 2018/3/2
* @description $END$
*/

describe('ssr-spec', function () {
test('ssr-spec case 1', () => {
expect(1 + 2).toBe(3)
})
})

0 comments on commit 97de2d4

Please sign in to comment.