diff --git a/NEXT_STEPS.md b/NEXT_STEPS.md index b16292e..556a729 100644 --- a/NEXT_STEPS.md +++ b/NEXT_STEPS.md @@ -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/`. diff --git a/src/arts/fmts/curr/active-currency.svelte.ts b/src/arts/fmts/curr/active-currency.svelte.ts index 905b2ff..d614f69 100644 --- a/src/arts/fmts/curr/active-currency.svelte.ts +++ b/src/arts/fmts/curr/active-currency.svelte.ts @@ -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 { - runtime.read(); - return engine.convert(value, to); - }, + convert: readFrom(runtime, engine.convert), - convertAs(value: number, from: CurrencyCode, to: CurrencyCode): Promise { - runtime.read(); - return engine.convertAs(value, from, to); - }, + convertAs: readFrom(runtime, engine.convertAs), get ratesSupported(): boolean { return engine.ratesSupported; diff --git a/src/arts/fmts/curr/engine-currency.ts b/src/arts/fmts/curr/engine-currency.ts index 8278eb7..fdaf9a3 100644 --- a/src/arts/fmts/curr/engine-currency.ts +++ b/src/arts/fmts/curr/engine-currency.ts @@ -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({ 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); } diff --git a/src/arts/fmts/dates/engine-dates.ts b/src/arts/fmts/dates/engine-dates.ts index 2b0908b..0ee8ddf 100644 --- a/src/arts/fmts/dates/engine-dates.ts +++ b/src/arts/fmts/dates/engine-dates.ts @@ -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({ 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())); } diff --git a/src/arts/fmts/nums/engine-numbers.ts b/src/arts/fmts/nums/engine-numbers.ts index 9466f83..96fb4b9 100644 --- a/src/arts/fmts/nums/engine-numbers.ts +++ b/src/arts/fmts/nums/engine-numbers.ts @@ -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({ 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') || '.'); } diff --git a/src/arts/fmts/unts/engine-units.ts b/src/arts/fmts/unts/engine-units.ts index b64e0ca..32e624b 100644 --- a/src/arts/fmts/unts/engine-units.ts +++ b/src/arts/fmts/unts/engine-units.ts @@ -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({ 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())); }