-
Notifications
You must be signed in to change notification settings - Fork 6
/
ParticipantRegister.ts
137 lines (124 loc) · 3.37 KB
/
ParticipantRegister.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
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
/*
Built for compatibility with https://github.com/rapid-sensemaking-framework/rsf-http-register
*/
import * as noflo from 'noflo'
import * as socketClient from 'socket.io-client'
import { ProcessHandler, NofloComponent } from '../libs/noflo-types'
import { ContactableConfig, ParticipantRegisterConfig } from 'rsf-types'
const guidGenerator = () => {
const S4 = () =>
(((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
return (
S4() +
S4() +
'-' +
S4() +
'-' +
S4() +
'-' +
S4() +
'-' +
S4() +
S4() +
S4()
)
}
const process: ProcessHandler = (input, output) => {
// TODO set a timeout
// Check preconditions on input data
if (
!input.hasData('socket_url', 'max_time', 'max_participants', 'description')
) {
return
}
// Read packets we need to process
const httpUrl: string = input.getData('http_url')
const socketUrl: string = input.getData('socket_url')
const maxTime: number = input.getData('max_time')
const maxParticipants: number = parseInt(input.getData('max_participants'))
const description: string = input.getData('description')
// create a brand new id which will be used
// in the url address on the site, where people will register
const id = guidGenerator()
const participantRegisterData: ParticipantRegisterConfig = {
id,
maxParticipants,
maxTime,
description
}
const socket = socketClient(socketUrl)
socket.on('connect', () => {
socket.emit('participant_register', participantRegisterData)
output.send({
register_url: `${httpUrl}/register/${id}`
})
setTimeout(() => {
socket.emit('open_register', id)
}, 50)
})
// single one
socket.on('participant_register_result', (result: ContactableConfig) => {
output.send({
result
})
})
// all results
socket.on('participant_register_results', (results: ContactableConfig[]) => {
output.send({
results
})
output.done()
})
}
const getComponent = (): NofloComponent => {
const c: NofloComponent = new noflo.Component()
/* META */
c.description =
'Spins up a web server to collect participant configs that are rsf-contactable compatible'
c.icon = 'compress'
/* IN PORTS */
c.inPorts.add('http_url', {
datatype: 'string',
description:
'the http url used to determine the address for the register page',
required: true
})
c.inPorts.add('socket_url', {
datatype: 'string',
description:
'the url with websocket protocol to connect to run this function',
required: true
})
c.inPorts.add('max_time', {
datatype: 'int',
description:
'the number of seconds to wait until stopping this process automatically',
required: true
})
c.inPorts.add('max_participants', {
datatype: 'int',
description:
'the number of participants to welcome to the process, default is unlimited',
required: true
})
c.inPorts.add('description', {
datatype: 'string',
description:
'the text to display to potential participants explaining the process',
required: true
})
/* OUT PORTS */
c.outPorts.add('register_url', {
datatype: 'string'
})
c.outPorts.add('result', {
datatype: 'object' // ContactableConfig
})
c.outPorts.add('results', {
datatype: 'array' // ContactableConfig[]
})
/* DEFINE PROCESS */
c.process(process)
return c
}
export { getComponent }