From 6fbe517ccb9a3a56963ad13252ea1c5eaaf82860 Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Mon, 8 Apr 2024 12:26:34 +0700 Subject: [PATCH] Expose instead of --- .changeset/poor-carrots-argue.md | 2 +- apps/website/docs/i18next/index.md | 8 +++---- packages/i18next/src/integration.ts | 30 ++++++++++++--------------- packages/i18next/src/language.test.ts | 13 ++++++------ packages/i18next/src/t.test.ts | 6 +++--- 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/.changeset/poor-carrots-argue.md b/.changeset/poor-carrots-argue.md index 21d42292..69ce3b53 100644 --- a/.changeset/poor-carrots-argue.md +++ b/.changeset/poor-carrots-argue.md @@ -2,4 +2,4 @@ '@withease/i18next': minor --- -Add _Store_ `$language` and _EventCallable_ `changeLanguage` to integration +Add _Store_ `$language` and _Effect_ `changeLanguageFx` to integration diff --git a/apps/website/docs/i18next/index.md b/apps/website/docs/i18next/index.md index c48cd754..d54fb44a 100644 --- a/apps/website/docs/i18next/index.md +++ b/apps/website/docs/i18next/index.md @@ -157,19 +157,19 @@ const { $language } = createI18nextIntegration({ }); ``` -#### `changeLanguage` +#### `changeLanguageFx` -An [_EventCallable_](https://effector.dev/en/api/effector/event/) that can be called with a language code to change the current language. +An [_Effect_](https://effector.dev/en/api/effector/effect/) that can be called with a language code to change the current language. ```ts -const { changeLanguage } = createI18nextIntegration({ +const { changeLanguageFx } = createI18nextIntegration({ /* ... */ }); sample({ clock: someButtonClicked, fn: () => 'en', - target: changeLanguage, + target: changeLanguageFx, }); ``` diff --git a/packages/i18next/src/integration.ts b/packages/i18next/src/integration.ts index b8846aed..98f049be 100644 --- a/packages/i18next/src/integration.ts +++ b/packages/i18next/src/integration.ts @@ -1,7 +1,7 @@ import { type Event, type Store, - type EventCallable, + type Effect, attach, combine, createEffect, @@ -30,7 +30,7 @@ type I18nextIntegration = { translated: Translated; $isReady: Store; $language: Store; - changeLanguage: EventCallable; + changeLanguageFx: Effect; reporting: { missingKey: Event; }; @@ -85,7 +85,16 @@ export function createI18nextIntegration({ const $language = createStore(null, { serialize: 'ignore' }); - const changeLanguage = createEvent(); + const changeLanguageFx = attach({ + source: $instance, + async effect(instance, nextLangauge: string) { + if (!instance) { + return; + } + + await instance.changeLanguage(nextLangauge); + }, + }); // -- End of public API @@ -107,19 +116,6 @@ export function createI18nextIntegration({ target: $language, }); - const changeLanguageFx = attach({ - source: $instance, - async effect(instance, nextLangauge: string) { - if (!instance) { - return; - } - - await instance.changeLanguage(nextLangauge); - }, - }); - - sample({ clock: changeLanguage, target: changeLanguageFx }); - sample({ clock: instanceInitialized, fn: () => true, @@ -261,7 +257,7 @@ export function createI18nextIntegration({ $isReady, $t, $language, - changeLanguage, + changeLanguageFx, translated: (firstArg, ...args: any[]) => { if (typeof firstArg === 'string') { return translatedWithVariables(firstArg, args[0]); diff --git a/packages/i18next/src/language.test.ts b/packages/i18next/src/language.test.ts index c0da044b..01406152 100644 --- a/packages/i18next/src/language.test.ts +++ b/packages/i18next/src/language.test.ts @@ -4,7 +4,7 @@ import { createInstance } from 'i18next'; import { createI18nextIntegration } from './integration'; -describe('integration.$language/changeLanguage', () => { +describe('integration.$language/changeLanguageFx', () => { test('change language', async () => { const setup = createEvent(); @@ -16,10 +16,11 @@ describe('integration.$language/changeLanguage', () => { lng: 'th', }); - const { $language, changeLanguage, translated } = createI18nextIntegration({ - instance, - setup, - }); + const { $language, changeLanguageFx, translated } = + createI18nextIntegration({ + instance, + setup, + }); const $val = translated('common:key'); @@ -34,7 +35,7 @@ describe('integration.$language/changeLanguage', () => { expect(scope.getState($language)).toBe('th'); expect(scope.getState($val)).toBe('th value'); - await allSettled(changeLanguage, { scope, params: 'en' }); + await allSettled(changeLanguageFx, { scope, params: 'en' }); // After change expect(scope.getState($language)).toBe('en'); diff --git a/packages/i18next/src/t.test.ts b/packages/i18next/src/t.test.ts index fca7a982..dca93b0e 100644 --- a/packages/i18next/src/t.test.ts +++ b/packages/i18next/src/t.test.ts @@ -118,7 +118,7 @@ describe('integration.$t', () => { const setup = createEvent(); - const { $t } = createI18nextIntegration({ + const { $t, changeLanguageFx } = createI18nextIntegration({ instance, setup, }); @@ -131,7 +131,7 @@ describe('integration.$t', () => { expect(scope.getState($result)).toBe('Sawa dee'); - instance.changeLanguage('en'); + await allSettled(changeLanguageFx, { params: 'en', scope }); await allSettled(scope); expect(scope.getState($result)).toBe('Hello'); @@ -240,7 +240,7 @@ describe('integration.$t', () => { await allSettled(teardown, { scope }); - instance.changeLanguage('en'); + await allSettled(changeLanguageFx, { params: 'en', scope }); await allSettled(scope); expect(scope.getState($result)).toBe('Sawa dee');