-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf403f8
commit c2f6dda
Showing
2 changed files
with
77 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,82 @@ | ||
import './style.css'; | ||
import {makeDerivedStore, makeStore} from './lib'; | ||
import {makeDerivedStore, makeReactiveRoot, makeStore} from './lib'; | ||
|
||
const appDiv = document.getElementById('app') as HTMLDivElement; | ||
|
||
const random1$ = makeStore(0); | ||
const random2$ = makeStore(0); | ||
const sum$ = makeDerivedStore({r1: random1$, r2: random2$}, ({r1, r2}) => r1 + r2); | ||
// Using just stores | ||
{ | ||
const titleH2 = document.createElement('h2'); | ||
titleH2.textContent = 'Using just stores'; | ||
appDiv.appendChild(titleH2); | ||
|
||
const span1 = document.createElement('span'); | ||
appDiv.appendChild(span1); | ||
const span2 = document.createElement('span'); | ||
appDiv.appendChild(span2); | ||
const spanSum = document.createElement('span'); | ||
appDiv.appendChild(spanSum); | ||
const random1$ = makeStore(0); | ||
const random2$ = makeStore(0); | ||
const sum$ = makeDerivedStore({r1: random1$, r2: random2$}, ({r1, r2}) => r1 + r2); | ||
|
||
random1$.subscribe((r1) => (span1.innerText = String(r1) + ' + ')); | ||
random2$.subscribe((r2) => (span2.innerText = String(r2) + ' = ')); | ||
sum$.subscribe((sum) => (spanSum.innerText = String(sum))); | ||
const span1 = document.createElement('span'); | ||
appDiv.appendChild(span1); | ||
const span2 = document.createElement('span'); | ||
appDiv.appendChild(span2); | ||
const spanSum = document.createElement('span'); | ||
appDiv.appendChild(spanSum); | ||
|
||
appDiv.appendChild(document.createElement('br')); | ||
random1$.subscribe((r1) => (span1.textContent = `${r1} + `)); | ||
random2$.subscribe((r2) => (span2.textContent = `${r2} = `)); | ||
sum$.subscribe((sum) => (spanSum.textContent = `${sum}`)); | ||
|
||
const button1 = document.createElement('button'); | ||
button1.innerText = 'random1'; | ||
button1.addEventListener('click', () => random1$.set(Math.floor(10 * Math.random()))); | ||
appDiv.appendChild(button1); | ||
appDiv.appendChild(document.createElement('br')); | ||
|
||
const button2 = document.createElement('button'); | ||
button2.innerText = 'random2'; | ||
button2.addEventListener('click', () => random2$.set(Math.floor(10 * Math.random()))); | ||
appDiv.appendChild(button2); | ||
const button1 = document.createElement('button'); | ||
button1.textContent = 'random1'; | ||
button1.addEventListener('click', () => random1$.set(Math.floor(100 * Math.random()))); | ||
appDiv.appendChild(button1); | ||
|
||
const button2 = document.createElement('button'); | ||
button2.textContent = 'random2'; | ||
button2.addEventListener('click', () => random2$.set(Math.floor(100 * Math.random()))); | ||
appDiv.appendChild(button2); | ||
|
||
appDiv.appendChild(document.createElement('br')); | ||
appDiv.appendChild(document.createElement('br')); | ||
appDiv.appendChild(document.createElement('br')); | ||
appDiv.appendChild(document.createElement('br')); | ||
} | ||
// Using the effect system | ||
{ | ||
const titleH2 = document.createElement('h2'); | ||
titleH2.textContent = 'Using the effect system'; | ||
appDiv.appendChild(titleH2); | ||
|
||
const random1$ = makeStore(0); | ||
const random2$ = makeStore(0); | ||
const sum = () => random1$.watch() + random2$.watch(); | ||
|
||
const span1 = document.createElement('span'); | ||
appDiv.appendChild(span1); | ||
const span2 = document.createElement('span'); | ||
appDiv.appendChild(span2); | ||
const spanSum = document.createElement('span'); | ||
appDiv.appendChild(spanSum); | ||
|
||
const {makeEffect} = makeReactiveRoot(); | ||
makeEffect(() => { | ||
span1.textContent = `${random1$.watch()} + `; | ||
span2.textContent = `${random2$.watch()} = `; | ||
}); | ||
// showcase a derived-like behavior | ||
makeEffect(() => { | ||
spanSum.textContent = `${sum()}`; | ||
}); | ||
|
||
appDiv.appendChild(document.createElement('br')); | ||
|
||
const button1 = document.createElement('button'); | ||
button1.textContent = 'random1'; | ||
button1.addEventListener('click', () => random1$.set(Math.floor(100 * Math.random()))); | ||
appDiv.appendChild(button1); | ||
|
||
const button2 = document.createElement('button'); | ||
button2.textContent = 'random2'; | ||
button2.addEventListener('click', () => random2$.set(Math.floor(100 * Math.random()))); | ||
appDiv.appendChild(button2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters