Created
          October 12, 2019 10:04 
        
      - 
      
- 
        Save daniel12fsp/bcb473ff41dd7b6010aab6c7d2183fe7 to your computer and use it in GitHub Desktop. 
    WebWorker + Webpack4 + Saga
  
        
  
    
      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 { all, call, take } from 'redux-saga/effects'; | |
| import { takeLatest } from 'redux-saga/effects' | |
| import { createStore, compose, applyMiddleware } from 'redux'; | |
| import createSagaMiddleware from 'redux-saga' | |
| //@ts-ignore | |
| import * as MyWorker from "worker-loader!./trywebWorker" | |
| export function WebWorker() { | |
| return new Promise((resolve, reject) => { | |
| const worker = new MyWorker(); | |
| worker.postMessage("valor qualquer") | |
| worker.addEventListener('message', (e:any) => { | |
| worker.terminate(); | |
| resolve(e.data) | |
| }, false); | |
| }); | |
| } | |
| export function* webWorkerSaga() { | |
| const worker = yield call(WebWorker); | |
| console.log({ worker }); | |
| } | |
| const sagaMiddleware = createSagaMiddleware(); | |
| const reducer = () => ({}); | |
| export const store: any = createStore(reducer, applyMiddleware(sagaMiddleware)); | |
| sagaMiddleware.run(webWorkerSaga) | |
| store.dispatch({ type: "WEB_WORKER_RUN" }) | 
  
    
      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
    
  
  
    
  | const _ = require('lodash') | |
| const obj = { foo1: 'foo' } | |
| console.log(_.has(obj, 'foo')) | |
| // Respond to message from parent thread | |
| self.addEventListener('message', (event) => | |
| // Post data to parent thread | |
| self.postMessage({ foo: 'foo' })) | 
  
    
      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
    
  
  
    
  | { | |
| output: { | |
| filename: 'bundle.js', | |
| globalObject: `(typeof self !== 'undefined' ? self : this)` | |
| }, | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment