Skip to content

Instantly share code, notes, and snippets.

View MikeBild's full-sized avatar
🏠
Working from home

Mike Bild MikeBild

🏠
Working from home
View GitHub Profile
@MikeBild
MikeBild / cpu-bound.js
Last active October 25, 2016 14:12
Node.js - CPU bound operations with Rx
const RxNode = require('rx-node');
const Rx = require('rx');
const spawn = require('child_process').spawn;
RxNode.fromStream(spawn('find', ['/','-type','f','-exec', 'cat', '{}', '\+']).stdout)
.map(x => x.toString())
// using python
.map(x => RxNode.fromStream(spawn('./word-count.py', [x]).stdout))
// or using javascript
//.map(x => RxNode.fromStream(spawn('./word-count.js', [x]).stdout))
@MikeBild
MikeBild / event-sourced.js
Created October 25, 2016 16:32
Event-Source with Event-Log persistence with RxJS
const fs = require('fs');
const Rx = require('rx');
const RxNode = require('rx-node');
const timerStream = new Rx.Subject();
const initialState = {count: 0};
const updateState = (state, msg) => {
state.count++;
return state;
};
@MikeBild
MikeBild / coin-changer.js
Last active October 29, 2016 09:13
Resource lifetime binding to RxJS stream
const Rx = require('rx');
const coinsStream = Rx.Observable.fromArray([1,2,5,10,20,50,100].reverse());
Rx.Observable
// bind CoinResource lifetime to the stream lifetime
.using(() => new CoinResource(9), resource => coinsStream.map(x => ({ amount: resource.getValue(), coin: x })))
// calculations
.reduce((state, e) => {
const amount = (state.length > 0) ? state[state.length-1].rest : e.amount;
@MikeBild
MikeBild / order-saga.js
Last active October 29, 2016 17:27
Simple Order-Saga example in RxJS (without Saga-Log)
const Rx = require('rx');
// simulate transactions
const bookCar = id => Rx.Observable.return({sagaId: id, 'Car A': true}).delay(1000);
const bookHotel = id => Rx.Observable.return({sagaId: id, 'Hotel B': true}).delay(1000);
const bookFlight = id => Rx.Observable.return({sagaId: id, 'Flight C': true}).delay(1000);
// simulate error
//const bookFlight = id => Rx.Observable.throw({sagaId: id, 'Flight C': true}).delay(1000);
// transaction compensation (idempotence)
@MikeBild
MikeBild / app.js
Last active October 30, 2016 06:22
Memoize - you never need constructor/property based dependency injection in JavaScript
const require1 = require('./require1');
const require2 = require('./require2');
const require3 = require('./require3');
console.log(require1.getValue());
console.log(require2.getValue());
console.log(require3.getValue());
@MikeBild
MikeBild / demonstration.js
Created November 10, 2016 10:26
Q&A - I wanna understand TypeScript - Part TypeScript Union Types as Redux Action Tags
// demonstrate
const simulateActions = [
addTodo({text: 'todo 1'}),
addTodo({text: 'todo 2'}),
toggleTodo({index: 0}),
toggleTodo({index: 1}),
];
const actual = simulateActions.reduce(todosReducer, []);
console.log(actual);
@MikeBild
MikeBild / bank-account-system-poc.js
Last active April 10, 2017 12:40
Proof of Concept (recompose) - Bank-Account-System composition with higher-order functions (NodeJS6)
const account = openBankAccount({name: 'Max Muster'})({})
account.deposit(1000)
account.withdraw(800)
account.withdraw(800)
// use compose(...) building block
const { calculateBalance, name, transactions, save} = compose(withBalanceCalculation({}), withStore())(account)
console.log(`${name}: ${calculateBalance()} [${transactions.map(x => x.deposit || 0 - x.withdraw)}]`)
console.log(save())
// same stuff, but use the handy way
@MikeBild
MikeBild / compose-graphql-queries.js
Last active September 12, 2017 20:28
API Prototype - compose React components to Higher-Order-Components with GraphQL Fragment / Query composer
export const Gists = props =>
<div>
{
props.viewer.gists &&
props.viewer.gists.map(x => <p>{x.description}</p>)
}
</div>
export const GistsWithGraphQL = compose(
withGraphQL(variables => Fragment`fragment gists on Gist {
@MikeBild
MikeBild / async-await-promise-all-error-handling.js
Last active May 10, 2018 23:07
Avoid async/await on integration level
const defaultFirst = 'A`'
const defaultSecond = 'B`'
const defaultThird = 'C`'
async function doFirst() {
// throw new Error('Error A')
return await 'A'
}
async function doSecond() {
import React, { useState } from 'react';
import { render } from 'react-dom';
const todosStore = {
todos: {
'1': {
id: '1',
text: '',
},
},