Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 2x 2x 6340x 6340x 6340x 6340x 6340x 2x 8432x 8432x 5582x 5582x 5644x 5582x 17490x 11908x 5610x 5582x 5582x 5582x 5582x 5422x 5422x 8434x 8434x 8290x 8290x 8290x 8290x 8278x 8278x 12x 12x 12x 8290x 8290x 8288x 11606x 384x 11222x 8432x 8432x 2790x 2790x 5568x 5568x 5568x 5568x 35633x 11606x 10163x 2x 2x 8288x | /**
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {commons} from '../../../coral-utils';
const SCOPE_SELECTOR = ':scope > ';
/**
* Messenger will used to pass the messages from child component to its parent. Currently we are relying on
* events to do the job. When a large DOM is connected, these events as a bulk leads to delays.
* With the use of messenger we will directly call the parent method provided in the observed messages list.
* The current implmentation only supports one to many mapping i.e. one parent and many children and any
* in child property will result in execution of only one parent method. This should be used purely for
* coral internal events and not any DOM based or public events.
*
* Limitations :
* - This doesnot support the case where any change in child property, needs to be notified
* to two or more parents. This is achievable, but not currently supported.
* - Use this to post message only coral internal events.
* - Do not use for DOM events or public events.
* @private
*/
class Messenger {
/** @ignore */
constructor(element) {
this._element = element;
this._connected = false;
this._clearQueue();
this._clearListeners();
}
/**
* checks whether Messenger is connected or not.
* @returns {Boolean} true if connected
* @private
*/
get isConnected() {
return this._connected === true;
}
/**
* checks whether the event is silenced or not
* @returns {Boolean} true if silenced
* @private
*/
get isSilenced() {
return this._element._silenced === true;
}
/**
* specifies the list of listener attached to messenger.
* @Ereturns {Array} array of listeners
* @private
*/
get listeners() {
return this._listeners;
}
/**
* add a message to the queue only if messenger is not connected
* message will be added only if element is not connected.
* @private
*/
_addMessageToQueue(message, detail) {
if(!this.isConnected) {
this._queue.push({
message: message,
detail: detail
});
}
}
/**
* executes the stored queue messages.
* It will be executed when element is connected.
* @private
*/
_executeQueue() {
this._queue.forEach((options) => {
this._postMessage(options.message, options.detail);
});
this._clearQueue();
}
/**
* empty the stored queue message
* @private
*/
_clearQueue() {
this._queue = [];
}
/**
* clears the listeners
* @private
*/
_clearListeners() {
this._listeners = [];
}
/**
* element should call this method when they are connected in DOM.
* its the responsibility of the element to call this hook
* @triggers `${element.tagName.toLowerCase()}:_messengerconnected`
* @private
*/
connect() {
if(!this.isConnected) {
let element = this._element;
this._connected = true;
element.trigger(`${element.tagName.toLowerCase()}:_messengerconnected`, {
handler : this.registerListener.bind(this)
});
// post all stored messages
this._executeQueue();
}
}
/**
* add the listener to messenger
* this handler will be passed when messengerconnect event is trigger
* the handler needs to be executed by listeners.
* @private
*/E
registerListener(listener) {
if(listener) {
this._listeners.push(listener);
}
}
/**
* post the provided message to all listener.
* @param {String} message which should be posted
* @param {Object} additional detail which needs to be posted.
* @private
*/
_postMessage(message, detail) {
let element = this._element;
this.listeners.forEach((listener) => {
let observedMessages = listener.observedMessages;
let messageInfo = observedMessages[message];
ifE(messageInfo) {
let selector;
let handler;
if(typeof messageInfo === 'string') {
selector = "*";
handler = messageInfo;
} else if(typeof messageInfo === 'object') {
selectoEr = messageInfo.selector || "*";
handler = messageInfo.handler;
}
if(selector.indexOf(SCOPE_SELECTOR) === 0 ) {
Iif(!listener.id) {
listener.id = commons.getUID();
}
selector = selector.replace(SCOPE_SELECTOR, `#${listener.id} > `);
}
if(element.matches(selector)) {
listener[handler].call(listener, new Event({
target: element,
detail: detail,
type: message,
currentTarget: listener
}));
}
}
});
}
/**
* post the provided message to all listener,
* along with validating silencing and storing in queue
* @param {String} message which should be posted
* @param {Object} additional detail which needs to be posted.
* @private
*/
postMessage(message, detail) {
if(this.isSilenced) {
return;
}
if(!this.isConnected) {
this._addMessageToQueue(message, detail);
return;
}
// element got disconnect and messenger not notified.
if(!this._element.isConnected) {
// disconnect messenger and again post the same message,
// message will get store in queue.
this.disconnect();
this.postMessage(message, detail);
Ireturn;
}
this._postMessage(message, detail);
}
/**
* element should call this method when they are disconnected from DOM.
* Its the responsibility of the element to call this hook
* @private
*/
disconnect() {
if(this.isConnected) {
this._connected = false;
this._clearListeners();
this._clearQueue();
}
}
}
E
/**
* This Event class is just a bogus class, current message callback aspects
* actual event as a parameter, since we are directly calling the method instead
* of triggering event, will pass an instance of this disguised object,
* to avoid breaks.
* This just disguise the most used functionality of event object
* @private
*/
class Event {
constructor(options) {
this._detail = options.detail;
this._target = options.target;
this._type = options.type;
this._currentTarget = options.currentTarget;
this._defaultPrevented = false;
this._propagationStopped = false;
this._immediatePropagationStopped = false;
}
get detail() {
return this._detail;
}
get type() {
return this._type;
}
get target() {
return this._target;
}
get currentTarget() {
return this._currentTarget;
}
preventDefault() {
this._defaultPrevented = true;
}
stopPropagation() {
this._propagationStopped = true;
}
stopImmediatePropagation() {
this._immediatePropagationStopped = true;
}
}
Messenger.Event = Event;
export default Messenger;
|