-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from lppjunior/develop
Add pipeline pattern
- Loading branch information
Showing
16 changed files
with
163 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function testMiddleware () { | ||
const middleware = new Middleware() | ||
middleware.use((param1, param2, next) => { | ||
param1.test += ' UPDATED' | ||
console.log('[middleware] ', param1, param2) | ||
next(param1, param2) | ||
}) | ||
|
||
middleware.use((param1, param2, next) => { | ||
param2 = param2 + ' UPDATED' | ||
console.log('[middleware] ', param1, param2) | ||
next(param1, param2) | ||
}) | ||
|
||
middleware.use((param1, param2, next) => { | ||
console.log('[middleware] ', param1, param2) | ||
next(param1, param2) | ||
}) | ||
|
||
middleware.process({ test: 123, value: 'aaa' }, 'test') | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', testMiddleware, false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function testPipeline () { | ||
const result = (new Pipeline()) | ||
.pipe((data) => { | ||
console.log('[pipeline] ', data + ' + ' + 5) | ||
return data + 5 | ||
}) | ||
.pipe((data) => { | ||
console.log('[pipeline] ', data + ' * ' + 3) | ||
return data * 3 | ||
}) | ||
.pipe((data) => { | ||
console.log('[pipeline] ', data + ' + ' + 150) | ||
return data + 150 | ||
}) | ||
.process(5) | ||
|
||
console.log('[pipeline] result: ', result) | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', testPipeline, false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import Middleware from './pattern/middleware' | ||
import Observer from './pattern/observer' | ||
import Pipeline from './pattern/pipeline' | ||
|
||
export { | ||
Observer | ||
Middleware, | ||
Observer, | ||
Pipeline | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Pipeline { | ||
constructor () { | ||
this.pipelines = [] | ||
} | ||
|
||
pipe (callback) { | ||
this.pipelines.push(callback) | ||
return this | ||
} | ||
|
||
process (data) { | ||
let result = data | ||
return this.pipelines.map(fn => (result = fn(result))).pop() | ||
} | ||
} | ||
|
||
export default Pipeline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Pipeline from '../Pipeline' | ||
|
||
describe('Test Pipeline call all methods', () => { | ||
const pipeline = new Pipeline() | ||
|
||
test('should assert method pipeline.pipe was called', () => { | ||
jest.spyOn(pipeline, 'pipe') | ||
pipeline.pipe(() => {}) | ||
expect(pipeline.pipe).toHaveBeenCalled() | ||
}) | ||
|
||
test('should assert method pipeline.process was called', () => { | ||
jest.spyOn(pipeline, 'process') | ||
pipeline.process(() => {}) | ||
expect(pipeline.process).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('Test Pipeline call perfect scenaries', () => { | ||
const result = (new Pipeline()) | ||
.pipe((data) => { | ||
test('Data should be 5', () => { | ||
expect(data).toBe(5) | ||
}) | ||
|
||
return data + 5 | ||
}) | ||
.pipe((data) => { | ||
test('Data should be 10', () => { | ||
expect(data).toBe(10) | ||
}) | ||
|
||
return data * 2 | ||
}) | ||
.process(5) | ||
|
||
test('Result should be 20', () => { | ||
expect(result).toBe(20) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Pipeline from './Pipeline' | ||
|
||
export default Pipeline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters