SlideShare a Scribd company logo
1 of 26
Reactive Programming 是什麼?
能吃嗎?
By Kevin Yang
Reactive
Programming
•An API for asynchronous
programming
with observable streams
Observer pattern ( 觀察者模式)
• The observer pattern is a software design pattern in which an object,
called the subject, maintains a list of its dependents, called observers,
and notifies them automatically of any state changes, usually by
calling one of their methods.
• 在物件之間定義一個主題和多個觀察者,當主題的狀態改變,觀
察者都會收到通知,並自行更新。
Marble
Diagram (彈
珠圖)
Better Codebases
• Functional
• Avoid intricate stateful programs, using clean input/output functions over
observable streams.
• Less is more
• ReactiveX's operators often reduce what was once an elaborate challenge into a few
lines of code.
• Async error handling
• Traditional try/catch is powerless for errors in asynchronous computations, but
ReactiveX is equipped with proper mechanisms for handling errors.
• Concurrency made easy
• Observables and Schedulers in ReactiveX allow the programmer to abstract away
low-level threading, synchronization, and concurrency issues.
Support Languages
• Java: RxJava
• JavaScript: RxJS
• C#: Rx.NET
• C#(Unity): UniRx
• Scala: RxScala
• Clojure: RxClojure
• C++: RxCpp
• Lua: RxLua
• Ruby: Rx.rb
• Python: RxPY
• Go: RxGo
• Groovy: RxGroovy
• JRuby: RxJRuby
• Kotlin: RxKotlin
• Swift: RxSwift
• PHP: RxPHP
• Elixir: reaxive
• Dart: RxDart
RxJS
RxJS 基本元素
• Observable
• Observer
• Subscription
• Subject
• Operators
• Schedulers
什麼是 Observable
• Any number of values
• Over any amount of time
• Lazy: Set up observation and resources when subscribed to
• Cancellable: Will tear down observation and resources
• Laziness bonus: Can be retried/replayed by calling them again.
• Can be thought of as functions
什麼是 Observer
• Observer is a collection of callbacks that knows how to listen to values
delivered by the Observable.
• Collection of Callbacks如下
• next:Observer接收到一個值
• error : Observer接收到一個錯誤
• complete : Observer接收到完成的訊息
什麼是 Subscription
• Subscription represents the execution of an Observable, is primarily
useful for cancelling the execution.
什麼是 Operator
• Operators are pure functions that enable a functional programming
style of dealing with collections with operators like map, filter, concat,
mergeMap, etc.
Operators 的種類
• Filtering Operators: take, filter, first, ..
• Combination Operators: concat, merge, ..
• Transformation Operators: buffer, debounceTime, ...
• Utility Operation: do
• Error handling Operations: catch, retry …
幾個 Operators 範例
幾個 Operators 範例
幾個 Operators 範例
什麼是 Subject
• A Subject is a special type of Observable that allows values to be
multicasted to many Observables. Subjects are like EventEmitters.
什麼是 Scheduler
• An execution context and a data structure to order tasks and schedule
their execution. Provides a notion of (potentially virtual) time,
through the now() getter method
RxJS Learning Resource
• https://reactive.how/
• https://www.learnrxjs.io/
• http://reactivex.io/rxjs
Demo
範例1: Array 操作
var source = [1,2,3,4,5];
source.map((item) => {
console.log("onNext: "+item);
})
var source = [1,2,3,4,5];
from(source) .pipe(
map((item) => console.log("onNext: "+item))
)
範例2: Event (原生寫法)
<div id="test"></div>
<script>
document.getElementById("test")
.addEventListener("click", function( event ) {
event.target.innerHTML = "click count: " +
event.detail;
}, false);
</script>
範例2: Event (RxJS)
const ele = document.getElementById('test');
fromEvent(ele, 'click').pipe(
map((event: any)=> {
event.target.innerHTML = "click count: " +
event.detail;
return event;
})
).subscribe();
範例3: Painting
範例程式: https://stackblitz.com/edit/rxjs-drag-paint?file=index.ts
import { paint } from './canvas.js';
import { of, fromEvent } from 'rxjs';
import { mergeMap, takeUntil } from 'rxjs/operators';
const move$ = fromEvent(document, 'mousemove');
const down$ = fromEvent(document, 'mousedown');
const up$ = fromEvent(document, 'mouseup');
const paints$ = down$.pipe(
mergeMap(down => move$.pipe(takeUntil(up$)))
);
paints$.subscribe(v=> paint(v));
楊捷凱 (Kevin)
• 微軟 MVP
• Angular / Web Technologies GDE
• Blog:https://blog.kevinyang.net/
• FB Page:https://www.facebook.com/CKNotepad
• Organizers:
• Angular Taiwan
• Angular Girls Taiwan
• Angular 線上讀書會

More Related Content

Similar to Reactive Programmin

Predictable reactive state management - ngrx
Predictable reactive state management - ngrxPredictable reactive state management - ngrx
Predictable reactive state management - ngrxIlia Idakiev
 
Kubernetes Infra 2.0
Kubernetes Infra 2.0Kubernetes Infra 2.0
Kubernetes Infra 2.0Deepak Sood
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaKasun Indrasiri
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examplesPeter Lawrey
 
Kubernetes 101
Kubernetes 101Kubernetes 101
Kubernetes 101Huy Vo
 
Solve it Differently with Reactive Programming
Solve it Differently with Reactive ProgrammingSolve it Differently with Reactive Programming
Solve it Differently with Reactive ProgrammingSupun Dissanayake
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaNexThoughts Technologies
 
Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Richard Langlois P. Eng.
 
Reactive Thinking in Java
Reactive Thinking in JavaReactive Thinking in Java
Reactive Thinking in JavaYakov Fain
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaMax Alexejev
 
Hot to build continuously processing for 24/7 real-time data streaming platform?
Hot to build continuously processing for 24/7 real-time data streaming platform?Hot to build continuously processing for 24/7 real-time data streaming platform?
Hot to build continuously processing for 24/7 real-time data streaming platform?GetInData
 
Observability beyond logging for Java Microservices
Observability beyond logging for Java MicroservicesObservability beyond logging for Java Microservices
Observability beyond logging for Java MicroservicesLuke Marsden
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternNitin Bhide
 
Observables in Angular
Observables in AngularObservables in Angular
Observables in AngularKnoldus Inc.
 
Architecting for Microservices Part 2
Architecting for Microservices Part 2Architecting for Microservices Part 2
Architecting for Microservices Part 2Elana Krasner
 
20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the tradeshinolajla
 
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive ProgrammingMicroservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive ProgrammingAraf Karsh Hamid
 
Reactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorReactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorOrenEzer1
 
Building Reactive applications with Akka
Building Reactive applications with AkkaBuilding Reactive applications with Akka
Building Reactive applications with AkkaKnoldus Inc.
 

Similar to Reactive Programmin (20)

Predictable reactive state management - ngrx
Predictable reactive state management - ngrxPredictable reactive state management - ngrx
Predictable reactive state management - ngrx
 
Kubernetes Infra 2.0
Kubernetes Infra 2.0Kubernetes Infra 2.0
Kubernetes Infra 2.0
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
 
Reactive programming with examples
Reactive programming with examplesReactive programming with examples
Reactive programming with examples
 
Kubernetes 101
Kubernetes 101Kubernetes 101
Kubernetes 101
 
Solve it Differently with Reactive Programming
Solve it Differently with Reactive ProgrammingSolve it Differently with Reactive Programming
Solve it Differently with Reactive Programming
 
Java8
Java8Java8
Java8
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5Reactive Programming in Java and Spring Framework 5
Reactive Programming in Java and Spring Framework 5
 
Reactive Thinking in Java
Reactive Thinking in JavaReactive Thinking in Java
Reactive Thinking in Java
 
Distributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and Scala
 
Hot to build continuously processing for 24/7 real-time data streaming platform?
Hot to build continuously processing for 24/7 real-time data streaming platform?Hot to build continuously processing for 24/7 real-time data streaming platform?
Hot to build continuously processing for 24/7 real-time data streaming platform?
 
Observability beyond logging for Java Microservices
Observability beyond logging for Java MicroservicesObservability beyond logging for Java Microservices
Observability beyond logging for Java Microservices
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Observables in Angular
Observables in AngularObservables in Angular
Observables in Angular
 
Architecting for Microservices Part 2
Architecting for Microservices Part 2Architecting for Microservices Part 2
Architecting for Microservices Part 2
 
20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the trade
 
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive ProgrammingMicroservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive Programming
 
Reactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactorReactive solutions using java 9 and spring reactor
Reactive solutions using java 9 and spring reactor
 
Building Reactive applications with Akka
Building Reactive applications with AkkaBuilding Reactive applications with Akka
Building Reactive applications with Akka
 

More from Chieh Kai Yang

Dotnet Conf 2021 - F# and Safe Stack
Dotnet Conf 2021 - F# and Safe StackDotnet Conf 2021 - F# and Safe Stack
Dotnet Conf 2021 - F# and Safe StackChieh Kai Yang
 
無密碼時代終於要來了嗎
無密碼時代終於要來了嗎無密碼時代終於要來了嗎
無密碼時代終於要來了嗎Chieh Kai Yang
 
Web.dev extended : What's new in Web [GDG Taichung]
Web.dev extended : What's new in Web [GDG Taichung]Web.dev extended : What's new in Web [GDG Taichung]
Web.dev extended : What's new in Web [GDG Taichung]Chieh Kai Yang
 
Study4TW - .NET Conf 2019 - Rx
Study4TW - .NET Conf 2019 - RxStudy4TW - .NET Conf 2019 - Rx
Study4TW - .NET Conf 2019 - RxChieh Kai Yang
 
從零走到 Angular 世界
從零走到 Angular 世界從零走到 Angular 世界
從零走到 Angular 世界Chieh Kai Yang
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器Chieh Kai Yang
 
Study4.TW .NET Conf 2018 - Fp in c#
Study4.TW .NET Conf 2018  - Fp in c#Study4.TW .NET Conf 2018  - Fp in c#
Study4.TW .NET Conf 2018 - Fp in c#Chieh Kai Yang
 
How to 系列 - Hosting a website
How to 系列 - Hosting a websiteHow to 系列 - Hosting a website
How to 系列 - Hosting a websiteChieh Kai Yang
 
2018-01-06 Study4Love Conference - Rendertron
2018-01-06 Study4Love Conference - Rendertron2018-01-06 Study4Love Conference - Rendertron
2018-01-06 Study4Love Conference - RendertronChieh Kai Yang
 
ModernWeb 2017 angular component
ModernWeb 2017 angular componentModernWeb 2017 angular component
ModernWeb 2017 angular componentChieh Kai Yang
 

More from Chieh Kai Yang (11)

Dotnet Conf 2021 - F# and Safe Stack
Dotnet Conf 2021 - F# and Safe StackDotnet Conf 2021 - F# and Safe Stack
Dotnet Conf 2021 - F# and Safe Stack
 
無密碼時代終於要來了嗎
無密碼時代終於要來了嗎無密碼時代終於要來了嗎
無密碼時代終於要來了嗎
 
Structured data
Structured dataStructured data
Structured data
 
Web.dev extended : What's new in Web [GDG Taichung]
Web.dev extended : What's new in Web [GDG Taichung]Web.dev extended : What's new in Web [GDG Taichung]
Web.dev extended : What's new in Web [GDG Taichung]
 
Study4TW - .NET Conf 2019 - Rx
Study4TW - .NET Conf 2019 - RxStudy4TW - .NET Conf 2019 - Rx
Study4TW - .NET Conf 2019 - Rx
 
從零走到 Angular 世界
從零走到 Angular 世界從零走到 Angular 世界
從零走到 Angular 世界
 
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
改善 Angular 開發流程:你所不知道的 Schematics 程式碼產生器
 
Study4.TW .NET Conf 2018 - Fp in c#
Study4.TW .NET Conf 2018  - Fp in c#Study4.TW .NET Conf 2018  - Fp in c#
Study4.TW .NET Conf 2018 - Fp in c#
 
How to 系列 - Hosting a website
How to 系列 - Hosting a websiteHow to 系列 - Hosting a website
How to 系列 - Hosting a website
 
2018-01-06 Study4Love Conference - Rendertron
2018-01-06 Study4Love Conference - Rendertron2018-01-06 Study4Love Conference - Rendertron
2018-01-06 Study4Love Conference - Rendertron
 
ModernWeb 2017 angular component
ModernWeb 2017 angular componentModernWeb 2017 angular component
ModernWeb 2017 angular component
 

Recently uploaded

APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Recently uploaded (20)

APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

Reactive Programmin

  • 2. Reactive Programming •An API for asynchronous programming with observable streams
  • 3. Observer pattern ( 觀察者模式) • The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. • 在物件之間定義一個主題和多個觀察者,當主題的狀態改變,觀 察者都會收到通知,並自行更新。
  • 4.
  • 6. Better Codebases • Functional • Avoid intricate stateful programs, using clean input/output functions over observable streams. • Less is more • ReactiveX's operators often reduce what was once an elaborate challenge into a few lines of code. • Async error handling • Traditional try/catch is powerless for errors in asynchronous computations, but ReactiveX is equipped with proper mechanisms for handling errors. • Concurrency made easy • Observables and Schedulers in ReactiveX allow the programmer to abstract away low-level threading, synchronization, and concurrency issues.
  • 7. Support Languages • Java: RxJava • JavaScript: RxJS • C#: Rx.NET • C#(Unity): UniRx • Scala: RxScala • Clojure: RxClojure • C++: RxCpp • Lua: RxLua • Ruby: Rx.rb • Python: RxPY • Go: RxGo • Groovy: RxGroovy • JRuby: RxJRuby • Kotlin: RxKotlin • Swift: RxSwift • PHP: RxPHP • Elixir: reaxive • Dart: RxDart
  • 9. RxJS 基本元素 • Observable • Observer • Subscription • Subject • Operators • Schedulers
  • 10. 什麼是 Observable • Any number of values • Over any amount of time • Lazy: Set up observation and resources when subscribed to • Cancellable: Will tear down observation and resources • Laziness bonus: Can be retried/replayed by calling them again. • Can be thought of as functions
  • 11. 什麼是 Observer • Observer is a collection of callbacks that knows how to listen to values delivered by the Observable. • Collection of Callbacks如下 • next:Observer接收到一個值 • error : Observer接收到一個錯誤 • complete : Observer接收到完成的訊息
  • 12. 什麼是 Subscription • Subscription represents the execution of an Observable, is primarily useful for cancelling the execution.
  • 13. 什麼是 Operator • Operators are pure functions that enable a functional programming style of dealing with collections with operators like map, filter, concat, mergeMap, etc.
  • 14. Operators 的種類 • Filtering Operators: take, filter, first, .. • Combination Operators: concat, merge, .. • Transformation Operators: buffer, debounceTime, ... • Utility Operation: do • Error handling Operations: catch, retry …
  • 18. 什麼是 Subject • A Subject is a special type of Observable that allows values to be multicasted to many Observables. Subjects are like EventEmitters.
  • 19. 什麼是 Scheduler • An execution context and a data structure to order tasks and schedule their execution. Provides a notion of (potentially virtual) time, through the now() getter method
  • 20. RxJS Learning Resource • https://reactive.how/ • https://www.learnrxjs.io/ • http://reactivex.io/rxjs
  • 21. Demo
  • 22. 範例1: Array 操作 var source = [1,2,3,4,5]; source.map((item) => { console.log("onNext: "+item); }) var source = [1,2,3,4,5]; from(source) .pipe( map((item) => console.log("onNext: "+item)) )
  • 23. 範例2: Event (原生寫法) <div id="test"></div> <script> document.getElementById("test") .addEventListener("click", function( event ) { event.target.innerHTML = "click count: " + event.detail; }, false); </script>
  • 24. 範例2: Event (RxJS) const ele = document.getElementById('test'); fromEvent(ele, 'click').pipe( map((event: any)=> { event.target.innerHTML = "click count: " + event.detail; return event; }) ).subscribe();
  • 25. 範例3: Painting 範例程式: https://stackblitz.com/edit/rxjs-drag-paint?file=index.ts import { paint } from './canvas.js'; import { of, fromEvent } from 'rxjs'; import { mergeMap, takeUntil } from 'rxjs/operators'; const move$ = fromEvent(document, 'mousemove'); const down$ = fromEvent(document, 'mousedown'); const up$ = fromEvent(document, 'mouseup'); const paints$ = down$.pipe( mergeMap(down => move$.pipe(takeUntil(up$))) ); paints$.subscribe(v=> paint(v));
  • 26. 楊捷凱 (Kevin) • 微軟 MVP • Angular / Web Technologies GDE • Blog:https://blog.kevinyang.net/ • FB Page:https://www.facebook.com/CKNotepad • Organizers: • Angular Taiwan • Angular Girls Taiwan • Angular 線上讀書會