fix: add modes export, fix event firing with modes and cloudflare

This commit is contained in:
dswbx
2025-10-28 09:18:16 +01:00
parent 2847e64b77
commit ef41b71921
10 changed files with 33 additions and 10 deletions

View File

@@ -295,6 +295,7 @@ export class App<
return this.module.auth.createUser(p);
}
// @todo: potentially add option to clone the app, so that when used in listeners, it won't trigger listeners
getApi(options?: LocalApiOptions) {
const fetcher = this.server.request as typeof fetch;
if (options && options instanceof Request) {

View File

@@ -37,19 +37,19 @@ export async function createApp<Env extends CloudflareEnv = CloudflareEnv>(
config: CloudflareBkndConfig<Env> = {},
ctx: Partial<CloudflareContext<Env>> = {},
) {
const appConfig = await makeConfig(
const appConfig = await makeConfig(config, ctx);
return await createRuntimeApp<Env>(
{
...config,
...appConfig,
onBuilt: async (app) => {
if (ctx.ctx) {
registerAsyncsExecutionContext(app, ctx?.ctx);
}
await config.onBuilt?.(app);
await appConfig.onBuilt?.(app);
},
},
ctx,
ctx?.env,
);
return await createRuntimeApp<Env>(appConfig, ctx?.env);
}
// compatiblity

View File

@@ -158,6 +158,7 @@ export async function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
sessionHelper.set(c, session);
await next();
});
appConfig.options?.manager?.onServerInit?.(server);
},
},
};

View File

@@ -205,7 +205,17 @@ export class EventManager<
if (listener.mode === "sync") {
syncs.push(listener);
} else {
asyncs.push(async () => await listener.handler(event, listener.event.slug));
asyncs.push(async () => {
try {
await listener.handler(event, listener.event.slug);
} catch (e) {
if (this.options?.onError) {
this.options.onError(event, e);
} else {
$console.error("Error executing async listener", listener, e);
}
}
});
}
// Remove if `once` is true, otherwise keep
return !listener.once;

View File

@@ -9,7 +9,6 @@ import {
pickKeys,
mcpTool,
convertNumberedObjectToArray,
mergeObject,
} from "bknd/utils";
import * as SystemPermissions from "modules/permissions";
import type { AppDataConfig } from "../data-schema";

View File

@@ -50,6 +50,7 @@ export { getFlashMessage } from "core/server/flash";
export * from "core/drivers";
export { Event, InvalidEventReturn } from "core/events/Event";
export type {
EventListener,
ListenerMode,
ListenerHandler,
} from "core/events/EventListener";

View File

@@ -68,6 +68,7 @@ export function hybrid<Args>({
const mm = app.modules as DbModuleManager;
mm.buildSyncConfig = syncSchemaOptions;
}
await appConfig.beforeBuild?.(app);
},
config: fileConfig,
options: {

View File

@@ -59,8 +59,8 @@ export type BkndModeConfig<Args = any, Additional = {}> = BkndConfig<
export async function makeModeConfig<
Args = any,
Config extends BkndModeConfig<Args> = BkndModeConfig<Args>,
>(_config: Config, args: Args) {
const appConfig = typeof _config.app === "function" ? await _config.app(args) : _config.app;
>({ app, ..._config }: Config, args: Args) {
const appConfig = typeof app === "function" ? await app(args) : app;
const config = {
..._config,