@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
78 lines (77 loc) • 2.65 kB
JavaScript
"use strict";
import { PolyPlugin } from "./Plugin";
export class PluginsRegister {
constructor(poly) {
this.poly = poly;
this._pluginsByName = /* @__PURE__ */ new Map();
this._pluginNameByNodeContextByType = /* @__PURE__ */ new Map();
this._pluginNameByOperationContextByType = /* @__PURE__ */ new Map();
}
async wrapConfigurePolygonjs(callback) {
this._configurePolygonjsPlugin = this._configurePolygonjsPlugin || new PolyPlugin("configurePolygonjs", () => {
}, { libraryImportPath: "../PolyConfig", libraryName: "" });
this._currentPlugin = this._configurePolygonjsPlugin;
this._pluginsByName.set(this._currentPlugin.name(), this._currentPlugin);
await callback();
this._currentPlugin = void 0;
}
register(plugin) {
const previousCurrentPlugin = this._currentPlugin;
this._currentPlugin = plugin;
this._pluginsByName.set(plugin.name(), plugin);
plugin.init(this.poly);
this._currentPlugin = previousCurrentPlugin;
}
pluginByName(pluginName) {
return this._pluginsByName.get(pluginName);
}
registerNode(node) {
if (!this._currentPlugin) {
return;
}
const context = node.context();
const type = node.type();
let mapForContext = this._pluginNameByNodeContextByType.get(context);
if (!mapForContext) {
mapForContext = /* @__PURE__ */ new Map();
this._pluginNameByNodeContextByType.set(context, mapForContext);
}
mapForContext.set(type, this._currentPlugin.name());
}
registerOperation(operation) {
if (!this._currentPlugin) {
return;
}
const context = operation.context();
const type = operation.type();
let mapForContext = this._pluginNameByOperationContextByType.get(context);
if (!mapForContext) {
mapForContext = /* @__PURE__ */ new Map();
this._pluginNameByOperationContextByType.set(context, mapForContext);
}
mapForContext.set(type, this._currentPlugin.name());
}
toJson() {
const data = {
plugins: {},
nodes: {},
operations: {}
};
this._pluginsByName.forEach((plugin, name) => {
data.plugins[name] = plugin.toJSON();
});
this._pluginNameByNodeContextByType.forEach((mapForContext, context) => {
data.nodes[context] = {};
mapForContext.forEach((pluginName, type) => {
data.nodes[context][type] = pluginName;
});
});
this._pluginNameByOperationContextByType.forEach((mapForContext, context) => {
data.operations[context] = {};
mapForContext.forEach((pluginName, type) => {
data.operations[context][type] = pluginName;
});
});
return data;
}
}