Timeline stability improvements

This commit is contained in:
tmshv committed 2016-03-26 15:11:12 +03:00
1 parent beebd497b3
commit f2d1f71064
2 files changed
+42 -16

No files matched your search

+15 -10
View File
@@ -1,6 +1,6 @@
import template from '../../templates/components/timeline.html'; import template from '../../templates/components/timeline.html';
import {createInstance} from '../../models/TimelinePostInstagram'; import {createInstance as createInstagram} from '../../models/TimelinePostInstagram';
// function personByInstagram(list, instagram){ // function personByInstagram(list, instagram){
// list = list.filter(function (member) { // list = list.filter(function (member) {
@@ -43,11 +43,18 @@ import {createInstance} from '../../models/TimelinePostInstagram';
// }; // };
// } // }
const createPost = post => [createInstance] const createPost = post => post.type === 'post' ? post : null;
.reduce((post, map) => { const createPosts = items => items
let result = map(post); .map(createRecord)
return result ? result : post .filter(i => i);
}, post);
function createRecord(post){
for (let fn of [createInstagram, createPost]){
let result = fn(post);
if(result) return result;
}
return null;
}
export default function (app) { export default function (app) {
app.component('timeline', { app.component('timeline', {
@@ -74,7 +81,7 @@ export default function (app) {
// if (post.type == 'instagram') return toInstagram(post); // if (post.type == 'instagram') return toInstagram(post);
// else return post; // else return post;
// }); // });
feed = feed.map(createPost); feed = createPosts(feed);
skip += limit; skip += limit;
this.feed = this.feed.concat(feed); this.feed = this.feed.concat(feed);
@@ -86,9 +93,7 @@ export default function (app) {
io.on('feed', feed => { io.on('feed', feed => {
console.log(feed); console.log(feed);
feed = createPosts(feed instanceof Array ? feed : [feed]);
feed = feed instanceof Array ? feed : [feed];
feed = feed.map(createPost);
$timeout(()=> { $timeout(()=> {
this.feed = feed.concat(this.feed); this.feed = feed.concat(this.feed);
+27 -6
View File
@@ -28,13 +28,18 @@ export function fromInstagram1(post) {
} }
export function fromInstagram2(post) { export function fromInstagram2(post) {
const caption = safe(post => post.data.caption.text, '');
const username = safe(post => post.data.user['username'], '');
const userpic = safe(post => post.data.user['profile_picture'], '');
const image = safe(post => post.data.images['standard_resolution'].url, '');
return new TimelinePostInstagram( return new TimelinePostInstagram(
post.date, post.date,
post.data.images.standard_resolution.url, image(post),
post.data.link, post.data.link,
post.data.user.username, username(post),
post.data.user.profile_picture, userpic(post),
post.data.caption.text caption(post)
); );
} }
@@ -46,7 +51,23 @@ export const types = {
export function createInstance(post) { export function createInstance(post) {
if(post.type in types) { if(post.type in types) {
let fn = types[post.type]; let fn = types[post.type];
return fn(post);
try{
return fn(post);
}catch(e){
return null;
}
} }
return null; return null;
} }
function safe(fn, defaultValue=null){
return function(){
try{
let i = fn.apply(undefined, arguments);
return i ? i : defaultValue;
}catch(e){
return defaultValue
}
}
}