Skip to content

Commit

Permalink
Implemented Observable unsubscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kentico-ericd committed Dec 5, 2018
1 parent fd1d849 commit fa5a67e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 34 deletions.
20 changes: 9 additions & 11 deletions repositories/brewer-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ class BrewerRepository extends RepositoryBase {
return super.createDummyObservable();
}

const obs2 = defer(function(){
return deliveryClient.items()
.type('brewer')
.getObservable();
});
const obs1 = defer(function(){
return deliveryClient.taxonomy('manufacturer')
.getObservable();
});
const final = Observable.create(observer => {
const obs2 = defer(function(){
return deliveryClient.items()
.type('brewer')
.getObservable();
});
const obs1 = defer(function(){
return deliveryClient.taxonomy('manufacturer')
.getObservable();
});

//Get manufacturers
obs1.subscribe(res1 => {
this.manufacturers = res1.taxonomy.terms;
Expand All @@ -39,7 +38,6 @@ class BrewerRepository extends RepositoryBase {
});

return final;

}

containsManufacturers(keys) {
Expand Down
9 changes: 7 additions & 2 deletions routes/about-us.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ const deliveryClient = require('../delivery');
const linkResolver = require('../resolvers/link-resolver');
const express = require('express');
const router = express.Router();
let data;

//eslint-disable-next-line
router.get('/about-us', function(req, res, next){
//Get about-us
deliveryClient.item('about_us')
data = deliveryClient.item('about_us')
.queryConfig({
linkResolver: (link) => linkResolver.resolveContentLink(link)
})
.getObservable()
.subscribe(response => {
res.render('about-us', { 'content_item': response.item });
res.render('about-us', { 'content_item': response.item }, (err, html) => {
if(data) data.unsubscribe();
res.send(html);
res.end();
});
});
});

Expand Down
16 changes: 12 additions & 4 deletions routes/articles.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
const express = require('express');
const router = express.Router();
let articleRepo;
let articleRepo, data;

//eslint-disable-next-line
const ensureArticles = function(req, res, next) {
articleRepo = app.getRepository("ArticleRepository");//eslint-disable-line
articleRepo.ensureItems().subscribe(() => {
data = articleRepo.ensureItems().subscribe(() => {
next();
});
}

//eslint-disable-next-line
const renderListing = function(req, res, next) {
res.render('articles', {'articleList': articleRepo.getAllArticles()});
res.render('articles', {'articleList': articleRepo.getAllArticles()}, (err, html) =>{
data.unsubscribe();
res.send(html);
res.end();
});
}

//eslint-disable-next-line
const renderSingle = function(req, res, next) {
res.render('articles', {'articleList': articleRepo.getArticle(req.params.id)});
res.render('articles', {'articleList': articleRepo.getArticle(req.params.id)}, (err, html) =>{
if(data) data.unsubscribe();
res.send(html);
res.end();
});
}

router.get('/articles', [ensureArticles, renderListing]);
Expand Down
10 changes: 7 additions & 3 deletions routes/brewer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const express = require('express');
const router = express.Router();
let brewerRepo;
let brewerRepo, data;

//eslint-disable-next-line
const ensureBrewers = function(req, res, next) {
brewerRepo = app.getRepository("BrewerRepository");//eslint-disable-line
brewerRepo.ensureItems().subscribe(() => {
data = brewerRepo.ensureItems().subscribe(() => {
next();
});
}

//eslint-disable-next-line
const render = function(req, res, next) {
res.render('brewer', { 'brewer': brewerRepo.getBrewer(req.params.codename)});
res.render('brewer', { 'brewer': brewerRepo.getBrewer(req.params.codename)}, (err, html) => {
if(data) data.unsubscribe();
res.send(html);
res.end();
});
}

router.get('/brewer/:codename', [ensureBrewers, render]);
Expand Down
8 changes: 6 additions & 2 deletions routes/cafes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const express = require('express');
const router = express.Router();
let cafeRepo;
let cafeRepo, data;

//eslint-disable-next-line
const ensureCafes = function(req, res, next) {
cafeRepo = app.getRepository("CafeRepository");//eslint-disable-line
cafeRepo.ensureItems().subscribe(() => {
data = cafeRepo.ensureItems().subscribe(() => {
next();
});
}
Expand All @@ -15,6 +15,10 @@ const render = function(req, res, next){
res.render('cafes', {
'partnerCafes': cafeRepo.getCafesNotInCountry('USA'),
'americanCafes': cafeRepo.getCafesInCountry('USA')
}, (err, html) => {
if(data) data.unsubscribe();
res.send(html);
res.end();
});
}

Expand Down
10 changes: 7 additions & 3 deletions routes/coffee.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
const express = require('express');
const router = express.Router();
let coffeeRepo;
let coffeeRepo, data;

//eslint-disable-next-line
const ensureCoffees = function(req, res, next) {
coffeeRepo = app.getRepository("CoffeeRepository");//eslint-disable-line
coffeeRepo.ensureItems().subscribe(() => {
data = coffeeRepo.ensureItems().subscribe(() => {
next();
});
}

//eslint-disable-next-line
const render = function(req, res, next) {
res.render('coffee', { 'coffee': coffeeRepo.getCoffee(req.params.codename)});
res.render('coffee', { 'coffee': coffeeRepo.getCoffee(req.params.codename)}, (err, html) => {
if(data) data.unsubscribe();
res.send(html);
res.end();
});
}

router.get('/coffee/:codename', [ensureCoffees, render]);
Expand Down
8 changes: 6 additions & 2 deletions routes/contacts.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const express = require('express');
const router = express.Router();
let cafeRepo;
let cafeRepo, data;

//eslint-disable-next-line
const ensureCafes = function(req, res, next) {
cafeRepo = app.getRepository("CafeRepository");//eslint-disable-line
cafeRepo.ensureItems().subscribe(() => {
data = cafeRepo.ensureItems().subscribe(() => {
next();
});
}
Expand All @@ -14,6 +14,10 @@ const ensureCafes = function(req, res, next) {
const render = function(req, res, next){
res.render('contacts', {
'americanCafes': cafeRepo.getCafesInCountry('USA')
}, (err, html) => {
if(data) data.unsubscribe();
res.send(html);
res.end();
});
}

Expand Down
12 changes: 8 additions & 4 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
const express = require('express');
const router = express.Router();
let articleRepo;
let cafeRepo;
let articleRepo, cafeRepo, articleData, cafeData;

//eslint-disable-next-line
const ensureCafes = function(req, res, next) {
cafeRepo = app.getRepository("CafeRepository");//eslint-disable-line
cafeRepo.ensureItems().subscribe(() => {
cafeData = cafeRepo.ensureItems().subscribe(() => {
next();
});
}

//eslint-disable-next-line
const ensureArticles = function(req, res, next) {
articleRepo = app.getRepository("ArticleRepository");//eslint-disable-line
articleRepo.ensureItems().subscribe(() => {
articleData = articleRepo.ensureItems().subscribe(() => {
next();
});
}
Expand All @@ -24,6 +23,11 @@ const render = function(req, res) {
res.render('index', {
'articleList': articleRepo.getAllArticles(),
'cafeList': cafeRepo.getCafesInCountry('USA')
}, (err, html) => {
if(cafeData) cafeData.unsubscribe();
if(articleData) articleData.unsubscribe();
res.send(html);
res.end();
});
}

Expand Down
13 changes: 10 additions & 3 deletions routes/store.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const express = require('express');
const router = express.Router();
let coffeeRepo, brewerRepo, storeRepo;
let coffeeData, brewerData, storeData;

//eslint-disable-next-line
const ensureCoffees = function(req, res, next) {
coffeeRepo = app.getRepository("CoffeeRepository");//eslint-disable-line
coffeeRepo.ensureItems().subscribe(() => {
coffeeData = coffeeRepo.ensureItems().subscribe(() => {
next();
});
}
Expand All @@ -14,7 +15,7 @@ const ensureCoffees = function(req, res, next) {
const ensureBrewers = function(req, res, next) {
if(req.params.type == "brewers") {
brewerRepo = app.getRepository("BrewerRepository");//eslint-disable-line
brewerRepo.ensureItems().subscribe(() => {
brewerData = brewerRepo.ensureItems().subscribe(() => {
next();
});
}
Expand All @@ -24,7 +25,7 @@ const ensureBrewers = function(req, res, next) {
//eslint-disable-next-line
const ensureStore = function(req, res, next) {
storeRepo = app.getRepository("StoreRepository");//eslint-disable-line
storeRepo.ensureItems().subscribe(() => {
storeData = storeRepo.ensureItems().subscribe(() => {
next();
});
}
Expand All @@ -45,6 +46,12 @@ const render = function(req, res, next) {
//Brewer items
'brewers': (type == "brewers") ? brewerRepo.getAllBrewers(req.query) : [],
'manufacturers': (type == "brewers") ? brewerRepo.getAllManufacturers() : [],
}, (err, html) => {
if(storeData) storeData.unsubscribe();
if(brewerData) brewerData.unsubscribe();
if(coffeeData) coffeeData.unsubscribe();
res.send(html);
res.end();
});
}

Expand Down

0 comments on commit fa5a67e

Please sign in to comment.