Skip to content

Commit de9948e

Browse files
authored
Merge pull request #9 from lppjunior/develop
### Fixbug - Fix bug on middleware implementation ### Update - Update documentation structure - Add separated pattern webpack build
2 parents f2e6087 + c6a7ff1 commit de9948e

13 files changed

+198
-108
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
## [1.2.1] - 2020-08-25
1111
### Fixbug
1212
- Fix bug on middleware implementation
13+
### Update
14+
- Update documentation structure
15+
- Add separated pattern webpack build
1316

1417
## [1.2.0] - 2020-08-25
1518
### Feature

README.md

Lines changed: 6 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,12 @@ See many characteristics of this library:
77
- All pattern hav 100% coverage unity tests and confiability.
88
- Use 100% native javascript to write this code
99

10-
| Available Patterns |
11-
|---------------------------|
12-
| [Middleware](#Middleware) |
13-
| [Observer](#Observer) |
14-
| [Pipeline](#Pipeline) |
15-
16-
## Middleware
17-
A single implementation of Middleware pattern
18-
19-
**Class structure**
20-
```js
21-
class Middleware() {
22-
use(callback: function)
23-
process(...data: object)
24-
}
25-
```
26-
27-
## Example
28-
29-
```js
30-
const middleware = new Middleware()
31-
32-
middleware.use((param1, param2, param3, next) => {
33-
param1 = 'param 1 UPDATED'
34-
console.log('Execution 1', param1, param2, param3)
35-
next()
36-
})
37-
38-
middleware.use((param1, param2, param3, next) => {
39-
console.log('Execution 2', param1, param2, param3) // param1 = 'param 1 UPDATED'
40-
next()
41-
})
42-
43-
middleware.process('param 1', 'param 2', 'param 3')
44-
```
45-
46-
## Observer
47-
The instance (or model) maintains a collection of objects (observers) and will notify them of any changes in their state.
48-
49-
**Class structure**
50-
```js
51-
class Observer() {
52-
on(event: string, callback: function)
53-
emit(event: string, data: object)
54-
}
55-
```
56-
57-
## Example
58-
59-
```js
60-
const observer = new Observer()
61-
62-
observer.on('test', (data) => console.log(data)) // result: Observer emit successful
63-
observer.on('test', (data) => console.log('Test 2 > ', data)) // result: Test 2 > Observer emit successful
64-
65-
observer.emit('test', { status: 'Observer emit successful' })
66-
```
67-
68-
## Pipeline
69-
A Pipeline pattern implementation
70-
71-
**Class structure**
72-
```js
73-
class Pipeline() {
74-
pipe(callback: function)
75-
process(data: object)
76-
}
77-
```
78-
79-
## Example
80-
81-
```js
82-
const result = (new Pipeline())
83-
.pipe((data) => data + 5)
84-
.pipe((data) => data * 20)
85-
.pipe((data) => data / 2)
86-
.process(5) // result 100
87-
88-
```
10+
## Available Patterns
11+
| Pattern | Description|
12+
| - | - |
13+
| [Middleware](docs/MIDDLEWARE-PATTERN-JS.md) | The Middleware standard creates an execution queue where a step has the power to call the next step or interrupt the execution |
14+
| [Observer](docs/OBSERVER-PATTERN-JS.md) | The Observer pattern is powerfull resource to emit and receive notifications |
15+
| [Pipeline](docs/PIPELINE-PATTERN-JS.md) | The pipeline pattern make a execution queue passing processed result to next step |
8916

9017
## Versioning
9118

dist/favicon.png

552 Bytes
Loading

dist/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!doctype html><html><head><meta charset="utf-8"><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"><title>Pattern JS</title><link rel="icon" href="favicon.png"></head><body><h1>Patter JS</h1><p>Open you inspection to see result</p><script src="middleware.js"></script><script src="observer.js"></script><script src="pipeline.js"></script><script src="main.js"></script></body></html>

dist/main.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/middleware.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function testMiddleware () {
2+
const middleware = new Middleware()
3+
middleware.use((param1, param2, next) => {
4+
console.log('[middleware] 1 ', param1, param2)
5+
next()
6+
})
7+
8+
middleware.use((param1, param2, next) => {
9+
console.log('[middleware] 2 ', param1, param2)
10+
next()
11+
})
12+
13+
middleware.use((param1, param2, next) => {
14+
console.log('[middleware] 3 ', param1, param2)
15+
next()
16+
})
17+
18+
middleware.process({ test: 123, value: 'aaa' }, 'test')
19+
}
20+
21+
document.addEventListener('DOMContentLoaded', testMiddleware, false)

dist/observer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function runExample () {
2+
testObserver()
3+
}
4+
5+
function testObserver () {
6+
console.log('[observer] ', 'test')
7+
const observer = new Observer()
8+
observer.on('test', (data) => console.log('[observer] ', data))
9+
observer.on('test', (data) => console.log('[observer] ', 'Test 2 > ', data))
10+
observer.emit('test', { status: 'Observer emit successful' })
11+
}
12+
13+
document.addEventListener('DOMContentLoaded', runExample, false)

dist/pipeline.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function testPipeline () {
2+
const result = (new Pipeline())
3+
.pipe((data) => {
4+
console.log('[pipeline] ', data + ' + ' + 5)
5+
return data + 5
6+
})
7+
.pipe((data) => {
8+
console.log('[pipeline] ', data + ' * ' + 3)
9+
return data * 3
10+
})
11+
.pipe((data) => {
12+
console.log('[pipeline] ', data + ' + ' + 150)
13+
return data + 150
14+
})
15+
.process(5)
16+
17+
console.log('[pipeline] result: ', result)
18+
}
19+
20+
document.addEventListener('DOMContentLoaded', testPipeline, false)

docs/MIDDLEWARE-PATTERN-JS.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Middleware Pattern
2+
The Middleware standard creates an execution queue where a step has the power to call the next step or interrupt the execution
3+
4+
**Class structure**
5+
```js
6+
class Middleware() {
7+
use(callback: function)
8+
process(...data: object)
9+
}
10+
```
11+
12+
## Example
13+
14+
```js
15+
const middleware = new Middleware()
16+
17+
middleware.use((param1, param2, param3, next) => {
18+
console.log('Execution 1', param1, param2, param3)
19+
20+
if ( param1 !== 'param 1') {
21+
throw new Error('Param 1 is incorrect')
22+
}
23+
24+
next()
25+
})
26+
27+
middleware.use((param1, param2, param3, next) => {
28+
console.log('Execution 2', param1, param2, param3)
29+
30+
next()
31+
})
32+
33+
middleware.process('param 1', 'param 2', 'param 3')
34+
```

docs/OBSERVER-PATTERN-JS.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Observer Pattern
2+
The Observer pattern is powerfull resource to emit and receive notifications.
3+
4+
**Class structure**
5+
```js
6+
class Observer() {
7+
on(event: string, callback: function)
8+
emit(event: string, data: object)
9+
}
10+
```
11+
12+
## Example
13+
14+
```js
15+
const observer = new Observer()
16+
17+
observer.on('test', (data) => console.log(data)) // result: Observer emit successful
18+
observer.on('test', (data) => console.log('Test 2 > ', data)) // result: Test 2 > Observer emit successful
19+
20+
observer.emit('test', { status: 'Observer emit successful' })
21+
```

docs/PIPELINE-PATTERN-JS.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Pipeline Pattern
2+
The pipeline pattern make a execution queue passing processed result to next step.
3+
4+
**Class structure**
5+
```js
6+
class Pipeline() {
7+
pipe(callback: function)
8+
process(data: object)
9+
}
10+
```
11+
12+
## Example
13+
14+
```js
15+
const result = (new Pipeline())
16+
.pipe((data) => data + 5)
17+
.pipe((data) => data * 20)
18+
.pipe((data) => data / 2)
19+
.process(5) // result 100
20+
21+
```

webpack.config.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,61 @@ const CopyPlugin = require('copy-webpack-plugin')
33
var HtmlWebpackPlugin = require('html-webpack-plugin')
44
const path = require('path')
55

6-
module.exports = {
7-
entry: {
8-
patterns: './src'
9-
},
10-
output: {
11-
filename: 'src/[name].min.js',
12-
path: path.resolve(__dirname, 'dist'),
13-
libraryTarget: 'umd'
6+
const config = {}
7+
8+
const process = [
9+
{
10+
...config,
11+
name: "all",
12+
entry: {
13+
patterns: './src'
14+
},
15+
output: {
16+
filename: 'src/[name].min.js',
17+
path: path.resolve(__dirname, 'dist'),
18+
libraryTarget: 'umd'
19+
}
1420
},
15-
devtool: 'source-map',
16-
plugins: [
17-
new CleanWebpackPlugin(),
18-
new HtmlWebpackPlugin({
19-
title: 'Pattern JS',
20-
favicon: 'src-example/favicon.png',
21-
template: 'src-example/index.html'
22-
}),
23-
new CopyPlugin({
24-
patterns: [
25-
path.resolve(__dirname, 'src-example', 'src')
26-
]
27-
})
28-
],
21+
{
22+
...config,
23+
name: "unity",
24+
entry: {
25+
middleware: './src/pattern/middleware',
26+
pipeline: './src/pattern/pipeline',
27+
observer: './src/pattern/observer'
28+
},
29+
output: {
30+
filename: 'src/pattern/[name].min.js',
31+
path: path.resolve(__dirname, 'dist'),
32+
libraryTarget: 'umd'
33+
}
34+
}
35+
]
36+
37+
module.exports = [
38+
...process,
39+
{
40+
devtool: 'source-map',
41+
plugins: [
42+
new CleanWebpackPlugin(),
43+
new HtmlWebpackPlugin({
44+
title: 'Pattern JS',
45+
favicon: 'src-example/favicon.png',
46+
template: 'src-example/index.html'
47+
}),
48+
new CopyPlugin({
49+
patterns: [
50+
path.resolve(__dirname, 'src-example', 'src')
51+
]
52+
})
53+
],
2954

30-
// webpack-dev-server configurations
31-
devServer: {
32-
compress: true,
33-
contentBase: path.join(__dirname, 'dist'),
34-
open: true,
35-
port: 9000
55+
// webpack-dev-server configurations
56+
devServer: {
57+
compress: true,
58+
contentBase: path.join(__dirname, 'dist'),
59+
open: true,
60+
port: 9000
61+
}
3662
}
37-
}
63+
]

0 commit comments

Comments
 (0)