@tsparticles/engine
Version:
Easily create highly customizable particle, confetti and fireworks animations and use them as animated backgrounds for your website. Ready to use components available also for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Riot.js, Inferno.
153 lines (152 loc) • 4.25 kB
JavaScript
import { ErrorCodes, getErrorMessage } from "../../Utils/ErrorUtils.js";
import { EventType } from "../../Enums/Types/EventType.js";
export class PluginManager {
colorManagers = new Map();
easingFunctions = new Map();
initializers = {
effects: new Map(),
shapes: new Map(),
updaters: new Map(),
};
palettes = new Map();
plugins = [];
presets = new Map();
#allLoadersSet = new Set();
#configs = new Map();
#engine;
#executedSet = new Set();
#initialized = false;
#isRunningLoaders = false;
#loadPromises = new Set();
constructor(engine) {
this.#engine = engine;
}
get configs() {
const res = {};
for (const [name, config] of this.#configs) {
res[name] = config;
}
return res;
}
get initialized() {
return this.#initialized;
}
addColorManager(name, manager) {
if (this.#initialized) {
return;
}
this.colorManagers.set(name, manager);
}
addConfig(config) {
if (this.#initialized) {
return;
}
const key = config.key ?? config.name ?? "default";
this.#configs.set(key, config);
this.#engine.dispatchEvent(EventType.configAdded, { data: { name: key, config } });
}
addEasing(name, easing) {
if (this.#initialized) {
return;
}
if (this.easingFunctions.get(name)) {
return;
}
this.easingFunctions.set(name, easing);
}
addEffect(effect, drawer) {
if (this.#initialized) {
return;
}
this.initializers.effects.set(effect, drawer);
}
addPalette(name, palette) {
if (this.#initialized) {
return;
}
this.palettes.set(name, palette);
}
addParticleUpdater(name, updaterInitializer) {
if (this.#initialized) {
return;
}
this.initializers.updaters.set(name, updaterInitializer);
}
addPlugin(plugin) {
if (this.#initialized) {
return;
}
if (this.getPlugin(plugin.id)) {
return;
}
this.plugins.push(plugin);
}
addPreset(preset, options, override = false) {
if (this.#initialized) {
return;
}
if (!(override || !this.getPreset(preset))) {
return;
}
this.presets.set(preset, options);
}
addShape(shapes, drawer) {
if (this.#initialized) {
return;
}
for (const shape of shapes) {
this.initializers.shapes.set(shape, drawer);
}
}
getEasing(name) {
return this.easingFunctions.get(name) ?? ((value) => value);
}
getPalette(name) {
return this.palettes.get(name);
}
getPlugin(plugin) {
return this.plugins.find(t => t.id === plugin);
}
getPreset(preset) {
return this.presets.get(preset);
}
async init() {
if (this.#initialized || this.#isRunningLoaders) {
return;
}
this.#isRunningLoaders = true;
this.#executedSet = new Set();
this.#allLoadersSet = new Set(this.#loadPromises);
try {
for (const loader of this.#allLoadersSet) {
await this.#runLoader(loader, this.#executedSet, this.#allLoadersSet);
}
}
finally {
this.#loadPromises.clear();
this.#isRunningLoaders = false;
this.#initialized = true;
}
}
async register(...loaders) {
if (this.#initialized) {
throw new Error(getErrorMessage(ErrorCodes.pluginRegisterAfterLoad));
}
for (const loader of loaders) {
if (this.#isRunningLoaders) {
await this.#runLoader(loader, this.#executedSet, this.#allLoadersSet);
}
else {
this.#loadPromises.add(loader);
}
}
}
async #runLoader(loader, executed, allLoaders) {
if (executed.has(loader)) {
return;
}
executed.add(loader);
allLoaders.add(loader);
await loader(this.#engine);
}
}