Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit a7b6049

Browse files
committed
docs(example:file-feed): Refactor and cleanup
Use callbacks instead of Promises. Refactor DataStore. Move fonts import to CSS. Add "postinstall" step to package.json to copy the webpack config file to its correct directory. Remove old files that were added back to due to a messed up rebase.
1 parent c37258d commit a7b6049

File tree

8 files changed

+81
-54
lines changed

8 files changed

+81
-54
lines changed

examples/file-feed/complete/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"react-router": "^3.0.1"
1818
},
1919
"scripts": {
20+
"postinstall": "cp config/webpack.config.dev.js node_modules/react-scripts/config/webpack.config.dev.js",
2021
"start": "react-scripts start",
2122
"build": "react-scripts build",
2223
"test": "react-scripts test --env=jsdom",

examples/file-feed/complete/public/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<link href="https://fonts.googleapis.com/css?family=Anonymous+Pro|Source+Code+Pro" rel="stylesheet">
76
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
87
<title>File Feed</title>
98
</head>

examples/file-feed/complete/src/browser-app/App.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ class App extends Component {
2828
this.setStatus('Starting IPFS')
2929

3030
// Initialize our DataStore, ie. start IPFS
31-
dataStore = DataStore.init({
32-
path: '/ipfs/' + new Date().toString(),
31+
DataStore.init({
32+
path: '/ipfs-file-feed/',
3333
signalAddr: '/dns4/star-signal.cloud.ipfs.team'
34-
})
34+
}, (err, res) => {
35+
if (err) {
36+
console.error(err)
37+
return
38+
}
3539

36-
dataStore.on('error', (e) => console.error(e))
37-
dataStore.on('ready', () => {
40+
dataStore = res
3841
this.setStatus('IPFS Started', 5000)
3942

4043
const feedName = this.props.params.hash

examples/file-feed/complete/src/browser-app/DataStore.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,50 @@ let instance
1010
class DataStore extends EventEmitter {
1111
constructor (options) {
1212
super()
13+
this.ipfs = null
14+
}
15+
16+
init (options, callback) {
1317
spawnNode(options, (err, node) => {
1418
if (err) {
15-
console.log(err)
19+
callback(err)
20+
} else {
21+
this.ipfs = node
22+
callback(null, this)
1623
}
17-
this.ipfs = node
18-
this.emit('ready')
1924
})
2025
}
2126

2227
// Open an orbit-db database and hook up to the emitted events
2328
openFeed (name) {
2429
this.orbitdb = new OrbitDB(this.ipfs)
25-
this.feed = this.orbitdb.feed(name, { cachePath: '/ipfd/ipfd2.db' })
30+
this.feed = this.orbitdb.feed(name, { cachePath: '/file-feed.db' })
2631
this.feed.events.on('ready', () => this.emit('feed'))
2732
this.feed.events.on('history', () => this.emit('update'))
2833
this.feed.events.on('data', () => this.emit('update'))
2934
if (this.timer) clearInterval(this.timer)
3035
this.timer = setInterval(() => this._updatePeers(name), 1000)
3136
}
3237

33-
getFile (hash) {
34-
return new Promise((resolve, reject) => {
35-
// Cat the contents of a multihash
36-
this.ipfs.files.cat(hash)
37-
.then((stream) => {
38-
// Buffer all contents of the file as text
39-
// and once buffered, create a blob for the picture
40-
let buffer = []
41-
let bytes = 0
42-
stream.on('error', (e) => console.error(e))
43-
stream.on('data', (d) => {
44-
bytes += d.length
45-
buffer.push(d)
46-
this.emit('load', hash, bytes)
47-
})
48-
stream.on('end', () => {
49-
this.emit('load', hash, bytes)
50-
resolve(buffer)
51-
})
38+
getFile (hash, callback) {
39+
// Cat the contents of a multihash
40+
this.ipfs.files.cat(hash)
41+
.then((stream) => {
42+
// Buffer all contents of the file
43+
let buffer = []
44+
let bytes = 0
45+
stream.on('error', callback)
46+
stream.on('data', (d) => {
47+
bytes += d.length
48+
buffer.push(d)
49+
this.emit('load', hash, bytes)
5250
})
53-
.catch(reject)
54-
})
51+
stream.on('end', () => {
52+
this.emit('load', hash, bytes)
53+
callback(null, buffer)
54+
})
55+
})
56+
.catch(callback)
5557
}
5658

5759
addFiles (file) {
@@ -70,13 +72,14 @@ class DataStore extends EventEmitter {
7072
})
7173
}
7274

73-
readFileContents(file)
74-
.then((content) => addToIpfs(file.name, content))
75-
.then((files) => {
76-
files.forEach((e) => addToOrbitDB(e, file.type))
77-
})
78-
.then(() => this.emit('file', file))
79-
.catch((e) => console.error(e))
75+
readFileContents(file, (err, content) => {
76+
addToIpfs(file.name, content)
77+
.then((files) => {
78+
files.forEach((e) => addToOrbitDB(e, file.type))
79+
})
80+
.then(() => this.emit('file', file))
81+
.catch((e) => console.error(e))
82+
})
8083
}
8184

8285
connectToPeer (multiaddr) {
@@ -95,9 +98,13 @@ class DataStore extends EventEmitter {
9598
}
9699

97100
class DataStoreSingleton {
98-
static init (options) {
99-
instance = !instance ? new DataStore(options) : instance
100-
return instance
101+
static init (options, callback) {
102+
if (!instance) {
103+
instance = new DataStore()
104+
instance.init(options, callback)
105+
} else {
106+
callback(null, instance)
107+
}
101108
}
102109
}
103110

examples/file-feed/complete/src/browser-app/Preview.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class Preview extends Component {
1313
progress: 'Loading...'
1414
}
1515
// Get a DataStore instance (singleton across the app)
16-
dataStore = DataStore.init()
16+
DataStore.init(null, (err, res) => {
17+
if (err) { console.error(err) }
18+
dataStore = res
19+
})
1720
this.handleLoadProgress = this.handleLoadProgress.bind(this)
1821
}
1922

@@ -22,16 +25,18 @@ class Preview extends Component {
2225

2326
dataStore.on('load', this.handleLoadProgress)
2427

25-
dataStore.getFile(this.props.hash)
26-
.then((content) => {
28+
dataStore.getFile(this.props.hash, (err, content) => {
29+
if (err) {
30+
console.error(err)
31+
} else {
2732
const data = isMediaFile(this.props.type)
2833
? URL.createObjectURL(new Blob(content))
2934
: content.map((e) => e.toString()).join('')
3035

3136
// Set the content for this preview
3237
this.setState({ data: data })
33-
})
34-
.catch((e) => console.error(e))
38+
}
39+
})
3540
}
3641

3742
componentWillUnmount () {

examples/file-feed/complete/src/browser-app/index.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import url('https://fonts.googleapis.com/css?family=Anonymous+Pro|Source+Code+Pro');
2+
13
body {
24
margin: 0;
35
padding: 0;
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Read contents of a File (TODO: link to MDN) and
22
// return the contents as an ArrayBuffer
3-
export function readFileContents (file) {
4-
return new Promise((resolve) => {
5-
const reader = new FileReader()
6-
reader.onload = (event) => resolve(event.target.result)
7-
reader.readAsArrayBuffer(file)
8-
})
3+
export function readFileContents (file, callback) {
4+
const reader = new FileReader()
5+
reader.onload = (event) => callback(null, event.target.result)
6+
reader.readAsArrayBuffer(file)
97
}

examples/file-feed/complete/src/util/spawn-node.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@ const multiaddr = require('multiaddr')
88
const series = require('async/series')
99

1010
function spawnNode (options, callback) {
11-
options.path = options.path || '/ipfd/tmp/' + Math.random()
11+
options.path = options.path || '/ipfs/'
12+
options.initRepo = options.initRepo || true
1213

1314
const node = new IPFS(options.path)
1415

1516
series([
16-
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
17+
(cb) => {
18+
node.init({ emptyRepo: true, bits: 2048 }, (err) => {
19+
if (err) {
20+
const errStr = String(err).match(/repo already exists/)
21+
if (errStr.length > 0) {
22+
cb(null)
23+
}
24+
} else {
25+
cb(null)
26+
}
27+
})
28+
},
1729
(cb) => {
1830
node.config.get((err, config) => {
1931
if (err) { return cb(err) }

0 commit comments

Comments
 (0)