feat: events (#717)
This commit is contained in:
committed by
GitHub
parent
455bcdc0f2
commit
79877fcd1f
@ -54,7 +54,9 @@ export function commitMessageFormatter<EF extends BaseField>(
|
||||
case 'author-name':
|
||||
return authorName || '';
|
||||
default:
|
||||
console.warn(`Ignoring unknown variable “${variable}” in commit message template.`);
|
||||
console.warn(
|
||||
`[StaticCMS] Ignoring unknown variable “${variable}” in commit message template.`,
|
||||
);
|
||||
return '';
|
||||
}
|
||||
});
|
||||
|
@ -246,7 +246,7 @@ function mergeValues<EF extends BaseField>(
|
||||
let defaultEntry = values.find(e => e.locale === defaultLocale);
|
||||
if (!defaultEntry) {
|
||||
defaultEntry = values[0];
|
||||
console.warn(`Could not locale entry for default locale '${defaultLocale}'`);
|
||||
console.warn(`[StaticCMS] Could not locale entry for default locale '${defaultLocale}'`);
|
||||
}
|
||||
const i18n = values
|
||||
.filter(e => e.locale !== defaultEntry!.locale)
|
||||
|
@ -26,7 +26,7 @@ import type {
|
||||
WidgetValueSerializer,
|
||||
} from '../interface';
|
||||
|
||||
export const allowedEvents = ['preSave', 'postSave'] as const;
|
||||
export const allowedEvents = ['mounted', 'preSave', 'postSave'] as const;
|
||||
export type AllowedEvent = (typeof allowedEvents)[number];
|
||||
|
||||
const eventHandlers = allowedEvents.reduce((acc, e) => {
|
||||
@ -219,7 +219,7 @@ export function registerWidget<T = unknown, F extends BaseField = UnknownField>(
|
||||
} = nameOrWidgetOrWidgets;
|
||||
if (registry.widgets[widgetName]) {
|
||||
console.warn(oneLine`
|
||||
Multiple widgets registered with name "${widgetName}". Only the last widget registered with
|
||||
[StaticCMS] Multiple widgets registered with name "${widgetName}". Only the last widget registered with
|
||||
this name will be used.
|
||||
`);
|
||||
}
|
||||
@ -323,22 +323,24 @@ export function registerEventListener(
|
||||
registry.eventHandlers[name].push({ handler, options });
|
||||
}
|
||||
|
||||
export async function invokeEvent({ name, data }: { name: AllowedEvent; data: EventData }) {
|
||||
export async function invokeEvent({ name, data }: { name: AllowedEvent; data?: EventData }) {
|
||||
validateEventName(name);
|
||||
const handlers = registry.eventHandlers[name];
|
||||
|
||||
let _data = { ...data };
|
||||
console.info(`[StaticCMS] Firing event ${name}`, data);
|
||||
|
||||
let _data = data ? { ...data } : undefined;
|
||||
for (const { handler, options } of handlers) {
|
||||
const result = await handler(_data, options);
|
||||
if (result !== undefined) {
|
||||
if (_data !== undefined && result !== undefined) {
|
||||
const entry = {
|
||||
..._data.entry,
|
||||
data: result,
|
||||
} as Entry;
|
||||
_data = { ...data, entry };
|
||||
_data = { ..._data, entry };
|
||||
}
|
||||
}
|
||||
return _data.entry.data;
|
||||
return _data?.entry.data;
|
||||
}
|
||||
|
||||
export function removeEventListener({ name, handler }: EventListener) {
|
||||
|
@ -83,7 +83,7 @@ export async function requestWithBackoff(
|
||||
if (!api.rateLimiter) {
|
||||
const timeout = error.resetSeconds || attempt * attempt;
|
||||
console.info(
|
||||
`Pausing requests for ${timeout} ${
|
||||
`[StaticCMS] Pausing requests for ${timeout} ${
|
||||
attempt === 1 ? 'second' : 'seconds'
|
||||
} due to fetch failures:`,
|
||||
error.message,
|
||||
@ -93,7 +93,7 @@ export async function requestWithBackoff(
|
||||
setTimeout(() => {
|
||||
api.rateLimiter?.release();
|
||||
api.rateLimiter = undefined;
|
||||
console.info(`Done pausing requests`);
|
||||
console.info('[StaticCMS] Done pausing requests');
|
||||
}, 1000 * timeout);
|
||||
}
|
||||
return requestWithBackoff(api, req, attempt + 1);
|
||||
|
@ -33,7 +33,7 @@ export function asyncLock(): AsyncLock {
|
||||
if (e instanceof Error && e.message !== 'leave called too many times.') {
|
||||
throw e;
|
||||
} else {
|
||||
console.warn('leave called too many times.');
|
||||
console.warn('[StaticCMS] Leave called too many times.');
|
||||
lock = semaphore(1);
|
||||
}
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ export async function runWithLock(lock: AsyncLock, func: Function, message: stri
|
||||
try {
|
||||
const acquired = await lock.acquire();
|
||||
if (!acquired) {
|
||||
console.warn(message);
|
||||
console.warn('[StaticCMS]', message);
|
||||
}
|
||||
|
||||
const result = await func();
|
||||
@ -315,7 +315,7 @@ export async function allEntriesByFolder({
|
||||
const localTreeInBranch = await isShaExistsInBranch(branch.name, localTree.head);
|
||||
if (!localTreeInBranch) {
|
||||
console.info(
|
||||
`Can't find local tree head '${localTree.head}' in branch '${branch.name}', rebuilding local tree`,
|
||||
`[StaticCMS] Can't find local tree head '${localTree.head}' in branch '${branch.name}', rebuilding local tree`,
|
||||
);
|
||||
return listAllFilesAndPersist();
|
||||
}
|
||||
@ -329,12 +329,12 @@ export async function allEntriesByFolder({
|
||||
getFileId,
|
||||
filterFile,
|
||||
}).catch(e => {
|
||||
console.info('Failed getting diff from local tree:', e);
|
||||
console.info('[StaticCMS] Failed getting diff from local tree:', e);
|
||||
return null;
|
||||
});
|
||||
|
||||
if (!diff) {
|
||||
console.info(`Diff is null, rebuilding local tree`);
|
||||
console.info(`[StaticCMS] Diff is null, rebuilding local tree`);
|
||||
return listAllFilesAndPersist();
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ function localForageTest() {
|
||||
.catch(err => {
|
||||
if (err.code === 22) {
|
||||
const message = 'Unable to set localStorage key. Quota exceeded! Full disk?';
|
||||
console.warn(message);
|
||||
console.warn('[StaticCMS]', message);
|
||||
}
|
||||
console.info(err);
|
||||
});
|
||||
|
Reference in New Issue
Block a user