Comparing version 2.0.0-beta.4 to 2.0.0-beta.5
{ | ||
"name": "kea", | ||
"version": "2.0.0-beta.4", | ||
"version": "2.0.0-beta.5", | ||
"description": "Smart front-end architecture", | ||
@@ -5,0 +5,0 @@ "author": "Marius Andra", |
@@ -435,2 +435,4 @@ /* global test, expect */ | ||
expect(getContext().mount.counter).toEqual({}) | ||
expect(() => { logic.values.name }).toThrow() // eslint-disable-line | ||
@@ -492,2 +494,3 @@ expect(() => { secondLogic.values.secondName }).toThrow() // eslint-disable-line | ||
unmount1() | ||
expect(getContext().mount.counter).toEqual({}) | ||
@@ -554,2 +557,3 @@ expect(() => { logic.values.name }).toThrow() // eslint-disable-line | ||
unmount1() | ||
expect(getContext().mount.counter).toEqual({}) | ||
@@ -912,1 +916,235 @@ expect(() => { logic.values.name }).toThrow() // eslint-disable-line | ||
}) | ||
test('multiple mounts and unmounts with listener connection', () => { | ||
resetContext({ createStore: true, autoMount: false }) | ||
let nameFromAction = '' | ||
const secondLogic = kea({ | ||
path: () => ['mount', 'second'], | ||
actions: () => ({ | ||
secondAction: name => ({ name }) | ||
}), | ||
reducers: ({ actions }) => ({ | ||
secondName: ['second', { | ||
[actions.secondAction]: (_, { name }) => name | ||
}] | ||
}), | ||
listeners: ({ actions }) => ({ | ||
[actions.secondAction]: ({ name }) => { | ||
nameFromAction = name | ||
} | ||
}) | ||
}) | ||
const logic = kea({ | ||
path: () => ['mount', 'logic'], | ||
actions: () => ({ | ||
updateName: name => ({ name }) | ||
}), | ||
reducers: ({ actions }) => ({ | ||
name: ['first', { | ||
[actions.updateName]: (_, { name }) => name | ||
}] | ||
}), | ||
listeners: ({ actions }) => ({ | ||
[actions.updateName]: ({ name }) => { | ||
secondLogic.actions.secondAction('new name') | ||
} | ||
}) | ||
}) | ||
const unmount1 = logic.mount() | ||
const unmount2 = logic.mount() | ||
const unmount3 = logic.mount() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3 }) | ||
expect(logic.values.name).toEqual('first') | ||
expect(nameFromAction).toEqual('') | ||
logic.actions.updateName('new name') | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3, 'mount.second': 3 }) | ||
unmount3() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 2, 'mount.second': 2 }) | ||
unmount2() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 1, 'mount.second': 1 }) | ||
unmount1() | ||
expect(getContext().mount.counter).toEqual({}) | ||
}) | ||
test('multiple mounts and unmounts with double connection', () => { | ||
resetContext({ createStore: true, autoMount: false }) | ||
let nameFromAction = '' | ||
const thirdLogic = kea({ | ||
path: () => ['mount', 'third'], | ||
reducers: ({ actions }) => ({ | ||
thirdName: ['third'] | ||
}) | ||
}) | ||
const secondLogic = kea({ | ||
connect: [thirdLogic], | ||
path: () => ['mount', 'second'], | ||
actions: () => ({ | ||
secondAction: name => ({ name }) | ||
}), | ||
reducers: ({ actions }) => ({ | ||
secondName: ['second', { | ||
[actions.secondAction]: (_, { name }) => name | ||
}] | ||
}), | ||
listeners: ({ actions }) => ({ | ||
[actions.secondAction]: ({ name }) => { | ||
nameFromAction = name | ||
} | ||
}) | ||
}) | ||
const logic = kea({ | ||
path: () => ['mount', 'logic'], | ||
actions: () => ({ | ||
updateName: name => ({ name }) | ||
}), | ||
reducers: ({ actions }) => ({ | ||
name: ['first', { | ||
[actions.updateName]: (_, { name }) => name | ||
}] | ||
}), | ||
listeners: ({ actions }) => ({ | ||
[actions.updateName]: ({ name }) => { | ||
secondLogic.actions.secondAction('new name') | ||
} | ||
}) | ||
}) | ||
const unmount1 = logic.mount() | ||
const unmount2 = logic.mount() | ||
const unmount3 = logic.mount() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3 }) | ||
expect(logic.values.name).toEqual('first') | ||
expect(nameFromAction).toEqual('') | ||
logic.actions.updateName('new name') | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3, 'mount.second': 3, 'mount.third': 3 }) | ||
const unmount4 = logic.mount() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 4, 'mount.second': 4, 'mount.third': 4 }) | ||
unmount4() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3, 'mount.second': 3, 'mount.third': 3 }) | ||
unmount3() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 2, 'mount.second': 2, 'mount.third': 2 }) | ||
unmount2() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 1, 'mount.second': 1, 'mount.third': 1 }) | ||
unmount1() | ||
expect(getContext().mount.counter).toEqual({}) | ||
}) | ||
test('multiple mounts and unmounts with double connection in actions/values', () => { | ||
resetContext({ createStore: true, autoMount: false }) | ||
let nameFromAction = '' | ||
const fourthLogic = kea({ | ||
path: () => ['mount', 'fourth'], | ||
actions: () => ({ | ||
updateFourth: true | ||
}), | ||
reducers: ({ actions }) => ({ | ||
fourthName: ['fourth'] | ||
}) | ||
}) | ||
const thirdLogic = kea({ | ||
path: () => ['mount', 'third'], | ||
reducers: ({ actions }) => ({ | ||
thirdName: ['third'] | ||
}) | ||
}) | ||
const secondLogic = kea({ | ||
connect: { | ||
values: [ | ||
thirdLogic, ['thirdName'] | ||
], | ||
actions: [ | ||
fourthLogic, ['updateFourth'] | ||
] | ||
}, | ||
path: () => ['mount', 'second'], | ||
actions: () => ({ | ||
secondAction: name => ({ name }) | ||
}), | ||
reducers: ({ actions }) => ({ | ||
secondName: ['second', { | ||
[actions.secondAction]: (_, { name }) => name | ||
}] | ||
}), | ||
listeners: ({ actions }) => ({ | ||
[actions.secondAction]: ({ name }) => { | ||
nameFromAction = name | ||
} | ||
}) | ||
}) | ||
const logic = kea({ | ||
path: () => ['mount', 'logic'], | ||
actions: () => ({ | ||
updateName: name => ({ name }) | ||
}), | ||
reducers: ({ actions }) => ({ | ||
name: ['first', { | ||
[actions.updateName]: (_, { name }) => name | ||
}] | ||
}), | ||
listeners: ({ actions }) => ({ | ||
[actions.updateName]: ({ name }) => { | ||
secondLogic.actions.secondAction('new name') | ||
} | ||
}) | ||
}) | ||
const unmount1 = logic.mount() | ||
const unmount2 = logic.mount() | ||
const unmount3 = logic.mount() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3 }) | ||
expect(logic.values.name).toEqual('first') | ||
expect(nameFromAction).toEqual('') | ||
logic.actions.updateName('new name') | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3, 'mount.second': 3, 'mount.third': 3, 'mount.fourth': 3 }) | ||
const unmount4 = logic.mount() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 4, 'mount.second': 4, 'mount.third': 4, 'mount.fourth': 4 }) | ||
unmount4() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 3, 'mount.second': 3, 'mount.third': 3, 'mount.fourth': 3 }) | ||
unmount3() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 2, 'mount.second': 2, 'mount.third': 2, 'mount.fourth': 2 }) | ||
unmount2() | ||
expect(getContext().mount.counter).toEqual({ 'mount.logic': 1, 'mount.second': 1, 'mount.third': 1, 'mount.fourth': 1 }) | ||
unmount1() | ||
expect(getContext().mount.counter).toEqual({}) | ||
}) |
@@ -23,3 +23,4 @@ import { runPlugins } from '../plugins' | ||
run: { heap: runHeap }, | ||
options: { autoConnect: globalAutoConnect } | ||
options: { autoConnect: globalAutoConnect }, | ||
mount: { counter: mountCounter } | ||
} = getContext() | ||
@@ -37,11 +38,15 @@ | ||
// always connect these, even if autoConnectInListener is false | ||
if (buildHeap.length > 0 && !buildHeap[buildHeap.length - 1].connections[pathString]) { | ||
addConnection(buildHeap[buildHeap.length - 1], buildCache[pathString]) | ||
} | ||
if (buildHeap.length > 0) { | ||
if (!buildHeap[buildHeap.length - 1].connections[pathString]) { | ||
addConnection(buildHeap[buildHeap.length - 1], buildCache[pathString]) | ||
} | ||
// if we were running a listener and built this logic, mount it directly | ||
// ... except if autoConnectInListener is false | ||
if (autoConnectInListener && runHeap.length > 0 && !runHeap[runHeap.length - 1].connections[pathString]) { | ||
addConnection(runHeap[runHeap.length - 1], buildCache[pathString]) | ||
mountLogic(buildCache[pathString]) // will be unmounted via the connection | ||
} else if (autoConnectInListener && runHeap.length > 0) { | ||
const runningInLogic = runHeap[runHeap.length - 1] | ||
if (!runningInLogic.connections[pathString]) { | ||
addConnection(runningInLogic, buildCache[pathString]) | ||
mountLogic(buildCache[pathString], mountCounter[runningInLogic.pathString]) // will be unmounted via the connection | ||
} | ||
} | ||
@@ -96,3 +101,3 @@ } | ||
mount: (callback) => { | ||
mountLogic(logic, wrapper) | ||
mountLogic(logic) | ||
if (callback) { | ||
@@ -99,0 +104,0 @@ const response = callback(logic) |
import { attachReducer, detachReducer } from '../store/reducer' | ||
import { runPlugins, reservedProxiedKeys } from '../plugins' | ||
import { runPlugins } from '../plugins' | ||
import { getContext } from '../context' | ||
export function mountLogic (logic) { | ||
export function mountLogic (logic, count = 1) { | ||
const { mount: { counter, mounted } } = getContext() | ||
for (const pathString of Object.keys(logic.connections)) { | ||
counter[pathString] = (counter[pathString] || 0) + 1 | ||
if (counter[pathString] === 1) { | ||
// mount this logic after all the dependencies | ||
const pathStrings = Object.keys(logic.connections).filter(k => k !== logic.pathString).concat([logic.pathString]) | ||
for (const pathString of pathStrings) { | ||
counter[pathString] = (counter[pathString] || 0) + count | ||
if (counter[pathString] === count) { | ||
const connectedLogic = logic.connections[pathString] | ||
@@ -32,3 +35,6 @@ | ||
for (const pathString of Object.keys(logic.connections).reverse()) { | ||
// unmount in reverse order | ||
const pathStrings = Object.keys(logic.connections).filter(k => k !== logic.pathString).concat([logic.pathString]).reverse() | ||
for (const pathString of pathStrings) { | ||
counter[pathString] = (counter[pathString] || 0) - 1 | ||
@@ -35,0 +41,0 @@ if (counter[pathString] === 0) { |
Sorry, the diff of this file is too big to display
346601
9737