Skip to content

Commit 5661516

Browse files
authored
withLogger (#20)
window.logger => window.hyperappLogger.withLogger
1 parent a2cdca1 commit 5661516

4 files changed

Lines changed: 115 additions & 53 deletions

File tree

README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# <img height=24 src=https://cdn.rawgit.com/JorgeBucaran/f53d2c00bafcf36e84ffd862f0dc2950/raw/882f20c970ff7d61aa04d44b92fc3530fa758bc0/Hyperapp.svg> Hyperapp Logger
1+
# <img height=24 src=https://cdn.rawgit.com/jorgebucaran/f53d2c00bafcf36e84ffd862f0dc2950/raw/882f20c970ff7d61aa04d44b92fc3530fa758bc0/Hyperapp.svg> Hyperapp Logger
22

33
[![Travis CI](https://img.shields.io/travis/hyperapp/logger/master.svg)](https://travis-ci.org/hyperapp/logger)
44
[![Codecov](https://img.shields.io/codecov/c/github/hyperapp/logger/master.svg)](https://codecov.io/gh/hyperapp/logger)
@@ -7,9 +7,37 @@
77

88
A [Hyperapp](https://github.com/hyperapp/hyperapp) higher-order `app` that logs state updates and action information to the console.
99

10-
[Try it Online](https://codepen.io/okwolf/pen/xLQmvW?editors=0010)
10+
## Getting Started
1111

12-
![Screenshot](https://user-images.githubusercontent.com/3735164/34082934-657f864c-e31c-11e7-93d2-d70f190aa928.png)
12+
This example shows a counter that can be incremented or decremented. Go ahead and [try it online](https://codepen.io/okwolf/pen/xLQmvW?editors=0010) with your browser console open to see the log messages.
13+
14+
```js
15+
import { h, app } from "hyperapp"
16+
import { withLogger } from "@hyperapp/logger"
17+
18+
const state = {
19+
count: 0
20+
}
21+
22+
const actions = {
23+
down: () => state => ({ count: state.count - 1 }),
24+
up: () => state => ({ count: state.count + 1 })
25+
}
26+
27+
const view = (state, actions) => (
28+
<main>
29+
<h1>{state.count}</h1>
30+
<button onclick={actions.down} disabled={state.count <= 0}>
31+
32+
</button>
33+
<button onclick={actions.up}></button>
34+
</main>
35+
)
36+
37+
withLogger(app)(state, actions, view, document.body)
38+
```
39+
40+
![Screenshot](https://user-images.githubusercontent.com/3735164/36941306-d7233132-1f0c-11e8-9b97-335f7957a685.png)
1341

1442
## Installation
1543

@@ -23,8 +51,8 @@ npm i <a href="https://www.npmjs.com/package/@hyperapp/logger">@hyperapp/logger<
2351

2452
Then with a module bundler like [rollup](https://github.com/rollup/rollup) or [webpack](https://github.com/webpack/webpack) use as you would anything else.
2553

26-
```jsx
27-
import logger from "@hyperapp/logger"
54+
```js
55+
import { withLogger } from "@hyperapp/logger"
2856
```
2957

3058
### Browser
@@ -35,12 +63,23 @@ Download the minified library from the [CDN](https://unpkg.com/@hyperapp/logger)
3563
<script src="https://unpkg.com/@hyperapp/logger"></script>
3664
```
3765

38-
You can find the library in `window.logger`.
66+
You can find the library in `window.hyperappLogger`.
67+
68+
```js
69+
const { withLogger } = hyperappLogger
70+
```
3971

4072
## Usage
4173

74+
Compose the `withLogger` function with your `app` before calling it with the usual arguments.
75+
4276
```js
43-
logger(options)(app)(state, actions, view, document.body)
77+
import { withLogger } from "@hyperapp/logger"
78+
79+
withLogger(app)(state, actions, view, document.body)
80+
81+
// Or if you need to pass options
82+
withLogger(options)(app)(state, actions, view, document.body)
4483
```
4584

4685
### Options
@@ -50,7 +89,7 @@ logger(options)(app)(state, actions, view, document.body)
5089
Use it to customize the log function.
5190

5291
```js
53-
logger({
92+
withLogger({
5493
log(prevState, action, nextState) {
5594
// format and send your log messages anywhere you like
5695
}

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "@hyperapp/logger",
3-
"version": "0.4.2",
4-
"description": "Logger for Hyperapp",
5-
"main": "dist/logger.js",
3+
"version": "0.5.0",
4+
"description": "Log Hyperapp state updates and action information to the console",
5+
"main": "dist/hyperappLogger.js",
66
"jsnext:main": "src/index.js",
77
"module": "src/index.js",
88
"repository": {
@@ -16,8 +16,8 @@
1616
"scripts": {
1717
"test": "jest --coverage --no-cache",
1818
"build": "npm run bundle && npm run minify",
19-
"bundle": "rollup -i src/index.js -o dist/logger.js -m -f umd -n logger",
20-
"minify": "uglifyjs dist/logger.js -o dist/logger.js -mc pure_funcs=['Object.defineProperty'] --source-map includeSources,url=logger.js.map",
19+
"bundle": "rollup -i src/index.js -o dist/hyperappLogger.js -m -f umd -n hyperappLogger",
20+
"minify": "uglifyjs dist/hyperappLogger.js -o dist/hyperappLogger.js -mc pure_funcs=['Object.defineProperty'] --source-map includeSources,url=hyperappLogger.js.map",
2121
"prepare": "npm run build",
2222
"format": "prettier --semi false --write '{src,test}/**/*.js'"
2323
},

src/index.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
import defaultLog from "./defaultLog"
22

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+
}
66

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
3025
}
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)
3533

36-
var enhancedActions = enhanceActions(actionsTemplate)
34+
var appActions = nextApp(initialState, enhancedActions, view, container)
35+
return appActions
36+
}
37+
}
3738

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)
4046
}
4147
}
4248
}

test/logger.test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h, app } from "hyperapp"
2-
import logger from "../src"
2+
import { withLogger } from "../src"
33

44
const defaultConsole = console
55

@@ -8,7 +8,7 @@ afterEach(() => {
88
})
99

1010
test("without actions", done =>
11-
logger()(app)(undefined, undefined, () => done()))
11+
withLogger(app)(undefined, undefined, () => done()))
1212

1313
test("log", done => {
1414
console = {
@@ -19,7 +19,24 @@ test("log", done => {
1919
}
2020
}
2121

22-
logger()(app)(
22+
withLogger(app)(
23+
{},
24+
{
25+
foo: () => state => state
26+
}
27+
).foo()
28+
})
29+
30+
test("options without custom log", done => {
31+
console = {
32+
log() {},
33+
group() {},
34+
groupEnd() {
35+
done()
36+
}
37+
}
38+
39+
withLogger({})(app)(
2340
{},
2441
{
2542
foo: () => state => state
@@ -28,7 +45,7 @@ test("log", done => {
2845
})
2946

3047
test("custom log function", done =>
31-
logger({
48+
withLogger({
3249
log(state, action, nextState) {
3350
expect(state).toEqual({ value: 0 })
3451
expect(action).toEqual({ name: "inc", data: 2 })
@@ -45,7 +62,7 @@ test("custom log function", done =>
4562
).inc(2))
4663

4764
test("state slices", done => {
48-
const actions = logger({
65+
const actions = withLogger({
4966
log(state, action, nextState) {
5067
switch (action.name) {
5168
case "hello":
@@ -80,7 +97,7 @@ test("state slices", done => {
8097
})
8198

8299
test("doesn't interfere with state updates", done => {
83-
const actions = logger({
100+
const actions = withLogger({
84101
log(state, action, nextState) {}
85102
})(app)(
86103
{
@@ -117,7 +134,7 @@ test("doesn't interfere with state updates", done => {
117134

118135
test("doesn't interfere with custom container", done => {
119136
document.body.innerHTML = "<main></main>"
120-
logger({
137+
withLogger({
121138
log(state, action, nextState) {}
122139
})(app)(
123140
{},

0 commit comments

Comments
 (0)