|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 - present Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +/*global require, Promise, exports*/ |
| 25 | +/*eslint no-invalid-this: 0*/ |
| 26 | +/*eslint max-len: ["error", { "code": 200 }]*/ |
| 27 | +(function () { |
| 28 | + |
| 29 | + "use strict"; |
| 30 | + |
| 31 | + var EventEmitter = require("events"), |
| 32 | + bracketsEventHandler = new EventEmitter(); |
| 33 | + |
| 34 | + /** https://gist.github.com/LeverOne/1308368 */ |
| 35 | + /*eslint-disable */ |
| 36 | + function _generateUUID() { |
| 37 | + var result, |
| 38 | + numericSeed; |
| 39 | + for ( |
| 40 | + result = numericSeed = ''; |
| 41 | + numericSeed++ < 36; |
| 42 | + result += numericSeed * 51 & 52 ? (numericSeed ^ 15 ? 8 ^ Math.random() * (numericSeed ^ 20 ? 16 : 4) : 4).toString(16) : '-' |
| 43 | + ); |
| 44 | + |
| 45 | + return result; |
| 46 | + } |
| 47 | + /*eslint-enable */ |
| 48 | + |
| 49 | + function NodeToBracketsInterface(domainManager, domainName) { |
| 50 | + this.domainManager = domainManager; |
| 51 | + this.domainName = domainName; |
| 52 | + this.nodeFn = {}; |
| 53 | + |
| 54 | + this._registerDataEvents(); |
| 55 | + } |
| 56 | + |
| 57 | + NodeToBracketsInterface.prototype.processRequest = function (params) { |
| 58 | + var methodName = params.method; |
| 59 | + if (this.nodeFn[methodName]) { |
| 60 | + var method = this.nodeFn[methodName]; |
| 61 | + return method.call(null, params.params); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + NodeToBracketsInterface.prototype.processAsyncRequest = function (params, resolver) { |
| 66 | + var methodName = params.method; |
| 67 | + if (this.nodeFn[methodName]) { |
| 68 | + var method = this.nodeFn[methodName]; |
| 69 | + method.call(null, params.params) //The Async function should return a promise |
| 70 | + .then(function (result) { |
| 71 | + resolver(null, result); |
| 72 | + }).catch(function (err) { |
| 73 | + resolver(err, null); |
| 74 | + }); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + NodeToBracketsInterface.prototype.processResponse = function (params) { |
| 79 | + if (params.requestId) { |
| 80 | + if (params.error) { |
| 81 | + bracketsEventHandler.emit(params.requestId, params.error); |
| 82 | + } else { |
| 83 | + bracketsEventHandler.emit(params.requestId, false, params.params); |
| 84 | + } |
| 85 | + } else { |
| 86 | + bracketsEventHandler.emit(params.requestId, "error"); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + NodeToBracketsInterface.prototype.createInterface = function (methodName, respond) { |
| 91 | + var self = this; |
| 92 | + return function (params) { |
| 93 | + var callObject = { |
| 94 | + method: methodName, |
| 95 | + params: params |
| 96 | + }; |
| 97 | + |
| 98 | + var retval = undefined; |
| 99 | + if (respond) { |
| 100 | + var requestId = _generateUUID(); |
| 101 | + |
| 102 | + callObject["respond"] = true; |
| 103 | + callObject["requestId"] = requestId; |
| 104 | + |
| 105 | + self.domainManager.emitEvent(self.domainName, "data", callObject); |
| 106 | + |
| 107 | + retval = new Promise(function (resolve, reject) { |
| 108 | + bracketsEventHandler.once(requestId, function (err, response) { |
| 109 | + if (err) { |
| 110 | + reject(err); |
| 111 | + } else { |
| 112 | + resolve(response); |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | + } else { |
| 117 | + self.domainManager.emitEvent(self.domainName, "data", callObject); |
| 118 | + } |
| 119 | + return retval; |
| 120 | + }; |
| 121 | + }; |
| 122 | + |
| 123 | + NodeToBracketsInterface.prototype.registerMethod = function (methodName, methodHandle) { |
| 124 | + var self = this; |
| 125 | + if (methodName && methodHandle && |
| 126 | + typeof methodName === "string" && typeof methodHandle === "function") { |
| 127 | + self.nodeFn[methodName] = methodHandle; |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + NodeToBracketsInterface.prototype.registerMethods = function (methodList) { |
| 132 | + var self = this; |
| 133 | + methodList.forEach(function (methodObj) { |
| 134 | + self.registerMethod(methodObj.methodName, methodObj.methodHandle); |
| 135 | + }); |
| 136 | + }; |
| 137 | + |
| 138 | + NodeToBracketsInterface.prototype._registerDataEvents = function () { |
| 139 | + if (!this.domainManager.hasDomain(this.domainName)) { |
| 140 | + this.domainManager.registerDomain(this.domainName, { |
| 141 | + major: 0, |
| 142 | + minor: 1 |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + this.domainManager.registerCommand( |
| 147 | + this.domainName, |
| 148 | + "data", |
| 149 | + this.processRequest.bind(this), |
| 150 | + false, |
| 151 | + "Receives sync request from brackets", |
| 152 | + [ |
| 153 | + { |
| 154 | + name: "params", |
| 155 | + type: "object", |
| 156 | + description: "json object containing message info" |
| 157 | + } |
| 158 | + ], |
| 159 | + [] |
| 160 | + ); |
| 161 | + |
| 162 | + this.domainManager.registerCommand( |
| 163 | + this.domainName, |
| 164 | + "response", |
| 165 | + this.processResponse.bind(this), |
| 166 | + false, |
| 167 | + "Receives response from brackets for an earlier request", |
| 168 | + [ |
| 169 | + { |
| 170 | + name: "params", |
| 171 | + type: "object", |
| 172 | + description: "json object containing message info" |
| 173 | + } |
| 174 | + ], |
| 175 | + [] |
| 176 | + ); |
| 177 | + |
| 178 | + this.domainManager.registerCommand( |
| 179 | + this.domainName, |
| 180 | + "asyncData", |
| 181 | + this.processAsyncRequest.bind(this), |
| 182 | + true, |
| 183 | + "Receives async call request from brackets", |
| 184 | + [ |
| 185 | + { |
| 186 | + name: "params", |
| 187 | + type: "object", |
| 188 | + description: "json object containing message info" |
| 189 | + }, |
| 190 | + { |
| 191 | + name: "resolver", |
| 192 | + type: "function", |
| 193 | + description: "callback required to resolve the async request" |
| 194 | + } |
| 195 | + ], |
| 196 | + [] |
| 197 | + ); |
| 198 | + |
| 199 | + this.domainManager.registerEvent( |
| 200 | + this.domainName, |
| 201 | + "data", |
| 202 | + [ |
| 203 | + { |
| 204 | + name: "params", |
| 205 | + type: "object", |
| 206 | + description: "json object containing message info to pass to brackets" |
| 207 | + } |
| 208 | + ] |
| 209 | + ); |
| 210 | + }; |
| 211 | + |
| 212 | + exports.NodeToBracketsInterface = NodeToBracketsInterface; |
| 213 | +}()); |
0 commit comments