Last active
March 8, 2018 17:27
-
-
Save kechol/3b89dc2b37d8abcdcefbba70a00737c7 to your computer and use it in GitHub Desktop.
This file contains 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 { IpcMessageEvent, ipcRenderer } from 'electron' | |
import { Channel, eventChannel, SagaIterator } from 'redux-saga'; | |
import { apply, call, fork, put, take } from 'redux-saga/effects'; | |
import { ActionCreator } from 'typescript-fsa' | |
function createIpcChannel(action: ActionCreator<any>): Channel<{}> { | |
return eventChannel(emit => { | |
ipcRenderer.on(action.type, (_event: IpcMessageEvent, payload: any) => { | |
emit(payload); | |
}); | |
// tslint:disable-next-line:no-empty | |
return () => {}; | |
}); | |
} | |
function* watchIpcReceiver(action: ActionCreator<any>): SagaIterator { | |
const ipcChannel = yield call(createIpcChannel, action); | |
while(true) { | |
const payload = yield take(ipcChannel); | |
yield put({ type: action.type, payload }); | |
} | |
} | |
function* watchIpcSender(action: ActionCreator<any>): SagaIterator { | |
while (true) { | |
const request = yield take(action.type); | |
yield apply(ipcRenderer, ipcRenderer.send, [request.type, request.payload]); | |
} | |
} | |
export function createWatchIpcReceiver(action: ActionCreator<any>) { | |
return () => watchIpcReceiver(action); | |
} | |
export function createWatchIpcSender(action: ActionCreator<any>) { | |
return () => watchIpcSender(action); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment