2 * 20 * £4 = £1601
"As part of its normal process, the UTP distributed test data and certain third parties improperly propagated the data."
- NASDAQ
^ ts 1.8
^ syncing state means that I couldn't reboot the server easily ^ hard to test rabbitMQ. proxy-require modules ^ no need for multiple workers ^ hard to manage deployments
const message = {
type: 'PRICE_TICK',
price: 1.3508
};
const state = {
currentPrice: 1.3502
};
function update(state, message) {
switch(message.type) {
case 'PRICE_TICK':
return {
...state,
currentPrice: message.price
};
default:
return state;
}
}
const newState = update(state, message);
console.log(newState); // {currentPrice: 1.3508};
^ ts 1.9
^ 1. leveldb is a 🃏 ^ 2. crashes
function update(state, message) {
switch(message.type) {
case 'PRICE_TICK':
/* SIDE EFFECT! */
updateOrderWithPrice(message.price);
return {
...state,
currentPrice: message.price
};
default:
return state;
}
}
function getPrice(dispatch) {
dispatch({type: 'PRICE_TICK'});
updateOrderWithPrice(message.price).then(order => {
dispatch({type: 'ORDER_UPDATED', order});
});
}
function(state, action) {
switch(action.type) {
case 'PRICE_TICK':
return {
...state,
currentPrice: message.price
};
case 'ORDER_UPDATED':
return {
...state,
openOrder: message.order
};
}
}
function(state, message) {
switch(message.type) {
case 'PRICE_TICK':
return [
{...state, currentPrice: message.price},
updateOrderWithPrice(message.price)
];
}
}
function updateOrderWithPrice(price) {
return {
commandType: 'UPDATE_ORDER',
price
};
}
console.log(updateOrderWithPrice(1.2)) // {
// price: 1.2,
// commandType: 'UPDATE_ORDER'
// }
^ ts 2.0, 2.1, 2.2 ^ redux-like state ^ redis
const fetch = {
commandType: 'FETCH',
url: '/any_url'
};
const log = {
commandType: 'LOG',
value: 'Hello World!'
};
function interpreter(commands) {
commands.forEach(command => {
switch(command.commandType) {
case 'FETCH':
request(command.url);
case 'LOG':
console.log(command.value);
}
});
}
^ ts 2.3, 2.4 ^ redux-like state ^ redis
updateOrder(order: Order, price: number): State
enum State {
UPDATE,
DO_NOTHING,
CANCEL
}
it('should update the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.11;
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
it('should update the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.11;
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
it('should cancel the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.22;
expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL);
});
it('should update the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.11;
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
it('should do nothing', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.12;
expect(updateOrder(order, currentTickPrice)).toEqual(DO_NOTHING);
});
it('should cancel the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.22;
expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL);
});
it('should update the order');
it('should do nothing');
it('should cancel the order');
it('should update the order');
it('should do nothing');
it('should cancel the order');
it('should NOT update the order');
it('should NOT do nothing');
it('should NOT cancel the order');
it('should update the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.11;
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
it('should do nothing', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.12;
expect(updateOrder(order, currentTickPrice)).toEqual(DO_NOTHING);
});
it('should cancel the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.22;
expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL);
});
it('should NOT update the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.11;
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
it('should NOT do nothing', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.12;
expect(updateOrder(order, currentTickPrice)).toEqual(DO_NOTHING);
});
it('should NOT cancel the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.22;
expect(updateOrder(order, currentTickPrice)).toEqual(CANCEL);
});
it('should update the order', () => {
const order = {id: 1, price: 1.12, active: true};
const currentTickPrice = 1.11;
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
it('should update the order', () => {
const order = {id: 1, price: 1.12, active: true}; // CHANGE ME
const currentTickPrice = 1.11; // CHANGE ME
expect(updateOrder(order, currentTickPrice)).toEqual(UPDATE);
});
test(updateOrder, () => {
given({id: 1, price: 1.11, active: true}, 1.12).expect(UPDATE);
given({id: 1, price: 1.12, active: true}, 1.12).expect(DO_NOTHING);
given({id: 1, price: 1.12, active: true}, 1.22).expect(CANCEL);
});
test(updateOrder, () => {
given({id: 1, price: 1.11, active: true}, 1.12).expect(UPDATE);
given({id: 1, price: 1.12, active: true}, 1.12).expect(DO_NOTHING);
given({id: 1, price: 1.12, active: true}, 1.22).expect(CANCEL);
given({id: 1, price: 1.11, active: false}, 1.12).expect(DO_NOTHING);
given({id: 1, price: 1.12, active: false}, 1.12).expect(DO_NOTHING);
given({id: 1, price: 1.12, active: false}, 1.22).expect(DO_NOTHING);
});
const order = {
id: 1,
price: 1.12
};
const order = {
id: 1,
price: 1.12,
active: true // new field
};
interace IOrder = {
id: number
price: number
};
const order: IOrder = {
id: 1,
price: 1.12
};
interace IOrder = {
id: number
price: number
active: boolean
};
const order: IOrder = { // ERROR! `active` is missing
id: 1,
price: 1.12
};
const messages = [
{type: 'TICK', price: 1.12},
{type: 'TICK', price: 1.13},
{type: 'SUBMITTED', price: 1.13},
{type: 'COMPLETED', id: 1},
];
const initialState = {
openOrders: []
};
const finalState = messages.reduce((state, message) => {
return Update(state, message);
});
expect(finalState).toMatchSnapshot();
- The Elm Architecture(0.18)effect moduleメモ
- Effect Manager のしくみ
- How to structure Elm with multiple models?
- Tribeca
- Blackbird
- Extensible Effects in Node.js, Part 1
Footnotes
-
best case scenario ↩