Skip to content

Commit

Permalink
Added RPC type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
OrionNebula committed Jun 7, 2018
1 parent 9083d23 commit 11ffa85
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/components/MediaBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MediaPlugin } from '../types/MediaPlugin'
import { Status } from '../types/Status'
import { State } from '../types/State'
import { EventEmitter } from 'events'
import '../types/Rpc'

interface MediaBarProps {
playerManager: PlayerManager
Expand Down Expand Up @@ -40,24 +41,23 @@ function MediaBarFactory (React: typeof ExternReact) {

componentDidMount () {
const { playerManager } = this.props
const { rpc }: { rpc: EventEmitter } = window as any

rpc.on('hyper-media-control:previousTrack', () => {
window.rpc.on('hyper-media-control:previousTrack', () => {
const { plugin } = this.state
if (plugin) this.handleActionResult(plugin.previousTrack())
})

rpc.on('hyper-media-control:playPause', () => {
window.rpc.on('hyper-media-control:playPause', () => {
const { plugin } = this.state
if (plugin) this.handleActionResult(plugin.playPause())
})

rpc.on('hyper-media-control:nextTrack', () => {
window.rpc.on('hyper-media-control:nextTrack', () => {
const { plugin } = this.state
if (plugin) this.handleActionResult(plugin.nextTrack())
})

rpc.on('hyper-media-control:nextPlayer', () => {
window.rpc.on('hyper-media-control:nextPlayer', () => {
this.cyclePlugin()
})

Expand Down
9 changes: 5 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MediaPluginConstructor, MediaPlugin } from './types/MediaPlugin'
import { State } from './types/State'
import { Status } from './types/Status'
import { FooterFactory } from './components/Footer'
import './types/Rpc'
import * as ExternReact from 'react'

const parentPluginName = 'hyper-media-control'
Expand Down Expand Up @@ -80,7 +81,7 @@ function decorateMenu (menu) {
submenu: [
{
label: 'Previous Track',
click: (clickedItem, focusedWindow) => {
click: (clickedItem, focusedWindow: Window) => {
if (focusedWindow) {
focusedWindow.rpc.emit('hyper-media-control:previousTrack', { focusedWindow })
}
Expand All @@ -89,15 +90,15 @@ function decorateMenu (menu) {
{
label: 'Play/Pause',
accelerator: `CmdOrCtrl+Alt+Space`,
click: (clickedItem, focusedWindow) => {
click: (clickedItem, focusedWindow: Window) => {
if (focusedWindow) {
focusedWindow.rpc.emit('hyper-media-control:playPause', { focusedWindow })
}
}
},
{
label: 'Next Track',
click: (clickedItem, focusedWindow) => {
click: (clickedItem, focusedWindow: Window) => {
if (focusedWindow) {
focusedWindow.rpc.emit('hyper-media-control:nextTrack', { focusedWindow })
}
Expand All @@ -106,7 +107,7 @@ function decorateMenu (menu) {
{
label: 'Next Media Player',
accelerator: `CmdOrCtrl+Shift+Space`,
click: (clickedItem, focusedWindow) => {
click: (clickedItem, focusedWindow: Window) => {
if (focusedWindow) {
focusedWindow.rpc.emit('hyper-media-control:nextPlayer', { focusedWindow })
}
Expand Down
15 changes: 15 additions & 0 deletions src/types/Rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare global {
interface Window {
rpc: RpcClient
}
}

export interface RpcClient {
on (ev: string, fn: (...args: any[]) => void)
once (ev: string, fn: (...args: any[]) => void)
emit (ev: string, data: any)
removeListener (ev: string, fn: (...args: any[]) => void)
removeAllListeners ()
destroy ()
ipcListener (event: any, { ch, data }: { ch: string, data: any })
}

0 comments on commit 11ffa85

Please sign in to comment.