UNPKG

get-set-action

Version:

A React State Management Library similar and alternative to mobx, redux.etc. it's the simplest, smallest and fastest state management library for react with dev tools support.

117 lines (80 loc) 3.66 kB
# Action Management Library This library provides a set of utilities for managing actions, async actions, and fetch operations, particularly useful for making AI calls. It is designed to simplify the handling of asynchronous operations and state management in JavaScript applications. ## Features - **Debounce and Throttle**: Easily debounce or throttle your functions to control the rate of execution. - **Async Actions**: Manage asynchronous operations with built-in support for debouncing, throttling, and state management. - **Fetch Utility**: Simplified fetch operations with interceptors for request, response, and error handling. - **State Management**: Track and manage the state of your actions with ease. ## Installation To use this library, simply import the functions you need from the `index.ts` file. ## Usage ### Action The `action` function allows you to create enhanced functions with interceptors and subscription capabilities. #### API - **intercept**: Add an interceptor to modify the input before execution. - **subscribe**: Subscribe to the result of the action. - **retry**: Retry the last action with the same parameters. - **getState**: Get the current state of the action. #### Example ```typescript import { action } from './index'; const myAction = action((props) => { console.log('Action executed with:', props); return props; }); myAction.intercept((props) => { console.log('Intercepted:', props); return { ...props, intercepted: true }; }); myAction.subscribe((result) => { console.log('Result:', result); }); myAction({ key: 'value' }); ``` ### AsyncAction The `asyncAction` function is used to manage asynchronous operations with support for debouncing and throttling. #### API - **run**: Execute the async action. - **abort**: Abort the current async operation. - **intercept**: Add an interceptor to modify the input before execution. - **interceptAfter**: Add an interceptor to modify the result after execution. - **interceptError**: Add an interceptor to handle errors. #### Example ```typescript import { asyncAction } from './index'; const myAsyncAction = asyncAction(async (payload) => { const response = await fetch(payload.url); return response.json(); }, { debounce: 500 }); myAsyncAction.run({ url: 'https://api.example.com/data' }) .then(data => console.log('Data:', data)) .catch(error => console.error('Error:', error)); ``` ### Fetch The `Fetch` function simplifies fetch operations with built-in interceptors for request, response, and error handling. #### API - **intercept**: Add an interceptor to modify the request before execution. - **interceptAfter**: Add an interceptor to modify the response after execution. - **interceptError**: Add an interceptor to handle errors. #### Example ```typescript import { Fetch } from './index'; const fetchAction = Fetch({ debounce: 1000 }); fetchAction.run({ url: 'https://api.example.com/data' }) .then(response => console.log('Response:', response)) .catch(error => console.error('Error:', error)); fetchAction.intercept((payload) => { console.log('Request Intercepted:', payload); return payload; }); fetchAction.interceptAfter((data) => { console.log('Response Intercepted:', data); return data; }); fetchAction.interceptError((error) => { console.error('Error Intercepted:', error); return error; }); ``` ## Conclusion This library provides a comprehensive set of tools for managing actions and asynchronous operations in your applications. Whether you need to debounce a function, manage async actions, or perform fetch operations with custom interceptors, this library has you covered.