-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path1-ipfs.js
157 lines (127 loc) · 3.7 KB
/
1-ipfs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
// ------ Y.js: import and wire dependencies --------
// const Y = require('yjs')
// require('y-array')(Y)
// require('y-memory')(Y)
// require('y-indexeddb')(Y)
// require('y-ipfs-connector')(Y)
const d3 = require('d3')
// ------ IPFS node creation ------
const ipfs = new Ipfs({
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
]
}
}
})
// ------ Wait for IPFS to start ------
ipfs.once('start', ipfsStarted)
async function ipfsStarted () {
console.log('IPFS started')
// ------ Y.js: Initialize CRDT ------
// const y = await Y({
// db: {
// name: 'indexeddb'
// },
// connector: {
// name: 'ipfs',
// room: 'p2p-flipchart-demo-room',
// ipfs: ipfs
// },
// share: {
// flipchart: 'Array'
// }
// })
// var drawing = y.share.flipchart
// ------ IPFS: print Peer Id ------
ipfs.id(haveIPFSId)
function haveIPFSId (err, peerId) {
if (err) { throw err }
document.getElementById('status').innerHTML = 'Started. Peer Id is ' + peerId.id
}
// ------ D3: translate line point into D3 render path
var renderPath = d3.line()
.x(function (d) { return d[0] })
.y(function (d) { return d[1] })
.curve(d3.curveNatural)
// ------ D3 Flipchart drawing initialization ------
var svg = d3.select('#flipchart')
// ------ CRDT and D3: Draw a new line ------
// function drawLine (yarray) {
// var line = svg.append('path')
// .datum(yarray.toArray())
// .attr('class', 'line')
// line.attr('d', renderPath)
// // Observe changes that happen on this line
// yarray.observe(lineChanged)
// function lineChanged(event) {
// // we only implement insert events that are appended to the end of the array
// event.values.forEach(function (value) {
// line.datum().push(value)
// })
// line.attr('d', renderPath)
// }
// }
// ------ CRDT: listen for new and removed lines ------
// drawing.observe(drawingChanged)
// function drawingChanged (event) {
// if (event.type === 'insert') {
// event.values.forEach(drawLine)
// } else {
// // just remove all elements (thats what we do anyway)
// svg.selectAll('path').remove()
// }
// }
// ------ CRDT: draw all existing content ------
// for (var i = 0; i < drawing.length; i++) {
// drawLine(drawing.get(i))
// }
// ------ User interaction: handle drag events ------
svg.call(d3.drag()
.on('start', dragStarted)
.on('drag', dragged)
.on('end', dragEnded))
var sharedLine = null
function dragStarted () {
// --- With CRDT:
// drawing.insert(drawing.length, [Y.Array])
// sharedLine = drawing.get(drawing.length - 1)
// --- Without CRDT:
sharedLine = svg.append('path')
.datum([])
.attr('class', 'line')
}
// After one dragged event is recognized, we ignore them for 33ms.
var ignoreDrag = null
function dragged () {
if (sharedLine && !ignoreDrag) {
ignoreDrag = window.setTimeout(function () {
ignoreDrag = null
}, 33)
const mouse = d3.mouse(this)
// --- With CRDT:
// sharedLine.push([mouse])
// --- Without CRDT:
sharedLine.datum().push(mouse)
sharedLine.attr('d', renderPath)
}
}
function dragEnded () {
sharedLine = null
window.clearTimeout(ignoreDrag)
ignoreDrag = null
}
// ------ User interaction: Clear all ------
document.getElementById('clear').onclick = clickedClear
function clickedClear() {
// --- With CRDT:
// drawing.delete(0, drawing.length)
// --- Without CRDT:
svg.selectAll('path').remove()
}
}