Skip to content

Instantly share code, notes, and snippets.

@slorber
Last active August 17, 2020 17:44
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save slorber/7ae19043ffb43d60ff17 to your computer and use it in GitHub Desktop.
Save slorber/7ae19043ffb43d60ff17 to your computer and use it in GitHub Desktop.
function* runTimer(getState) {
while(yield take('START')) {
while(true) {
const {stop, tick} = yield race({
stop : take('STOP'),
tick : call(wait, ONE_SECOND);
})
if ( !stop ) {
yield put(actions.tick());
} else {
break;
}
}
}
}
@d4ncer
Copy link

d4ncer commented Jan 16, 2016

You want to check for

if (!stop) { ... }

rather than

if (tick) { ... }

because

call(wait, ONE_SECOND)

returns undefined, causing you to break out of your loop on the first iteration.

@slorber
Copy link
Author

slorber commented Jan 16, 2016

@d4ncer thanks fixed

@d4ncer
Copy link

d4ncer commented Jan 16, 2016

👍

@mrsoto
Copy link

mrsoto commented Sep 10, 2016

I like this approach just that this if waist my brain. I don't like else

if ( stop ) {
break;
}
yield put(actions.tick());

@granmoe
Copy link

granmoe commented Sep 11, 2016

I like @mrsoto's suggestion. A little one-liner is more readable:

if (stop) { break }
yield put(actions.tick())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment