@@ -24,24 +24,33 @@ connections.add = function(addr)
2424 return ulid
2525end
2626
27- connections .remove = function (addr )
28- local ix
29-
30- -- Remove current connections first
31- -- Then remove remaining
32- for pwd , v in pairs (connections .current ) do
33- if v [2 ] == addr [2 ] and v [1 ] == addr [1 ] then
34- ix = connections .current [pwd ]
35- connections .store [ix ] = nil
36- connections .current [pwd ] = nil
27+ connections .remove = function (pwd , addr )
28+ -- If removed address is current to a pwd
29+ local key = connections .current [pwd ]
30+ local conn = connections .store [key ]
31+
32+ -- Double-check if address is correct
33+ if key ~= nil and conn ~= nil and conn [2 ] == addr [2 ] and conn [1 ] == addr [1 ] then
34+ -- Then remove it from current pwd
35+ connections .current [pwd ] = nil
36+ -- Remove it's definition
37+ connections .store [key ] = nil
38+
39+ -- And remove all other addresses that point to it
40+ for ix , v in pairs (connections .current ) do
41+ if v == key then
42+ connections .current [ix ] = nil
43+ end
3744 end
38- end
39-
40- for i , v in ipairs (connections .store ) do
41- if v [2 ] == addr [2 ] and v [1 ] == addr [1 ] then
42- connections .store [i ] = nil
45+ else
46+ -- Else, remove it from the connections if no address points to it
47+ for i , v in pairs (connections .store ) do
48+ if v [2 ] == addr [2 ] and v [1 ] == addr [1 ] then
49+ connections .store [i ] = nil
50+ end
4351 end
4452 end
53+
4554end
4655
4756--- Elects selected connection as primary (thus default) for a certain address
0 commit comments