This API allows plugins to transform a list of model objects before they're rendered in the UI. At the moment, this API is limited to items/lists of the experimental user menu, but it may be extended in the future to other parts of the app. Additional context can be found in https://github.com/discourse/discourse/pull/18046.
35 lines
876 B
JavaScript
35 lines
876 B
JavaScript
import { consolePrefix } from "discourse/lib/source-identifier";
|
|
|
|
let modelTransformersMap = {};
|
|
|
|
export function registerModelTransformer(modelName, func) {
|
|
if (!modelTransformersMap[modelName]) {
|
|
modelTransformersMap[modelName] = [];
|
|
}
|
|
const transformer = {
|
|
prefix: consolePrefix(),
|
|
execute: func,
|
|
};
|
|
modelTransformersMap[modelName].push(transformer);
|
|
}
|
|
|
|
export async function applyModelTransformations(modelName, models) {
|
|
for (const transformer of modelTransformersMap[modelName] || []) {
|
|
try {
|
|
await transformer.execute(models);
|
|
} catch (err) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(
|
|
transformer.prefix,
|
|
`transformer for the \`${modelName}\` model failed with:`,
|
|
err,
|
|
err.stack
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function resetModelTransformers() {
|
|
modelTransformersMap = {};
|
|
}
|