made with esnextbin
Last active
August 12, 2016 15:20
-
-
Save voronianski/845f80057dbee93fe7317f70e320e475 to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or 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> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<div id="app"></div> | |
<!-- put markup and other contents here --> | |
</body> | |
</html> |
This file contains hidden or 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
import Bacon from 'baconjs'; | |
import { EventEmitter } from 'events'; | |
Bacon.Property.prototype.triggeredBy = function(trigger, cb) { | |
return this.changes().merge(this.sampledBy(trigger, cb)); | |
}; | |
const events = window.__events = new EventEmitter(); | |
const storage = { | |
items: ['DE'], | |
read() { | |
return new Promise((resolve) => { | |
return resolve([]); | |
}); | |
}, | |
write(item) { | |
return []; | |
} | |
} | |
const readStorage$ = Bacon.fromPromise(storage.read()).mapError(() => []); | |
const addLocation$ = Bacon.fromEvent(events, 'addFavoriteLocation'); | |
const removeLocation$ = Bacon.fromEvent(events, 'removeFavoriteLocation'); | |
const favoriteLocations = Bacon | |
.update([], | |
[readStorage$], getFromStorage, | |
[addLocation$], addFavoriteLocation, | |
[removeLocation$], removeFavoriteLocation | |
); | |
function getFromStorage (prevLocales, savedLocales) { | |
return [].concat(prevLocales, savedLocales); | |
} | |
function addFavoriteLocation (prevLocales, addedLocale) { | |
return !prevLocales.includes(addedLocale) ? | |
[...prevLocales, addedLocale] : | |
[...prevLocales]; | |
} | |
function removeFavoriteLocation (prevLocales, removedLocale) { | |
return prevLocales.filter(locale => locale !== removedLocale); | |
} | |
favoriteLocations.onValue(locales => { | |
storage.write('favorite_locations', locales); | |
}); | |
const dataStream = Bacon.combineTemplate({ favoriteLocations }); | |
dataStream | |
.triggeredBy(Bacon.fromEvent(events, 'data')) | |
.onValue(val => { | |
console.log('stream', val); | |
}); | |
console.log(window.__events); |
This file contains hidden or 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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"baconjs": "0.7.85", | |
"undefined": "v0.10.26", | |
"babel-runtime": "6.11.6" | |
} | |
} |
This file contains hidden or 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
'use strict'; | |
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | |
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | |
var _promise = require('babel-runtime/core-js/promise'); | |
var _promise2 = _interopRequireDefault(_promise); | |
var _baconjs = require('baconjs'); | |
var _baconjs2 = _interopRequireDefault(_baconjs); | |
var _events = require('events'); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
_baconjs2.default.Property.prototype.triggeredBy = function (trigger, cb) { | |
return this.changes().merge(this.sampledBy(trigger, cb)); | |
}; | |
var events = window.__events = new _events.EventEmitter(); | |
var storage = { | |
items: ['DE'], | |
read: function read() { | |
return new _promise2.default(function (resolve) { | |
return resolve([]); | |
}); | |
}, | |
write: function write(item) { | |
return []; | |
} | |
}; | |
var readStorage$ = _baconjs2.default.fromPromise(storage.read()).mapError(function () { | |
return []; | |
}); | |
var addLocation$ = _baconjs2.default.fromEvent(events, 'addFavoriteLocation'); | |
var removeLocation$ = _baconjs2.default.fromEvent(events, 'removeFavoriteLocation'); | |
var favoriteLocations = _baconjs2.default.update([], [readStorage$], getFromStorage, [addLocation$], addFavoriteLocation, [removeLocation$], removeFavoriteLocation); | |
function getFromStorage(prevLocales, savedLocales) { | |
return [].concat(prevLocales, savedLocales); | |
} | |
function addFavoriteLocation(prevLocales, addedLocale) { | |
return !prevLocales.includes(addedLocale) ? [].concat((0, _toConsumableArray3.default)(prevLocales), [addedLocale]) : [].concat((0, _toConsumableArray3.default)(prevLocales)); | |
} | |
function removeFavoriteLocation(prevLocales, removedLocale) { | |
return prevLocales.filter(function (locale) { | |
return locale !== removedLocale; | |
}); | |
} | |
favoriteLocations.onValue(function (locales) { | |
storage.write('favorite_locations', locales); | |
}); | |
var dataStream = _baconjs2.default.combineTemplate({ favoriteLocations: favoriteLocations }); | |
dataStream.triggeredBy(_baconjs2.default.fromEvent(events, 'data')).onValue(function (val) { | |
console.log('stream', val); | |
}); | |
console.log(window.__events); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment