|
1 | 1 | import defaultLog from "./defaultLog" |
2 | 2 |
|
3 | | -export default function(options) { |
4 | | - options = options || {} |
5 | | - options.log = typeof options.log === "function" ? options.log : defaultLog |
| 3 | +var isFn = function(value) { |
| 4 | + return typeof value === "function" |
| 5 | +} |
6 | 6 |
|
7 | | - return function(app) { |
8 | | - return function(initialState, actionsTemplate, view, container) { |
9 | | - function enhanceActions(actions, prefix) { |
10 | | - var namespace = prefix ? prefix + "." : "" |
11 | | - return Object.keys(actions || {}).reduce(function(otherActions, name) { |
12 | | - var namedspacedName = namespace + name |
13 | | - var action = actions[name] |
14 | | - otherActions[name] = |
15 | | - typeof action === "function" |
16 | | - ? function(data) { |
17 | | - return function(state, actions) { |
18 | | - var result = action(data) |
19 | | - result = |
20 | | - typeof result === "function" |
21 | | - ? result(state, actions) |
22 | | - : result |
23 | | - options.log( |
24 | | - state, |
25 | | - { name: namedspacedName, data: data }, |
26 | | - result |
27 | | - ) |
28 | | - return result |
29 | | - } |
| 7 | +function makeLoggerApp(log, nextApp) { |
| 8 | + return function(initialState, actionsTemplate, view, container) { |
| 9 | + function enhanceActions(actions, prefix) { |
| 10 | + var namespace = prefix ? prefix + "." : "" |
| 11 | + return Object.keys(actions || {}).reduce(function(otherActions, name) { |
| 12 | + var namedspacedName = namespace + name |
| 13 | + var action = actions[name] |
| 14 | + otherActions[name] = |
| 15 | + typeof action === "function" |
| 16 | + ? function(data) { |
| 17 | + return function(state, actions) { |
| 18 | + var result = action(data) |
| 19 | + result = |
| 20 | + typeof result === "function" |
| 21 | + ? result(state, actions) |
| 22 | + : result |
| 23 | + log(state, { name: namedspacedName, data: data }, result) |
| 24 | + return result |
30 | 25 | } |
31 | | - : enhanceActions(action, namedspacedName) |
32 | | - return otherActions |
33 | | - }, {}) |
34 | | - } |
| 26 | + } |
| 27 | + : enhanceActions(action, namedspacedName) |
| 28 | + return otherActions |
| 29 | + }, {}) |
| 30 | + } |
| 31 | + |
| 32 | + var enhancedActions = enhanceActions(actionsTemplate) |
35 | 33 |
|
36 | | - var enhancedActions = enhanceActions(actionsTemplate) |
| 34 | + var appActions = nextApp(initialState, enhancedActions, view, container) |
| 35 | + return appActions |
| 36 | + } |
| 37 | +} |
37 | 38 |
|
38 | | - var appActions = app(initialState, enhancedActions, view, container) |
39 | | - return appActions |
| 39 | +export function withLogger(optionsOrApp) { |
| 40 | + if (isFn(optionsOrApp)) { |
| 41 | + return makeLoggerApp(defaultLog, optionsOrApp) |
| 42 | + } else { |
| 43 | + var log = isFn(optionsOrApp.log) ? optionsOrApp.log : defaultLog |
| 44 | + return function(nextApp) { |
| 45 | + return makeLoggerApp(log, nextApp) |
40 | 46 | } |
41 | 47 | } |
42 | 48 | } |
0 commit comments