Skip to content

Commit

Permalink
refactor plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Aug 3, 2016
1 parent e84c537 commit 8da170f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 207 deletions.
26 changes: 10 additions & 16 deletions examples/plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(function() {

var chain = Matter.Common.chain,
last = Matter.Common.last;
var Common = Matter.Common;

var MatterPlugin = {
name: 'matter-plugin',
Expand All @@ -11,12 +10,7 @@
for: 'matter-js@^0.10.0',

uses: [
{
plugin: 'matter-plugin-2@^0.0.1',
options: {
message: 'hello'
}
},
'matter-plugin-2@^0.1.1',
'matter-plugin-3@^0.10.0'
],

Expand All @@ -25,25 +19,25 @@
},

install: function(base) {
base.Engine.create = chain(
base.Engine.create = Common.chain(
Matter.Engine.create,
MatterPlugin.engineCreate
MatterPlugin._engineCreate
);

base.Body.create = chain(
MatterPlugin.bodyCreate,
base.Body.create = Common.chain(
MatterPlugin._bodyCreate,
Matter.Body.create
);
},

engineCreate: function(element, options, engine) {
engine = last(arguments);
_engineCreate: function(element, options) {
var engine = this;

console.log('2nd patched engine create!', engine);
},

bodyCreate: function(options) {
console.log('patched body create!', arguments);
_bodyCreate: function(options) {
console.log('patched body create!', options);
}
};

Expand Down
14 changes: 8 additions & 6 deletions examples/plugin2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {

var chain = Matter.Common.chain;
var Common = Matter.Common;

var MatterPlugin2 = {
name: 'matter-plugin-2',
Expand All @@ -9,21 +9,23 @@

for: 'matter-js@^0.10.0',

uses: ['matter-plugin'],
uses: ['matter-plugin-fake'],

options: {
thing: 1
},

install: function(matter) {
matter.Engine.create = chain(
matter.Engine.create = Common.chain(
matter.Engine.create,
MatterPlugin2.engineCreate
MatterPlugin2._engineCreate
);
},

engineCreate: function(element, options, engine) {
console.log('patched engine create!', arguments);
_engineCreate: function(element, options) {
var engine = this;

console.log('patched engine create!', engine);
}
};

Expand Down
33 changes: 16 additions & 17 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ module.exports = Common;
Common.isPlainObject = function(obj) {
return typeof obj === 'object' && obj.constructor === Object;
};

/**
* Returns true if the object is a string.
* @method isString
* @param {object} obj
* @return {boolean} True if the object is a string, otherwise false
*/
Common.isString = function(obj) {
return toString.call(obj) === '[object String]';
};

/**
* Returns the given value clamped between a minimum and maximum value.
Expand Down Expand Up @@ -361,16 +371,6 @@ module.exports = Common;
return mapped;
};

/**
* Returns the last value in an array.
* @method last
* @param {array} list
* @return {} The last value in list.
*/
Common.last = function(list) {
return list[list.length - 1];
};

/**
* Takes a directed graph and returns the partially ordered set of vertices in topological order.
* Circular dependencies are allowed.
Expand Down Expand Up @@ -418,10 +418,10 @@ module.exports = Common;

/**
* Takes _n_ functions as arguments and returns a new function that calls them in order.
* The arguments and `this` value applied when calling the new function will be applied to every function passed.
* An additional final argument is passed to provide access to the last value returned (that was not `undefined`).
* Therefore if a passed function does not return a value, the previously returned value is passed as the final argument.
* After all passed functions have been called the new function returns the final value (if any).
* The arguments applied when calling the new function will also be applied to every function passed.
* The value of `this` refers to the last value returned in the chain that was not `undefined`.
* Therefore if a passed function does not return a value, the previously returned value is maintained.
* After all passed functions have been called the new function returns the last returned value (if any).
* @method chain
* @param ...funcs {function} The functions to chain.
* @return {function} A new function that calls the passed functions in order.
Expand All @@ -430,11 +430,10 @@ module.exports = Common;
var funcs = Array.prototype.slice.call(arguments);

return function() {
var args = Array.prototype.slice.call(arguments),
lastResult;
var lastResult;

for (var i = 0; i < funcs.length; i += 1) {
var result = funcs[i].apply(this, lastResult ? args.concat(lastResult) : args);
var result = funcs[i].apply(lastResult, arguments);

if (typeof result !== 'undefined') {
lastResult = result;
Expand Down
Loading

0 comments on commit 8da170f

Please sign in to comment.