-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(demo/signal2): add context example
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>@alwatr/signal2/context</title> | ||
<script type="module" src="./index.js"></script> | ||
<style> | ||
html { | ||
color-scheme: light dark; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<h3>Check the console</h3> | ||
<button id="myButton1">Set Value</button> | ||
<button id="myButton2">Subscribe</button> | ||
<button id="myButton3">Unsubscribe</button> | ||
<button id="myButton4">Expire</button> | ||
<button id="myButton5">Get Value</button> | ||
<button id="myButton6">Wait Until Change</button> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {AlwatrContextSignal} from '@alwatr/signal2'; | ||
|
||
const context = new AlwatrContextSignal<{name: string; age: number}>('user-info-context'); | ||
|
||
function subscribeHandler(context: {name: string; age: number}): void { | ||
console.log('subscribe: a new context signal received', context); | ||
} | ||
context.subscribe(subscribeHandler); | ||
|
||
document.getElementById('myButton1')?.addEventListener('click', () => { | ||
context.setValue({name: 'Ali', age: 20}); | ||
}); | ||
|
||
document.getElementById('myButton2')?.addEventListener('click', () => { | ||
context.subscribe(subscribeHandler); | ||
}); | ||
|
||
document.getElementById('myButton3')?.addEventListener('click', () => { | ||
context.unsubscribe(subscribeHandler); | ||
}); | ||
|
||
document.getElementById('myButton4')?.addEventListener('click', () => { | ||
context.expire(); | ||
}); | ||
|
||
document.getElementById('myButton5')?.addEventListener('click', () => { | ||
console.log('value:', context.getValue()); | ||
}); | ||
|
||
document.getElementById('myButton6')?.addEventListener('click', async () => { | ||
await context.untilChange(); | ||
console.log('until change reolved'); | ||
}); |