| 203 | } |
| 204 | |
| 205 | sync(type, data) { |
| 206 | return new Promise((resolve, reject) => { |
| 207 | const id = getUUID(); |
| 208 | const timeout = setTimeout(() => { |
| 209 | delete this.syncDoneHandler[id]; |
| 210 | reject(new Error('Sync timeout')); |
| 211 | }, this.TIMEOUT * 1000); |
| 212 | this.ports.syncHandler.emit('sync-event', { |
| 213 | type, |
| 214 | data, |
| 215 | id |
| 216 | }); |
| 217 | this.syncDoneHandler[id] = (err, data) => { |
| 218 | if (timeout) { |
| 219 | clearTimeout(timeout); |
| 220 | } |
| 221 | if (err) { |
| 222 | reject(err); |
| 223 | } else { |
| 224 | resolve(data); |
| 225 | } |
| 226 | }; |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | syncDone(data) { |
| 231 | if (this.syncDoneHandler[data.id]) { |