Skip to content

Is there best practice to use setInterval() in redux-ship? #11

@kuy

Description

@kuy

Hi, I'm new to redux-ship and have an experience of redux-saga.
I want to create a periodic timer which dispatches TIME_TICK action every second with redux-ship.
In redux-ship, however, a controller and an effect handler are event-driven.
Is there a good solution to handle setInterval() in redux-ship way?

Here is my example using setTimeout() instead of setInterval().
Project: https://github.com/kuy/lifegame-redux
Source Code: https://github.com/kuy/lifegame-redux/tree/master/src/ship

// From controllers.js

export default function* control(action) {
  switch (action.type) {
  case TIME_PLAY: {
    while (yield* Ship.getState(selectors.time)) {
      yield* Ship.call({ type: Effect.DELAY });
      yield* Ship.commit(timeTick());
    }
    return;
  }
  default:
    return;
  }
}
// From effects.js

function delay(msec) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(msec);
    }, msec);
  });
}

export async function run(effect) {
  switch (effect.type) {
  case DELAY:
    return await delay(PERIOD);
  default:
    return;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions