Skip to content

Commit

Permalink
Merge 5b81171 into cee3b3d
Browse files Browse the repository at this point in the history
  • Loading branch information
daltonmatos committed Dec 14, 2017
2 parents cee3b3d + 5b81171 commit dbc7331
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/js/helpers/ajaxWrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fetch from "isomorphic-fetch";
import Util from "./Util";
import PipelineStore from "../plugin/sdk/stores/PipelineStore";

var uniqueCalls = [];
var pipeline = new PipelineStore();

function removeCall(options) {
uniqueCalls.splice(uniqueCalls.indexOf(options.url), 1);
Expand Down Expand Up @@ -47,6 +49,10 @@ var ajaxWrapper = function (opts = {}) {
});
}

options = pipeline.applyFilter(
PipelineStore.PRE_AJAX_REQUEST,
options
);
return fetch(options.url, fetchOptions);
};

Expand Down
4 changes: 4 additions & 0 deletions src/js/plugin/PluginLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import PluginDispatcherProxy from "./PluginDispatcherProxy";

import MarathonService from "./sdk/services/MarathonService";
import MarathonActions from "./sdk/actions/MarathonActions";
import PipelineStore,
{Hooks as PipelineHooks} from "./sdk/stores/PipelineStore";

const PLUGIN_STARTUP_TIMEOUT = 10000; // in ms

Expand Down Expand Up @@ -51,6 +53,8 @@ const PluginLoader = {
config: Object.freeze(config),
MarathonService: MarathonService,
MarathonActions: MarathonActions,
PipelineStore: new PipelineStore(),
PipelineHooks: PipelineHooks,
});

let dispatchToken = PluginDispatcher.register(function (event) {
Expand Down
44 changes: 44 additions & 0 deletions src/js/plugin/sdk/stores/PipelineStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

export const FILTERS_PIPELINE = {};
const PRE_AJAX_REQUEST = "PRE_AJAX_REQUEST";

export const Hooks = {
PRE_AJAX_REQUEST: PRE_AJAX_REQUEST
};

export default class PipelineStore {

applyFilter(filterName, filterParameters) {
if (FILTERS_PIPELINE[filterName]) {
for (let filter of FILTERS_PIPELINE[filterName]) {
filter(filterParameters);
}
}
return filterParameters;
};

register(filterName, callback) {

if (!FILTERS_PIPELINE[filterName]) {
FILTERS_PIPELINE[filterName] = [];
}

let callBacklist = FILTERS_PIPELINE[filterName];
if (callBacklist.indexOf(callback) === -1) {
FILTERS_PIPELINE[filterName].push(callback);
}
};

deregister(filterName, callback) {
var callbckPosition = FILTERS_PIPELINE[filterName].indexOf(callback);
if (callbckPosition !== -1) {
FILTERS_PIPELINE[filterName].splice(callbckPosition, 1);
}
};

getPipeline() {
return FILTERS_PIPELINE;
};

};

102 changes: 102 additions & 0 deletions src/test/units/PipelineStore.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {expect} from "chai";
import PipelineStore, {Hooks} from "../../js/plugin/sdk/stores/PipelineStore"
import {FILTERS_PIPELINE} from "../../js/plugin/sdk/stores/PipelineStore"

describe("Pipeline tests", function () {

beforeEach(function (){
for (let a of Object.keys(FILTERS_PIPELINE)){
delete FILTERS_PIPELINE[a];
}
});

it("should add a new function to the pipeline", function () {
var filter = function() {};

let pipeline = new PipelineStore();
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.be.equal(undefined);
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter);
});

it("should add multiple functions to the pipeline", function () {
var filter = function() {};
var filter2 = function() {};

let pipeline = new PipelineStore();
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.be.equal(undefined);
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter);
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter2);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(2);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][1]).to.be.equal(filter2);
});

it("should remove a function from the pipeline", function () {
var filter = function() {};

let pipeline = new PipelineStore();
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.be.equal(undefined);

pipeline.register(Hooks.PRE_AJAX_REQUEST, filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1);

pipeline.deregister(Hooks.PRE_AJAX_REQUEST, filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(0);
});

it("should not error when removing the same function twice", function () {
var filter = function() {};

let pipeline = new PipelineStore();
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter);

pipeline.deregister(Hooks.PRE_AJAX_REQUEST, filter);
pipeline.deregister(Hooks.PRE_AJAX_REQUEST, filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(0);
});

it("should not add the same function twice", function () {
var filter = function() {};

let pipeline = new PipelineStore();
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter);
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1);
expect(FILTERS_PIPELINE[Hooks.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";
};

let pipeline = new PipelineStore();
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter_one);
pipeline.register(Hooks.PRE_AJAX_REQUEST, filter_two);
let filtered_data = pipeline.applyFilter(Hooks.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 pipeline = new PipelineStore();
let filtered_data = pipeline.applyFilter(Hooks.PRE_AJAX_REQUEST, {value: "initial"});
expect(filtered_data.value).to.be.equal("initial");
});

it ("should share the same filters between multiple pipeline instances", function (){
let filter = function (){};

let pipeline_one = new PipelineStore();
let pipeline_two = new PipelineStore();
pipeline_one.register(Hooks.PRE_AJAX_REQUEST, filter);
pipeline_two.register(Hooks.PRE_AJAX_REQUEST, filter);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST]).to.have.lengthOf(1);
expect(FILTERS_PIPELINE[Hooks.PRE_AJAX_REQUEST][0]).to.be.equal(filter);
});

});

0 comments on commit dbc7331

Please sign in to comment.