• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Effect Unit in Clean Architecture for Frontend Applications

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
This article shares the concept and implementation of the Effect unit in frontend applications within the Clean Architecture.

Repository with example:

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



The Effect unit encapsulates logic interacting with external resources through gateways. It manages side effects, asynchronous operations, and request orchestration.

Effect units handle cross-gateway requests, data fetching, sending processes, and sharing fetch logic across multiple use case interactors.

Effect Implementation


An Effect implements an interface provided by a consumer (use case interactor). This interface could represent a function required by use case or more complex one, which is use globally across application.

Effect implementations evolve from an inline form within consumers to extracted units:


--------------- ------------------
| inline effect | ---> | extracted effect |
--------------- ------------------

Initially, effects are implemented inline, focused on fetching data, sending requests, and orchestrating interactions.

Inline Effect Implementation


Consider a use case interactor that initializes an entities store upon application startup, requiring orders and users data:


export const useInitializeApplicationUseCase = (): { execute: () => Promise<void> } => {
const usersGateway = useUsersGateway();
const ordersGateway = useOrdersGateway();

const execute = async () => {
// inline effect
const users = await usersGateway.getUsers();
const orders = await ordersGateway.getOrders();

setUsersAndOrdersTransaction({ users, orders });
};

return { execute };
};
Extracted Effect Implementation


The inline implementation can be extracted into a reusable effect unit:


// File: getUsersAndOrdersEffect.ts
export const getUsersAndOrdersEffect = async () => {
const usersGateway = useUsersGateway();
const ordersGateway = useOrdersGateway();

const users = await usersGateway.getUsers();
const orders = await ordersGateway.getOrders();

return { users, orders };
};

// File: useInitializeApplicationUseCase.ts
export const useInitializeApplicationUseCase = (): { execute: () => Promise<void> } => {
const execute = async () => {
const { users, orders } = await getUsersAndOrdersEffect();

setUsersAndOrdersTransaction({ users, orders });
};

return { execute };
};

Extracted effects encapsulate logic completely, allowing internal modifications such as parallel requests without affecting consumers:


// File: getUsersAndOrdersEffect.ts
export const getUsersAndOrdersEffect = async () => {
const usersGateway = useUsersGateway();
const ordersGateway = useOrdersGateway();

// Fetch users and orders in parallel
const [users, orders] = await Promise.all([
usersGateway.getUsers(),
ordersGateway.getOrders(),
]);

return { users, orders };
};

// File: useInitializeApplicationUseCase.ts
export const useInitializeApplicationUseCase = (): { execute: () => Promise<void> } => {
const execute = async () => {
const { users, orders } = await getUsersAndOrdersEffect();

setUsersAndOrdersTransaction({ users, orders });
};

return { execute };
};

Effect names should clearly indicate their function and end with the suffix Effect.

Q&A

How should an Effect unit be tested?


Effect units can be tested in integration with their dependent units or in isolation using mocks.

Where should Effects be placed?


Effects should be placed in a dedicated effects directory.

Can Effects be nested?


Yes, Effects can be nested. However, nesting may complicate reasoning and testing, so use with care.

Can multiple Effects be used simultaneously?


Yes, using multiple effects simultaneously is common and practical.

Conclusion


Effect units effectively manage asynchronous operations and coordinate cross-gateway interactions. Begin with inline implementations and extract effects only as needed, maintaining architecture flexibility as application complexity grows.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу