Svelte + Redux + Redux-saga

An attempt at a pathetic similarity to the useSelector, useDispatch hooks, as in react-redux.

Most of us have come across redux, and those who used it in ReactJS could feel the useSelector, useDispatch hooks, otherwise via mstp, mdtp + HOC connect. What about svelte? You can screw it up, or find something similar to connect, like svelte-redux-connect, describe huge constructions that we will send to the same connect:





const mapStateToProps = state => ({
  users: state.users,
  filters: state.filters
});

const mapDispatchToProps = dispatch => ({
  addUser: (name) => dispatch({
    type: 'ADD_USER',
    payload: { name }
  }),
  setFilter: (filter) => dispatch({
    type: 'SET_FILTER',
    payload: { filter }
  }) 
});
      
      



Just some scary flashbacks until mid-2018, before the introduction of hooks :). I want hooks in svelte. What can we take from it? Hmm ... svelte's store is global, no providers with a context are needed (just kidding, they are needed to separate contexts, but let's throw them out for now). It means like this: we create a redux-store, then we will try to write our pathetic hooks for ease of use.





So our constants:





//constants.js
export const GET_USER = '@@user/get'
export const FETCHING_USER = '@@user/fetch'
export const SET_USER = '@@user/set'
      
      



Reducer:





//user.js
import {FETCHING_USER, SET_USER} from "./constants";

const initialState = {
  user: null,
  isFetching: false
}

export default function user(state = initialState, action = {}){
  switch (action.type){
    case FETCHING_USER:
    case SET_USER:
      return {
        ...state,
        ...action.payload
      }
    default:
      return state
  }
}
      
      



Actions:





//actions.js
import {FETCHING_USER, GET_USER, SET_USER} from "./constants";

export const getUser = () => ({
  type: GET_USER
})

export const setUser = (user) => ({
  type: SET_USER,
  payload: {
    user
  }
})

export const setIsFetchingUser = (isFetching) => ({
  type: FETCHING_USER,
  payload: {
    isFetching
  }
})
      
      



Selectors. Let's return to them separately:





//selectors.js
import {createSelector} from "reselect";
import path from 'ramda/src/path'

export const selectUser = createSelector(
  path(['user', 'user']),
  user => user
)

export const selectIsFetchingUser = createSelector(
  path(['user', 'isFetching']),
  isFetching => isFetching
)
      
      



And the main combineReducers:





//rootReducer.js
import {combineReducers} from "redux";
import user from "./user/user";

export const reducers = combineReducers({
  user
})
      
      



Now we need to attach redux-saga, and as an api we will have https://randomuser.me/api/ . During testing of the whole process, this api worked very quickly, and I really wanted to look at the loader longer (everyone has their own masochism), so I wrapped the timeout in a promise for 3 seconds.





//saga.js
import {takeLatest, put, call, cancelled} from 'redux-saga/effects'
import {GET_USER} from "./constants";
import {setIsFetchingUser, setUser} from "./actions";
import axios from "axios";

const timeout = () => new Promise(resolve => {
  setTimeout(()=>{
    resolve()
  }, 3000)
})

function* getUser(){
  const cancelToken = axios.CancelToken.source()
  try{
    yield put(setIsFetchingUser(true))
    const response = yield call(axios.get, 'https://randomuser.me/api/', {cancelToken: cancelToken.token})
    yield call(timeout)
    yield put(setUser(response.data.results[0]))
    yield put(setIsFetchingUser(false))
  }catch (error){
    console.error(error)
  }finally {
    if(yield cancelled()){
      cancelToken.cancel('cancel fetching user')
    }
    yield put(setIsFetchingUser(false))
  }
}

export default function* userSaga(){
  yield takeLatest(GET_USER, getUser)
}
      
      



//rootSaga.js
import {all} from 'redux-saga/effects'
import userSaga from "./user/saga";

export default function* rootSaga(){
  yield all([userSaga()])
}
      
      



Finally, initializing the store:





//store.js
import {applyMiddleware, createStore} from "redux";
import {reducers} from "./rootReducer";
import {composeWithDevTools} from 'redux-devtools-extension';
import {writable} from "svelte/store";

import createSagaMiddleware from 'redux-saga';
import rootSaga from "./rootSaga";

const sagaMiddleware = createSagaMiddleware()

const middleware = applyMiddleware(sagaMiddleware)

const store = createStore(reducers, composeWithDevTools(middleware))

sagaMiddleware.run(rootSaga)

//     store
const initialState = store.getState()

//  writable store  useSelector
export const useSelector = writable((selector)=>selector(initialState))

//  writable store  useDispatch,      
//      
export const useDispatch = writable(() => store.dispatch)

//    store
store.subscribe(()=>{
  const state = store.getState()
  //   store  useSelector,    , 
  //  ,        
  useSelector.set(selector => selector(state))
})
      
      



. 18 . , , , - useSelector 3 store - ? , , . , store , , . , , :)









, ?

c useDispatch. svelte-store

export const useDispatch = () => store.dispatch



, useSelector store bindings, useDispatch - , . useDispatch App.svelte:





<!--App.svelte-->
<script>
  import {getUser} from "./store/user/actions";
  import {useDispatch} from "./store/store";
  import Loader from "./Loader.svelte";
  import User from "./User.svelte";
  //  
  const dispatch = $useDispatch()
  const handleClick = () => {
    //  
    dispatch(getUser())
  }
</script>

<style>
    .wrapper {
        display: inline-block;
        padding: 20px;
    }
    .button {
        padding: 10px;
        margin: 20px 0;
        border: none;
        background: #1d7373;
        color: #fff;
        border-radius: 8px;
        outline: none;
        cursor: pointer;
    }
    .heading {
        line-height: 20px;
        font-size: 20px;
    }
</style>

<div class="wrapper">
    <h1 class="heading">Random user</h1>
    <button class="button" on:click={handleClick}>Fetch user</button>
    <Loader/>
    <User/>
</div>
      
      



The button that triggers the action

. Fetch user, GET_USER. Redux-dev-tools - , . network - , :





. useSelector:





<!--Loader.svelte-->
<script>
    import {useSelector} from "./store/store";
    import {selectIsFetchingUser} from "./store/user/selector";
		//         store , 
    //       ,   :3
    $: isFetchingUser = $useSelector(selectIsFetchingUser)
</script>

<style>
    @keyframes loading {
        0% {
            background: #000;
            color: #fff;
        }
        100% {
            background: #fff;
            color: #000;
        }
    }
    .loader {
        background: #fff;
        box-shadow: 0px 0px 7px rgba(0,0,0,0.3);
        padding: 10px;
        border-radius: 8px;
        transition: color 0.3s ease-in-out, background 0.3s ease-in-out;
        animation: loading 3s ease-in-out forwards;
    }
</style>

{#if isFetchingUser}
    <div class="loader">Loading...</div>
{/if}
      
      



. store , :





<!--User.svelte-->
<script>
    import {useSelector} from "./store/store";
    import {selectIsFetchingUser,selectUser} from "./store/user/selector";

    $: user = $useSelector(selectUser)
    $: isFetchingUser = $useSelector(selectIsFetchingUser)
</script>
<style>
    .user {
        background: #fff;
        box-shadow: 0px 0px 7px rgba(0,0,0,0.3);
        display: grid;
        padding: 20px;
        justify-content: center;
        align-items: center;
        border-radius: 8px;
    }
    .user-image {
        width: 100px;
        height: 100px;
        background-position: center;
        background-size: contain;
        border-radius: 50%;
        margin-bottom: 20px;
        justify-self: center;
    }
</style>
{#if user && !isFetchingUser}
    <div class="user">
        <div class="user-image" style={`background-image: url(${user.picture.large});`}></div>
        <div>{user.name.title}. {user.name.first} {user.name.last}</div>
    </div>
{/if}
      
      



.





We wrote down some similarities to hooks, it seems convenient, but it is not known how this will affect in the future, if we make a mini-app out of this for a couple of pages. The sagas also plow. Through redux devtools, you can debug redux and jump from action to action, everything works well.








All Articles