-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
55 lines (45 loc) · 1.54 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var ipcMain = require('electron').ipcMain
function Server (webContents) {
this.methods = {}
this.webContents = webContents
this._requestMessageHandler = requestMessageHandler.bind(this)
ipcMain.on('request-message', this._requestMessageHandler)
}
Server.prototype.send = function (action, body) {
var response = {action: action, body: body}
sendResponse.call(this, response)
}
function sendResponse (response) {
if (!this.webContents) return console.error(new Error("The electron-rpc Server isn't configured. Please use server.configure(eventEmitter)"))
this.webContents.send('response-message', response)
}
Server.prototype.configure = function (webContents) {
this.webContents = webContents
return this
}
Server.prototype.on = function (action, callback) {
this.methods[action] = callback
return this
}
Server.prototype.destroy = function () {
this.methods = {}
this.webContents = undefined
ipcMain.removeListener('request-message', this._requestMessageHandler)
}
function requestMessageHandler (evt, data) {
var self = this
var response = {id: data.id, action: data.action}
var request = {id: data.id, action: data.action, body: data.body}
var actionHandler = self.methods[request.action]
if (!actionHandler) {
response.error = {message: 'Route not found', statusCode: 404}
return sendResponse.call(self, response)
} else {
actionHandler(request, function (error, body) {
response.error = error
response.body = body
sendResponse.call(self, response)
})
}
}
module.exports = Server