-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update readme to provide a simple example of how to dispatch multiple actions without notifying subscribers until all complete (without react or or other dependencies) #11
Comments
We for example batch using request animation frame and React's batchedUpdates. Below is a simple example using just request animation frame. This means that when mouse / touch events trigger actions (which are triggered more often than animation frames), you can make sure the subscribed (connected / smart) components only get notified every animation frame (and only when there where actions called of course). import raf from 'raf';
import { createStore } from 'redux';
import { batchedSubscribe } from 'redux-batched-subscribe';
let rafID;
function rafUpdateBatcher(notify) {
if (rafID) return; // prevent multiple request animation frame callbacks
rafID = raf(() => {
rafID = null;
notify();
});
}
// Note: passing batchedSubscribe as the last argument to createStore requires redux@>=3.1.0
const store = createStore(reducer, intialState, batchedSubscribe(rafUpdateBatcher)); |
@peteruithoven can you show the Complete example,i still cannot understand what the notify mean? thank you very much. :) |
oh~ it seem like control the subscription asynchronously |
Testing my PR (#17) I created the following simple example, would that clarify things? import { createStore, compose, applyMiddleware } from 'redux';
import raf from 'raf';
import { batchedSubscribe } from 'redux-batched-subscribe';
// Actions
export const ADD = 'ADD';
export function add(text) {
return { type: ADD, text };
}
// Reducers
const initialState = [];
function reducer(state = initialState, action) {
console.log('reduce: ', action.type);
switch(action.type) {
case ADD:
return [...state, action.text];
default:
return state;
}
}
// Batcher
let rafID;
export default function rafUpdateBatcher(notify, action, getState) {
if (rafID) return;
rafID = raf(() => {
rafID = null;
notify();
});
}
// Store
const store = createStore(
reducer,
initialState,
// disable this line to see difference
batchedSubscribe(rafUpdateBatcher)
);
// Experiment
store.subscribe(function() {
const state = store.getState();
console.log('notified: ', state);
});
store.dispatch(add('a'));
store.dispatch(add('b'));
store.dispatch(add('c')); |
This RAF example looks handy but feels like more of an advanced implementation. I think it would be hard to follow in a readme. debouncing is a fairly common pattern for throttling how often a function gets called. Perhaps more of a description here would help out? |
@tappleby I think @peteruithoven's example code is one of the more compelling use cases I've seen for this lib. I believe it should be noted in the docs even if it's just a link to it. In any case, thanks for this lib! 👍 |
It seems like this might be what I'm looking for, but can't fully understand it with the advanced/indirect examples.
The text was updated successfully, but these errors were encountered: