Reduce formats runtime boilerplate

master
dev 5 months ago
parent 530afaec76
commit 0224ace98a

@ -18,7 +18,7 @@ Estado al cierre:
- `src/arts/aapp/test/ecosystem.integration.test.ts` ampliado para cubrir rol `viewer` no-allow y cache re-scoped por locale.
- Referencias residuales de marca anterior eliminadas de `src/` fuera de rutas temporales: docs, páginas de test y constantes de cookies/headers auth usan ahora `Active/active`.
- `src/arts/conn/README.md` ampliado: contrato de raíz/conexión/canal, estados, transportes, request/reply, reconnect, heartbeat, sesión, diagnostics/logger, errores y testing.
- `fmts` redujo boilerplate activo con helpers `readFrom` / `writeTo` en `active-runtime.svelte.ts`; APIs públicas sin cambios.
- `fmts` redujo boilerplate activo con helpers `readFrom` / `writeTo` y los engines comparten directamente las funciones de `createFormatsLocaleState`; APIs públicas sin cambios.
- No tocar `src/web/routes/temp/` hasta decidir que hacer con esa pagina.
- No commitear `.idea/`, `.claude/` ni `.opencode/`.

@ -1,11 +1,10 @@
import { createActiveFormatsRuntime } from '../active-runtime.svelte';
import { createActiveFormatsRuntime, readFrom } from '../active-runtime.svelte';
import { createEngineCurrency } from './engine-currency';
import type {
ActiveCurrency,
ActiveCurrencyOptions,
CurrencyCode,
CurrencyMode,
CurrencyFormatOptions
CurrencyMode
} from './types';
export function createActiveCurrency(options: ActiveCurrencyOptions = {}): ActiveCurrency {
@ -21,6 +20,14 @@ export function createActiveCurrency(options: ActiveCurrencyOptions = {}): Activ
currencyListeners.forEach((fn) => fn(currency));
}
function mutateCurrency(fn: () => void): void {
const before = engine.getCurrency();
fn();
const after = engine.getCurrency();
runtime.notifyChange();
if (before !== after) notifyCurrency(after);
}
const runtime = createActiveFormatsRuntime({
localeSource,
getLocale: () => engine.getLocale(),
@ -33,60 +40,29 @@ export function createActiveCurrency(options: ActiveCurrencyOptions = {}): Activ
});
return {
getLocale(): string {
runtime.read();
return engine.getLocale();
},
getLocale: readFrom(runtime, engine.getLocale),
setLocale(locale: string): void {
runtime.syncLocale(locale);
},
setLocale: runtime.syncLocale,
getCurrency(): CurrencyCode {
runtime.read();
return engine.getCurrency();
},
getCurrency: readFrom(runtime, engine.getCurrency),
setCurrency(currency: CurrencyMode): void {
const before = engine.getCurrency();
engine.setCurrency(currency);
const after = engine.getCurrency();
runtime.notifyChange();
if (before !== after) notifyCurrency(after);
mutateCurrency(() => engine.setCurrency(currency));
},
clearCurrency(): void {
const before = engine.getCurrency();
engine.clearCurrency();
const after = engine.getCurrency();
runtime.notifyChange();
if (before !== after) notifyCurrency(after);
mutateCurrency(engine.clearCurrency);
},
isCurrencyAuto(): boolean {
runtime.read();
return engine.isCurrencyAuto();
},
isCurrencyAuto: readFrom(runtime, engine.isCurrencyAuto),
format(value: number, options?: CurrencyFormatOptions): string {
runtime.read();
return engine.format(value, options);
},
format: readFrom(runtime, engine.format) as ActiveCurrency['format'],
formatAs(value: number, currency: CurrencyCode, options?: CurrencyFormatOptions): string {
runtime.read();
return engine.formatAs(value, currency, options);
},
formatAs: readFrom(runtime, engine.formatAs) as ActiveCurrency['formatAs'],
convert(value: number, to: CurrencyCode): Promise<number | undefined> {
runtime.read();
return engine.convert(value, to);
},
convert: readFrom(runtime, engine.convert),
convertAs(value: number, from: CurrencyCode, to: CurrencyCode): Promise<number | undefined> {
runtime.read();
return engine.convertAs(value, from, to);
},
convertAs: readFrom(runtime, engine.convertAs),
get ratesSupported(): boolean {
return engine.ratesSupported;

@ -49,7 +49,7 @@ function getCurrencyFormatter(
}
export function createEngineCurrency(options: EngineCurrencyOptions = {}): EngineCurrency {
const localeState = createFormatsLocaleState(options.locale);
const { getLocale, setLocale } = createFormatsLocaleState(options.locale);
const currencyState = createAutoState<CurrencyMode, CurrencyCode>({
initial: options.currency,
auto: AUTO_CURRENCY,
@ -60,14 +60,6 @@ export function createEngineCurrency(options: EngineCurrencyOptions = {}): Engin
const diagnostics = createCurrencyDiagnostics(logger);
const defaultFormat = options.format ?? {};
function getLocale(): string {
return localeState.getLocale();
}
function setLocale(locale: string): void {
localeState.setLocale(locale);
}
function getCurrency(): CurrencyCode {
return currencyState.get(() => resolveCurrency(getLocale()) ?? defaultCurrency);
}

@ -23,7 +23,7 @@ function withHourCycle(
}
export function createEngineDates(options: EngineDatesOptions = {}): EngineDates {
const localeState = createFormatsLocaleState(options.locale);
const { getLocale, setLocale } = createFormatsLocaleState(options.locale);
const dateOrder = createAutoState<DateOrderMode, DateOrder>({
initial: options.dateOrder,
auto: 'auto'
@ -33,14 +33,6 @@ export function createEngineDates(options: EngineDatesOptions = {}): EngineDates
auto: 'auto'
});
function getLocale(): string {
return localeState.getLocale();
}
function setLocale(locale: string): void {
localeState.setLocale(locale);
}
function getDateOrder() {
return dateOrder.get(() => resolveDateOrder(getLocale()));
}

@ -18,7 +18,7 @@ import type {
} from './types';
export function createEngineNumbers(options: EngineNumbersOptions = {}): EngineNumbers {
const localeState = createFormatsLocaleState(options.locale);
const { getLocale, setLocale } = createFormatsLocaleState(options.locale);
const decimalSeparator = createAutoState<NumbersSeparatorMode, string>({
initial: options.decimalSeparator,
auto: AUTO_VALUE
@ -33,14 +33,6 @@ export function createEngineNumbers(options: EngineNumbersOptions = {}): EngineN
});
const defaultFormat = options.format ?? {};
function getLocale(): string {
return localeState.getLocale();
}
function setLocale(locale: string): void {
localeState.setLocale(locale);
}
function getDecimalSeparator(): string {
return decimalSeparator.get(() => getNumberFormatPart(getLocale(), 1.1, 'decimal') || '.');
}

@ -16,21 +16,13 @@ import type {
} from './types';
export function createEngineUnits(options: EngineUnitsOptions = {}): EngineUnits {
const localeState = createFormatsLocaleState(options.locale);
const { getLocale, setLocale } = createFormatsLocaleState(options.locale);
const unitSystem = createAutoState<UnitSystemMode, UnitSystem>({
initial: options.system,
auto: AUTO_UNIT_SYSTEM
});
const { numbers } = options;
function getLocale(): string {
return localeState.getLocale();
}
function setLocale(locale: string): void {
localeState.setLocale(locale);
}
function getSystem(): UnitSystem {
return unitSystem.get(() => resolveUnitSystem(getLocale()));
}

Loading…
Cancel
Save

Powered by TurnKey Linux.