2024-05-22 21:43:49 +02:00
|
|
|
import { style, el, S, isSignal } from '../exports.js';
|
2023-12-16 16:14:00 +01:00
|
|
|
const className= style.host(thirdParty).css`
|
|
|
|
:host {
|
|
|
|
color: green;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const store_adapter= {
|
|
|
|
read(){ return (new URL(location)).searchParams; },
|
|
|
|
write(data){ console.log(data); history.replaceState("", "", "?"+(new URLSearchParams(data)).toString()); }
|
|
|
|
};
|
|
|
|
export function thirdParty(){
|
2024-05-22 21:43:49 +02:00
|
|
|
const store= S({
|
|
|
|
value: S("initial")
|
2023-12-16 16:14:00 +01:00
|
|
|
}, {
|
|
|
|
set(key, value){
|
2024-05-22 21:43:49 +02:00
|
|
|
const p= this.value[key] || S();
|
2023-12-16 16:14:00 +01:00
|
|
|
p(value);
|
|
|
|
this.value[key]= p;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Array.from((new URL(location)).searchParams.entries())
|
2024-05-22 21:43:49 +02:00
|
|
|
// .forEach(([ key, value ])=> S.action(store, "set", key, value));
|
2024-11-22 16:26:42 +01:00
|
|
|
// S.on(store, data=> history.replaceState("", "",
|
|
|
|
// "?"+(new URLSearchParams(JSON.parse(JSON.stringify(data)))).toString()));
|
2024-01-05 16:49:05 +01:00
|
|
|
useStore(store_adapter, {
|
2023-12-16 16:14:00 +01:00
|
|
|
onread(data){
|
|
|
|
Array.from(data.entries())
|
2024-05-22 21:43:49 +02:00
|
|
|
.forEach(([ key, value ])=> S.action(store, "set", key, value));
|
2024-01-05 16:49:05 +01:00
|
|
|
return store;
|
2023-12-16 16:14:00 +01:00
|
|
|
}
|
2024-01-05 16:49:05 +01:00
|
|
|
})();
|
2023-12-16 16:14:00 +01:00
|
|
|
return el("input", {
|
|
|
|
className,
|
|
|
|
value: store().value(),
|
|
|
|
type: "text",
|
2024-05-22 21:43:49 +02:00
|
|
|
onchange: ev=> S.action(store, "set", "value", ev.target.value)
|
2023-12-16 16:14:00 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:49:05 +01:00
|
|
|
function useStore(adapter_in, { onread, onbeforewrite }= {}){
|
|
|
|
const adapter= typeof adapter_in === "function" ? { read: adapter_in } : adapter_in;
|
2024-05-22 21:43:49 +02:00
|
|
|
if(!onread) onread= S;
|
2023-12-16 16:14:00 +01:00
|
|
|
if(!onbeforewrite) onbeforewrite= data=> JSON.parse(JSON.stringify(data));
|
2024-01-05 16:49:05 +01:00
|
|
|
return function useStoreInner(data_read){
|
2024-05-22 21:43:49 +02:00
|
|
|
const signal= onread(adapter.read(data_read)); //TODO OK as synchronous
|
|
|
|
if(adapter.write && isSignal(signal))
|
|
|
|
S.on(signal, data=> adapter.write(onbeforewrite(data)));
|
|
|
|
return signal;
|
2024-01-05 16:49:05 +01:00
|
|
|
};
|
2023-12-16 16:14:00 +01:00
|
|
|
}
|