Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(signal2): more demos #1224

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
<li><a href="./logger/">Logger</a></li>
<li><a href="./fetch/">Fetch</a></li>
<li><a href="./signal/">Signal</a></li>
<li><a href="./signal2/simple/">Signal simple</a></li>
<li><a href="./signal2/simple/">Signal Simple</a></li>
<li><a href="./signal2/context/">Context Signal</a></li>
<li><a href="./signal2/multithread-context/">Signal multithread</a></li>
<li><a href="./router/">Router</a></li>
<li><a href="./font/">Font</a></li>
Expand Down
24 changes: 24 additions & 0 deletions demo/signal2/context/index.html
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>
33 changes: 33 additions & 0 deletions demo/signal2/context/index.ts
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');
});
4 changes: 3 additions & 1 deletion demo/signal2/simple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

<body>
<h3>Check the console</h3>
<button id="myButton">Event without any detail</button>
<button id="myButton1">Event without any detail</button>
<button id="myButton2">Event With detail</button>
<button id="myButton3">Subscribe</button>
<button id="myButton4">Unsubscribe</button>
</body>
</html>
33 changes: 24 additions & 9 deletions demo/signal2/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import {AlwatrEventSignal, AlwatrSimpleSignal} from '@alwatr/signal2';

const clickEvent = new AlwatrSimpleSignal('myClick');
// button 1
const clickEvent = new AlwatrSimpleSignal('button-1-click-event');
document.getElementById('myButton1')?.addEventListener('click', () => clickEvent.dispatch());
function clickEventSubscribeHandler(): void {
console.log('subscribe: a new signal received');
}
clickEvent.subscribe(clickEventSubscribeHandler);

document.getElementById('myButton')?.addEventListener('click', () => clickEvent.dispatch());

clickEvent.subscribe(() => {
console.log('subscribe: a new signal received ');
});
// button 2
const clickEvent2 = new AlwatrEventSignal('button-2-click-event');
document.getElementById('myButton2')?.addEventListener('click', () => clickEvent2.dispatch({clickedBy: 'BTN_2'}));
function clickEvent2SubscribeHandler(detail: unknown): void {
console.log('subscribe: a new signal received ', detail);
}
clickEvent2.subscribe(clickEvent2SubscribeHandler);

const clickEvent2 = new AlwatrEventSignal('myClick2');

document.getElementById('myButton2')?.addEventListener('click', () => clickEvent2.dispatch({clickedBy: 'MHF'}));
// button 3
document.getElementById('myButton3')?.addEventListener('click', () => {
clickEvent.subscribe(clickEventSubscribeHandler);
clickEvent2.subscribe(clickEvent2SubscribeHandler);
});

clickEvent2.subscribe((detail) => {
console.log('subscribe: a new signal received ', detail);

// button 4
document.getElementById('myButton4')?.addEventListener('click', () => {
clickEvent.unsubscribe(clickEventSubscribeHandler);
clickEvent2.unsubscribe(clickEvent2SubscribeHandler);
});