question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subscribe on dispatch after store changed

See original GitHub issue

Hello.

I trying implement keypress form validation.

When user typed one symbol in input, I want to validate this value.

How can i do it?

store.on(SET_NAME, (state: any, name: string) => {
        const data: IState = state[SECTION];

        return {
            ...state,
            [SECTION]: {
                ...data,
                name
            }
        }
    })
store.on('@dispatch', (state, [event]) => {
    console.log(state);// This state show me moment BEFORE any changes created by event
})

store.on('@changed', () => {
// I can not know what event create state changes
})
 

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
majo44commented, Mar 25, 2021

@omcg33

The sage or redux-observables are able to achieve such requirement just because they modify the storage behavior, Redux has middlewares and enhancers to achieve that, but you have to do that in the place where you are creating the store, you can’t enhance the store behavior after it is created. It is declarative approach.

On the other hand Storeon doesn’t have middlewares and enhancers, by default you can’t modify the store behavior, but you can in imperative way enhance it just by the event handlers as I showed in the previous comment. The other good example of tjis difference is the storeon-observables vs redux-observables.

// Redux - declarative
import { createStore, applyMiddleware } from 'redux';
import { createEpicMiddleware, combineEpics } from 'redux-observables';
 
const epic = action$ => action$.pipe(....); 
const epicMiddleware = createEpicMiddleware();
 
// you can do this only on store creation !!!
const store = createStore(rootReducer, applyMiddleware(epicMiddleware));
epicMiddleware.run(combineEpics(pingEpic));

vs

// Storeon - imperative
import { combineEpics, ofEvent, toEvent, createEpicModule } from 'storeon-observable';
// any of your storeon modules
export const someStoreonModule = (store) =>
    const epic = event$ => event$.pipe(....);
    // attaching the epics to store within the module, we are not expecting any 'middleware' on it
    createEpicModule(combineEpics(epic))(store);
}
// and in any point of time, even with lazy loading you can attach the module to store
// you do not have to attach modules to store on store creation point
someStoreonModule(store);

But if you want, there is no problem to enhance the Storeon store behavior during the store creation, like in Redux, example:

import { createStoreon } from 'storeon';
const createEnhancedStoreon = (modules) => {
    const store = createStoreon([]);
    const enhancedStore = {
        ...store,
        dispatch: (event, data) => {
            const preState = store.get();
            store.dispatch(event, data);
            const newState = store.get();
            if (preState !== newState && event !== '@after') {
                store.dispatch('@after', event);
            }
        }
    }
    modules.forEach(i => { if (i) i(enhancedStore) });
    store.dispatch('@init');
    return enhancedStore;
}
0reactions
omcg33commented, Mar 25, 2021

Sorry. I have not a good idea too. It was one of my complaints about the whole Redux idea.

Look for redux-saga. They did it. Any action that you got by take() will return you actual state yield take(SET_NAME, () => { const state = yield select(); // state has changes of SET_NAME })

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subscribe to single property change in store in Redux
There is no way to subscribe to part of the store when using subscribe directly, but as the creator of Redux says himself...
Read more >
Store - Redux
In Redux, subscriptions are called after the root reducer has returned the new state, so you may dispatch in the subscription listeners. You...
Read more >
Dispatch Actions and Subscribe to State | Simple Store
Dispatch Actions and Subscribe to State. Open the src/app/app.component.ts file. Import store. First, import the store instance and create a new private ...
Read more >
Actions and reducers: updating state - Human Redux
So store.subscribe() is simply a way to be notified that an action was dispatched, and therefore, something may have changed. You may be...
Read more >
The Redux Store - LearnHowToProgram.com
Our store changes as actions are dispatched to it! Finally, we can call unsubscribe() to end our subscription. In the magazine metaphor, this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found