react-universally icon indicating copy to clipboard operation
react-universally copied to clipboard

redux-saga

Open bradennapier opened this issue 9 years ago • 3 comments

Do you have any experience with redux-saga? I use it heavily and would think a branch with it might be powerful and/or it being added to the opinionated redux workflow. I have also been developing redux-saga-process which I already use heavily in my apps to handle back-end logic and processing of asynchronous jobs, with a strong focus on being extremely modular.

Here is an example of a redux-saga-process (it reads static properties of all processes and builds special reducers, actions, and routers based upon the properties given for each process):

import Process from 'redux-saga-process'

import { call} from 'redux-saga/effects'
import { GET_PRODUCT, PRODUCT_DETAILS, REMOVE_PRODUCTS } from './types'

/*
  ProductFetcherProcess
    This is a fairly straight forward example of how you might build a 
    process which is used to handle fetching product data from a server,
    processing the results, optionally modify / normalize it, and dispatch 
    the results to our UI (which would be connected to the products key within 
    our store).
    
    We can store any data which does not pertain to our UI internally and only 
    dispatch a pure representation to the UI once it is ready.
*/

export default class ProductFetcherProcess extends Process {

  // this.history will contain data to throttle requests
  history = {};
  
  // Set a throttleTimeout property for throttling fetch requests.
  throttleTimeout = 30000
  
  // Reduce the 'products' key within our store.
  static config = { reduces: 'products' };
  
  // Reduce the product details state when product data is received
  static reducer = {
    [PRODUCT_DETAILS]: (state, action) => ({
      ...state,
      [action.productSKU]: action.data
    }),
    [REMOVE_PRODUCTS]: (state, action) => ({})
  };
  
  // call * getProduct method whenever GET_PRODUCT is dispatched.
  static actionRoutes = {
    [GET_PRODUCT]: 'getProduct'
  };
  
  // yield* this.dispatch('removeProducts') dispatches
  //  { type: 'REMOVE_PRODUCTS' }
  // yield* this.dispatch('productDetails', productSKU, product) dispatches
  //  { type: 'PRODUCT_DETAILS', productSKU: productSKU, data: product }
  static actions = {
    removeProducts: null,
    productDetails: [ 'productSKU', 'data' ],
  };
  
  // Get the product asynchronously within our method (called within a fork by
  // the Process) and dispatch the product details when completed
  * getProduct({ type, productSKU, force, ...action }) {
    const now = Date.now()
    if ( ! force && this.history[productSKU] && now - this.history[productSKU] < this.throttleTimeout ) {
      /* Only attempt fetch at most once every 30 seconds unless force = true */
      return
    }
    this.history[productSKU] = now
    const product = yield call(fetch(/*...fetch args...*/))
    /* Normalize / Modify Product Data before dispatch? */
    if ( product && product.productSKU === productSKU ) {
      yield* this.dispatch('productDetails', productSKU, product)
    }
  }

}

}

bradennapier avatar Jan 13 '17 19:01 bradennapier

https://github.com/Dash-OS/redux-saga-process-universally

i started on this as a preliminary example

bradennapier avatar Jan 14 '17 18:01 bradennapier

Great looking project! I love redux-saga and use it myself for larger and more complex projects. I am not sure about creating and/or merging into an opinionated branch. In fact I am considering removing all opinionated branches as it just becomes a management overhead. I think it might be better for branches to have a minimal set of functionality per library it adds.

We can however add a new section to the docs that links to projects such as yours, especially if you have a forked version of react-universally. 👍

ctrlplusb avatar Jan 23 '17 16:01 ctrlplusb

@ctrlplusb do you have an example boilerplate of react-universally with redux-saga ? Thanks

nzeugaa avatar Apr 02 '17 21:04 nzeugaa