Skip to content

Instantly share code, notes, and snippets.

@CharlieHess
Last active September 20, 2017 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharlieHess/b3d6312e3e961e2f9f9a949bb0a32833 to your computer and use it in GitHub Desktop.
Save CharlieHess/b3d6312e3e961e2f9f9a949bb0a32833 to your computer and use it in GitHub Desktop.
import { webContents as WebContents } from 'electron';
import { NOTIFICATIONS } from '../constants/actions';
import {
notificationClickCode,
notificationReplyCode
} from '../constants/code';
/**
* An epic that executes some code in the guest page when the user clicks or
* replies to a native notification.
*/
const notificationActionsEpic: Epic<Action, State> = (
action$: ActionsObservable<Action>,
store: MiddlewareAPI<State>
) => {
// We can monitor as many different action types as we want…
const clickOrReplyAction = action$.ofType(
NOTIFICATIONS.CLICK_NOTIFICATION,
NOTIFICATIONS.REPLY_TO_NOTIFICATION
);
return clickOrReplyAction
.filter(({ payload }) => payload && payload.workspaceId)
.flatMap((action) => webContentsDomReady(action))
.map(({ id, action }) => ({
id,
code: action.type === NOTIFICATIONS.CLICK_NOTIFICATION
? notificationClickCode(action.payload)
: notificationReplyCode(action.payload)
}))
.let(executeJavaScriptOnWebContents);
};
// Map the dom-ready event to an Observable
function webContentsDomReady(action: Action) {
const id = action.payload.workspaceId;
const webContents = WebContents.fromId(id);
return Observable.fromEvent(webContents, 'dom-ready').mapTo({ action, id });
}
// Map executeJavaScript to an Observable
function executeJavaScriptOnWebContents(input: Observable<ExecuteJavaScriptArguments>) {
return input.mergeMap((value: ExecuteJavaScriptArguments) => {
const { id, code } = value;
const webContents = WebContents.fromId(id);
const executeJavaScriptObservable = Observable.bindCallback(webContents.executeJavaScript);
return executeJavaScriptObservable(code, false);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment