RequestsHandler

RequestsHandler can handle your requests. You can abort last requests before ended or resolve them. You can do that using methods from RequestsHandler class instance or by emit events from components. This is example how handle request:

import { RequestsHandler, EventsReceiver, Eventrix } from 'eventrix';
import axios from 'axios';

const eventrix = new Eventrix();
const requestsHandler = new RequestsHandler(eventrix);

const fetchUsersReceiver = new EventsReceiver(
    'Users:fetchList',
    (eventName, eventData, stateManager) => {
        const requestId = 'fetchUsers';
        return requestsHandler.handleRequest(
            axios.get('https://abc.com/users'),
            requestId
        ).then(({ data }) => {
            stateManager.setState('users', data);
        });
    }
);

eventrix.useReceiver(fetchUsersReceiver)

export default eventrix;

When you handle request in this way you can abort them by emit event

emit('AbortRequest:fetchUsers', rejectData)

You can also abort last requests using RequestsHandler instance

import { RequestsHandler, EventsReceiver, Eventrix } from 'eventrix';
import axios from 'axios';

const eventrix = new Eventrix();
const requestsHandler = new RequestsHandler(eventrix);

const fetchUsersReceiver = new EventsReceiver(
    'Users:fetchList',
    (eventName, eventData, stateManager) => {
        const requestId = 'fetchUsers';
        requestsHandler.abortAllById(requestId, 'request rejected'); // reject last requests
        return requestsHandler.handleRequest(
            axios.get('https://abc.com/users'),
            requestId
        ).then(({ data }) => {
            stateManager.setState('users', data);
        });
    }
);

eventrix.useReceiver(fetchUsersReceiver)

export default eventrix;

Abort event structure

"AbortRequest:" + request_id

Resolve event structure

"ResolveRequest:" + request_id

RequestsHandler instance methods

abortAll(rejectData: any) - abort all pending requests

abortAllById(requestId: string, responseData: any) - abort all pending requests with requestId

resolveAll(rejectData: any) - resolve all pending requests

resolveAllById(requestId: string, responseData: any) - resolve all pending requests with requestId

isAnyPending(): boolean - check if any request pending

isPending(requestId: string): boolean - check if any request pending with requestId

Last updated