Last active
December 17, 2015 04:38
-
-
Save cwharris/5551649 to your computer and use it in GitHub Desktop.
Stick THIS in your browser and run it.
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
Rx.Observable.select = function (element, source) { | |
return source.select(function (value) { | |
return { | |
element: element, | |
value: value | |
}; | |
}); | |
}; | |
Rx.Observable.prototype.append = function (type) { | |
return this.selectMany(function (x) { | |
var element = document.createElement(type); | |
x.element.appendChild(element); | |
return x.value | |
.finallyAction(function () { | |
x.element.removeChild(element); | |
}) | |
.select(function (value) { | |
return { | |
element: element, | |
value: value | |
}; | |
}); | |
}); | |
}; | |
Rx.Observable.prototype.text = function (func) { | |
return this.doAction(function (x) { | |
x.element.innerText = func(x.value); | |
}); | |
}; | |
Rx.Observable.prototype.attr = function (name, func) { | |
return this.doAction(function (x) { | |
x.element.setAttribute(name, func(x.value)); | |
}); | |
}; | |
Rx.Observable.prototype.style = function (name, func) { | |
return this.doAction(function (x) { | |
var val = func(x.value); | |
if (typeof val === 'number') { val = val + 'px'; } | |
x.element.style[name] = val; | |
}); | |
}; | |
// - Side Effects | |
var source = Rx.Observable.timer(0, 100).select(function () { | |
return Rx.Observable.timer(0, 250).take(10).merge(Rx.Observable.never()).select(function () { | |
return Math.floor(Math.random() * 50); | |
}); | |
}); | |
Rx.Observable.select(document.body, source) | |
.append('span') | |
.text(function (x) { return x; }) | |
.style('paddingBottom', function (x) { return x; }) | |
.style('borderBottomWidth', function (x) { return 50 - x + 1; }) | |
.subscribe(); |
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
span { | |
display: inline-block; | |
margin: 0px; | |
border: 5px solid black; | |
transition: padding 0.1s, border 0.1s; | |
width: 25px; | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment