Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@redux-saga/core

Package Overview
Dependencies
Maintainers
3
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@redux-saga/core

Saga middleware for Redux to handle Side Effects

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
502K
decreased by-42.75%
Maintainers
3
Weekly downloads
 
Created

What is @redux-saga/core?

@redux-saga/core is a library that aims to make application side effects (i.e., asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, and better at handling failures. It uses an ES6 feature called Generators to make those asynchronous flows easy to read, write, and test.

What are @redux-saga/core's main functionalities?

Handling Side Effects

This code demonstrates how to handle side effects using @redux-saga/core. The `fetchUser` worker saga performs an asynchronous API call to fetch user data and dispatches actions based on the result. The `mySaga` watcher saga listens for 'USER_FETCH_REQUESTED' actions and triggers the worker saga.

```javascript
import { call, put, takeEvery } from 'redux-saga/effects';
import axios from 'axios';

// Worker saga
function* fetchUser(action) {
  try {
    const user = yield call(axios.get, `/api/user/${action.payload.userId}`);
    yield put({ type: 'USER_FETCH_SUCCEEDED', user: user.data });
  } catch (e) {
    yield put({ type: 'USER_FETCH_FAILED', message: e.message });
  }
}

// Watcher saga
function* mySaga() {
  yield takeEvery('USER_FETCH_REQUESTED', fetchUser);
}

export default mySaga;
```

Managing Complex Async Flows

This code demonstrates managing complex asynchronous flows using @redux-saga/core. The `authorize` worker saga handles the login process, and the `loginFlow` watcher saga continuously listens for 'LOGIN_REQUEST' actions and forks the `authorize` saga.

```javascript
import { call, put, take, fork } from 'redux-saga/effects';
import axios from 'axios';

function* authorize(user, password) {
  try {
    const token = yield call(axios.post, '/api/auth', { user, password });
    yield put({ type: 'LOGIN_SUCCESS', token: token.data });
  } catch (e) {
    yield put({ type: 'LOGIN_FAILURE', message: e.message });
  }
}

function* loginFlow() {
  while (true) {
    const { user, password } = yield take('LOGIN_REQUEST');
    yield fork(authorize, user, password);
  }
}

export default loginFlow;
```

Testing Sagas

This code demonstrates how to test sagas using @redux-saga/core. The test case verifies that the `fetchUser` saga yields the correct effects when handling a 'USER_FETCH_REQUESTED' action.

```javascript
import test from 'tape';
import { call, put } from 'redux-saga/effects';
import { fetchUser } from './sagas';
import axios from 'axios';

// Test case
test('fetchUser Saga test', (assert) => {
  const action = { type: 'USER_FETCH_REQUESTED', payload: { userId: 1 } };
  const generator = fetchUser(action);

  let next = generator.next();
  assert.deepEqual(
    next.value,
    call(axios.get, '/api/user/1'),
    'fetchUser should yield an Effect call(axios.get, "/api/user/1")'
  );

  const user = { data: { id: 1, name: 'John Doe' } };
  next = generator.next(user);
  assert.deepEqual(
    next.value,
    put({ type: 'USER_FETCH_SUCCEEDED', user: user.data }),
    'fetchUser should yield an Effect put({ type: "USER_FETCH_SUCCEEDED", user: user.data })'
  );

  assert.end();
});
```

Other packages similar to @redux-saga/core

Keywords

FAQs

Package last updated on 02 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc