refactor(server): standalone runtime module (#9120)

This commit is contained in:
forehalo
2024-12-13 06:27:14 +00:00
parent 4c23991047
commit 81c68032e1
18 changed files with 85 additions and 75 deletions
@@ -3,7 +3,6 @@ import { merge } from 'lodash-es';
import { AFFiNEConfig } from './def';
import { Config } from './provider';
import { Runtime } from './runtime/service';
export * from './def';
export * from './default';
@@ -17,10 +16,10 @@ function createConfigProvider(
): FactoryProvider<Config> {
return {
provide: Config,
useFactory: (runtime: Runtime) => {
return Object.freeze(merge({}, globalThis.AFFiNE, override, { runtime }));
useFactory: () => {
return Object.freeze(merge({}, globalThis.AFFiNE, override));
},
inject: [Runtime],
inject: [],
};
}
@@ -31,10 +30,8 @@ export class ConfigModule {
return {
global: true,
module: ConfigModule,
providers: [provider, Runtime],
providers: [provider],
exports: [provider],
};
};
}
export { Runtime };
@@ -1,6 +1,5 @@
import { ApplyType } from '../utils/types';
import { AFFiNEConfig } from './def';
import type { Runtime } from './runtime/service';
/**
* @example
@@ -14,6 +13,4 @@ import type { Runtime } from './runtime/service';
* }
* }
*/
export class Config extends ApplyType<AFFiNEConfig>() {
runtime!: Runtime;
}
export class Config extends ApplyType<AFFiNEConfig>() {}
@@ -30,6 +30,7 @@ export {
OptionalModule,
} from './nestjs';
export { type PrismaTransaction } from './prisma';
export { Runtime } from './runtime';
export * from './storage';
export { type StorageProvider, StorageProviderFactory } from './storage';
export { CloudThrottlerGuard, SkipThrottle, Throttle } from './throttler';
@@ -1,10 +1,10 @@
import { OnEvent } from '../../event';
import { Payload } from '../../event/def';
import { FlattenedAppRuntimeConfig } from '../types';
import { FlattenedAppRuntimeConfig } from '../config/types';
import { OnEvent } from '../event';
import { Payload } from '../event/def';
declare module '../../event/def' {
declare module '../event/def' {
interface EventDefinitions {
runtimeConfig: {
runtime: {
[K in keyof FlattenedAppRuntimeConfig]: {
changed: Payload<FlattenedAppRuntimeConfig[K]>;
};
@@ -18,5 +18,5 @@ declare module '../../event/def' {
export const OnRuntimeConfigChange_DO_NOT_USE = (
nameWithModule: keyof FlattenedAppRuntimeConfig
) => {
return OnEvent(`runtimeConfig.${nameWithModule}.changed`);
return OnEvent(`runtime.${nameWithModule}.changed`);
};
@@ -0,0 +1,11 @@
import { Global, Module } from '@nestjs/common';
import { Runtime } from './service';
@Global()
@Module({
providers: [Runtime],
exports: [Runtime],
})
export class RuntimeModule {}
export { Runtime };
@@ -8,11 +8,14 @@ import {
import { PrismaClient } from '@prisma/client';
import { difference, keyBy } from 'lodash-es';
import { Cache } from '../../cache';
import { InvalidRuntimeConfigType, RuntimeConfigNotFound } from '../../error';
import { defer } from '../../utils/promise';
import { defaultRuntimeConfig, runtimeConfigType } from '../register';
import { AppRuntimeConfigModules, FlattenedAppRuntimeConfig } from '../types';
import { Cache } from '../cache';
import { defaultRuntimeConfig, runtimeConfigType } from '../config/register';
import {
AppRuntimeConfigModules,
FlattenedAppRuntimeConfig,
} from '../config/types';
import { InvalidRuntimeConfigType, RuntimeConfigNotFound } from '../error';
import { defer } from '../utils/promise';
function validateConfigType<K extends keyof FlattenedAppRuntimeConfig>(
key: K,