---
agentTools:
  projectIndex: https://whsjs.readme.io/llms.txt
---

# ModuleSystem

[block:callout]
{
  "type": "success",
  "body": "`.on()`, `.off()`, `.emit()` methods are supported",
  "title": "This class includes basic event system"
}
[/block]

`ModuleSystem` provides API for classes that will use [Modules](https://whsjs.readme.io/docs/modules-1)

[block:api-header]
{
  "type": "fn",
  "title": ".integrateModules(source[optional])"
}
[/block]

This method applies all modules from `.modules` collection. If `source` (should be a component) is provided - will replace `.modules` with `source`'s one before executing modules.

[block:callout]
{
  "type": "success",
  "body": "",
  "title": "This method is frequently used in core"
}
[/block]

[block:api-header]
{
  "type": "fn",
  "title": ".applyBridge(bridgeMap)"
}
[/block]

`.applyBridge()` makes component-specific API to work with modules. Let's say your bridgeMap is:

[block:code]
{
  "codes": [
    {
      "code": "{\n  \"geometry\": new THREE.Geometry()\n}",
      "language": "json"
    }
  ]
}
[/block]

Then `.applyBridge()` will pass this value (`THREE.Geometry`) to first module if it has the following `bridge` in it's structure.

[block:callout]
{
  "type": "info",
  "title": "Helper",
  "body": "Remember that `bridge` that we talk about is a function that has `input` and `output`. `bridge` modifies your `input` and returns you an `output`."
}
[/block]

The following implementation allows modules access your geometry & material objects and modify them and is strongly recommended if you want to get maximum flexibility from **WhitestormJS**.

[block:code]
{
  "codes": [
    {
      "code": "// ...\n\nexport default class BasicSphere extends MeshComponent {\n  build() {\n    const {geometry, material} = this.applyBridge({\n      geometry: new THREE.SphereGeometry(3, 16, 16),\n      material: new THREE.MeshBasicMaterial({color: 0xff0000})\n    })\n    \n    return new THREE.Mesh(\n      geometry,\n      material\n    );\n  }\n}",
      "language": "javascript",
      "name": "BasicSphere.js"
    }
  ]
}
[/block]

[block:callout]
{
  "type": "success",
  "body": "Returns object of modified values."
}
[/block]

[block:api-header]
{
  "type": "fn",
  "title": ".applyModule(module, push = true)"
}
[/block]

`.applyModule` is also used in `.integrateModules()` function. It does exactly what its name says (applies module to component or app).

[block:code]
{
  "codes": [
    {
      "code": "app.applyModule(new WHS.controls.OrbitModule()); // OrbitControls.",
      "language": "javascript",
      "name": "app.js"
    }
  ]
}
[/block]

[block:callout]
{
  "type": "success",
  "body": "Returns module that was applied."
}
[/block]

[block:api-header]
{
  "type": "fn",
  "title": ".module()"
}
[/block]

[block:callout]
{
  "type": "info",
  "title": "",
  "body": "`.module()` is a piped version of `.applyModule()`. It returns `this` - app/component."
}
[/block]

[block:code]
{
  "codes": [
    {
      "code": "component\n  .module(new Module1())\n  .module(new Module2())\n  .module(new Module3())",
      "language": "javascript"
    }
  ]
}
[/block]