Created
May 24, 2016 04:03
-
-
Save seamusv/8b266a6e7ac41951fee0ee761de88e6a to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/rxjs-dom/7.0.3/rx.dom.min.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var items = [ | |
"apple", | |
"orange", | |
"apple", | |
"apple", | |
"apple", | |
"peach" | |
]; | |
var priceTable = new Rx.ReplaySubject(); | |
var calculator = new Rx.Subject(); | |
var receipt = new Rx.Subject(); | |
receipt.subscribe(function (x) { | |
console.log("Receipt", x); | |
}); | |
var groupedItems = Rx.Observable.from(items) | |
.groupBy(function(x){return x}) | |
.flatMap(function(x){return x.underlyingObservable.toArray()}) | |
.map(function(x) {return {key: x[0], items: x}}); | |
Rx.DOM | |
.ajax({ | |
url: 'prices.json', | |
responseType: 'json' | |
}) | |
.map(function (x) { | |
return x.response | |
}) | |
.flatMap(function (x) { | |
return Rx.Observable.from(x) | |
}) | |
.reduce(function (acc, item, index, source) { | |
var till$ = acc[0]; | |
var receipt$ = acc[1]; | |
var name = item.name; | |
var pricePoints = []; | |
for (idx in item.specialOffers) { | |
var specialOffer = item.specialOffers[idx]; | |
pricePoints.push([specialOffer.numOfItemsForSpecial, specialOffer.price]); | |
} | |
pricePoints | |
.sort(function (a, b) { | |
if (a[0] < b[0]) return -1; | |
if (a[0] > b[0]) return 1; | |
return 0; | |
}); | |
var subject = new Rx.Subject(); | |
var itemSubject = subject | |
.filter(function(x) {return x.key == name}) | |
.map(function(x) {return x.items}) | |
till$ | |
.subscribe(subject); | |
var chain = new Rx.Subject(); | |
chain | |
.flatMap(function(x) {return Rx.Observable.from(x)}) | |
.map(function(x){return {label: name, price: item.price}}) | |
.do(function(x){receipt$.onNext(x)}) | |
.subscribe(); | |
for (idx in pricePoints) { | |
var pricePoint = pricePoints[idx]; | |
var numOfItemsForSpecial = pricePoint[0]; | |
var price = pricePoint[1]; | |
var priceSubject = new Rx.Subject(); | |
var priceBuffer = priceSubject | |
.flatMap(function(x) {return Rx.Observable.from(x)}) | |
.bufferWithCount(numOfItemsForSpecial) | |
.share(); | |
priceBuffer | |
.filter(function(x){return x.length < numOfItemsForSpecial}) | |
.subscribe(chain); | |
priceBuffer | |
.filter(function(x){return x.length == numOfItemsForSpecial}) | |
.map(function(x){return {label: name + " - " + numOfItemsForSpecial + " for " + price, price: price}}) | |
.do(function(x){receipt$.onNext(x)}) | |
.subscribe(); | |
chain = priceSubject; | |
} | |
itemSubject | |
.subscribe(chain); | |
return [till$, receipt$]; | |
}, [new Rx.Subject(), new Rx.Subject()]) | |
.subscribe(function (subjects) { | |
var till$ = subjects[0]; | |
var receipt$ = subjects[1]; | |
calculator.subscribe(till$); | |
receipt$.subscribe(receipt); | |
priceTable.onNext(true); | |
calculator.onCompleted() | |
}); | |
priceTable | |
.flatMap(groupedItems) | |
.subscribe(calculator); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment