Skip to content

Instantly share code, notes, and snippets.

@Sawtaytoes
Sawtaytoes / startLoggingServiceEpic.js
Last active September 30, 2018 04:25
Use Redux-Observable to start a logging service after receiving user info.
const startLoggingServiceEpic = (
action$,
) => (
action$
.pipe(
ofType(UPDATE_USER_INFO),
map(({
email,
phone,
userName,
@Sawtaytoes
Sawtaytoes / startChatSystemEpic.js
Last active September 30, 2018 04:27
Redux-Observable can prevent an action from firing more than once. For instance, a 3rd party tool that only needs to execute once such as a 3rd party chat client.
const startChatSystemEpic = (
action$,
) => (
action$
.pipe(
ofType(START_CHAT_CLIENT),
take(1)
tap(startChatClient),
ignoreElements(),
)
@Sawtaytoes
Sawtaytoes / listenForChatMessageEpic.js
Last active September 30, 2018 05:32
Execute an observable switched to from an observable only once.
const listenForChatMessageEpic = (
action$,
) => (
action$
.pipe(
ofType(START_CHAT_CLIENT),
tap(startChatClient),
takeUntil(
action$
.pipe(
@Sawtaytoes
Sawtaytoes / logAppVersionEpic.js
Created September 30, 2018 05:10
Immediately logging the app version when Redux-Observable starts.
// Redux-Observable 1.x.x
const logAppVersionEpic = () => (
of(appVersion)
.pipe(
tap(console.info),
ignoreElements(),
)
)
// Another Option for Redux-Observable 1.x.x
@Sawtaytoes
Sawtaytoes / storeUserInfoEpic.js
Created September 30, 2018 05:31
You might want to modify Redux's state using an epic. This is as easy as listening for an action and calling another action.
const storeUserInfoEpic = (
action$,
) => (
action$
.pipe(
ofType(UPDATE_USER_INFO),
map(({
authInfo,
userInfo,
}) => ({
@Sawtaytoes
Sawtaytoes / reduxObservableMiddleware.js
Last active September 30, 2018 05:45
A basic example of the Redux-Observable middleware function.
const reduxObservableMiddleware = (
store => {
const reduxObservable$ = (
new Subject()
)
reduxObservable$
.subscribe(store.dispatch)
return (
@Sawtaytoes
Sawtaytoes / timer.js
Created September 30, 2018 05:51
An example implementation of Redux-Observable's `timer` observable.
const timer = (
timeToWait,
) => (
Observable
.create(observer => (
setTimeout(
() => {
observer.next(0)
observer.complete()
},
@Sawtaytoes
Sawtaytoes / userInfoReducer.js
Last active September 30, 2018 05:59
An example of a simplified `userInfoReducer` can be used if Redux-Observable handles data transformations.
const initialState = {}
const reducerActions = {
[STORE_USER_INFO]: (
(prevState, { data }) => (
data
|| initialState
)
),
}
@Sawtaytoes
Sawtaytoes / hideLoginModalEpic.js
Last active September 30, 2018 06:28
This method checks the redux state after an action dispatches and calls the close-modal action.
const hideLoginModalEpic = (
action$,
state$,
) => (
action$
.pipe(
ofType(UPDATE_AUTH_INFO),
map(() => (
state$
.value
@Sawtaytoes
Sawtaytoes / hideLoginModalEpic.js
Last active September 30, 2018 06:40
This method listens to `state$` directly from Redux-Observable and decouples state updates from dispatched actions.
const hideLoginModalEpic = (
action$,
state$,
) => (
state$
.pipe(
map(authInfoSelector),
pluck('isAuthenticated'),
distinctUntilChanged(),
filter(Boolean),