MCPcopy Create free account
hub / github.com/firebase/codelab-friendlychat-web / createAndInsertMessage

Function createAndInsertMessage

web/src/index.js:312–350  ·  view source on GitHub ↗
(id, timestamp)

Source from the content-addressed store, hash-verified

310 }
311
312 function createAndInsertMessage(id, timestamp) {
313 const container = document.createElement('div');
314 container.innerHTML = MESSAGE_TEMPLATE;
315 const div = container.firstChild;
316 div.setAttribute('id', id);
317
318 // If timestamp is null, assume we've gotten a brand new message.
319 // https://stackoverflow.com/a/47781432/4816918
320 timestamp = timestamp ? timestamp.toMillis() : Date.now();
321 div.setAttribute('timestamp', timestamp);
322
323 // figure out where to insert new message
324 const existingMessages = messageListElement.children;
325 if (existingMessages.length === 0) {
326 messageListElement.appendChild(div);
327 } else {
328 let messageListNode = existingMessages[0];
329
330 while (messageListNode) {
331 const messageListNodeTime = messageListNode.getAttribute('timestamp');
332
333 if (!messageListNodeTime) {
334 throw new Error(
335 `Child ${messageListNode.id} has no 'timestamp' attribute`
336 );
337 }
338
339 if (messageListNodeTime > timestamp) {
340 break;
341 }
342
343 messageListNode = messageListNode.nextSibling;
344 }
345
346 messageListElement.insertBefore(div, messageListNode);
347 }
348
349 return div;
350 }
351
352 // Displays a Message in the UI.
353 function displayMessage(id, timestamp, name, text, picUrl, imageUrl) {

Callers 1

displayMessageFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected