This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathipc-promise.js
169 lines (146 loc) · 4.91 KB
/
ipc-promise.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
* @license ipc-promise Copyright(c) 2015 sasa+1
* https://github.com/sasaplus1/ipc-promise
* Released under the MIT license.
*/
/**
* export to AMD/CommonJS/global.
*
* @param {Object} root root object.
* @param {Function} factory factory method.
*/
(function(root, factory){
'use strict';
if (typeof define === 'function' && define.amd) {
define(['electron', 'events'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('electron'), require('events'));
} else {
root.ipcPromise = factory(global.require('electron'), global.require('events'));
}
}((this || 0).self || global, function(electron, events){
'use strict';
var ipcMain = electron.ipcMain,
ipcRenderer = electron.ipcRenderer;
// constants
var COMMON_EVENT_NAME = 'ipc-promise-common-event',
SUCCESS_EVENT_SUFFIX = '-success',
FAILURE_EVENT_SUFFIX = '-failure',
COMMON_SUCCESS_EVENT_NAME = COMMON_EVENT_NAME + SUCCESS_EVENT_SUFFIX,
COMMON_FAILURE_EVENT_NAME = COMMON_EVENT_NAME + FAILURE_EVENT_SUFFIX;
// common event emitter
var cee = new events.EventEmitter();
// get temporary event name for success
function getSuccessEventName(eventName, id) {
return eventName + id.toString() + SUCCESS_EVENT_SUFFIX;
}
// get temporary event name for failure
function getFailureEventName(eventName, id) {
return eventName + id.toString() + FAILURE_EVENT_SUFFIX;
}
/**
* common event handler for ipc.
*
* @private
* @param {Event} event event object.
* @param {Object} arg argument object.
*/
function commonEventHandler(event, arg) {
// NOTE: send from renderer process always.
var successEventName = getSuccessEventName(arg.eventName, arg.id);
var failureEventName = getFailureEventName(arg.eventName, arg.id);
var onSuccess = function(result) {
// send success to ipc for renderer process.
event.sender.send(COMMON_SUCCESS_EVENT_NAME, {
data: result,
eventName: arg.eventName,
id: arg.id
});
cee.removeListener(successEventName, onSuccess);
cee.removeListener(failureEventName, onFailure);
};
var onFailure = function(result) {
// send failure to ipc for renderer process.
event.sender.send(COMMON_FAILURE_EVENT_NAME, {
data: result,
eventName: arg.eventName,
id: arg.id
});
cee.removeListener(successEventName, onSuccess);
cee.removeListener(failureEventName, onFailure);
};
// add listener to common event emitter for main process.
cee.on(successEventName, onSuccess);
cee.on(failureEventName, onFailure);
// emit to common event emitter for main process.
cee.emit(arg.eventName, arg.id, arg.data, event);
}
/**
* trigger event.
*
* @param {String} eventName event name of common event emitter on main process.
* @param {*} data data for send.
* @return {Promise} promise.
*/
function send(eventName, data) {
// NOTE: call from renderer process always.
return new Promise(function(resolve, reject) {
var id = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
var onSuccess = function(event, params) {
if (params.id !== id || params.eventName !== eventName) {
return;
}
// remove this listener.
ipcRenderer.removeListener(COMMON_SUCCESS_EVENT_NAME, onSuccess);
ipcRenderer.removeListener(COMMON_FAILURE_EVENT_NAME, onFailure);
resolve(params.data);
};
var onFailure = function(event, params) {
if (params.id !== id || params.eventName !== eventName) {
return;
}
// remove this listener.
ipcRenderer.removeListener(COMMON_SUCCESS_EVENT_NAME, onSuccess);
ipcRenderer.removeListener(COMMON_FAILURE_EVENT_NAME, onFailure);
reject(params.data);
};
// add listener to ipc for renderer process.
ipcRenderer.on(COMMON_SUCCESS_EVENT_NAME, onSuccess);
ipcRenderer.on(COMMON_FAILURE_EVENT_NAME, onFailure);
// send to ipc for main process.
ipcRenderer.send(COMMON_EVENT_NAME, {
data: data,
eventName: eventName,
id: id
});
});
}
/**
* listen event.
*
* @param {String} event event name.
* @param {Function} listener listener function.
*/
function on(event, listener) {
// call from main process always.
// add listener to common event emitter for main process.
cee.on(event, function(id, data, ipcEvent) {
listener(data, ipcEvent)
.then(function(result) {
cee.emit(getSuccessEventName(event, id), result);
})
.catch(function(result) {
cee.emit(getFailureEventName(event, id), result);
});
});
}
// main process
if (typeof window === 'undefined') {
// add common event handler for ipc of main process.
ipcMain.on(COMMON_EVENT_NAME, commonEventHandler);
}
return {
on: on,
send: send
};
}));