Communicating with Channels

46

Transcript of Communicating with Channels

Page 1: Communicating with Channels
Page 2: Communicating with Channels

Communicating with Channels JAMES LONG

Page 3: Communicating with Channels

Who am I

Page 4: Communicating with Channels

ChannelsCommunicating Sequential Processes

and

(not Content Security Policy)

Page 5: Communicating with Channels
Page 6: Communicating with Channels

Outline• Explain channels in more detail • Show some examples of integration with React components • Show how Flux can be re-envisioned with channels

Page 7: Communicating with Channels

Making Ravioli

Page 8: Communicating with Channels

PUTputs a value

TAKEtakes a value

GOstarts a process

CHANcreates a channel

Page 9: Communicating with Channels

let { chan, go, take, put } = require('js-csp');let ch = chan();!

go(function*() { while(true) { console.log(yield take(ch)); }});!

go(function*() { yield put(ch, 1); yield put(ch, 2); yield put(ch, 3);});!

// Output: 1 2 3

https://github.com/ubolonton/js-csp

Page 10: Communicating with Channels

let { chan, go, take, put } = require('js-csp');let ch = chan();!

go(function*() { while(true) { console.log(yield ch); }});!

go(function*() { yield put(ch, 1); yield put(ch, 2); yield put(ch, 3);});!

// Output: 1 2 3

https://github.com/ubolonton/js-csp

Page 11: Communicating with Channels

(livecode)

Page 12: Communicating with Channels

Additional Features

Promise Channels (coming soon) let ch = promiseChan();

yield put(ch, 5);!

// Somewhere else...yield take(ch); // <- 5yield take(ch); // <- 5

Page 13: Communicating with Channels

Additional Features

Closing a Channel ch.close();!

yield take(ch); // <- csp.CLOSEDyield put(ch, 5); // <- false

Page 14: Communicating with Channels

Advanced Usage

Higher-order Operations multmixmergepipeline

Page 15: Communicating with Channels

Advanced Usage

Custom Buffering let ch1 = chan(5);let ch2 = chan(csp.buffers.dropping(10));let ch3 = chan(csp.buffers.sliding(1));

Page 16: Communicating with Channels

Advanced UsageTransducers let { compose, map, filter, take } = require(‘transducers.js’);!

let ch = chan(1, map(x => x * 2));let ch = chan(1, compose( map(x => x.amount), filter(x => x > 0), take(10)));

Page 17: Communicating with Channels

Why isn’t this used?

•Generators not available until recently •An idea from Go/Clojure that simply hasn’tbeen tried yet

•Susceptible to deadlocks

Page 18: Communicating with Channels

Component EVENT HANDLINGwith channels

Page 19: Communicating with Channels

Example #1: Simple List

Page 20: Communicating with Channels

App

MessageCreator

Input

onChange/onKeyDown

onCreate

Page 21: Communicating with Channels

(code walkthrough)

Page 22: Communicating with Channels

ChannelsMixin

Adds send method !

•Creates an event handler •Takes a name and arguments •If method exists with same name, simply forwards events with supplied arguments

•Otherwise sends events onto a named channel

Page 23: Communicating with Channels

ChannelsMixin

Adds getEventChannel method !

•Takes a name and returns the named channel

Page 24: Communicating with Channels

Benefits & Disadvantages

•Good: Transducer-compatible API •Bad: More verbose •Neutral: Not really much different

Page 25: Communicating with Channels

Example #2: Recording the Mouse

Page 26: Communicating with Channels

(code walkthrough)

Page 27: Communicating with Channels

Events

•onMouseMove handles mouse events •extracts data •dispatches

•velocityRecord sticks temporal state oncomponent instance

Page 28: Communicating with Channels

Channels

•mouseCoord channel instead of onMouseMove event •data extraction with transducer •no dispatching required

•velocityRecord uses lexical scoping to keep state

Page 29: Communicating with Channels

Benefits & Disadvantages

•Good: separation of concerns, modular •Good: data flow as a first-class value •Good: no leaking state •Bad: Still more verbose

Page 30: Communicating with Channels

Example #3: Tooltip

Page 31: Communicating with Channels

(code walkthrough)

Page 32: Communicating with Channels

Channels

•cancellation and timeout signals •more complex UIs would compose with data flow (think modal that sends back data)

Page 33: Communicating with Channels

Benefits & Disadvantages

•Good: very clear workflow •Good: tiny amount of code •Good: avoids tracking tons of UI state

Page 34: Communicating with Channels

More Possibilities

•Cross-component channels •Allow arbitrary types of channels: mix, mult, pub/sub

Page 35: Communicating with Channels

Takeaways

Using event handlers for simple events is fine

Channels allow more complex composition ofsignals and async workflows

They also help separate related logic into a single place

Page 36: Communicating with Channels

FLUXwith channels

Page 37: Communicating with Channels

(code walkthrough)

Page 38: Communicating with Channels

Transactions

Uncategorized

Other Component

Dispatcher

TransactionStore

TotalStore

BudgetStore

UncategorizedStore

Page 39: Communicating with Channels

Notes about Flux

• Extracts out data into “one source of truth” • Explicit forward flow • Separates data into logical units • Serves two purposes: • Allows data dependencies • Optimizes rendering

Page 40: Communicating with Channels

Transactions

Uncategorized

Other Component

Data Blob

App

Page 41: Communicating with Channels

Re-using Abstractions

• pub/sub channels for firing actions fromcomponents

• cursors for watching for data changes

Page 42: Communicating with Channels

(code walkthrough)

Page 43: Communicating with Channels

Transactions

Uncategorized

Other Component

pub/sub

Transaction Add Process

Transaction Update Process

Other Process

Transactions Cursor

Budget Cursor

Page 44: Communicating with Channels

Potential Advantages

• Composability of event flows • Cursor abstraction removes need for emitting

change events • Potential global app state structure • Component subscribing to events

Page 45: Communicating with Channels

Disadvantages

• Cursors can be invalidated easily, needs work • Doesn’t enforce as much structure • May not need any event composability

Page 46: Communicating with Channels

Thanks!@jlongster

https://github.com/ubolonton/js-csp