3. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
A GENERAL THEORY OF RELATIVITY
3
Albert Einstein in 1921, as he rode in a motorcade in New York City with crowds welcoming his first visit to the U.S., Life Magazine, Public Domain
4.
5.
6. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
• sync / async
• singular / plural
• single consumer / multiple consumers
• unicast (cancelable) / broadcast
• coping with fast producer | slow consumer
DIMENSIONS OF REACTIVITY
6
7. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
• push → discrete over time, observables, gauges, drop messages
• pull ← continuous over time, behaviors, counters, lose fidelity
• pressure ↔ reliable, streams
FAST PRODUCER | SLOW CONSUMER
7
8. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
STREAMS ARE LIKE ARRAYS (PLURAL) PROMISES (ASYNC)
return Stream.from(fileNames).map(fileName =>
fs.readFile(fileName).then(content =>
({fileName, content})
}, null, 20).forEach(({fileName, content}) =>
console.log(fileName, content.length)
);
8
14. generator and observer (push, sync)
{observe(onNext(Value),
onReturn(Value),
onThrow(Error))}
{next(Value), return(Value), throw(Error)}
information
15. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PUSH
•Use Observer
•For discrete time series data
•changes in response to an event
•e.g., estimated time to completion
•(02:30)
•Rx, a là Erik Meijer
PUSH VS PULL FOR TIME SERIES VALUES
15
PULL
• Use Iterator
• For continuous time series data
• always changing, must be sampled
periodically
• e.g., progress to completion
• (50%)
• FRP, a là Conal Elliott
16. generator function (pull, sync)
next(Void):Iteration<Value>
yield Value / return Value / throw Error
information
17. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
ARRAY-RETURNING FUNCTION
function range(start, stop, step) {
var result = [];
while (start < stop) {
result.push(start);
start += step;
}
return result;
}
17
18. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
GENERATOR FUNCTION
function* range(start, stop, step) {
while (start < stop) {
yield start;
start += step;
}
}
var iterator = range(0, 4, 2);
iterator.next(); // {done: false, value: 0}
iterator.next(); // {done: false, value: 2}
iterator.next(); // {done: true, value: undefined}
18
20. deferred (pomise and resolver)
{then(onReturn(Value), onThrow(Error))}
{return(Value | Promise<Value>), throw(Error)}
information
21. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PROMISE IN A NUTSHELL
out = in.then(
inval => outres,
inerr => outres
);
21
22. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PLAN INTERFERENCE
var i = 0;
yolo(function lol() {
i++;
});
console.log(i);
22
24. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
ASYNC FUNCTION
function time(job) {
let start = Date.now();
return job().then(
() => Date.now() - start
);
}
async function sum(getX, getY) {
return await getX() + await getY();
}
sum(time(task), time(task)).then(console.log);
24
25. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
PROMISES
25
•order independence
•guaranteed async (zalgo containment)
•defensive
•POLA (one-way communication)
•chainability
•composability
•gateway to proxies for remote objects
•a là Mark Miller
35. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
SHARE A STREAM
var source = Stream.from(Iterator.range(100))
.map((n) => Task.delay(250).thenReturn(n))
Iterator.range(3).forEach((delay) =>
return source.map(
(n) => Task.delay(delay * 1000).thenReturn(n);
);
);
35
36.
37. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
FORK A STREAM
var branches = Stream.from(Iterator.range(100))
.map((n) => Task.delay(250).thenReturn(n))
.fork(3);
branches.forEach(
(branch) => branch.forEach(
(n) => Task.delay(1000 + Math.random() * 1000)
.thenReturn(n)
).done()
);
37
38.
39. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
MAP A STREAM WITH A CONCURRENCY LIMIT
var branches = Stream.from(Iterator.range(100))
.map((n) => Task.delay(random()).thenReturn(n), null, 32)
.map((n) => Task.delay(random()).thenReturn(n), null, 16)
.map((n) => Task.delay(random()).thenReturn(n), null, 4)
.map((n) => Task.delay(random()).thenReturn(n), null, 1)
.forEach((n) => null).done()
39
40.
41. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
REDUCE A STREAM TO FIND THE MAX
return Stream.from(source).reduce((max, value) =>
Task.delay(500 + 500 * Math.random())
.thenReturn(Math.max(max, value))
).then((max) => {
console.log(max);
})
41
42.
43. A GENERAL THEORY OF REACTIVITY (GTOR)
KRIS KOWAL
MARCH 1, 2015
MAP | REDUCE
return Stream.from(source).map(
(value) => Task.delay(random())
.thenReturn(value),
null, 32
).reduce(
(max, value) => Task.delay(random())
.thenReturn(Math.max(max, value)),
null, 32
);
43