mirror of
				https://github.com/jaandrle/deka-dom-el
				synced 2025-11-04 07:09:15 +01:00 
			
		
		
		
	⚡ Refatc signals to .get/.set syntax #26
This commit is contained in:
		@@ -26,7 +26,7 @@ function ddeComponent({ attr }){
 | 
			
		||||
		on.connected(e=> console.log(( /** @type {HTMLParagraphElement} */ (e.target)).outerHTML)),
 | 
			
		||||
	);
 | 
			
		||||
	return el().append(
 | 
			
		||||
		el("p", S(()=> `Hello from Custom Element with attribute '${attr()}'`))
 | 
			
		||||
		el("p", S(()=> `Hello from Custom Element with attribute '${attr.get()}'`))
 | 
			
		||||
	);
 | 
			
		||||
}
 | 
			
		||||
customElementWithDDE(HTMLCustomElement);
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,11 @@ const threePS= ({ emoji= "🚀" })=> {
 | 
			
		||||
	const clicks= S(0); // A
 | 
			
		||||
	return el().append(
 | 
			
		||||
		el("p", S(()=>
 | 
			
		||||
			"Hello World "+emoji.repeat(clicks()) // B
 | 
			
		||||
			"Hello World "+emoji.repeat(clicks.get()) // B
 | 
			
		||||
		)),
 | 
			
		||||
		el("button", {
 | 
			
		||||
			type: "button",
 | 
			
		||||
			onclick: ()=> clicks(clicks()+1), // C
 | 
			
		||||
			onclick: ()=> clicks.set(clicks.get()+1), // C
 | 
			
		||||
			textContent: "Fire",
 | 
			
		||||
		})
 | 
			
		||||
	);
 | 
			
		||||
 
 | 
			
		||||
@@ -15,4 +15,4 @@ setTimeout(
 | 
			
		||||
	clearInterval,
 | 
			
		||||
	10*interval,
 | 
			
		||||
	setInterval(oninterval, interval)
 | 
			
		||||
);
 | 
			
		||||
);
 | 
			
		||||
@@ -22,9 +22,9 @@ const onsubmit= on("submit", function(event){
 | 
			
		||||
			S.action(todos, "push", data.get("todo"));
 | 
			
		||||
			break;
 | 
			
		||||
		case "E"/*dit*/: {
 | 
			
		||||
			const last= todos().at(-1);
 | 
			
		||||
			const last= todos.get().at(-1);
 | 
			
		||||
			if(!last) break;
 | 
			
		||||
			last(data.get("todo"));
 | 
			
		||||
			last.set(data.get("todo"));
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
		case "R"/*emove*/:
 | 
			
		||||
 
 | 
			
		||||
@@ -1,16 +1,16 @@
 | 
			
		||||
import { S } from "deka-dom-el/signals";
 | 
			
		||||
const signal= S(0);
 | 
			
		||||
// computation pattern
 | 
			
		||||
const double= S(()=> 2*signal());
 | 
			
		||||
const double= S(()=> 2*signal.get());
 | 
			
		||||
 | 
			
		||||
const ac= new AbortController();
 | 
			
		||||
S.on(signal, v=> console.log("signal", v), { signal: ac.signal });
 | 
			
		||||
S.on(double, v=> console.log("double", v), { signal: ac.signal });
 | 
			
		||||
 | 
			
		||||
signal(signal()+1);
 | 
			
		||||
signal.set(signal.get()+1);
 | 
			
		||||
const interval= 5 * 1000;
 | 
			
		||||
const id= setInterval(()=> signal(signal()+1), interval);
 | 
			
		||||
const id= setInterval(()=> signal.set(signal.get()+1), interval);
 | 
			
		||||
ac.signal.addEventListener("abort",
 | 
			
		||||
	()=> setTimeout(()=> clearInterval(id), 2*interval));
 | 
			
		||||
 | 
			
		||||
setTimeout(()=> ac.abort(), 3*interval)
 | 
			
		||||
setTimeout(()=> ac.abort(), 3*interval)
 | 
			
		||||
@@ -3,8 +3,8 @@ const count= S(0);
 | 
			
		||||
 | 
			
		||||
import { el } from "deka-dom-el";
 | 
			
		||||
document.body.append(
 | 
			
		||||
	el("p", S(()=> "Currently: "+count())),
 | 
			
		||||
	el("p", { classList: { red: S(()=> count()%2 === 0) }, dataset: { count }, textContent: "Attributes example" }),
 | 
			
		||||
	el("p", S(()=> "Currently: "+count.get())),
 | 
			
		||||
	el("p", { classList: { red: S(()=> count.get()%2 === 0) }, dataset: { count }, textContent: "Attributes example" }),
 | 
			
		||||
);
 | 
			
		||||
document.head.append(
 | 
			
		||||
	el("style", ".red { color: red; }")
 | 
			
		||||
@@ -12,4 +12,4 @@ document.head.append(
 | 
			
		||||
 | 
			
		||||
const interval= 5 * 1000;
 | 
			
		||||
setTimeout(clearInterval, 10*interval,
 | 
			
		||||
	setInterval(()=> count(count()+1), interval));
 | 
			
		||||
	setInterval(()=> count.set(count.get()+1), interval));
 | 
			
		||||
@@ -2,7 +2,7 @@ import { S } from "deka-dom-el/signals";
 | 
			
		||||
const count= S(0, {
 | 
			
		||||
	add(){ this.value= this.value + Math.round(Math.random()*10); }
 | 
			
		||||
});
 | 
			
		||||
const numbers= S([ count() ], {
 | 
			
		||||
const numbers= S([ count.get() ], {
 | 
			
		||||
	push(next){ this.value.push(next); }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@@ -22,5 +22,5 @@ document.body.append(
 | 
			
		||||
const interval= 5*1000;
 | 
			
		||||
setTimeout(clearInterval, 10*interval, setInterval(function(){
 | 
			
		||||
	S.action(count, "add");
 | 
			
		||||
	S.action(numbers, "push", count());
 | 
			
		||||
}, interval));
 | 
			
		||||
	S.action(numbers, "push", count.get());
 | 
			
		||||
}, interval));
 | 
			
		||||
@@ -4,9 +4,9 @@ const signal= S(0);
 | 
			
		||||
// β — just reacts on signal changes
 | 
			
		||||
S.on(signal, console.log);
 | 
			
		||||
// γ — just updates the value
 | 
			
		||||
const update= ()=> signal(signal()+1);
 | 
			
		||||
const update= ()=> signal.set(signal.get()+1);
 | 
			
		||||
 | 
			
		||||
update();
 | 
			
		||||
const interval= 5*1000;
 | 
			
		||||
setTimeout(clearInterval, 10*interval,
 | 
			
		||||
	setInterval(update, interval));
 | 
			
		||||
	setInterval(update, interval));
 | 
			
		||||
@@ -69,9 +69,9 @@ export function page({ pkg, info }){
 | 
			
		||||
			some manner independent and still connected to the same reactive entity.
 | 
			
		||||
		`),
 | 
			
		||||
		el("p").append(...T`
 | 
			
		||||
			Signals are implemented in the library as functions. To see current value of signal, just call it without
 | 
			
		||||
			any arguments ${el("code", "console.log(signal())")}. To update the signal value, pass any argument
 | 
			
		||||
			${el("code", `signal('${t`a new value`}')`)}. For listenning the signal value changes, use
 | 
			
		||||
			Signals are implemented in the library as objects with methods. To see current value of signal,
 | 
			
		||||
			call the get method ${el("code", "console.log(signal.get())")}. To update the signal value, use the set method
 | 
			
		||||
			${el("code", `signal.set('${t`a new value`}')`)}. For listenning the signal value changes, use
 | 
			
		||||
			${el("code", "S.on(signal, console.log)")}.
 | 
			
		||||
		`),
 | 
			
		||||
		el("p").append(...T`
 | 
			
		||||
@@ -114,7 +114,7 @@ export function page({ pkg, info }){
 | 
			
		||||
		`),
 | 
			
		||||
		el("p").append(...T`
 | 
			
		||||
			For computation, you can use the “derived signal” (see above) like
 | 
			
		||||
			${el("code", "assign(element, { textContent: S(()=> 'Hello '+WorldSignal()) })")}. This is read-only signal
 | 
			
		||||
			${el("code", "assign(element, { textContent: S(()=> 'Hello '+WorldSignal.get()) })")}. This is read-only signal
 | 
			
		||||
			its value is computed based on given function and updated when any signal used in the function changes.
 | 
			
		||||
		`),
 | 
			
		||||
		el("p").append(...T`
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user