Namespace: reearth.plugins

Methods:

instances:

Description: This property holds an array of PluginExtensionInstance objects. This array represents the instances of various plugin extensions that are currently active or have been utilized within a Reearth project. This property is essential for managing and tracking the active plugin extensions that are currently loaded and running in the environment. It provides details such as the plugin's identity, its type, and how often it has been activated or run, facilitating comprehensive oversight and operational control over the plugins.

Syntax: reearth.plugins.instances:PluginExtensionInstance[];

Type:

PluginExtensionInstance[] 

// An array of PluginExtensionInstance objects, each representing an instance of a plugin extension active in the project.

Type Definitions:

PluginExtensionInstance: //Represents an individual instantiation of a plugin extension, detailed as follows:
id: string //A unique identifier for this particular instance of the plugin extension.
pluginId: string // The identifier of the plugin to which this extension belongs.
name: string // The name of the plugin extension, typically used for display or identification purposes.
extensionId: string // A unique identifier for the extension within the plugin.
extensionType: "widget" | "block" // Specifies the type of the extension, which can be either a 'widget' or a 'block', indicating how the extension is utilized within the Reearth UI.
runTimes: number | undefined // Optionally records the number of times the plugin extension has been run, which can be useful for usage tracking or performance analysis.

<aside> 💡 For extensionType, please find more information on widget here, and more information on block here.

</aside>

Example Usage:

// Log details about each active plugin extension instance
reearth.plugins.instances.forEach(instance => {
  console.log(`Plugin Instance ID: ${instance.id}`);
  console.log(`Plugin ID: ${instance.pluginId}`);
  console.log(`Extension Name: ${instance.name}`);
  console.log(`Extension Type: ${instance.extensionType}`);
  console.log(`Run Times: ${instance.runTimes || 'Not tracked'}`);
});

// Example use case: Find all widget type extensions currently active
const activeWidgets = reearth.plugins.instances.filter(instance => instance.extensionType === "widget");
console.log(`Active Widgets Count: ${activeWidgets.length}`);

Examples Without Comments

postMessage:

Description: This is an optional property that facilitates communication between the main application and individual plugin instances. This method allows the sending of messages from the Reearth environment to a specific plugin, identified by its unique instance ID. It is designed to support dynamic interactions and data exchange between the core application and plugins, enhancing the plugins' functionality and integration within the platform. This function takes two parameters: An ID which is a string, and a Message, which can be of any data type.

Syntax: reearth.plugins.postMessage?: (id: string, message: any) => void;

Type:

id: string // The unique identifier of the plugin instance to which the message should be sent. This ID ensures that the message is directed to the correct recipient within the multitude of possibly active plugins.
message: any // The message to be sent to the plugin. This can be of any type and contain any content, depending on the requirements of the plugin and the purpose of the message.

Return Value: None (void). The method performs its operation without returning a value.

Example Usage:

1. // Define the plugin instance ID and the message to be sent
const pluginInstanceId = "plugin123";
const messageData = { command: "update", content: "New data available" };

// Send a message to the specified plugin instance
reearth.plugins.postMessage(pluginInstanceId, messageData);

// Log the action for confirmation
console.log(`Message sent to plugin instance ${pluginInstanceId}`);

2. // Function to send a configuration update to a plugin
function updatePluginConfiguration(pluginId, config) {
  reearth.plugins.postMessage(pluginId, {
    type: 'update-config',
    data: config
  });
  console.log(`Configuration update sent to plugin ${pluginId}`);
}

// Example configuration data
const newConfig = { color: "blue", size: "large" };

// Send configuration to a plugin with ID 'plugin123'
updatePluginConfiguration('01hvr4h8431rfx7e13f2g8nffr', newConfig);

Examples Without Comments