-
-
Save jhusain/635312d21cb8faff082d to your computer and use it in GitHub Desktop.
Iterable and Observable are dual
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
interface Iterable { | |
Generator @@iterator(Generator); | |
} | |
interface Observable { | |
Generator observer(Generator); | |
} | |
Array.prototype[@@observer] = function(generator) { | |
var decoratedGenerator = Object.create(generator), | |
done = false; | |
["throw","return"].forEach(method => { | |
decoratedGenerator[method] = v => { | |
var superMethod = generator[method]; | |
done = true; | |
if (superMethod) { | |
return superMethod.call(generator, v); | |
} | |
} | |
}); | |
for(let x of this) { | |
decoratedGenerator.next(x) | |
if (done) { | |
break; | |
} | |
} | |
if (!done) { | |
decoratedGenerator.return(); | |
} | |
return decoratedGenerator; | |
} | |
[1,2,3][@@observer]({ | |
next: function(value) { | |
console.log(value); | |
if (value ===2 ) { | |
this.return(); | |
} | |
} | |
}) | |
for(let x of [1,2,3]) { | |
console.log(value); | |
if (x == 2) { | |
break; | |
} | |
} | |
async function test() { | |
for(let x on [1,2,3]) { | |
console.log(value); | |
if (x === 2) { | |
break; | |
} | |
} | |
} | |
document.addEventListener("mousemove", function next(e) { | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment