This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/lib/model-transformers.js
Osama Sayegh 40fd82e2d1
DEV: Add model transformer plugin API (#18081)
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.
2022-08-25 15:41:58 +03:00

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 = {};
}