From 3f8569174e060d010c91e9da08fa64fc65645e81 Mon Sep 17 00:00:00 2001
From: adbenitez <asieldbenitez@gmail.com>
Date: Sun, 2 Jan 2022 15:47:10 -0500
Subject: [PATCH] add selfName to API

---
 index.html | 4 ++--
 webxdc.js  | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/index.html b/index.html
index 9831560..d02b41a 100644
--- a/index.html
+++ b/index.html
@@ -12,11 +12,11 @@
 
     function sendMsg() {
         msg = document.getElementById("input").value;
-        window.webxdc.sendUpdate('someone typed "'+msg+'"', {"addr": window.webxdc.selfAddr(), "msg": msg});
+        window.webxdc.sendUpdate('someone typed "'+msg+'"', {"name": window.webxdc.selfName(), "msg": msg});
     }
 
     function receiveUpdate(update) {
-        document.getElementById('output').innerHTML += "<strong>&lt;" + update.payload.addr + "&gt;</strong> " + update.payload.msg + "<br>";
+        document.getElementById('output').innerHTML += "<strong>&lt;" + update.payload.name + "&gt;</strong> " + update.payload.msg + "<br>";
     }
 
     window.webxdc.setUpdateListener(receiveUpdate);
diff --git a/webxdc.js b/webxdc.js
index d80e91f..c96c58a 100644
--- a/webxdc.js
+++ b/webxdc.js
@@ -6,6 +6,7 @@ window.webxdc = (() => {
 
     return {
         selfAddr: () => window.xdcSelfAddr || "device0@local.host",
+        selfName: () => window.xdcSelfName || "device0",
         setUpdateListener: (cb) => (window.xdcUpdateListener = cb),
         getAllUpdates: () => {
             return JSON.parse(
@@ -47,7 +48,8 @@ function addPeer() {
     var xdcChild = window.open(window.location);
     var xdcRoot = getXdcRoot();
     xdcChild.xdcRoot = xdcRoot;
-    xdcChild.xdcSelfAddr = "device" + xdcRoot.allXdcWindows.length + "@local.host";
+    xdcChild.xdcSelfName = "device" + getAllXdcWindows().length;
+    xdcChild.xdcSelfAddr = xdcChild.xdcSelfName + "@local.host";
     xdcRoot.allXdcWindows.push(xdcChild);
 }