-
Notifications
You must be signed in to change notification settings - Fork 5
/
room.js
133 lines (113 loc) · 3.55 KB
/
room.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
/**
* Creates a new client to a roomdb instance
* This client will try and communicate over socket.io or http
*
* @param {host} host of living room server (defaults to http://localhost:3000)
*/
import { io } from 'socket.io-client'
export default class Room {
constructor (host) {
this._subscribeTimeout = 2500 // ms
this._messages = []
this._host = host || process.env.LIVING_ROOM_HOST || 'localhost:3000'
if (!this._host.startsWith('http://')) this._host = `http://${this._host}`
this._hosts = new Set([this._host])
this._socket = io(this._host)
this._socket.on('error', (error) => console.error(error))
if (typeof window === 'object') {
this._socket.on('reconnect', () => {
window.location.reload(true)
})
}
}
/**
* @param {String} ...facts
* @param {Function} callback
*/
unsubscribe (...facts) {
const callback = facts.splice(facts.length - 1)[0]
const patternsString = JSON.stringify(facts)
const unwrapped = results => {
const unwrappedResults = this._unwrap(results)
callback(unwrappedResults)
}
this._socket.off(patternsString, unwrapped)
return new Promise(resolve => {
this._socket.emit('unsubscribe', patternsString, resolve)
})
}
/**
* Subscribe lets a callback listen to all assertions and retractions
* Compare to .on() which fires off only for assertions
*
* @param {String} ...facts
* @param {Function} callback
*/
subscribe (...facts) {
const callback = facts.splice(facts.length - 1)[0]
return new Promise((resolve, reject) => {
this._socket.on('error', reject)
this._socket.on('subscribe', resolve)
this._socket.on(facts, results => {
// this.facts().then(f => console.log(f))
// console.log({ results })
const unwrappedResults = this._unwrap(results)
callback(unwrappedResults)
})
this._socket.emit('subscribe', facts)
})
}
_unwrap ({ assertions, retractions }) {
const unwrap = fact => {
const unwrapped = {}
for (const key in fact) {
const val = fact[key]
if (typeof val === 'undefined') continue
unwrapped[key] = val.value || val.word || val.text || val.id
}
return unwrapped
}
return {
assertions: assertions.map(unwrap),
retractions: retractions.map(unwrap)
}
}
on (...facts) {
const callback = facts.splice(facts.length - 1)[0]
const cb = ({ assertions }) => assertions.forEach(callback)
this.subscribe(...facts, cb)
}
off (...facts) {
const callback = facts.splice(facts.length - 1)[0]
const cb = ({ assertions }) => assertions.forEach(callback)
this.unsubscribe(...facts, cb)
}
async send () {
const messages = this._messages.splice(0, this._messages.length)
return Promise.all(messages.map(verb => {
return new Promise((resolve) => {
if (verb.assert) this._socket.emit('assert', verb.assert, resolve)
if (verb.retract) this._socket.emit('retract', verb.retract, resolve)
})
}))
}
assert (...facts) {
this._messages.push(...facts.map(assert => ({ assert })))
return this
}
retract (...facts) {
this._messages.push(...facts.map(retract => ({ retract })))
return this
}
select (...facts) {
return new Promise(resolve => {
this._socket.emit('select', facts, resolve)
})
}
async count (...facts) {
return this.select(...facts).then(assertions => assertions.length)
}
async exists (...facts) {
return this.count(...facts).then(count => count > 0)
}
}