Add examples for Speaker
This commit is contained in:
parent
649286dcdf
commit
be61a88f55
1 changed files with 46 additions and 0 deletions
46
speaker.md
46
speaker.md
|
@ -15,3 +15,49 @@ Speaker.speak(...args)
|
||||||
Speaker.silence(callback)
|
Speaker.silence(callback)
|
||||||
// Removes a given callback
|
// Removes a given callback
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
A simple example:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const speaker = new Speaker(true /* run callbacks immediately */)
|
||||||
|
|
||||||
|
speaker.listen(value => console.log(value))
|
||||||
|
speaker.listen(value => console.warn(value))
|
||||||
|
|
||||||
|
speaker.speak("This will be logged twice")
|
||||||
|
|
||||||
|
console.log("This message will appear last")
|
||||||
|
```
|
||||||
|
|
||||||
|
A slightly more convoluted example:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import Speaker from '/speaker.js'
|
||||||
|
|
||||||
|
// A new message that schedules messages as microtasks
|
||||||
|
const speaker = new Speaker()
|
||||||
|
|
||||||
|
// A callback that calls messages as functions
|
||||||
|
speaker.listen(fn => fn())
|
||||||
|
|
||||||
|
// A shorthand wrapper around the speaker
|
||||||
|
const defer = fn => speaker.speak(fn)
|
||||||
|
|
||||||
|
// Defer a function to be called in a microtask
|
||||||
|
defer(() => console.log("This message will appear second"))
|
||||||
|
|
||||||
|
console.log("This message will appear first")
|
||||||
|
|
||||||
|
// Defer another function to be called in a microtask
|
||||||
|
defer(() => console.log("This message will appear third"))
|
||||||
|
// Note: A single speaker will use a single microtask even
|
||||||
|
// for multiple messages.
|
||||||
|
|
||||||
|
defer(() => {
|
||||||
|
// You can continue pushing messages inside the callback;
|
||||||
|
// they will be processed within the same microtask.
|
||||||
|
defer(() => console.log("This message will appear last"))
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in a new issue