penpot/frontend/resources/js/event-callbacks.js
Alejandro Alonso 3e697c2794
✨ Forward frontend audit events to host callbacks (#13175)
Hosts need a vendor-agnostic way to react to named audit events in the
browser (for example SaaS config.js). Penpot exposes
penpotAddEventCallback / penpotEventCallbacks, loads an early stub before
config.js, and invokes matching callbacks after make-event, without
coupling to any analytics SDK.

Closes #13091

AI-assisted-by: Composer
2026-11-19 16:02:45 +01:00

33 lines
965 B
JavaScript

/**
* Early registry for host-facing frontend audit event callbacks.
*
* Loaded before config.js so SaaS / local config can call
* `penpotAddEventCallback(name, fn)` at parse time. The ClojureScript
* app reads `penpotEventCallbacks` when prepared audit events fire and
* leaves a host-provided `penpotAddEventCallback` alone when present.
*/
(function (g) {
"use strict";
if (!(g.penpotEventCallbacks instanceof Map)) {
g.penpotEventCallbacks = new Map();
}
if (typeof g.penpotAddEventCallback !== "function") {
g.penpotAddEventCallback = function (name, callback) {
var m = g.penpotEventCallbacks;
var entry = m.get(name);
if (entry == null) {
m.set(name, [callback]);
} else if (Array.isArray(entry)) {
entry.push(callback);
} else if (typeof entry === "function") {
m.set(name, [entry, callback]);
} else {
m.set(name, [callback]);
}
};
}
})(globalThis);