-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
36295c0
commit 287834b
Showing
27 changed files
with
690 additions
and
244 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { EventEmitter } = require( 'events' ); | ||
const { ipcMain } = require( 'electron' ); | ||
const uuid = require( 'uuid' ); | ||
|
||
class Server extends EventEmitter { | ||
|
||
constructor ( win ) { | ||
super(); | ||
this.win = win; | ||
this.ipcListener = this.ipcListener.bind( this ); | ||
|
||
if ( this.destroyed ) { | ||
return; | ||
} | ||
|
||
const uid = uuid.v4(); | ||
this.id = uid; | ||
|
||
ipcMain.on( uid, this.ipcListener ); | ||
|
||
// we intentionally subscribe to `on` instead of `once` | ||
// to support reloading the window and re-initializing | ||
// the channel | ||
this.wc.on( 'did-finish-load', () => { | ||
this.wc.send( 'init', uid ); | ||
} ); | ||
} | ||
|
||
get wc () { | ||
return this.win.webContents; | ||
} | ||
|
||
ipcListener ( event, { ev, data } ) { | ||
super.emit( ev, data ); | ||
} | ||
|
||
emit ( ch, data ) { | ||
this.wc.send( this.id, { ch, data } ); | ||
} | ||
|
||
destroy () { | ||
this.removeAllListeners(); | ||
this.wc.removeAllListeners(); | ||
|
||
if ( this.id ) { | ||
ipcMain.removeListener( this.id, this.ipcListener ); | ||
} else { | ||
// mark for `genUid` in constructor | ||
this.destroyed = true; | ||
} | ||
} | ||
|
||
} | ||
|
||
module.exports = function createRPC ( win ) { | ||
return new Server( win ); | ||
}; |
Oops, something went wrong.