Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mixin init order. Close #329. #332

Merged
merged 1 commit into from Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions docs/mixins.md
Expand Up @@ -113,14 +113,3 @@ app({
])]
})
```

## Presets

Group mixins under the same category to create a preset. Then use it like any other mixin.

```jsx
const DevTools = () => ({
mixins: [Logger, Debugger, Replayer]
})
```

18 changes: 8 additions & 10 deletions src/app.js
Expand Up @@ -2,26 +2,24 @@ var globalInvokeLaterStack = []

export function app(props) {
var appState
var appView = props.view
var appActions = {}
var appEvents = {}
var appMixins = []
var appView = props.view
var appMixins = props.mixins || []
var appRoot = props.root || document.body
var element = appRoot.children[0]
var oldNode
var willRender

for (var i = -1; i < appMixins.length; i++) {
props = appMixins[i] ? appMixins[i](emit) : props
for (var i = 0; i <= appMixins.length; i++) {
var mixin = appMixins[i] ? appMixins[i](emit) : props

Object.keys(props.events || []).map(function(key) {
appEvents[key] = (appEvents[key] || []).concat(props.events[key])
Object.keys(mixin.events || []).map(function(key) {
appEvents[key] = (appEvents[key] || []).concat(mixin.events[key])
})

initialize(appActions, props.actions)

appMixins = appMixins.concat(props.mixins || [])
appState = merge(appState, props.state)
initialize(appActions, mixin.actions)
appState = merge(appState, mixin.state)
}

requestRender(
Expand Down
41 changes: 7 additions & 34 deletions test/mixins.test.js
Expand Up @@ -43,25 +43,25 @@ test("extend events", done => {
}
},
events: {
load(state, actions) {
expect(state.value).toBe(0)
actions.up()
load(state) {
expect(state.value).toBe(2)
done()
}
},
mixins: [
() => ({
events: {
load(state, actions) {
expect(state.value).toBe(1)
expect(state.value).toBe(0)
actions.up()
}
}
}),
() => ({
events: {
load(state) {
expect(state.value).toBe(2)
done()
load(state, actions) {
expect(state.value).toBe(1)
actions.up()
}
}
})
Expand Down Expand Up @@ -140,33 +140,6 @@ test("extend namespace", done => {
})
})

test("presets", () => {
const foobar = () => ({
mixins: [
() => ({
state: {
foo: 1
}
}),
() => ({
state: {
bar: 2
}
})
]
})

app({
events: {
load(state) {
expect(state.foo).toBe(1)
expect(state.bar).toBe(2)
}
},
mixins: [foobar]
})
})

test("receive emit function", done => {
app({
events: {
Expand Down