made with esnextbin
Last active
March 1, 2017 20:38
-
-
Save tijhaart/4b8d3e3bc3f7206251e595ab2dccb0e7 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> | |
| <!-- put markup and other contents here --> | |
| <app /> | |
| </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
| console.clear(); | |
| console.log(Rx); | |
| import Rx from 'rxjs'; | |
| class Manager { | |
| _doOtherThing() { | |
| console.info('Manager::doOtherThing'); | |
| } | |
| _sendMessage(msg) { | |
| return (device) => { | |
| console.info(`Manager::sendMessage(${msg} ${device.id})`) | |
| this._doOtherThing(); | |
| } | |
| } | |
| _download() { | |
| const file = { currentBytes: 0, totalBytes: 10 }; | |
| return Rx.Observable.interval(100) | |
| .scan((acc, i) => { | |
| acc.currentBytes++; | |
| return acc; | |
| }, file) | |
| .takeWhile((x) => { | |
| return x.currentBytes <= x.totalBytes; | |
| }) | |
| .do(() => console.info('Download in progress')) | |
| ; | |
| } | |
| _connectToDevice(device) { | |
| return Rx.Observable.of(device) | |
| .do(({ id }) => console.info('Connecting to device: %o', id)) | |
| .delay(100) | |
| .do(({ id }) => console.info('Successful connection to device: %o', id)) | |
| ; | |
| } | |
| downloadFileFromDevice(device) { | |
| return Rx.Observable.combineLatest( | |
| this | |
| ._connectToDevice(device) | |
| .do(this._sendMessage('Hello')), | |
| this._download(), | |
| (device, file) => { | |
| return { device, file }; | |
| }) | |
| ; | |
| } | |
| } | |
| const manager = new Manager(); | |
| manager | |
| .downloadFileFromDevice({ id: 'xyz' }) | |
| .subscribe({ | |
| complete() { | |
| console.info('Download ready'); | |
| } | |
| }) | |
| ; |
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": { | |
| "rxjs": "5.1.0", | |
| "babel-runtime": "6.22.0" | |
| } | |
| } |
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 _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
| var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
| var _createClass2 = require('babel-runtime/helpers/createClass'); | |
| var _createClass3 = _interopRequireDefault(_createClass2); | |
| var _rxjs = require('rxjs'); | |
| var _rxjs2 = _interopRequireDefault(_rxjs); | |
| function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
| console.clear(); | |
| console.log(_rxjs2.default); | |
| var Manager = function () { | |
| function Manager() { | |
| (0, _classCallCheck3.default)(this, Manager); | |
| } | |
| (0, _createClass3.default)(Manager, [{ | |
| key: '_doOtherThing', | |
| value: function _doOtherThing() { | |
| console.info('Manager::doOtherThing'); | |
| } | |
| }, { | |
| key: '_sendMessage', | |
| value: function _sendMessage(msg) { | |
| var _this = this; | |
| return function (device) { | |
| console.info('Manager::sendMessage(' + msg + ' ' + device.id + ')'); | |
| _this._doOtherThing(); | |
| }; | |
| } | |
| }, { | |
| key: '_download', | |
| value: function _download() { | |
| var file = { currentBytes: 0, totalBytes: 10 }; | |
| return _rxjs2.default.Observable.interval(100).scan(function (acc, i) { | |
| acc.currentBytes++; | |
| return acc; | |
| }, file).takeWhile(function (x) { | |
| return x.currentBytes <= x.totalBytes; | |
| }).do(function () { | |
| return console.info('Download in progress'); | |
| }); | |
| } | |
| }, { | |
| key: '_connectToDevice', | |
| value: function _connectToDevice(device) { | |
| return _rxjs2.default.Observable.of(device).do(function (_ref) { | |
| var id = _ref.id; | |
| return console.info('Connecting to device: %o', id); | |
| }).delay(100).do(function (_ref2) { | |
| var id = _ref2.id; | |
| return console.info('Successful connection to device: %o', id); | |
| }); | |
| } | |
| }, { | |
| key: 'downloadFileFromDevice', | |
| value: function downloadFileFromDevice(device) { | |
| return _rxjs2.default.Observable.combineLatest(this._connectToDevice(device).do(this._sendMessage('Hello')), this._download(), function (device, file) { | |
| return { device: device, file: file }; | |
| }); | |
| } | |
| }]); | |
| return Manager; | |
| }(); | |
| var manager = new Manager(); | |
| manager.downloadFileFromDevice({ id: 'xyz' }).subscribe({ | |
| complete: function complete() { | |
| console.info('Download ready'); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment