-
Notifications
You must be signed in to change notification settings - Fork 11
/
configureProtocol.ts
104 lines (92 loc) · 2.54 KB
/
configureProtocol.ts
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
import * as IPFS from 'ipfs';
import Y from 'yjs/dist/y';
import yMemory from 'y-memory/dist/y-memory';
import yArray from 'y-array/dist/y-array';
import yMap from 'y-map/dist/y-map';
import yIndexedDb from 'y-indexeddb/dist/y-indexeddb';
import * as ipfsConnector from '../lib/y-ipfs-connector';
// import ipfsConnector from 'y-ipfs-connector';
yMemory(Y);
yArray(Y);
yMap(Y);
yIndexedDb(Y);
ipfsConnector(Y);
import { ApplicationState } from './index';
import { Commit } from './Cards/models';
export default (
terminalId: string, user: string,
dispatch: (action: any) => void,
getState: () => ApplicationState,
cb: (protocol: any) => void) => {
const ipfs = new IPFS({
repo: 'ipfs/PM-POS/' + terminalId + '/' + (user ? user : Math.random()),
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
]
}
}
});
ipfs.once('ready', () => {
ipfs.id((err, info) => {
if (err) {
throw err;
}
});
});
Y({
db: {
name: 'indexeddb'
},
connector: {
name: 'ipfs',
room: 'pmpos-protocol',
ipfs: ipfs
},
share: {
chat: 'Array',
commits: 'Array'
}
}).then((y) => {
dispatchCommitProtocol(dispatch, y.share.commits);
dispatchCommitEvent(dispatch, y.share.commits.toArray());
y.share.commits.observe(event => {
dispatchCommitEvent(dispatch, event.values);
});
y.share.chat.toArray().forEach(x => dispatchChatEvent(dispatch, x));
y.share.chat.observe(event => {
if (event.type === 'insert') {
for (let i = 0; i < event.length; i++) {
dispatchChatEvent(dispatch, event.values[i]);
}
}
});
cb(y);
});
};
function dispatchCommitProtocol(dispatch: any, protocol: any) {
dispatch({
type: 'SET_COMMIT_PROTOCOL',
protocol
});
}
function dispatchCommitEvent(dispatch: any, values: Commit[]) {
dispatch({
type: 'COMMIT_RECEIVED',
values
});
}
function dispatchChatEvent(dispatch: any, value: any) {
dispatch({
type: 'ADD_MESSAGE',
time: value.time,
message: value.message,
user: value.user,
id: value.id,
lamport: value.lamport
});
}