made with esnextbin
Last active
September 20, 2016 20:34
-
-
Save tijhaart/9579ed6557496273aaf9ca7b722ad431 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>ESNextbin Sketch</title> | |
| <!-- put additional styles and scripts here --> | |
| </head> | |
| <body> | |
| <!-- put markup and other contents here --> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createStore, applyMiddleware } from 'redux'; | |
| import createSageMiddleware, { takeEvery, delay } from 'redux-saga'; | |
| import { take, fork, call, put, race } from 'redux-saga/effects'; | |
| import u from 'updeep'; | |
| import debug from 'debug'; | |
| debug.enable('*'); | |
| const log = debug('snackbar'); | |
| console.clear(); | |
| function* hide() { | |
| yield call(delay, 2000); | |
| yield put({ type: 'HIDE_SNACKBAR' }); | |
| } | |
| function* snackbarSaga() { | |
| yield take('SHOW_SNACKBAR'); | |
| yield race({ | |
| hide: call(hide), | |
| cancel: take('SHOW_SNACKBAR') | |
| }); | |
| yield put({ type: 'HIDE_SNACKBAR' }); | |
| } | |
| function* rootSaga() { | |
| yield [ | |
| fork(snackbarSaga) | |
| ]; | |
| } | |
| function setupStore(rootReducer, middleware) { | |
| const store = createStore( | |
| rootReducer, | |
| applyMiddleware(middleware) | |
| ); | |
| return store; | |
| } | |
| function setupSaga() { | |
| return createSageMiddleware(); | |
| } | |
| function rootReducer(state = {}, action) { | |
| log('type: %s', action.type); | |
| if (action.type === 'SHOW_SNACKBAR') { | |
| state = u({ visible: true }, state); | |
| } | |
| if (action.type === 'HIDE_SNACKBAR') { | |
| state = u({ visible: false }, state); | |
| } | |
| return state; | |
| } | |
| const saga = setupSaga(); | |
| const store = setupStore(rootReducer, saga); | |
| store.subscribe(() => { | |
| console.debug(store.getState()); | |
| }); | |
| saga.run(rootSaga); | |
| store.dispatch({ | |
| type: 'SHOW_SNACKBAR' | |
| }); | |
| setTimeout(() => { | |
| store.dispatch({ | |
| type: 'SHOW_SNACKBAR' | |
| }); | |
| }, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "esnextbin-sketch", | |
| "version": "0.0.0", | |
| "dependencies": { | |
| "redux": "3.5.2", | |
| "redux-saga": "0.11.1", | |
| "updeep": "0.16.1", | |
| "debug": "2.2.0", | |
| "babel-runtime": "6.11.6" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var _regenerator = require('babel-runtime/regenerator'); | |
| var _regenerator2 = _interopRequireDefault(_regenerator); | |
| var _redux = require('redux'); | |
| var _reduxSaga = require('redux-saga'); | |
| var _reduxSaga2 = _interopRequireDefault(_reduxSaga); | |
| var _effects = require('redux-saga/effects'); | |
| var _updeep = require('updeep'); | |
| var _updeep2 = _interopRequireDefault(_updeep); | |
| var _debug = require('debug'); | |
| var _debug2 = _interopRequireDefault(_debug); | |
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| var _marked = [hide, snackbarSaga, rootSaga].map(_regenerator2.default.mark); | |
| _debug2.default.enable('*'); | |
| var log = (0, _debug2.default)('snackbar'); | |
| console.clear(); | |
| function hide() { | |
| return _regenerator2.default.wrap(function hide$(_context) { | |
| while (1) { | |
| switch (_context.prev = _context.next) { | |
| case 0: | |
| _context.next = 2; | |
| return (0, _effects.call)(_reduxSaga.delay, 2000); | |
| case 2: | |
| _context.next = 4; | |
| return (0, _effects.put)({ type: 'HIDE_SNACKBAR' }); | |
| case 4: | |
| case 'end': | |
| return _context.stop(); | |
| } | |
| } | |
| }, _marked[0], this); | |
| } | |
| function snackbarSaga() { | |
| return _regenerator2.default.wrap(function snackbarSaga$(_context2) { | |
| while (1) { | |
| switch (_context2.prev = _context2.next) { | |
| case 0: | |
| _context2.next = 2; | |
| return (0, _effects.take)('SHOW_SNACKBAR'); | |
| case 2: | |
| _context2.next = 4; | |
| return (0, _effects.race)({ | |
| hide: (0, _effects.call)(hide), | |
| cancel: (0, _effects.take)('SHOW_SNACKBAR') | |
| }); | |
| case 4: | |
| _context2.next = 6; | |
| return (0, _effects.put)({ type: 'HIDE_SNACKBAR' }); | |
| case 6: | |
| case 'end': | |
| return _context2.stop(); | |
| } | |
| } | |
| }, _marked[1], this); | |
| } | |
| function rootSaga() { | |
| return _regenerator2.default.wrap(function rootSaga$(_context3) { | |
| while (1) { | |
| switch (_context3.prev = _context3.next) { | |
| case 0: | |
| _context3.next = 2; | |
| return [(0, _effects.fork)(snackbarSaga)]; | |
| case 2: | |
| case 'end': | |
| return _context3.stop(); | |
| } | |
| } | |
| }, _marked[2], this); | |
| } | |
| function setupStore(rootReducer, middleware) { | |
| var store = (0, _redux.createStore)(rootReducer, (0, _redux.applyMiddleware)(middleware)); | |
| return store; | |
| } | |
| function setupSaga() { | |
| return (0, _reduxSaga2.default)(); | |
| } | |
| function rootReducer() { | |
| var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | |
| var action = arguments[1]; | |
| log('type: %s', action.type); | |
| if (action.type === 'SHOW_SNACKBAR') { | |
| state = (0, _updeep2.default)({ visible: true }, state); | |
| } | |
| if (action.type === 'HIDE_SNACKBAR') { | |
| state = (0, _updeep2.default)({ visible: false }, state); | |
| } | |
| return state; | |
| } | |
| var saga = setupSaga(); | |
| var store = setupStore(rootReducer, saga); | |
| store.subscribe(function () { | |
| console.debug(store.getState()); | |
| }); | |
| saga.run(rootSaga); | |
| store.dispatch({ | |
| type: 'SHOW_SNACKBAR' | |
| }); | |
| setTimeout(function () { | |
| store.dispatch({ | |
| type: 'SHOW_SNACKBAR' | |
| }); | |
| }, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment