// signal := ( creator:(listener) => void ) => ( listener:(data) => void )
function signal(creator) {
return function listen(listener) {
// The magic, hand the listener to the creator
creator(listener)
}
}
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
function signal(creator) { | |
return function listen(listener) { | |
return creator(listener) | |
} | |
} |
Its almost a port of the w3c specc, but uses callbacks instead of promises.
To see if i could, the w3c specc is one of the best speccs i have seen, very readable and easy to follow. The only thing i question is the use of promises in that situation. The only place you can reasnobly call read is from within the promise resolution. If you dont use the good parts of promises (cached values for multiple, possible late, readers) but treat them like a one shot event, it seams like a simple callback is the thing your looking for.
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
// HELPERS | |
// Sometimes it's easier to work with edges instead of point + size | |
// {x, y, height, width} => {top, bottom, left right} | |
function frameToRect(frame) { | |
return { | |
left: frame.x, | |
right: frame.x + frame.width, | |
top: frame.y, | |
bottom: frame.y + frame.height |