Skip to content

Instantly share code, notes, and snippets.

@viciouslabs
viciouslabs / mobx-bringing-back-immutability.js
Last active October 15, 2017 15:11
MobX for those who can't get over their ex, Redux
class Message {
@observable message = "Hello world"
// fictional example, if author is immutable, we just need to store a reference and shouldn't turn it into a mutable, observable object
@observable.ref author = null
}
/**
* Solving the problem:
* In a circular race of 1200 m length, A and B start with speeds of 18kmph and 27 kmph starting
* at the same time from the same point. When will they meet for the first time at the starting
* point when running in the same direction?
*/
// converts kmph to mps
function convertToMetersPerSecond(speedInKmph) {
return (1000/3600) * speedInKmph
@viciouslabs
viciouslabs / array_stream.js
Created November 23, 2017 08:24
Array vs Stream
// simple array
const arr = [{ x: 1, y: 4 }, { x: 20.2, y: 40.4 }, { x: 130.2, y: 240.1 }]
// stream of mouse position looks similar
const stream = [{ x: 1, y: 4 }, { x: 20.2, y: 40.4 }, { x: 130.2, y: 240.1 }]
// simply priting all array items
Rx.Observable.fromArray(arr)
.subscribe(x => console.log(`Array item: ${x.x}, ${x.y}`))
@viciouslabs
viciouslabs / stream_completion.js
Created November 23, 2017 08:45
RxJS: Stream Completion
// An array stream is a stream that completes
Rx.Observable.fromArray([1,2,3,4])
.subscribe(
function next(input) {
console.log('Received event data', input)
},
function error(err) {
consle.log('Log error', err)
},
function complete() {
@viciouslabs
viciouslabs / promise_observable_similarity.js
Created November 23, 2017 09:00
RxJS: Promise Observable Similarity
// example of a promise
const deferred = new Promise((resolve, reject) => {
setTimeout(function() {
resolve('Promise resolved.')
}, 2000)
})
deferred.then(function resolved(data) {
console.log(data)
}, function rejected(err) {