SlideShare a Scribd company logo
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rights
RXJS
TypeScript
Functional P.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Agenda
• Functional Programming.
• TypeScript.
• Reactive programming.
2
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Functional Programming in JS
• JS is a multi-paradigm language. That means it
doesn't restrict you to one particular paradigm
when writing code.
• JS is not optimized to be a Functional
Programming language. However, it has many of
the tools you need for FP.
• Using FP concepts in JS does not mean you have
to necessarily abandon other paradigms.
3
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
FP VS OOP
• What is OOP Paradigm.
• What is Functional Programming Paradigm.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is OOP Paradigm
Classes
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Functional Programming
Functions
R1
R2
R3
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
{
name: “Vivek”,
sayHi: () => `Hello, ${name}`
}
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Principles of FP
• Pure functions / Avoid side effects
• Higher order function.
• Function Currying.
• Function Composition over inheritance.
• Break functionality into reusable functions with single
responsibility.
• Avoid mutating state / Immutability .
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Pure Functions
• Any pure function simply maps input to output
that’s it.
• A function is called pure if it meets the following
two conditions:
1. There are no side-effects.
2. Given the same input, will always return the same
output.
10
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Pure function
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Examples of Impure Functions …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Don’t Mutate Input
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Not pure function
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Do not depend on global/shared
properties
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Don’t depend on external State
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
No Ajax Calls
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Avoiding side effects
• Don't mutate the input
• Don't modify any external variables/properties/
state
• Don't make any AJAX calls
• Don't manipulate the DOM
18
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Benefits of Pure Function
• Can memoize output
• Easy to test.
• No shared state, So highly immune to bugs.
• Easy for parallel processing.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Function currying
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Higher Order Functions
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Function Composition
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Function Composition
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is TypeScript
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Basic Types
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Classes
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Interfaces
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Generics
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Decorators
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Why Typescript
• Throws error in during development and saves time.
• Help avoid silent errors, like passing string instead of
number.
• Typescript helps to write static typed code.
• Typescript provides improved IntelliSense,
suggestions.
• Helps a lot while refactoring.
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
IntelliSense and errors
Demo …
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Reactive
Programming
Reactive Programming is an asynchronous
programming paradigm concerned with data
streams and the propagation of change
Describe your application in terms of data
flowing through pipelines of transformations (
respond to those pipelines & produce some sort
of result on the user interface )
In simple words, in reactive programming - data
flows emitted by one component and the
underlying structure provided by the language
will propagate those changes to all the other
components that are registered to receive those
changes
32
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Reactive
Programming
33
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Reactive
Programming
34
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
RxJS
• Library to build reactive applications
• JavaScript implementation of the ReactiveX API
• Build asynchronous, modular event-based apps
• Easily create event streams or data streams
• Compose or transform streams with query like
operators
• Allows streams of data to be observed – Listen to
any observable stream to perform side effects
35
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
ReactiveX ?
• An API for async programming with
observable streams
• More than an API, it's an idea
• The observer pattern done right!
• Combination of the best ideas from observer
pattern, iterator pattern and functional
programming
• Implementations -
– Java : RxJava
– JavaScript : RxJS
– Scala : RxScala
– and a lot more …
36
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
The Observer
design
pattern
• Observer pattern defines a one-to-many
dependency between objects such that when one
object's state changes, all its dependents are
notified and updated automatically.
• In observer pattern, there are many observers
(subscriber objects) that are observing a subject
(publisher object). Observers register themselves
to a subject to get a notification when there is a
change in the subject's state
• An observer object can register or unregister from
a subject at any point in time
37
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
The Observer
design
pattern
38
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
The Observer design pattern
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What are Observables ?
• Lazy push collections
of multiple values.
They fill the missing
spot in the
displayed table
40
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is an
Observable ?
• Observable is just a function that
takes an observer and returns a
function
• Shape:
– A function
– That accepts an observer: An
object with `next`, `error` and
`complete` methods on it
– And returns a cancellation
function
• Purpose:
– To connect the observer to
something that produces values
(a producer) and return a
means to tear down that
connection to the producer.
The observer is really a registry
of handlers that can be pushed
values over time
41
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
What is an Observer?
• Is a collection of callbacks that knows how to listen to values delivered by the
Observable. An object with `next`, `error` and `complete` methods on it
• When we talk about RxJS or Reactive programming, generally observables get top
billing. But the observer implementation is the workhorse of this type of reactive
programming. Observables are inert. They’re just functions. They sit there until
you `subscribe` to them, they set up your observer, and they’re done, back to
being boring old functions waiting to be called. The observers on the other hand,
stay active and listen for events from your producers
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Demo 1 : Let's create
an Observable
43
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
From Promiseland
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
To Observableland
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Promises vs Observables
46
• Single emission of value
• Eager
Promises
• Multi emission of values
• Lazy
Observables
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Operators
• Also Just Functions
• An operator in RxJS is little more than a function that takes a source observable,
and returns a new observable that will subscribe to that source observable when
you subscribe to it
• Some common operators :
– Map, filter, reduce
– scan
– combineLatest
– WithLatestFrom
– etc
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Example – The map operator
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Hot vs Cold
Observable
• Unicast vs Multicast
49
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Cold Observable
• COLD is when your observable creates the producer
• An observable is “cold” if its underlying producer is created and activated during
subscription. This means, that if observables are functions, then the producer is
created and activated by calling that function
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Cold Observable
• Example :
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Hot Observable
• HOT is when your observable closes over the producer
• An observable is “hot” if its underlying producer is either created or activated
outside of subscription
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Hot Observable
• Example :
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects An RxJS Subject is a special type of
Observable that allows values to be multi-
casted to many Observers. While plain
Observables are unicast (each subscribed
Observer owns an independent execution of
the Observable), Subjects are multicast
54
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects But how does it multicast?
55
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects
• Every Subject is an Observable. Given a Subject, you
can subscribe to it, providing an Observer, which will
start receiving values normally. From the perspective
of the Observer, it cannot tell whether the Observable
execution is coming from a plain unicast Observable
or a Subject
• Every Subject is an Observer. It is an object with the
methods next(v), error(e), and complete(). To feed a
new value to the Subject, just call next(theValue), and
it will be multi-casted to the Observers registered to
listen to the Subject
• Internally to the Subject, subscribe does not invoke a
new execution that delivers values. It simply registers
the given Observer in a list of Observers, similarly to
how addListener usually works in other libraries and
languages
56
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subjects -
remember
the observer
design
pattern?
57
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Cold to Hot using Subjects
• How to convert Cold Observables to Hot using Subjects?
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Types of Subject • Async Subject
• Behavior Subject
• Replay Subject
59
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Subject vs
Observable
• The most important distinction between a
Subject and an Observable is that a Subject
has state, it keeps a list of observers. On the
other hand, an Observable is just a
function that sets up observation
• While Subjects are Observables, Subjects
also implement an Observer interface. They
have next, error, and complete methods on
them. These methods are used to notify
their counterparts on observers in the
subject’s internal observers list. This means
a subject can be used as an observer to
subscribe to any observable.
60
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Demo 6 : A Reactive
example
61
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
Thank you …

More Related Content

Similar to Functional programming, TypeScript and RXJS

Redux JavaScript Meetup - Talentica, Saturday, July 6, 2019
Redux  JavaScript Meetup - Talentica,  Saturday, July 6, 2019 Redux  JavaScript Meetup - Talentica,  Saturday, July 6, 2019
Redux JavaScript Meetup - Talentica, Saturday, July 6, 2019
Vivek Tikar
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
Talentica Software
 
Functions and DevOps
Functions and DevOpsFunctions and DevOps
Functions and DevOps
Shaun Smith
 
Ensuring Project Success with SpiraTeam and Rapise from Inflectra pta - short
Ensuring Project Success with SpiraTeam and Rapise from Inflectra   pta - shortEnsuring Project Success with SpiraTeam and Rapise from Inflectra   pta - short
Ensuring Project Success with SpiraTeam and Rapise from Inflectra pta - short
Adam Sandman
 
Talentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular SchematicsTalentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular Schematics
Krishnan Mudaliar
 
Getting the most from your API management platform: A case study
Getting the most from your API management platform: A case studyGetting the most from your API management platform: A case study
Getting the most from your API management platform: A case study
Rogue Wave Software
 
Diagnose Your Microservices
Diagnose Your MicroservicesDiagnose Your Microservices
Diagnose Your Microservices
Marcus Hirt
 
[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...
[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...
[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...
Amazon Web Services
 
Advanced technologies and techniques for debugging HPC applications
Advanced technologies and techniques for debugging HPC applicationsAdvanced technologies and techniques for debugging HPC applications
Advanced technologies and techniques for debugging HPC applications
Rogue Wave Software
 
Life of a Code Change to a Tier 1 Service - AWS Online Tech Talks
Life of a Code Change to a Tier 1 Service - AWS Online Tech TalksLife of a Code Change to a Tier 1 Service - AWS Online Tech Talks
Life of a Code Change to a Tier 1 Service - AWS Online Tech Talks
Amazon Web Services
 
Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...
Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...
Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...
Amazon Web Services
 
Your Flight is Boarding Now!
Your Flight is Boarding Now!Your Flight is Boarding Now!
Your Flight is Boarding Now!
MeetupDataScienceRoma
 
Perth APAC Groundbreakers tour - The Autonomous Database
Perth APAC Groundbreakers tour - The Autonomous DatabasePerth APAC Groundbreakers tour - The Autonomous Database
Perth APAC Groundbreakers tour - The Autonomous Database
Connor McDonald
 
Sviluppare un backend serverless in real time attraverso GraphQL
Sviluppare un backend serverless in real time attraverso GraphQLSviluppare un backend serverless in real time attraverso GraphQL
Sviluppare un backend serverless in real time attraverso GraphQL
Amazon Web Services
 
PSD2 & Open Banking: How to go from standards to implementation and compliance
PSD2 & Open Banking: How to go from standards to implementation and compliancePSD2 & Open Banking: How to go from standards to implementation and compliance
PSD2 & Open Banking: How to go from standards to implementation and compliance
Rogue Wave Software
 
Git Internals
Git InternalsGit Internals
Git Internals
Vivek Tikar
 
Data fingerprinting
Data fingerprintingData fingerprinting
Data fingerprinting
Mrityunjay Kumar
 
Advanced angular
Advanced angularAdvanced angular
Advanced angular
Sumit Kumar Rakshit
 
Best practices iOS meetup - pmd
Best practices iOS meetup - pmdBest practices iOS meetup - pmd
Best practices iOS meetup - pmd
Suyash Gupta
 
Transforming Product Development- Transformation Day Philadelphia 2018
Transforming Product Development- Transformation Day Philadelphia 2018Transforming Product Development- Transformation Day Philadelphia 2018
Transforming Product Development- Transformation Day Philadelphia 2018
Amazon Web Services
 

Similar to Functional programming, TypeScript and RXJS (20)

Redux JavaScript Meetup - Talentica, Saturday, July 6, 2019
Redux  JavaScript Meetup - Talentica,  Saturday, July 6, 2019 Redux  JavaScript Meetup - Talentica,  Saturday, July 6, 2019
Redux JavaScript Meetup - Talentica, Saturday, July 6, 2019
 
Advanced JavaScript
Advanced JavaScriptAdvanced JavaScript
Advanced JavaScript
 
Functions and DevOps
Functions and DevOpsFunctions and DevOps
Functions and DevOps
 
Ensuring Project Success with SpiraTeam and Rapise from Inflectra pta - short
Ensuring Project Success with SpiraTeam and Rapise from Inflectra   pta - shortEnsuring Project Success with SpiraTeam and Rapise from Inflectra   pta - short
Ensuring Project Success with SpiraTeam and Rapise from Inflectra pta - short
 
Talentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular SchematicsTalentica - JS Meetup - Angular Schematics
Talentica - JS Meetup - Angular Schematics
 
Getting the most from your API management platform: A case study
Getting the most from your API management platform: A case studyGetting the most from your API management platform: A case study
Getting the most from your API management platform: A case study
 
Diagnose Your Microservices
Diagnose Your MicroservicesDiagnose Your Microservices
Diagnose Your Microservices
 
[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...
[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...
[NEW LAUNCH!] Introducing Amazon SageMaker RL - Build and Train Reinforcement...
 
Advanced technologies and techniques for debugging HPC applications
Advanced technologies and techniques for debugging HPC applicationsAdvanced technologies and techniques for debugging HPC applications
Advanced technologies and techniques for debugging HPC applications
 
Life of a Code Change to a Tier 1 Service - AWS Online Tech Talks
Life of a Code Change to a Tier 1 Service - AWS Online Tech TalksLife of a Code Change to a Tier 1 Service - AWS Online Tech Talks
Life of a Code Change to a Tier 1 Service - AWS Online Tech Talks
 
Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...
Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...
Leadership Session: Developing Mobile & Web Apps on AWS (MOB202-L) - AWS re:I...
 
Your Flight is Boarding Now!
Your Flight is Boarding Now!Your Flight is Boarding Now!
Your Flight is Boarding Now!
 
Perth APAC Groundbreakers tour - The Autonomous Database
Perth APAC Groundbreakers tour - The Autonomous DatabasePerth APAC Groundbreakers tour - The Autonomous Database
Perth APAC Groundbreakers tour - The Autonomous Database
 
Sviluppare un backend serverless in real time attraverso GraphQL
Sviluppare un backend serverless in real time attraverso GraphQLSviluppare un backend serverless in real time attraverso GraphQL
Sviluppare un backend serverless in real time attraverso GraphQL
 
PSD2 & Open Banking: How to go from standards to implementation and compliance
PSD2 & Open Banking: How to go from standards to implementation and compliancePSD2 & Open Banking: How to go from standards to implementation and compliance
PSD2 & Open Banking: How to go from standards to implementation and compliance
 
Git Internals
Git InternalsGit Internals
Git Internals
 
Data fingerprinting
Data fingerprintingData fingerprinting
Data fingerprinting
 
Advanced angular
Advanced angularAdvanced angular
Advanced angular
 
Best practices iOS meetup - pmd
Best practices iOS meetup - pmdBest practices iOS meetup - pmd
Best practices iOS meetup - pmd
 
Transforming Product Development- Transformation Day Philadelphia 2018
Transforming Product Development- Transformation Day Philadelphia 2018Transforming Product Development- Transformation Day Philadelphia 2018
Transforming Product Development- Transformation Day Philadelphia 2018
 

Recently uploaded

Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 

Recently uploaded (20)

Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 

Functional programming, TypeScript and RXJS

  • 1. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rights RXJS TypeScript Functional P.
  • 2. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Agenda • Functional Programming. • TypeScript. • Reactive programming. 2
  • 3. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Functional Programming in JS • JS is a multi-paradigm language. That means it doesn't restrict you to one particular paradigm when writing code. • JS is not optimized to be a Functional Programming language. However, it has many of the tools you need for FP. • Using FP concepts in JS does not mean you have to necessarily abandon other paradigms. 3
  • 4. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights FP VS OOP • What is OOP Paradigm. • What is Functional Programming Paradigm.
  • 5. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is OOP Paradigm Classes
  • 6. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
  • 7. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Functional Programming Functions R1 R2 R3
  • 8. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights { name: “Vivek”, sayHi: () => `Hello, ${name}` }
  • 9. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Principles of FP • Pure functions / Avoid side effects • Higher order function. • Function Currying. • Function Composition over inheritance. • Break functionality into reusable functions with single responsibility. • Avoid mutating state / Immutability .
  • 10. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Pure Functions • Any pure function simply maps input to output that’s it. • A function is called pure if it meets the following two conditions: 1. There are no side-effects. 2. Given the same input, will always return the same output. 10
  • 11. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Pure function
  • 12. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Examples of Impure Functions …
  • 13. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Don’t Mutate Input
  • 14. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Not pure function
  • 15. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Do not depend on global/shared properties
  • 16. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Don’t depend on external State
  • 17. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights No Ajax Calls
  • 18. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Avoiding side effects • Don't mutate the input • Don't modify any external variables/properties/ state • Don't make any AJAX calls • Don't manipulate the DOM 18
  • 19. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Benefits of Pure Function • Can memoize output • Easy to test. • No shared state, So highly immune to bugs. • Easy for parallel processing.
  • 20. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Function currying Demo …
  • 21. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Higher Order Functions
  • 22. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Function Composition
  • 23. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Function Composition
  • 24. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is TypeScript
  • 25. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Basic Types Demo …
  • 26. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Classes Demo …
  • 27. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Interfaces Demo …
  • 28. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Generics
  • 29. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Decorators Demo …
  • 30. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Why Typescript • Throws error in during development and saves time. • Help avoid silent errors, like passing string instead of number. • Typescript helps to write static typed code. • Typescript provides improved IntelliSense, suggestions. • Helps a lot while refactoring.
  • 31. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights IntelliSense and errors Demo …
  • 32. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Reactive Programming Reactive Programming is an asynchronous programming paradigm concerned with data streams and the propagation of change Describe your application in terms of data flowing through pipelines of transformations ( respond to those pipelines & produce some sort of result on the user interface ) In simple words, in reactive programming - data flows emitted by one component and the underlying structure provided by the language will propagate those changes to all the other components that are registered to receive those changes 32
  • 33. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Reactive Programming 33
  • 34. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Reactive Programming 34
  • 35. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights RxJS • Library to build reactive applications • JavaScript implementation of the ReactiveX API • Build asynchronous, modular event-based apps • Easily create event streams or data streams • Compose or transform streams with query like operators • Allows streams of data to be observed – Listen to any observable stream to perform side effects 35
  • 36. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights ReactiveX ? • An API for async programming with observable streams • More than an API, it's an idea • The observer pattern done right! • Combination of the best ideas from observer pattern, iterator pattern and functional programming • Implementations - – Java : RxJava – JavaScript : RxJS – Scala : RxScala – and a lot more … 36
  • 37. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights The Observer design pattern • Observer pattern defines a one-to-many dependency between objects such that when one object's state changes, all its dependents are notified and updated automatically. • In observer pattern, there are many observers (subscriber objects) that are observing a subject (publisher object). Observers register themselves to a subject to get a notification when there is a change in the subject's state • An observer object can register or unregister from a subject at any point in time 37
  • 38. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights The Observer design pattern 38
  • 39. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights The Observer design pattern
  • 40. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What are Observables ? • Lazy push collections of multiple values. They fill the missing spot in the displayed table 40
  • 41. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is an Observable ? • Observable is just a function that takes an observer and returns a function • Shape: – A function – That accepts an observer: An object with `next`, `error` and `complete` methods on it – And returns a cancellation function • Purpose: – To connect the observer to something that produces values (a producer) and return a means to tear down that connection to the producer. The observer is really a registry of handlers that can be pushed values over time 41
  • 42. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights What is an Observer? • Is a collection of callbacks that knows how to listen to values delivered by the Observable. An object with `next`, `error` and `complete` methods on it • When we talk about RxJS or Reactive programming, generally observables get top billing. But the observer implementation is the workhorse of this type of reactive programming. Observables are inert. They’re just functions. They sit there until you `subscribe` to them, they set up your observer, and they’re done, back to being boring old functions waiting to be called. The observers on the other hand, stay active and listen for events from your producers
  • 43. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Demo 1 : Let's create an Observable 43
  • 44. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights From Promiseland
  • 45. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights To Observableland
  • 46. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Promises vs Observables 46 • Single emission of value • Eager Promises • Multi emission of values • Lazy Observables
  • 47. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Operators • Also Just Functions • An operator in RxJS is little more than a function that takes a source observable, and returns a new observable that will subscribe to that source observable when you subscribe to it • Some common operators : – Map, filter, reduce – scan – combineLatest – WithLatestFrom – etc
  • 48. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Example – The map operator
  • 49. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Hot vs Cold Observable • Unicast vs Multicast 49
  • 50. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Cold Observable • COLD is when your observable creates the producer • An observable is “cold” if its underlying producer is created and activated during subscription. This means, that if observables are functions, then the producer is created and activated by calling that function
  • 51. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Cold Observable • Example :
  • 52. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Hot Observable • HOT is when your observable closes over the producer • An observable is “hot” if its underlying producer is either created or activated outside of subscription
  • 53. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Hot Observable • Example :
  • 54. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects An RxJS Subject is a special type of Observable that allows values to be multi- casted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast 54
  • 55. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects But how does it multicast? 55
  • 56. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects • Every Subject is an Observable. Given a Subject, you can subscribe to it, providing an Observer, which will start receiving values normally. From the perspective of the Observer, it cannot tell whether the Observable execution is coming from a plain unicast Observable or a Subject • Every Subject is an Observer. It is an object with the methods next(v), error(e), and complete(). To feed a new value to the Subject, just call next(theValue), and it will be multi-casted to the Observers registered to listen to the Subject • Internally to the Subject, subscribe does not invoke a new execution that delivers values. It simply registers the given Observer in a list of Observers, similarly to how addListener usually works in other libraries and languages 56
  • 57. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subjects - remember the observer design pattern? 57
  • 58. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Cold to Hot using Subjects • How to convert Cold Observables to Hot using Subjects?
  • 59. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Types of Subject • Async Subject • Behavior Subject • Replay Subject 59
  • 60. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Subject vs Observable • The most important distinction between a Subject and an Observable is that a Subject has state, it keeps a list of observers. On the other hand, an Observable is just a function that sets up observation • While Subjects are Observables, Subjects also implement an Observer interface. They have next, error, and complete methods on them. These methods are used to notify their counterparts on observers in the subject’s internal observers list. This means a subject can be used as an observer to subscribe to any observable. 60
  • 61. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Demo 6 : A Reactive example 61
  • 62. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights
  • 63. Copyright © 2018 Talentica Software (I) Pvt Ltd. All rightsCopyright © 2018 Talentica Software (I) Pvt Ltd. All rights Thank you …

Editor's Notes

  1. Function composition is the process of combining two or more functions to build a new bigger complicated function. Recommended to use composition over inheritance. To avoid complex hierarchies. const toSlug = compose( encodeURIComponent, join('-'), map(toLowerCase), split(' '));
  2. Show subject.js
  3. Show subject.js
  4. Show subject.js
  5. Show subject.js