Skip to content

Commit 3883981

Browse files
committed
added docs for Matter.Plugin with tweaks
1 parent 51b7b1d commit 3883981

5 files changed

Lines changed: 207 additions & 69 deletions

File tree

examples/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242
};
4343

44-
Matter.Plugin.exports(MatterPlugin);
44+
Matter.Plugin.register(MatterPlugin);
4545

4646
window.MatterPlugin = MatterPlugin;
4747

examples/plugin2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
}
3030
};
3131

32-
Matter.Plugin.exports(MatterPlugin2);
32+
Matter.Plugin.register(MatterPlugin2);
3333

3434
window.MatterPlugin2 = MatterPlugin2;
3535

src/core/Common.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ module.exports = Common;
303303
/**
304304
* The console logging level to use, where each level includes all levels above and excludes the levels below.
305305
* The default level is 'debug' which shows all console messages.
306+
*
306307
* Possible level values are:
307308
* - 0 = None
308309
* - 1 = Debug
@@ -317,7 +318,7 @@ module.exports = Common;
317318

318319
/**
319320
* Shows a `console.log` message only if the current `Common.logLevel` allows it.
320-
* The message will be prefixed with 'Matter.js' to make it easily identifiable.
321+
* The message will be prefixed with 'matter-js' to make it easily identifiable.
321322
* @method log
322323
* @param ...objs {} The objects to log.
323324
*/
@@ -329,7 +330,7 @@ module.exports = Common;
329330

330331
/**
331332
* Shows a `console.info` message only if the current `Common.logLevel` allows it.
332-
* The message will be prefixed with 'Matter.js' to make it easily identifiable.
333+
* The message will be prefixed with 'matter-js' to make it easily identifiable.
333334
* @method info
334335
* @param ...objs {} The objects to log.
335336
*/
@@ -341,7 +342,7 @@ module.exports = Common;
341342

342343
/**
343344
* Shows a `console.warn` message only if the current `Common.logLevel` allows it.
344-
* The message will be prefixed with 'Matter.js' to make it easily identifiable.
345+
* The message will be prefixed with 'matter-js' to make it easily identifiable.
345346
* @method warn
346347
* @param ...objs {} The objects to log.
347348
*/
@@ -451,14 +452,27 @@ module.exports = Common;
451452
* The value of `this` refers to the last value returned in the chain that was not `undefined`.
452453
* Therefore if a passed function does not return a value, the previously returned value is maintained.
453454
* After all passed functions have been called the new function returns the last returned value (if any).
455+
* If any of the passed functions are a chain, then the chain will be flattened.
454456
* @method chain
455457
* @param ...funcs {function} The functions to chain.
456458
* @return {function} A new function that calls the passed functions in order.
457459
*/
458460
Common.chain = function() {
459-
var funcs = Array.prototype.slice.call(arguments);
461+
var args = Array.prototype.slice.call(arguments),
462+
funcs = [];
460463

461-
return function() {
464+
for (var i = 0; i < args.length; i += 1) {
465+
var func = args[i];
466+
467+
if (func._chained) {
468+
// flatten already chained functions
469+
funcs.push.apply(funcs, func._chained);
470+
} else {
471+
funcs.push(func);
472+
}
473+
}
474+
475+
var chain = function() {
462476
var lastResult;
463477

464478
for (var i = 0; i < funcs.length; i += 1) {
@@ -471,6 +485,10 @@ module.exports = Common;
471485

472486
return lastResult;
473487
};
488+
489+
chain._chained = funcs;
490+
491+
return chain;
474492
};
475493

476494
})();

src/core/Matter.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* The `Matter` module is the top level namespace and includes functions for extending other modules.
2+
* The `Matter` module is the top level namespace. It also includes a function for installing plugins on top of the library.
33
*
44
* @class Matter
55
*/
@@ -14,47 +14,46 @@ var Plugin = require('./Plugin');
1414

1515
/**
1616
* The library name.
17-
* @property Matter.name
17+
* @property name
18+
* @readOnly
1819
* @type {String}
1920
*/
2021
Matter.name = 'matter-js';
2122

2223
/**
2324
* The library version.
24-
* @property Matter.version
25+
* @property version
26+
* @readOnly
2527
* @type {String}
2628
*/
2729
Matter.version = 'master';
2830

2931
/**
30-
* The plugins that have been _installed_ through `Matter.Plugin.install`. Read only.
31-
* @property Matter.used
32-
* @readOnly
32+
* A list of plugin dependencies to be installed. These are normally set and installed through `Matter.use`.
33+
* Alternatively you may set `Matter.uses` manually and install them by calling `Plugin.use(Matter)`.
34+
* @property uses
3335
* @type {Array}
3436
*/
35-
Matter.used = [];
37+
Matter.uses = [];
3638

3739
/**
38-
* A list of plugin dependencies to be installed. These are normally set and installed through `Matter.use`.
39-
* Alternatively set them and install manually through `Plugin.installDependencies`.
40-
* @property Matter.used
40+
* The plugins that have been installed through `Matter.Plugin.install`. Read only.
41+
* @property used
4142
* @readOnly
4243
* @type {Array}
4344
*/
44-
Matter.uses = [];
45+
Matter.used = [];
4546

4647
/**
47-
* Installs plugins on the `Matter` namespace.
48-
* Populates `Matter.used` with an array of the plugins in the order they were applied after dependencies were resolved.
49-
* See `Common.use` in `Matter.Common` for more information.
50-
* TODO: add link to wiki
48+
* Installs the given plugins on the `Matter` namespace.
49+
* This is a short-hand for `Plugin.use`, see it for more information.
50+
* Call this function once at the start of your code, with all of the plugins you wish to install as arguments.
51+
* Avoid calling this function multiple times unless you intend to manually control installation order.
5152
* @method use
52-
* @param ...plugins {Function} The plugins to install on `base`.
53+
* @param ...plugin {Function} The plugin(s) to install on `base` (multi-argument).
5354
*/
5455
Matter.use = function() {
55-
Matter.uses = Array.prototype.slice.call(arguments);
56-
57-
Plugin.installDependencies(Matter);
56+
Plugin.use(Matter, Array.prototype.slice.call(arguments));
5857
};
5958

6059
})();

0 commit comments

Comments
 (0)