Skip to content

Commit a29aba0

Browse files
authored
Merge pull request phaserjs#3859 from samme/feature/plugin-data
Pass `data` value to global plugins
2 parents eb30a21 + 4c23359 commit a29aba0

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/boot/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ var Config = new Class({
502502
*
503503
* plugins: {
504504
* global: [
505-
* { key: 'TestPlugin', plugin: TestPlugin, start: true },
505+
* { key: 'TestPlugin', plugin: TestPlugin, start: true, data: { msg: 'The plugin is alive' } },
506506
* ],
507507
* scene: [
508508
* { key: 'WireFramePlugin', plugin: WireFramePlugin, systemKey: 'wireFramePlugin', sceneKey: 'wireframe' }

src/plugins/BasePlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ var BasePlugin = new Class({
8080
*
8181
* @method Phaser.Plugins.BasePlugin#init
8282
* @since 3.8.0
83+
*
84+
* @param {?any} [data] - A value specified by the user, if any, from the `data` property of the plugin's configuration object (if started at game boot) or passed in the PluginManager's `install` method (if started manually).
8385
*/
8486
init: function ()
8587
{

src/plugins/PluginCache.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ PluginCache.register = function (key, plugin, mapping, custom)
6161
* @param {string} key - A reference used to get this plugin from the plugin cache.
6262
* @param {function} plugin - The plugin to be stored. Should be the core object, not instantiated.
6363
* @param {string} mapping - If this plugin is to be injected into the Scene Systems, this is the property key map used.
64+
* @param {?any} data - A value to be passed to the plugin's `init` method.
6465
*/
65-
PluginCache.registerCustom = function (key, plugin, mapping)
66+
PluginCache.registerCustom = function (key, plugin, mapping, data)
6667
{
67-
customPlugins[key] = { plugin: plugin, mapping: mapping };
68+
customPlugins[key] = { plugin: plugin, mapping: mapping, data: data };
6869
};
6970

7071
/**

src/plugins/PluginManager.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ var PluginManager = new Class({
146146
var plugin;
147147
var start;
148148
var mapping;
149+
var data;
149150
var config = this.game.config;
150151

151152
// Any plugins to install?
@@ -158,16 +159,17 @@ var PluginManager = new Class({
158159
{
159160
entry = list[i];
160161

161-
// { key: 'TestPlugin', plugin: TestPlugin, start: true, mapping: 'test' }
162+
// { key: 'TestPlugin', plugin: TestPlugin, start: true, mapping: 'test', data: { msg: 'The plugin is alive' } }
162163

163164
key = GetFastValue(entry, 'key', null);
164165
plugin = GetFastValue(entry, 'plugin', null);
165166
start = GetFastValue(entry, 'start', false);
166167
mapping = GetFastValue(entry, 'mapping', null);
168+
data = GetFastValue(entry, 'data', null);
167169

168170
if (key && plugin)
169171
{
170-
this.install(key, plugin, start, mapping);
172+
this.install(key, plugin, start, mapping, data);
171173
}
172174
}
173175

@@ -399,11 +401,13 @@ var PluginManager = new Class({
399401
* @param {function} plugin - The plugin code. This should be the non-instantiated version.
400402
* @param {boolean} [start=false] - Automatically start the plugin running? This is always `true` if you provide a mapping value.
401403
* @param {string} [mapping] - If this plugin is injected into the Phaser.Scene class, this is the property key to use.
404+
* @param {any} [data] - A value passed to the plugin's `init` method.
402405
*/
403-
install: function (key, plugin, start, mapping)
406+
install: function (key, plugin, start, mapping, data)
404407
{
405408
if (start === undefined) { start = false; }
406409
if (mapping === undefined) { mapping = null; }
410+
if (data === undefined) { data = null; }
407411

408412
if (typeof plugin !== 'function')
409413
{
@@ -424,12 +428,12 @@ var PluginManager = new Class({
424428

425429
if (!this.game.isBooted)
426430
{
427-
this._pendingGlobal.push({ key: key, plugin: plugin, start: start, mapping: mapping });
431+
this._pendingGlobal.push({ key: key, plugin: plugin, start: start, mapping: mapping, data: data });
428432
}
429433
else
430434
{
431435
// Add it to the plugin store
432-
PluginCache.registerCustom(key, plugin, mapping);
436+
PluginCache.registerCustom(key, plugin, mapping, data);
433437

434438
if (start)
435439
{
@@ -568,12 +572,13 @@ var PluginManager = new Class({
568572
key: runAs,
569573
plugin: instance,
570574
active: true,
571-
mapping: entry.mapping
575+
mapping: entry.mapping,
576+
data: entry.data
572577
};
573578

574579
this.plugins.push(entry);
575580

576-
instance.init();
581+
instance.init(entry.data);
577582
instance.start();
578583
}
579584

0 commit comments

Comments
 (0)