使用redux-saga的进程队列 [英] Process queue with redux-saga

查看:107
本文介绍了使用redux-saga的进程队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个队列处理程序,以使用redux-saga生成器来管理通知.

I am trying to implement a queue handler for managing notifications with a redux-saga generator.

基本上,我需要在通知进入队列时顺序显示它们.

Basically, I need to show notifications sequentually as they enter the queue.

为此,我在redux存储中有一个 queue 数组,将一个 QUQUE_NOTIFICATION 动作添加到队列中,并通过 SHOW_NOTIFICATION 删除了一个队列通知.

For this, I have a queue array in the redux store, an action QUQUE_NOTIFICATION action to add to queue and SHOW_NOTIFICATION to remove a notification for queue.

我当前的传奇实现很简单:

My current saga implementation is that simple :

export function* watchQueue() {
    while (true) {
        const state = yield select()
        const queue = state.queue
        if (queue.length > 0) {
            yield put({ action: 'SHOW_NOTIFICATION', queue[0])
        }
        yield call(delay, 5000);
      }
    }
}

当前实现的问题是,当队列为空时,调度了 QUQUE_NOTIFICATION 的生​​成器可以等待延迟完成.
但是,我想尽快显示第一个通知当它进入队列时.有什么想法吗?

The problem with current implementation is that when a queue is empty a QUQUE_NOTIFICATION is dispatched generator can be waiting for the delay to finish.
However, I want to show the first notification as soon as it enters the queue. Any ideas?

推荐答案

我也有相同的想法来显示通知(对它们进行排队),但是saga在渠道方面已提供了已实施的解决方案.我有:

I've had the same idea for showing up notification (queueing them) however saga provides already implemented solution in terms of channels. I have:

export function * notificationSaga () {                                                  
  const requestChan = yield actionChannel(Notification.request)                          
  while (true) {                                                                         
    const { payload } = yield take(requestChan)                                          
    yield call(showNotification, payload)                                                
  }                                                                                      
}

我认为这是对您的问题的完美解决方案.showNotification是另一个功能,它实际上显示通知并稍等片刻,然后将其取下来.

which I believe is elegant solution to your problem. showNotification is another function which actually shows notifications and waits a bit before taking it down.

这篇关于使用redux-saga的进程队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆