Skip to content

Lifecycle

Static Badge Static Badge

A module instance (a map viewer or a 3D viewer) holds resources that are not released when you drop your reference to it. When you are done with an instance, destroy it explicitly with destroy().

Destroying a module

destroy()

Asynchronous method that permanently destroys the module instance and releases every resource it holds.

  • Asynchronous. Resolves once the module is destroyed and its resources released.
  • Safe to call again. A second call does nothing.
  • Single-use. Do not call anything else on the instance afterward.

Output:

  • promise (Promise): resolves once the module is destroyed.

Example:

1
2
3
4
5
6
7
import { loadModule } from "@3ddv/dvm";

var viewer = await loadModule("map_viewer", { container: "container-id" });
await viewer.loadMap({ venue_id: "my-venue" });

// ...later, when the viewer is no longer needed:
await viewer.destroy();

Warning

This method is asynchronous, and the instance is unusable after it resolves.

isDestroyed()

Returns true once destroy() has been called on the instance. Use it to guard late-arriving callbacks or deferred work that might run after the module is destroyed.

Output:

  • destroyed (boolean): true if destroy() has been called.

Example:

1
2
3
if (!viewer.isDestroyed()) {
    viewer.setAvailability(nodes, true);
}

getModuleId()

Returns the unique per-instance id of the module. Use it to look the instance back up through the registry (below).

Output:

  • module_id (string): the unique per-instance id.

Example:

var id = viewer.getModuleId();

The live-module registry

Static Badge

The module manager keeps a registry of every module that is currently live, registered on construction and removed automatically once its destroy() resolves. Query it through the functions exported from @3ddv/dvm (and @3ddv/dvm-internal); all three are synchronous.

Function Returns Notes
getModules() Module[] Every top-level live module.
getModuleById(id) Module \| undefined Single instance by its getModuleId() value.
getModulesByType(type) Module[] Live modules of a type, e.g. "map_viewer".

getModules()

Returns every top-level module currently live in the registry. Synchronous.

Output:

  • modules (Module[]): the live top-level modules (empty array when none are loaded).

Example:

1
2
3
import { getModules } from "@3ddv/dvm";

getModules(); // returns [ map_viewer instance, 3d_viewer instance, ... ]

getModuleById()

Looks up a single live module by its per-instance id (the value returned by getModuleId()). Synchronous.

Input:

  • module_id (string): the per-instance module id to resolve.

Output:

  • module (Module | undefined): the matching module, or undefined if no live module has that id.

Example:

1
2
3
import { getModuleById } from "@3ddv/dvm";

getModuleById(viewer.getModuleId()); // returns the viewer, or undefined once destroyed

getModulesByType()

Returns every live module of a given type, e.g. "map_viewer" or "3d_viewer". Synchronous.

Input:

  • module_type (string): the module type to filter by.

Output:

  • modules (Module[]): the live modules of that type (empty array when none match).

Example:

1
2
3
import { getModulesByType } from "@3ddv/dvm";

getModulesByType("3d_viewer"); // returns [ 3d_viewer instance, ... ]

The registry only lists modules that are still live, so you can use it to destroy every module your application has open:

1
2
3
4
import { getModules } from "@3ddv/dvm";

await Promise.all(getModules().map(function (m) { return m.destroy(); }));
// getModules() is now empty.

Warning

Destroy what you discard. The registry keeps every module alive until its destroy() resolves, so a module discarded without destroy() leaks. Any code that creates and discards modules (route changes, component unmount, venue swaps) must call destroy().

Create / destroy / reload pattern

To swap what a module shows, prefer the in-place loaders (loadMap / loadView3d): they reuse the same instance. Go for destroy-and-recreate only when you are removing the module from the page entirely (e.g. a single-page-app route leaving the view):

import { loadModule } from "@3ddv/dvm";

var viewer = null;

async function mount(containerId, venue_id) {
    viewer = await loadModule("map_viewer", { container: containerId });
    await viewer.loadMap({ venue_id });
}

async function unmount() {
    if (viewer && !viewer.isDestroyed()) {
        await viewer.destroy();
    }
    viewer = null;
}

Further reading

  • Common API: methods shared by every module, including destroy(), isDestroyed(), and getModuleId().
  • map_viewer API: full reference for the map-viewer instance.
  • 3d_viewer API: full reference for the 3d-viewer instance.