anear-js-api 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -31,7 +31,7 @@ class AnearMessaging {
|
|
|
31
31
|
this.AnearEventClass = AnearEventClass
|
|
32
32
|
this.AnearParticipantClass = AnearParticipantClass
|
|
33
33
|
this.mutex = new Mutex()
|
|
34
|
-
|
|
34
|
+
this.anearEvents = {}
|
|
35
35
|
|
|
36
36
|
const baseUrl = this.api.api_base_url
|
|
37
37
|
const authUrl = `${baseUrl}/messaging_auth`
|
|
@@ -49,7 +49,7 @@ class AnearMessaging {
|
|
|
49
49
|
echoMessages: false,
|
|
50
50
|
log: {
|
|
51
51
|
level: AblyLogLevel,
|
|
52
|
-
handler:
|
|
52
|
+
handler: message => {logger.debug(message)}
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -94,34 +94,26 @@ class AnearMessaging {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
setParticipantTimer(
|
|
97
|
+
setParticipantTimer(anearEvent, participant, timeoutMilliseconds) {
|
|
98
98
|
const participantId = participant.id
|
|
99
99
|
|
|
100
100
|
this.clearParticipantTimer(participantId)
|
|
101
101
|
|
|
102
102
|
if (timeoutMilliseconds === 0) return
|
|
103
103
|
|
|
104
|
-
logger.debug(`setting ${timeoutMilliseconds} msec timer for event ${
|
|
104
|
+
logger.debug(`setting ${timeoutMilliseconds} msec timer for event ${anearEvent.id}, participant ${participant.id}`)
|
|
105
105
|
|
|
106
106
|
this.participantTimers[participantId] = setTimeout(
|
|
107
|
-
async () => await this.timerExpired(
|
|
107
|
+
async () => await this.timerExpired(anearEvent, participant, timeoutMilliseconds),
|
|
108
108
|
timeoutMilliseconds
|
|
109
109
|
)
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
async timerExpired(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
logger.debug(`participant (${eventId}, ${participantId}) TIMED OUT after ${timeoutMilliseconds} msecs`)
|
|
112
|
+
async timerExpired(anearEvent, participant, timeoutMilliseconds) {
|
|
113
|
+
logger.debug(`participant (${anearEvent.id}, ${participant.id}) TIMED OUT after ${timeoutMilliseconds} msecs`)
|
|
116
114
|
|
|
117
|
-
await
|
|
118
|
-
|
|
119
|
-
async anearEvent => {
|
|
120
|
-
const participant = await this.getAnearParticipantFromStorage(participantId)
|
|
121
|
-
await anearEvent.participantTimedOut(participant)
|
|
122
|
-
await anearEvent.update()
|
|
123
|
-
}
|
|
124
|
-
)
|
|
115
|
+
await anearEvent.participantTimedOut(participant)
|
|
116
|
+
await anearEvent.update()
|
|
125
117
|
}
|
|
126
118
|
|
|
127
119
|
async getAnearEventFromStorage(eventId) {
|
|
@@ -171,6 +163,7 @@ class AnearMessaging {
|
|
|
171
163
|
// we just return if the event already exists
|
|
172
164
|
//
|
|
173
165
|
await this.loadOrPersistEventAndInitialize(anearEvent)
|
|
166
|
+
|
|
174
167
|
} catch(err) {
|
|
175
168
|
logger.error(err)
|
|
176
169
|
}
|
|
@@ -187,6 +180,9 @@ class AnearMessaging {
|
|
|
187
180
|
await this.runExclusive("createEventCallback", async () => {
|
|
188
181
|
await anearEvent.createdEventCallback()
|
|
189
182
|
await anearEvent.persist()
|
|
183
|
+
// start the state machine before initialiing Realtime Messaging
|
|
184
|
+
// as REFRESH events come in and the state machine should be ready
|
|
185
|
+
// to handle those XState events
|
|
190
186
|
anearEvent.startStateMachine()
|
|
191
187
|
await this.initEventRealtimeMessaging(anearEvent)
|
|
192
188
|
})
|
|
@@ -198,6 +194,8 @@ class AnearMessaging {
|
|
|
198
194
|
|
|
199
195
|
logger.info(`New ${loadedEvent.constructor.name} Event: `, loadedEvent.toJSON())
|
|
200
196
|
|
|
197
|
+
this.anearEvents[loadedEvent.id] = loadedEvent
|
|
198
|
+
|
|
201
199
|
return loadedEvent
|
|
202
200
|
}
|
|
203
201
|
|
|
@@ -211,6 +209,9 @@ class AnearMessaging {
|
|
|
211
209
|
for (const eventData of events) {
|
|
212
210
|
const eventJson = await this.api.getEvent(eventData.id)
|
|
213
211
|
const anearEvent = new this.AnearEventClass(eventJson, this)
|
|
212
|
+
// THIS does NOT work yet. This is logic that restarte events in progress
|
|
213
|
+
// if the nodeJS process crahsed during play
|
|
214
|
+
//
|
|
214
215
|
// const loadedEvent = await this.loadOrPersistEventAndInitialize(anearEvent)
|
|
215
216
|
// await this.refreshActiveParticipants(loadedEvent) DOES NOT WORK YET
|
|
216
217
|
}
|
|
@@ -402,7 +403,9 @@ class AnearMessaging {
|
|
|
402
403
|
// this can be just a temporary leave (refresh browser for example), so we don't do anything
|
|
403
404
|
// for now
|
|
404
405
|
const userId = message.clientId
|
|
405
|
-
|
|
406
|
+
const participantId = message.data.participantId
|
|
407
|
+
|
|
408
|
+
logger.debug(`**** LEAVE PARTICIPANT **** participantLeaveMessagingCallback(user: ${participantId})`)
|
|
406
409
|
}
|
|
407
410
|
|
|
408
411
|
async closeParticipant(anearEvent, participantId, callback) {
|
|
@@ -422,8 +425,9 @@ class AnearMessaging {
|
|
|
422
425
|
}
|
|
423
426
|
}
|
|
424
427
|
|
|
425
|
-
async detachParticipantPrivateChannel(
|
|
428
|
+
async detachParticipantPrivateChannel(anearEvent, participant) {
|
|
426
429
|
const userId = participant.userId
|
|
430
|
+
const eventId = anearEvent.id
|
|
427
431
|
const channel = this.eventChannels[eventId].privates[userId]
|
|
428
432
|
|
|
429
433
|
if (channel) {
|
|
@@ -431,6 +435,7 @@ class AnearMessaging {
|
|
|
431
435
|
delete this.eventChannels[eventId].privates[userId]
|
|
432
436
|
}
|
|
433
437
|
}
|
|
438
|
+
|
|
434
439
|
async participantActionMessagingCallback(anearEvent, message) {
|
|
435
440
|
// e.g. message.data
|
|
436
441
|
// {
|
|
@@ -460,10 +465,9 @@ class AnearMessaging {
|
|
|
460
465
|
})
|
|
461
466
|
}
|
|
462
467
|
|
|
463
|
-
async eventBroadcastMessagingCallback(
|
|
464
|
-
logger.debug(`eventBroadcaseMessagingCallback(${
|
|
468
|
+
async eventBroadcastMessagingCallback(anearEvent, message) {
|
|
469
|
+
logger.debug(`eventBroadcaseMessagingCallback(${anearEvent.id}`)
|
|
465
470
|
|
|
466
|
-
const anearEvent = await this.getAnearEventFromStorage(eventId)
|
|
467
471
|
await anearEvent.eventBroadcast(message)
|
|
468
472
|
await anearEvent.update()
|
|
469
473
|
}
|
|
@@ -524,7 +528,8 @@ class AnearMessaging {
|
|
|
524
528
|
}
|
|
525
529
|
}
|
|
526
530
|
|
|
527
|
-
async publishEventParticipantsMessage(
|
|
531
|
+
async publishEventParticipantsMessage(anearEvent, participants, css, message, timeoutMilliseconds=0, timeoutCallback=null) {
|
|
532
|
+
const eventId = anearEvent.id
|
|
528
533
|
const channel = this.eventChannels[eventId].participants
|
|
529
534
|
|
|
530
535
|
const setTimerFunction = () => this.setMultipleParticipantTimers(eventId, participants, timeoutMilliseconds)
|
|
@@ -540,16 +545,16 @@ class AnearMessaging {
|
|
|
540
545
|
)
|
|
541
546
|
}
|
|
542
547
|
|
|
543
|
-
setMultipleParticipantTimers(
|
|
548
|
+
setMultipleParticipantTimers(anearEvent, participants, timeoutMilliseconds) {
|
|
544
549
|
if (timeoutMilliseconds === 0) return
|
|
545
550
|
|
|
546
551
|
participants.forEach(
|
|
547
|
-
participant => this.setParticipantTimer(
|
|
552
|
+
participant => this.setParticipantTimer(anearEvent, participant, timeoutMilliseconds)
|
|
548
553
|
)
|
|
549
554
|
}
|
|
550
555
|
|
|
551
|
-
async publishEventSpectatorsMessage(
|
|
552
|
-
const channel = this.eventChannels[
|
|
556
|
+
async publishEventSpectatorsMessage(anearEvent, css, message, messageType = PublicDisplayMessageType) {
|
|
557
|
+
const channel = this.eventChannels[anearEvent.id].spectators
|
|
553
558
|
const payload = {
|
|
554
559
|
css: css,
|
|
555
560
|
content: message
|
|
@@ -559,7 +564,7 @@ class AnearMessaging {
|
|
|
559
564
|
}
|
|
560
565
|
|
|
561
566
|
async publishEventPrivateMessage(
|
|
562
|
-
|
|
567
|
+
anearEvent,
|
|
563
568
|
participant,
|
|
564
569
|
messageType,
|
|
565
570
|
css,
|
|
@@ -568,10 +573,10 @@ class AnearMessaging {
|
|
|
568
573
|
timeoutCallback=null) {
|
|
569
574
|
|
|
570
575
|
const userId = participant.userId
|
|
571
|
-
const channel = this.eventChannels[
|
|
576
|
+
const channel = this.eventChannels[anearEvent.id].privates[userId]
|
|
572
577
|
if (!channel) throw new Error(`private channel not found. invalid user id ${userId}`)
|
|
573
578
|
|
|
574
|
-
const setTimerFunction = () => this.setParticipantTimer(
|
|
579
|
+
const setTimerFunction = () => this.setParticipantTimer(anearEvent, participant, timeoutMilliseconds)
|
|
575
580
|
|
|
576
581
|
await this.publishChannelMessageWithTimeout(
|
|
577
582
|
channel,
|
|
@@ -612,11 +617,11 @@ class AnearMessaging {
|
|
|
612
617
|
await timerCallback()
|
|
613
618
|
}
|
|
614
619
|
|
|
615
|
-
async publishEventTransitionMessage(
|
|
616
|
-
const channel = this.eventChannels[
|
|
620
|
+
async publishEventTransitionMessage(anearEvent, newState) {
|
|
621
|
+
const channel = this.eventChannels[anearEvent.id].events
|
|
617
622
|
const payload = {content: {state: newState}}
|
|
618
623
|
|
|
619
|
-
logger.debug(`publishEventTransitionMessage: event ${
|
|
624
|
+
logger.debug(`publishEventTransitionMessage: event ${anearEvent.id} transitioning to ${newState}`)
|
|
620
625
|
|
|
621
626
|
await this.publishChannelMessage(channel, EventTransitionMessageType, payload)
|
|
622
627
|
}
|
|
@@ -644,6 +649,8 @@ class AnearMessaging {
|
|
|
644
649
|
await this.detachChannel(channel)
|
|
645
650
|
}
|
|
646
651
|
delete this.eventChannels[eventId]
|
|
652
|
+
|
|
653
|
+
delete this.anearEvents[eventId]
|
|
647
654
|
}
|
|
648
655
|
|
|
649
656
|
async detachChannel (channel) {
|
package/lib/models/AnearEvent.js
CHANGED
|
@@ -165,7 +165,7 @@ class AnearEvent extends JsonApiResource {
|
|
|
165
165
|
|
|
166
166
|
async publishEventParticipantsMessage(message, timeoutMilliseconds=0, timeoutCallback=null) {
|
|
167
167
|
await this.messaging.publishEventParticipantsMessage(
|
|
168
|
-
this
|
|
168
|
+
this,
|
|
169
169
|
this.activeParticipants,
|
|
170
170
|
this.css,
|
|
171
171
|
message,
|
|
@@ -175,12 +175,12 @@ class AnearEvent extends JsonApiResource {
|
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
async publishEventSpectatorsMessage(message) {
|
|
178
|
-
await this.messaging.publishEventSpectatorsMessage(this
|
|
178
|
+
await this.messaging.publishEventSpectatorsMessage(this, this.css, message)
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
async publishEventPrivateMessage(participant, message, timeoutMilliseconds=0, timeoutCallback=null) {
|
|
182
182
|
await this.messaging.publishEventPrivateMessage(
|
|
183
|
-
this
|
|
183
|
+
this,
|
|
184
184
|
participant,
|
|
185
185
|
PrivateDisplayMessageType,
|
|
186
186
|
this.css,
|
|
@@ -192,7 +192,7 @@ class AnearEvent extends JsonApiResource {
|
|
|
192
192
|
|
|
193
193
|
async publishEventTransitionMessage(newState) {
|
|
194
194
|
await this.messaging.publishEventTransitionMessage(
|
|
195
|
-
this
|
|
195
|
+
this,
|
|
196
196
|
newState
|
|
197
197
|
)
|
|
198
198
|
}
|
|
@@ -252,9 +252,9 @@ class AnearEvent extends JsonApiResource {
|
|
|
252
252
|
logger.debug(`AnearEvent: transitionEvent(${eventName})`)
|
|
253
253
|
|
|
254
254
|
try {
|
|
255
|
-
const responseAttributes = await this.messaging.api.transitionEvent(this
|
|
255
|
+
const responseAttributes = await this.messaging.api.transitionEvent(this, eventName)
|
|
256
256
|
const newState = responseAttributes.state
|
|
257
|
-
await this.messaging.publishEventTransitionMessage(this
|
|
257
|
+
await this.messaging.publishEventTransitionMessage(this, newState)
|
|
258
258
|
} catch(err) {
|
|
259
259
|
logger.error(`AnearEvent: transitionEvent error: ${err}`)
|
|
260
260
|
}
|