MCPcopy Create free account
hub / github.com/melonjs/melonJS / register

Function register

packages/melonjs/src/plugin/plugin.ts:92–123  ·  view source on GitHub ↗
(
	pluginClass: new (...args: any[]) => BasePlugin,
	name?: string,
	...args: any[]
)

Source from the content-addressed store, hash-verified

90 * me.plugin.cache.testPlugin.myfunction ();
91 */
92export function register(
93 pluginClass: new (...args: any[]) => BasePlugin,
94 name?: string,
95 ...args: any[]
96): void {
97 // derive name from class if not provided
98 const pluginName =
99 name || pluginClass.name || pluginClass.toString().match(/ (\w+)/)![1];
100
101 // ensure me.plugins[name] is not already "used"
102 if (cache[pluginName]) {
103 throw new Error(`plugin ${pluginName} already registered`);
104 }
105
106 // try to instantiate the plugin
107 const instance = new pluginClass(...args);
108
109 // inheritance check
110 if (typeof instance === "undefined" || !(instance instanceof BasePlugin)) {
111 throw new Error("Plugin should extend the BasePlugin Class !");
112 }
113
114 // compatibility testing
115 if (checkVersion(instance.version, version) > 0) {
116 throw new Error(
117 `Plugin version mismatch, expected: ${instance.version}, got: ${version}`,
118 );
119 }
120
121 // create a reference to the new plugin
122 cache[pluginName] = instance;
123}
124
125/**
126 * returns the plugin instance with the specified class type or registered name

Callers

nothing calls this directly

Calls 2

checkVersionFunction · 0.90
toStringMethod · 0.45

Tested by

no test coverage detected