-
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
221 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,54 @@ | ||
|
||
export default class PipelineModel { | ||
|
||
constructor() { | ||
this.pipelines = new Map(); | ||
}; | ||
|
||
clearPipeline() { | ||
this.pipelines.clear(); | ||
}; | ||
|
||
applyPipeline(operationName, filterParameters) { | ||
if (!this.pipelines.has(operationName)) { | ||
return filterParameters; | ||
} | ||
|
||
return this.pipelines.get(operationName) | ||
.reduce((accumulator, pipelineCallback) => { | ||
try { | ||
return pipelineCallback(accumulator); | ||
} catch (error) { | ||
// We might want to log a proper error to help users debug | ||
return accumulator; | ||
} | ||
}, filterParameters); | ||
}; | ||
|
||
registerOperator(operationName, callback) { | ||
|
||
const pipeline = this.getPipeline(operationName); | ||
|
||
if (pipeline.indexOf(callback) === -1) { | ||
pipeline.push(callback); | ||
} | ||
}; | ||
|
||
deregisterOperator(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); | ||
}; | ||
|
||
}; | ||
|
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,23 @@ | ||
import PipelineModel from "./PipelineModel"; | ||
|
||
class PipelineStore { | ||
|
||
constructor() { | ||
Object.defineProperty(this, "pipeline", {value: new PipelineModel()}); | ||
}; | ||
|
||
applyPipeline(operationName, filterParameters) { | ||
return this.pipeline.applyPipeline(operationName, filterParameters); | ||
}; | ||
|
||
registerOperator(operationName, callback) { | ||
return this.pipeline.registerOperator(operationName, callback); | ||
}; | ||
|
||
deregisterOperator(operationName, callback) { | ||
return this.deregisterOperator(operationName, callback); | ||
}; | ||
|
||
}; | ||
|
||
export default new PipelineStore(); |
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,100 @@ | ||
import {expect} from "chai"; | ||
import PipelineModel from "../../js/plugin/sdk/pipeline/PipelineModel" | ||
|
||
import PipelineOperations from "../../js/plugin/sdk/pipeline/PipelineOperations" | ||
|
||
const pipelineModel = new PipelineModel(); | ||
|
||
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 (data) {return data;}; | ||
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(data) {return data;}; | ||
|
||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
pipelineModel.registerOperator(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(data) {return data;}; | ||
var filter2 = function(data) {return data;}; | ||
|
||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
pipelineModel.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipelineModel.registerOperator(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(data) {return data;}; | ||
|
||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(0); | ||
|
||
pipelineModel.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
expect(pipelineModel.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(1); | ||
|
||
pipelineModel.deregisterOperator(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(data) {return data;}; | ||
|
||
pipelineModel.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
|
||
pipelineModel.deregisterOperator(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipelineModel.deregisterOperator(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(data) {return data;}; | ||
|
||
pipelineModel.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipelineModel.registerOperator(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"; | ||
return data; | ||
}; | ||
var filter_two = function(data) { | ||
data.value = data.value + "_filter_two"; | ||
return data; | ||
}; | ||
|
||
pipelineModel.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter_one); | ||
pipelineModel.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter_two); | ||
let filtered_data = pipelineModel.applyPipeline(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.applyPipeline(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 (){ | ||
PipelineStore.pipeline.clearPipeline(); | ||
}); | ||
|
||
it ("should share the same filters between multiple pipeline instances", function (){ | ||
const filter = function (data) {return data;}; | ||
const filter2 = function (data) {return data;}; | ||
|
||
const pipeline_one = PipelineStore; | ||
const pipeline_two = PipelineStore; | ||
pipeline_one.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter); | ||
pipeline_two.registerOperator(PipelineOperations.PRE_AJAX_REQUEST, filter2); | ||
expect(PipelineStore.pipeline.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)).to.have.lengthOf(2); | ||
expect(PipelineStore.pipeline.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[0]).to.be.equal(filter); | ||
expect(PipelineStore.pipeline.getPipeline(PipelineOperations.PRE_AJAX_REQUEST)[1]).to.be.equal(filter2); | ||
}); | ||
|
||
}); |