Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Limpando Seu Código JS Com O Padrão Pub/Sub
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Filipe Costa
December 13, 2014
Programming
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Limpando Seu Código JS Com O Padrão Pub/Sub
CEJS 2014
Code sample:
https://github.com/filipebarcos/cejs_pubsub
Filipe Costa
December 13, 2014
More Decks by Filipe Costa
See All by Filipe Costa
Lidando com Efeitos Colaterais com Redux Saga
filipebarcos
0
230
Pitch - Lidando com Efeitos Colaterais com Redux Saga
filipebarcos
0
420
Rust for Rubysts
filipebarcos
2
260
Tu trabalha em casa?? Que moleza hein!
filipebarcos
0
140
Rediscovering OOP in Rails World
filipebarcos
0
110
jQuery Bad Practices
filipebarcos
2
280
Intro to Ruby
filipebarcos
1
140
Other Decks in Programming
See All in Programming
sbt 2.0: takeoff
eed3si9n
0
180
[2026-10-09]一枚の布で作らなくてもいい。 信頼性は「パッチワーク」で紡がれる~既存の運用と新しい可能性をつなぎ合わせてきたCREの話~
tosite
0
120
AmebaブログはKMPから卒業します
kmars0605
0
310
Next.js: The final boss of Web Performance
guaca
0
370
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
4
1.4k
続・コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions-again
handlename
0
150
AI-Powered Domain-Driven Design
ziobrando
0
360
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
120
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
180
Event is what happens
moro
2
2.2k
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
310
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
7.1k
Featured
See All Featured
Side Projects
sachag
456
43k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
290
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
950
Context Engineering - Making Every Token Count
addyosmani
9
1.3k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Ethics towards AI in product and experience design
skipperchong
2
450
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
640
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
520
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
450
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
600
Transcript
PUB/SUB -> CLEAN JS “Limpando Seu Código JS Com O
Padrão Pub/Sub"
FILIPE COSTA @filipebarcos
Stack Builders Estamos contratando!
DISCLAIMER
CLEAN CODE
TÁ NA MODA! Angular Ember Backbone React MVVM MVC MV******
CONTEXTO
• Comprar Passeio Turístico no Site • Página Única •
Descrição do produto • Horários (seletor de dia/hora) • Mapa • Seletor de quantidade • Resumo da Compra • Formulário de Cliente e Cartão • Total da Compra
None
MÓDULOS
Checkout.OrderSummary = function() { return { addItem: function(item) { //
Add item to summary // Update Order Total }; }; }; Checkout.CustomerForm = function() {};
O PROBLEMA
DEPENDÊNCIA
Checkout.OrderSummary = function() { var addItemToSummary = function(item) { var
orderSummary = document.getElementById('order-summary'); var rowSnippet = buildRow(item); orderSummary.appendChild(rowSnippet); }; var updateOrderTotal = function(item) { var orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + item.quantity * item.cost; }; return { addItem: function(item) { addItemToSummary(item); updateOrderTotal(item); }; }; };
Checkout.CustomerForm = function() { var fetchDeliveryCost = function(postalCode) { //fetch
from Post Office API }; var updateOrderTotal = function(cost) { var orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + cost; }; return { updateDeliveryCost: function(postalCode) { var cost = fetchDeliveryCost(postalCode); document.getElementById('delivery-cost').innerHTML = cost; updateOrderTotal(cost); }; }; };
SOLUÇÕES SOLUÇÃO
PUB/SUB
PUBlish e SUBscribe é um padrão de mensageria, onde os
“Publishers” enviam mensagens e os “Subscribers” escutam essas mensagens
None
PUBSUB.JS
None
PubSub.publish('my-channel', 'value'); PubSub.subscribe('my-channel', function(message, data) { console.log('this is what I
got'); console.log(data); });
DE VOLTA AO PROBLEMA
Checkout.CustomerForm = function() { var updateOrderTotal = function(cost) { var
orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + cost; }; }; Checkout.OrderSummary = function() { var updateOrderTotal = function(item) { var orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + item.quantity * item.cost; }; };
Checkout.OrderSummary = function() { var addItemToSummary = function(item) { var
orderSummary = document.getElementById('order-summary'); var rowSnippet = buildRow(item); orderSummary.appendChild(rowSnippet); }; return { addItem: function(item) { addItemToSummary(item); PubSub.publish('checkout.item', item); }; }; };
Checkout.CustomerForm = function() { var fetchDeliveryCost = function(postalCode) { //fetch
from Post Office API }; return { updateDeliveryCost: function(postalCode) { var cost = fetchDeliveryCost(postalCode); document.getElementById('delivery-cost').innerHTML = cost; PubSub.publish('checkout.delivery-cost', cost); }; }; };
Checkout.OrderTotal = function() { var updateOrderTotal = function(cost) { var
orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + cost; }; PubSub.subscribe('checkout.item', function(msg, item) { updateOrderTotal(item.quantity * item.cost); }); PubSub.subscribe('checkout.delivery-cost', function(msg, deliveryCost) { updateOrderTotal(deliveryCost); }); };
\O/
CONCLUSÃO
OBRIGADO https://github.com/filipebarcos/cejs_pubsub