Skip to content

Commit

Permalink
fix(feed): exchage feeds with same size but more new
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jul 20, 2018
1 parent 167e03c commit 0223ad9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
52 changes: 32 additions & 20 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,10 @@ module.exports = async ({
enumerable: true,
get: () => feed.size()
});
Object.defineProperty(p2p.info, 'feedDate', {
enumerable: true,
get: () => feed.feedDate
});
p2pStore.on('store', async ({data: record, temp, myself}) => {
if(record.type !== 'vote')
return
Expand Down Expand Up @@ -936,30 +940,38 @@ module.exports = async ({
recive('feed', mergeTorrentsWithDownloadsFn(feedCall, true)); // don't overwrite feed value

p2p.on('feed', (nil, callback) => {
feedCall((data) => callback(data))
callback({
feed: feed.feed,
feedDate: feed.feedDate
})
})

// call once to get bigest feed
let feedLock = false
p2p.events.on('peer', () => {
if(feedLock)
return
feedLock = true
setTimeout(() => {
p2p.emit('feed', null, (remoteFeed) => {
if(!remoteFeed)
return

if(remoteFeed.length <= feed.size())
return

console.log('replace our feed with remote feed')
feed.feed = remoteFeed
send('feedUpdate', {
feed: feed.feed
p2p.events.on('peer', (peer) => {
if(peer.info && peer.info.feed)
{
if(peer.info.feed > feed.size() // list bigger than our
|| (peer.info.feed == feed.size() && peer.info.feedDate > feed.feedDate)) // or same but more new
{
peer.emit('feed', null, (remoteFeed) => {
if(!remoteFeed)
return

if(Array.isArray(remoteFeed) || !remoteFeed.feed)
return // old version call

if(remoteFeed.feed.length > feed.size() || (remoteFeed.feed.length == feed.size() && remoteFeed.feedDate > feed.feedDate))
{
console.log('replace our feed with remote feed')
feed.feed = remoteFeed.feed
feed.feedDate = remoteFeed.feedDate || 0
send('feedUpdate', {
feed: feed.feed
});
}
});
});
}, 1000)
}
}
})

}
4 changes: 3 additions & 1 deletion src/background/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ let config = {

client: {
downloadPath: os.homedir() + '/Downloads'
}
},

feedDate: 0,
}

const fs = require('fs');
Expand Down
6 changes: 6 additions & 0 deletions src/background/feed.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const config = require('./config');

module.exports = class Feed {
constructor({sphinx})
{
this.feed = []
this.sphinx = sphinx
this.loaded = false
this.max = 1000
this.feedDate = 0
}

size()
Expand All @@ -17,6 +20,7 @@ module.exports = class Feed {
return // feed not loaded on begining, ignore saving

console.log('saving feed')
config.feedDate = this.feedDate
await this.sphinx.query('delete from feed where id > 0')
let id = 0
return Promise.all(
Expand All @@ -34,6 +38,7 @@ module.exports = class Feed {
this.feed = []

this._order()
this.feedDate = config.feedDate
this.loaded = true
console.log('lodead feed')
}
Expand Down Expand Up @@ -79,6 +84,7 @@ module.exports = class Feed {
}

this._order()
this.feedDate = Math.floor(Date.now() / 1000)
}

_order() {
Expand Down

0 comments on commit 0223ad9

Please sign in to comment.