Last active
September 7, 2018 03:46
-
-
Save shunkino/3c9ff24feb2816326c335287c6f71c27 to your computer and use it in GitHub Desktop.
extracted from the interface-ipfs-core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// var IPFS = require('ipfs-api') | |
var IPFS = require('../../src/') | |
var ipfs = IPFS() | |
function store () { | |
var toStore = document.getElementById('source').value | |
ipfs.files.add(Buffer.from(toStore), function (err, res) { | |
if (err || !res) { | |
return console.error('ipfs add error', err, res) | |
} | |
res.forEach(function (file) { | |
if (file && file.hash) { | |
console.log('successfully stored', file.hash) | |
display(file.hash) | |
} | |
}) | |
}) | |
} | |
function display (hash) { | |
// buffer: true results in the returned result being a buffer rather than a stream | |
ipfs.files.cat(hash, function (err, res) { | |
if (err || !res) { | |
return console.error('ipfs cat error', err, res) | |
} | |
document.getElementById('hash').innerText = hash | |
document.getElementById('content').innerText = res.toString() | |
}) | |
} | |
function pubsub_list () { | |
ipfs.pubsub.ls((err, response) => {console.log(err); console.log(response)}) | |
} | |
function pubsub_sub () { | |
console.log('in pubsub_sub') | |
ipfs.pubsub.subscribe('shunkintestpubsub', messageHandler, (message) => { | |
console.log('subscribed!') | |
}) | |
} | |
function pubsub_unsub () { | |
console.log('in pubsub_unsub') | |
ipfs.pubsub.unsubscribe('shunkintestpubsub', messageHandler, (err) => { | |
if (err) { | |
return console.error(`failed to unsubscribe from topic`, err) | |
} | |
console.log(`unsubscribed from topic`) | |
}) | |
} | |
function messageHandler (message) { | |
console.log('got message from ' + message.from) | |
// data is a buffer. Here we're converting it into a string | |
const data = message.data.toString() | |
console.log('containing data: ' + data) | |
} | |
function makeCheck (n, done) { | |
let i = 0 | |
return (err) => { | |
if (err) { | |
// return done(err) | |
console.log(err + "HAPPEND") | |
} | |
if (++i === n) { | |
// done() | |
console.log("Second call : success") | |
} else { | |
console.log("First call") | |
} | |
} | |
} | |
function test_function () { | |
// const sampleCheck = makeCheck(2, done) | |
const sampleCheck = makeCheck(2) | |
let topic = "shunkintesttest" | |
const handler = (msg) => { | |
console.log("handler is here") | |
ipfs.pubsub.unsubscribe(topic, handler, (err) => { | |
ipfs.pubsub.ls((err, topics) => { | |
console.log(topics) | |
sampleCheck() | |
}) | |
}) | |
} | |
ipfs.pubsub.subscribe(topic, handler, (err) => { | |
ipfs.pubsub.publish(topic, Buffer.from('hi'), sampleCheck) | |
}) | |
} | |
document.addEventListener('DOMContentLoaded', function () { | |
document.getElementById('store').onclick = store | |
document.getElementById('list').onclick = pubsub_list | |
document.getElementById('sub').onclick = pubsub_sub | |
document.getElementById('unsub').onclick = pubsub_unsub | |
document.getElementById('test').onclick = test_function | |
}) | |
ipfs.pubsub.subscribe('shunkintestpubsub', messageHandler) | |
const data = Buffer.from('some message content here') | |
ipfs.pubsub.publish('shunkintestpubsub', data, (err) => { | |
if (err) { | |
console.error('error publishing: ', err) | |
} else { | |
console.log('successfully published message') | |
} | |
}) | |
ipfs.pubsub.ls((err, response) => {console.log(err); console.log(response)}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment