Skip to content

Commit

Permalink
Find provs bundle
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
  • Loading branch information
hacdias committed Jul 19, 2018
1 parent 87cc9b1 commit 8a55c50
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 19 deletions.
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions src/bundles/files-provs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { createSelector } from 'redux-bundler'

export default function (opts) {
opts = opts || {}
opts.concurrency = opts.concurrency || 5

const defaultState = {
provs: {},
queue: [],
resolving: []
}

return {
name: 'filesProvs',

reducer (state = defaultState, action) {
if (action.type === 'FILES_PROVS_QUEUED') {
const hashes = action.payload
const provs = {}

hashes.forEach(hash => {
provs[hash] = {
state: 'queued'
}
})

return {
...state,
queue: state.queue.concat(hashes),
provs: {
...state.provs,
...provs
}
}
}

if (action.type === 'FILES_PROVS_RESOLVE_STARTED') {
const { hash } = action.payload

return {
...state,
queue: state.queue.filter(h => hash !== h),
resolving: state.resolving.concat(hash),
provs: {
...state.provs,
[hash]: {
state: 'resolving'
}
}
}
}

if (action.type === 'FILES_PROVS_RESOLVE_FINISHED') {
const { hash, count } = action.payload

return {
...state,
resolving: state.resolving.filter(h => h !== hash),
provs: {
...state.provs,
[hash]: {
state: 'resolved',
count: count
}
}
}
}

if (action.type === 'FILES_PROVS_RESOLVE_FAILED') {
const { hash, error } = action.payload

return {
...state,
resolving: state.resolving.filter(h => h !== hash),
provs: {
...state.provs,
[hash]: {
state: 'failed',
error: error
}
}
}
}

return state
},

selectFilesProvs: state => state.filesProvs.provs,
selectFilesProvsQueuing: state => state.filesProvs.queue,
selectFilesProvsResolving: state => state.filesProvs.resolving,

doFindProvs: hash => async ({ dispatch, getIpfs }) => {
dispatch({ type: 'FILES_PROVS_RESOLVE_STARTED', payload: { hash } })

const ipfs = getIpfs()
let count

try {
const res = await ipfs.dht.findprovs(hash, { timeout: '30s', 'num-providers': 5 })
count = res.filter(t => t.Type === 4).length
} catch (err) {
return dispatch({
type: 'FILES_PROVS_RESOLVE_FAILED',
payload: { hash, error: err }
})
}

dispatch({
type: 'FILES_PROVS_RESOLVE_FINISHED',
payload: { hash, count }
})
},

reactFindProvs: createSelector(
'selectIpfsReady',
'selectFilesProvsQueuing',
'selectFilesProvsResolving',
(ipfsReady, queuing, resolving) => {
if (ipfsReady && queuing.length && resolving.length < opts.concurrency) {
return {
actionCreator: 'doFindProvs',
args: [ queuing[0] ]
}
}
}
),

// When the peers list changes, queue up the peers we don't already know about
reactQueuePeerLocations: createSelector(
'selectFiles',
'selectFilesProvs',
(files, filesProvs) => {
if (!files || files.type !== 'directory') {
return
}

const payload = files.files.reduce((acc, { hash }) => {
if (!filesProvs[hash]) {
return [...acc, hash]
} else {
return acc
}
}, [])

return { type: 'FILES_PROVS_QUEUED', payload }
}
)
}
}
10 changes: 0 additions & 10 deletions src/bundles/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ const bundle = createAsyncResourceBundle({

res.forEach(file => {
file.type = file.type === 0 ? 'file' : 'directory'
file.peers = 0

if (provs[file.hash] >= 0) {
file.peers = provs[file.hash]
} else if (provs[file.hash] !== -1) {
provs[file.hash] = -1
getIpfs().dht.findprovs(file.hash, { timeout: '5s', 'num-providers': 5 })
.then((peers) => { provs[file.hash] = peers.filter(t => t.Type === 4).length })
.catch(console.log)
}
})

return {
Expand Down
2 changes: 2 additions & 0 deletions src/bundles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import peerBandwidthBundle from './peer-bandwidth'
import routesBundle from './routes'
import redirectsBundle from './redirects'
import filesBundle from './files'
import filesProvsBundle from './files-provs'
import configBundle from './config'
import configSaveBundle from './config-save'

Expand All @@ -24,6 +25,7 @@ export default composeBundles(
routesBundle,
redirectsBundle,
filesBundle,
filesProvsBundle(),
configBundle,
configSaveBundle
)
18 changes: 16 additions & 2 deletions src/files/FilesPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const action = (name) => {

class FilesPage extends React.Component {
static propTypes = {
files: PropTypes.object
files: PropTypes.object,
filesProvs: PropTypes.object
}

state = {
Expand Down Expand Up @@ -203,7 +204,19 @@ class FilesPage extends React.Component {
}

render () {
const {files} = this.props
const {files, filesProvs} = this.props

if (files && files.files) {
files.files = files.files.map(file => {
if (filesProvs[file.hash] && filesProvs[file.hash].count) {
file.peers = filesProvs[file.hash].count
} else {
file.peers = 0
}

return file
})
}

return (
<div data-id='FilesPage'>
Expand Down Expand Up @@ -275,6 +288,7 @@ export default connect(
'doFilesDownloadLink',
'doFilesMakeDir',
'selectFiles',
'selectFilesProvs',
'selectGatewayUrl',
FilesPage
)

0 comments on commit 8a55c50

Please sign in to comment.