-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
0 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
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,48 @@ | ||
|
||
class PipelineModel { | ||
|
||
constructor() { | ||
this.pipelines = new Map(); | ||
}; | ||
|
||
clearPipeline() { | ||
this.pipelines.clear(); | ||
}; | ||
|
||
applyFilter(operationName, filterParameters) { | ||
const pipeline = this.getPipeline(operationName); | ||
|
||
pipeline.forEach((filter) => { | ||
filter(filterParameters); | ||
}); | ||
return filterParameters; | ||
}; | ||
|
||
register(operationName, callback) { | ||
|
||
const pipeline = this.getPipeline(operationName); | ||
|
||
if (pipeline.indexOf(callback) === -1) { | ||
pipeline.push(callback); | ||
} | ||
}; | ||
|
||
deregister(operationName, callback) { | ||
const pipeline = this.getPipeline(operationName); | ||
|
||
var callbckPosition = pipeline.indexOf(callback); | ||
if (callbckPosition !== -1) { | ||
pipeline.splice(callbckPosition, 1); | ||
} | ||
}; | ||
|
||
getPipeline(pipelineName) { | ||
if (!this.pipelines.has(pipelineName)) { | ||
this.pipelines.set(pipelineName, []); | ||
} | ||
return this.pipelines.get(pipelineName); | ||
}; | ||
|
||
}; | ||
|
||
export default new PipelineModel(); |
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,8 @@ | ||
|
||
const PRE_AJAX_REQUEST = "PRE_AJAX_REQUEST"; | ||
|
||
const PipelineOperations = { | ||
PRE_AJAX_REQUEST | ||
}; | ||
|
||
export default PipelineOperations; |
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,21 @@ | ||
import PipelineModel from "./PipelineModel"; | ||
|
||
export default class PipelineStore { | ||
|
||
constructor() { | ||
Object.defineProperty(this, "pipeline", {value: PipelineModel}); | ||
}; | ||
|
||
applyFilter(operationName, filterParameters) { | ||
return this.pipeline.applyFilter(operationName, filterParameters); | ||
}; | ||
|
||
register(operationName, callback) { | ||
return this.pipeline.register(operationName, callback); | ||
}; | ||
|
||
deregister(operationName, callback) { | ||
return this.deregister(operationName, callback); | ||
}; | ||
|
||
}; |
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,96 @@ | ||
import {expect} from "chai"; | ||
import pipelineModel from "../../js/plugin/sdk/pipeline/PipelineModel" | ||
|
||
import PipelineOperations from "../../js/plugin/sdk/pipeline/PipelineOperations" | ||
|
||
describe("PipelineModel tests", function () { | ||
|
||
beforeEach(function (){ | ||
pipelineModel.clearPipeline(); | ||
}); | ||
|
||
it("should return an empty list for a non-existant pipeline", function () { | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
}); | ||
|
||
it("should clear the pipeline when requested to", function () { | ||
const filter = function () {}; | ||
pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST).push(filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(1); | ||
pipelineModel.clearPipeline(); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
}); | ||
|
||
it("should add a new function to the pipeline", function () { | ||
const filter = function() {}; | ||
|
||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(1); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[0]).to.be.equal(filter); | ||
}); | ||
|
||
it("should add multiple functions to the pipeline", function () { | ||
var filter = function() {}; | ||
var filter2 = function() {}; | ||
|
||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter2); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(2); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[0]).to.be.equal(filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[1]).to.be.equal(filter2); | ||
}); | ||
|
||
it("should remove a function from the pipeline", function () { | ||
var filter = function() {}; | ||
|
||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
|
||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(1); | ||
|
||
pipelineModel.deregister(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
}); | ||
|
||
it("should not error when removing the same function twice", function () { | ||
var filter = function() {}; | ||
|
||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
|
||
pipelineModel.deregister(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipelineModel.deregister(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
}); | ||
|
||
it("should not add the same function twice", function () { | ||
var filter = function() {}; | ||
|
||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(1); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[0]).to.be.equal(filter); | ||
}); | ||
|
||
it("should call all functions of the chosen pipeline", function () { | ||
var filter_one = function(data) { | ||
data.value = data.value + "_filter_one"; | ||
}; | ||
var filter_two = function(data) { | ||
data.value = data.value + "_filter_two"; | ||
}; | ||
|
||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter_one); | ||
pipelineModel.register(PipelineOperations.PRE_AJAX_REQUEST, filter_two); | ||
let filtered_data = pipelineModel.applyFilter(PipelineOperations.PRE_AJAX_REQUEST, {value: "initial"}); | ||
expect(filtered_data.value).to.be.equal("initial_filter_one_filter_two"); | ||
}); | ||
|
||
it("should be able to run an empty pipeline", function () { | ||
let filtered_data = pipelineModel.applyFilter(PipelineOperations.PRE_AJAX_REQUEST, {value: "initial"}); | ||
expect(filtered_data.value).to.be.equal("initial"); | ||
}); | ||
|
||
}); | ||
|
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,25 @@ | ||
import {expect} from "chai"; | ||
import PipelineStore from "../../js/plugin/sdk/pipeline/PipelineStore"; | ||
import pipelineModel from "../../js/plugin/sdk/pipeline/PipelineModel"; | ||
import PipelineOperations from "../../js/plugin/sdk/pipeline/PipelineOperations"; | ||
|
||
describe("PipelineStore tests", function () { | ||
|
||
beforeEach(function (){ | ||
pipelineModel.clearPipeline(); | ||
}); | ||
|
||
it ("should share the same filters between multiple pipeline instances", function (){ | ||
const filter = function (){}; | ||
const filter2 = function (){}; | ||
|
||
const pipeline_one = new PipelineStore(); | ||
const pipeline_two = new PipelineStore(); | ||
pipeline_one.register(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipeline_two.register(PipelineOperations.PRE_AJAX_REQUEST, filter2); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(2); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[0]).to.be.equal(filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[1]).to.be.equal(filter2); | ||
}); | ||
|
||
}); |