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.

How to dispatch in event handler?

See original GitHub issue

I have this function that I call inside one of my sagas

function getDeviceTokenAsync() {
  return new Promise((s,e) => {
    if (window.PushNotification) {
      let push = window.PushNotification.init({
        android: {
          senderID: GCM_SENDER_ID,
          icon: "ic_notification",
          iconColor: PRIMARY_COLOR,
        },
        ios: {
          alert: true,
          badge: false,
          sound: false,
        },
        windows: {
        },
      })
      push.on("registration", s)
      push.on("notification", (data) => {
        console.info("notification: " + JSON.stringify(data))
        //put(actions.refreshPendingTransactions())
      })
      push.on("error", e)
    } else {
      e(new Error("No push notification available"))
    }
  })
}

I call it with yield call(getDeviceTokenAsync)

The question is, how do I dispatch an action to the store whenever the onnotification handler is called by the push service? Should I hand in a function to getDeviceTokenAsync and that function yields a put effect then?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:26 (25 by maintainers)

github_iconTop GitHub Comments

6reactions
yelouaficommented, Jan 11, 2017

@pke eventChannels are now part of redux-saga API

https://redux-saga.github.io/redux-saga/docs/advanced/Channels.html

In your example, it’d be something like

function getDeviceTokenChannel() {
  if (window.PushNotification) {
    return eventChannel((emit) => {
      let push = window.PushNotification.init({...})
      push.on("notification", emit)
      push.on("error", e => emit({error: e})
      // return cleanup logic (called when chan.close() is invoked)
      return () => {}
    })
 } else {
    throw new Error("No push notification available"))
 }
}

function* saga() {
  const pushChan = yield call(getDeviceTokenChannel)
  while(true) {
    const msg = yield take(pushChan)
    if(!msg.error) {
      yield put(actions.refreshPendingTransactions())
    }
  }
}

EDIT : return dispose function from eventChannel factory

2reactions
stoyandamovcommented, Jan 24, 2018

So if you’re really really stuck:

// globals.js
let cachedStore

export const setStore = store => cachedStore = store
export const getStore = () => cachedStore

Wherever you’re creating the store:

const store = createStore(
  combineReducers({...}),
  applyMiddleware(middleware, sagaMiddleware, logger)
)
setStore(store) // <--- this

Wherever you need dispatch:

import {getStore} from '../globals'
const { dispatch } = getStore() // <--- this 
Read more comments on GitHub >

github_iconTop Results From Across the Web

EventTarget.dispatchEvent() - Web APIs | MDN
The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected EventListener s in the ...
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
Unlike "native" events, which are fired by the browser and invoke event handlers asynchronously via the event loop, dispatchEvent() invokes ...
Read more >
JavaScript dispatchEvent(): Generate Events programmatically
JavaScript dispatchEvent · First, create a new Event object using Event constructor. · Then, trigger the event using element.dispatchEvent() method.
Read more >
Handling and dispatching events with Node.js - LogRocket Blog
Learn how to create, dispatch, and manage events in Node.js. ... Instead, we'll discuss how to handle and dispatch events in Node.js.
Read more >
Create and Dispatch Events - Salesforce Developers
Create and dispatch events in a component's JavaScript class. To create an event, use the CustomEvent() constructor. To dispatch an event, call the...
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