Skip to content

Commit

Permalink
Merge pull request #100 from AliMD/fix/signal
Browse files Browse the repository at this point in the history
[Signal] Fix multi request and add demo
  • Loading branch information
alimd authored Mar 12, 2022
2 parents ca03444 + f617387 commit caf2e76
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
5 changes: 4 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</head>

<body>
<a href="./logger/index.html">Logger</a>
<ol>
<li><a href="./logger/">Logger</a></li>
<li><a href="./signal/">Signal</a></li>
</ol>
</body>
</html>
16 changes: 16 additions & 0 deletions demo/signal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<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></title>
</head>

<body>
<h3>Check the console</h3>
<button id="requestButton">Request Signal</button>
<button id="requestButton2">3x Request Signal</button>
<script src="./index.js" type="module"></script>

</body>
</html>
24 changes: 24 additions & 0 deletions demo/signal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {SignalInterface} from '@alwatr/signal';

const signal = new SignalInterface('easter-egg');

signal.setProvider((param) => {
const newValue = param * 10;
console.info('2. signal provider called', {param, newValue});
return newValue;
});

document.getElementById('requestButton')?.addEventListener('click', async () => {
console.info('1. request param: 1');
const value = await signal.request(1);
console.info('3. new signal value', {value});
});

document.getElementById('requestButton2')?.addEventListener('click', async () => {
console.info('1. request');
const value1 = await signal.request(1);
console.info('3. 2x request');
const value2p = signal.request(2);
const value3p = signal.request(3);
console.info('4. new signal value', {value1, value2: await value2p, value3: await value3p});
});
3 changes: 2 additions & 1 deletion package/signal/src/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ export class SignalInterface<SignalName extends keyof AlwatrSignals> {
*/
request(requestParam: AlwatrRequestSignals[SignalName]): Promise<AlwatrSignals[SignalName]> {
this._logger.logMethodArgs('request', {requestParam});
const nextSignalValuePromise = this.getNextSignalValue();
_dispatchSignal(
`request-${this.name}` as unknown as SignalName,
requestParam as unknown as AlwatrSignals[SignalName], // mastmalize to avoid type error
);
return this.getNextSignalValue();
return nextSignalValuePromise;
}

/**
Expand Down

0 comments on commit caf2e76

Please sign in to comment.