-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.ts
77 lines (65 loc) · 2.45 KB
/
receiver.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
import { init, Ditto, Logger, TransportConfig } from '@dittolive/ditto'
require('dotenv').config()
Logger.minimumLogLevel = 'Info'
async function main() {
// Initialize the Ditto module
await init()
let ditto = new Ditto({
type: 'onlinePlayground',
appID: process.env.APP_ID,
token: process.env.APP_TOKEN,
enableDittoCloudSync: false
})
const config = new TransportConfig()
config.peerToPeer.bluetoothLE.isEnabled = true
config.peerToPeer.lan.isEnabled = true
config.peerToPeer.awdl.isEnabled = false
ditto.observeTransportConditions((condition, source) => {
if (condition === 'BLEDisabled') {
console.log('BLE disabled')
} else if (condition === 'NoBLECentralPermission') {
console.log('Permission missing for BLE')
} else if (condition === 'NoBLEPeripheralPermission') {
console.log('Permissions missing for BLE')
}
})
ditto.setTransportConfig(config)
ditto.startSync()
let modelCollection = ditto.store.collection("models")
let modelSubscription = modelCollection.find("ACK == false").subscribe()
// LiveQuery to grab new documents
modelCollection.find("ACK == false").observeLocal(async (docs, event) => {
// new docs
for (let i = 0; i < docs.length; i++) {
// get attachmentToken
let doc = docs[i]
const attachmentToken = doc.at('my_attachment').attachmentToken
const attachmentFetcher = modelCollection.fetchAttachment(attachmentToken, async (attachmentFetchEvent) => {
switch (attachmentFetchEvent.type) {
case 'Completed':
const fetchedAttachment = attachmentFetchEvent.attachment
const target = doc.value["target"] + "/" + doc.value["filename"]
Logger.info(`Have new attachment, writing to local filesystem: ${target}`)
fetchedAttachment.copyToPath(`${target}`)
await modelCollection.findByID(doc.id).update((mutableDoc) => {
mutableDoc.at('ACK').set(true)
})
// write to local path - get from metadata
break
default:
Logger.error('Unable to fetch attachment completely')
break
}
})
}
})
// Fetch attachment for each new doc
let presence = ditto.presence.observe((graph) => {
if (graph.remotePeers.length != 0) {
graph.remotePeers.forEach((peer) => {
console.log("peer connection: ", peer.deviceName, peer.connections[0].connectionType)
})
}
})
}
main()