@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.
194 lines (193 loc) • 6.33 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();
presets = new Map();
#activeLoaderCount = 0;
#allLoadersSet = new Set();
#configs = new Map();
#engine;
#executedSet = new Set();
#inFlightLoaders = new Set();
#initialized = false;
#initPromise;
#isRunningLoaders = false;
#loaderEngine;
#loadPromises = new Set();
#plugins = [];
#pluginsView;
constructor(engine) {
this.#engine = engine;
const boundMethods = new WeakMap(), loaderInit = () => (this.#activeLoaderCount > 0 ? Promise.resolve() : engine.init());
this.#loaderEngine = new Proxy(engine, {
get: (target, property) => {
if (property === "init") {
return loaderInit;
}
const value = Reflect.get(target, property, target);
if (typeof value !== "function") {
return value;
}
let boundMethod = boundMethods.get(value);
if (!boundMethod) {
boundMethod = (...args) => Reflect.apply(value, target, args);
boundMethods.set(value, boundMethod);
}
return boundMethod;
},
});
}
get configs() {
const res = {};
for (const [name, config] of this.#configs) {
res[name] = config;
}
return res;
}
get initialized() {
return this.#initialized;
}
get plugins() {
return (this.#pluginsView ??= Object.freeze([...this.#plugins]));
}
addColorManager(name, manager) {
this.#checkRegistrationOpen();
this.colorManagers.set(name, manager);
}
addConfig(config) {
this.#checkRegistrationOpen();
const key = config.key ?? config.name ?? "default";
this.#configs.set(key, config);
this.#engine.dispatchEvent(EventType.configAdded, { data: { name: key, config } });
}
addEasing(name, easing) {
this.#checkRegistrationOpen();
if (this.easingFunctions.get(name)) {
return;
}
this.easingFunctions.set(name, easing);
}
addEffect(effect, drawer) {
this.#checkRegistrationOpen();
this.initializers.effects.set(effect, drawer);
}
addPalette(name, palette) {
this.#checkRegistrationOpen();
this.palettes.set(name, palette);
}
addParticleUpdater(name, updaterInitializer) {
this.#checkRegistrationOpen();
this.initializers.updaters.set(name, updaterInitializer);
}
addPlugin(plugin) {
if (!this.#isRunningLoaders) {
throw new Error(getErrorMessage(ErrorCodes.pluginRegisterAfterLoad));
}
if (this.getPlugin(plugin.id)) {
return;
}
this.#plugins.push(plugin);
this.#pluginsView = undefined;
}
addPreset(preset, options, override = false) {
this.#checkRegistrationOpen();
if (!(override || !this.getPreset(preset))) {
return;
}
this.presets.set(preset, options);
}
addShape(shapes, drawer) {
this.#checkRegistrationOpen();
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) {
return;
}
if (this.#initPromise) {
return this.#initPromise;
}
this.#initPromise = Promise.resolve().then(async () => {
this.#isRunningLoaders = true;
this.#allLoadersSet = new Set(this.#loadPromises);
try {
while (this.#inFlightLoaders.size > 0 ||
[...this.#allLoadersSet].some(loader => !this.#executedSet.has(loader))) {
for (const loader of this.#allLoadersSet) {
await this.#runLoader(loader, this.#executedSet, this.#allLoadersSet);
}
const inFlightLoaders = [...this.#inFlightLoaders];
if (inFlightLoaders.length) {
await Promise.all(inFlightLoaders);
}
}
this.#loadPromises.clear();
this.#initialized = true;
}
finally {
this.#isRunningLoaders = false;
this.#initPromise = undefined;
}
});
return this.#initPromise;
}
async register(...loaders) {
this.#checkRegistrationOpen();
for (const loader of loaders) {
if (this.#isRunningLoaders) {
this.#loadPromises.add(loader);
await this.#runLoader(loader, this.#executedSet, this.#allLoadersSet);
}
else {
this.#loadPromises.add(loader);
}
}
}
#checkRegistrationOpen() {
if (this.#initialized) {
throw new Error(getErrorMessage(ErrorCodes.pluginRegisterAfterLoad));
}
}
async #runLoader(loader, executed, allLoaders) {
if (executed.has(loader)) {
return;
}
executed.add(loader);
allLoaders.add(loader);
this.#activeLoaderCount++;
const loaderPromise = (async () => loader(this.#loaderEngine))();
this.#inFlightLoaders.add(loaderPromise);
try {
await loaderPromise;
}
catch (error) {
executed.delete(loader);
throw error;
}
finally {
this.#activeLoaderCount--;
this.#inFlightLoaders.delete(loaderPromise);
}
}
}