2023-08-26 17:32:58 +02:00
|
|
|
export { registerReactivity } from './signals-common.js';
|
|
|
|
|
2023-09-09 21:15:43 +02:00
|
|
|
export function dispatchEvent(element, name, ...d){
|
|
|
|
const event= d.length ? new CustomEvent(name, { detail: d[0] }) : new Event(name);
|
|
|
|
return element.dispatchEvent(event);
|
|
|
|
}
|
2023-08-25 12:04:15 +02:00
|
|
|
export function on(event, listener, options){
|
2023-09-09 21:15:43 +02:00
|
|
|
return function registerElement(element){
|
2023-09-05 09:25:47 +02:00
|
|
|
element.addEventListener(event, listener, options);
|
|
|
|
return element;
|
|
|
|
};
|
2023-08-23 15:37:32 +02:00
|
|
|
}
|
2023-08-25 12:04:58 +02:00
|
|
|
|
2023-09-08 15:54:47 +02:00
|
|
|
const c_ch_o= connectionsChangesObserverConstructor();
|
2023-09-09 21:15:43 +02:00
|
|
|
import { onAbort } from './helpers.js';
|
|
|
|
//TODO: cleanUp when event before abort?
|
2023-09-05 09:25:47 +02:00
|
|
|
on.connected= function(listener, options){
|
2023-08-25 12:04:58 +02:00
|
|
|
return function registerElement(element){
|
2023-09-11 18:31:23 +02:00
|
|
|
if(typeof element.connectedCallback === "function"){
|
|
|
|
element.addEventListener("dde:connected", listener, options);
|
|
|
|
return element;
|
|
|
|
}
|
2023-09-09 21:15:43 +02:00
|
|
|
const c= onAbort(options && options.signal, ()=> c_ch_o.offConnected(element, listener));
|
2023-09-11 18:31:23 +02:00
|
|
|
if(!c) return element;
|
|
|
|
if(element.isConnected) listener(new Event("dde:connected"));
|
|
|
|
else c_ch_o.onConnected(element, listener);
|
2023-09-09 21:15:43 +02:00
|
|
|
return element;
|
2023-08-25 12:04:58 +02:00
|
|
|
};
|
|
|
|
};
|
2023-09-05 09:25:47 +02:00
|
|
|
on.disconnected= function(listener, options){
|
2023-08-25 12:04:58 +02:00
|
|
|
return function registerElement(element){
|
2023-09-11 18:31:23 +02:00
|
|
|
if(typeof element.disconnectedCallback === "function"){
|
|
|
|
element.addEventListener("dde:disconnected", listener, options);
|
|
|
|
return element;
|
|
|
|
}
|
2023-09-09 21:15:43 +02:00
|
|
|
const c= onAbort(options && options.signal, ()=> c_ch_o.offDisconnected(element, listener));
|
|
|
|
if(c) c_ch_o.onDisconnected(element, listener);
|
|
|
|
return element;
|
2023-08-25 12:04:58 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function connectionsChangesObserverConstructor(){
|
|
|
|
const store= new Map();
|
|
|
|
let is_observing= false;
|
|
|
|
const observer= new MutationObserver(function(mutations){
|
|
|
|
for(const mutation of mutations){
|
|
|
|
if(mutation.type!=="childList") continue;
|
2023-08-26 17:47:46 +02:00
|
|
|
if(observerAdded(mutation.addedNodes, true)){
|
2023-08-25 12:04:58 +02:00
|
|
|
stop();
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-26 17:47:46 +02:00
|
|
|
if(observerRemoved(mutation.removedNodes, true))
|
2023-08-25 12:04:58 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
onConnected(element, listener){
|
|
|
|
start();
|
|
|
|
const listeners= getElementStore(element);
|
|
|
|
listeners.connected.push(listener);
|
|
|
|
},
|
|
|
|
offConnected(element, listener){
|
|
|
|
if(!store.has(element)) return;
|
|
|
|
const ls= store.get(element);
|
|
|
|
const l= ls.connected;
|
|
|
|
l.splice(l.indexOf(listener), 1);
|
|
|
|
cleanWhenOff(element, ls);
|
|
|
|
},
|
|
|
|
onDisconnected(element, listener){
|
|
|
|
start();
|
|
|
|
const listeners= getElementStore(element);
|
|
|
|
listeners.disconnected.push(listener);
|
|
|
|
},
|
|
|
|
offDisconnected(element, listener){
|
|
|
|
if(!store.has(element)) return;
|
|
|
|
const ls= store.get(element);
|
|
|
|
const l= ls.disconnected;
|
|
|
|
l.splice(l.indexOf(listener), 1);
|
|
|
|
cleanWhenOff(element, ls);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function cleanWhenOff(element, ls){
|
2023-09-09 21:15:43 +02:00
|
|
|
if(ls.connected.length || ls.disconnected.length)
|
2023-08-25 12:04:58 +02:00
|
|
|
return;
|
|
|
|
store.delete(element);
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
function getElementStore(element){
|
|
|
|
if(store.has(element)) return store.get(element);
|
|
|
|
const out= { connected: [], disconnected: [] };
|
|
|
|
store.set(element, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
function start(){
|
|
|
|
if(is_observing) return;
|
|
|
|
is_observing= true;
|
|
|
|
observer.observe(document.body, { childList: true, subtree: true });
|
|
|
|
}
|
|
|
|
function stop(){
|
|
|
|
if(!is_observing || store.size) return;
|
|
|
|
is_observing= false;
|
|
|
|
observer.disconnect();
|
|
|
|
}
|
|
|
|
//TODO remount support?
|
|
|
|
function requestIdle(){ return new Promise(function(resolve){
|
|
|
|
(requestIdleCallback || requestAnimationFrame)(resolve);
|
|
|
|
}); }
|
|
|
|
async function collectChildren(element){
|
|
|
|
if(store.size > 30)//TODO limit?
|
|
|
|
await requestIdle();
|
|
|
|
const out= [];
|
|
|
|
if(!(element instanceof Node)) return out;
|
|
|
|
for(const el of store.keys()){
|
|
|
|
if(el===element || !(el instanceof Node)) continue;
|
|
|
|
if(element.contains(el))
|
|
|
|
out.push(el);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2023-08-26 17:47:46 +02:00
|
|
|
function observerAdded(addedNodes, is_root){
|
2023-08-25 12:04:58 +02:00
|
|
|
for(const element of addedNodes){
|
2023-08-26 17:47:46 +02:00
|
|
|
if(is_root) collectChildren(element).then(observerAdded);
|
2023-09-09 21:15:43 +02:00
|
|
|
if(!store.has(element)) continue;
|
2023-08-25 12:04:58 +02:00
|
|
|
|
|
|
|
const ls= store.get(element);
|
|
|
|
ls.connected.forEach(listener=> listener(element));
|
|
|
|
ls.connected.length= 0;
|
|
|
|
if(!ls.disconnected.length) store.delete(element);
|
|
|
|
return true;
|
|
|
|
}
|
2023-09-09 21:15:43 +02:00
|
|
|
return false;
|
2023-08-25 12:04:58 +02:00
|
|
|
}
|
2023-08-26 17:47:46 +02:00
|
|
|
function observerRemoved(removedNodes, is_root){
|
2023-08-25 12:04:58 +02:00
|
|
|
for(const element of removedNodes){
|
2023-08-26 17:47:46 +02:00
|
|
|
if(is_root) collectChildren(element).then(observerRemoved);
|
2023-09-09 21:15:43 +02:00
|
|
|
if(!store.has(element)) continue;
|
2023-08-25 12:04:58 +02:00
|
|
|
|
|
|
|
const ls= store.get(element);
|
|
|
|
ls.disconnected.forEach(listener=> listener(element));
|
|
|
|
|
|
|
|
ls.connected.length= 0;
|
|
|
|
ls.disconnected.length= 0;
|
|
|
|
store.delete(element);
|
|
|
|
return true;
|
|
|
|
}
|
2023-09-09 21:15:43 +02:00
|
|
|
return false;
|
2023-08-25 12:04:58 +02:00
|
|
|
}
|
|
|
|
}
|