anear-js-api 0.3.2 → 0.3.5

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: (message) => {logger.debug(message)}
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(eventId, participant, timeoutMilliseconds) {
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 ${eventId}, participant ${participant.id}`)
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(eventId, participant, timeoutMilliseconds),
107
+ async () => await this.timerExpired(anearEvent, participant, timeoutMilliseconds),
108
108
  timeoutMilliseconds
109
109
  )
110
110
  }
111
111
 
112
- async timerExpired(eventId, participant, timeoutMilliseconds) {
113
- const participantId = participant.id
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 this.getAnearEventWithLockFromStorage(
118
- eventId,
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
- logger.debug(`**** LEAVE PARTICIPANT **** participantLeaveMessagingCallback(user: ${userId})`)
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) {
@@ -431,6 +434,7 @@ class AnearMessaging {
431
434
  delete this.eventChannels[eventId].privates[userId]
432
435
  }
433
436
  }
437
+
434
438
  async participantActionMessagingCallback(anearEvent, message) {
435
439
  // e.g. message.data
436
440
  // {
@@ -460,10 +464,9 @@ class AnearMessaging {
460
464
  })
461
465
  }
462
466
 
463
- async eventBroadcastMessagingCallback(eventId, message) {
464
- logger.debug(`eventBroadcaseMessagingCallback(${eventId}`)
467
+ async eventBroadcastMessagingCallback(anearEvent, message) {
468
+ logger.debug(`eventBroadcaseMessagingCallback(${anearEvent.id}`)
465
469
 
466
- const anearEvent = await this.getAnearEventFromStorage(eventId)
467
470
  await anearEvent.eventBroadcast(message)
468
471
  await anearEvent.update()
469
472
  }
@@ -524,7 +527,8 @@ class AnearMessaging {
524
527
  }
525
528
  }
526
529
 
527
- async publishEventParticipantsMessage(eventId, participants, css, message, timeoutMilliseconds=0, timeoutCallback=null) {
530
+ async publishEventParticipantsMessage(anearEvent, participants, css, message, timeoutMilliseconds=0, timeoutCallback=null) {
531
+ const eventId = anearEvent.id
528
532
  const channel = this.eventChannels[eventId].participants
529
533
 
530
534
  const setTimerFunction = () => this.setMultipleParticipantTimers(eventId, participants, timeoutMilliseconds)
@@ -540,16 +544,16 @@ class AnearMessaging {
540
544
  )
541
545
  }
542
546
 
543
- setMultipleParticipantTimers(eventId, participants, timeoutMilliseconds) {
547
+ setMultipleParticipantTimers(anearEvent, participants, timeoutMilliseconds) {
544
548
  if (timeoutMilliseconds === 0) return
545
549
 
546
550
  participants.forEach(
547
- participant => this.setParticipantTimer(eventId, participant, timeoutMilliseconds)
551
+ participant => this.setParticipantTimer(anearEvent, participant, timeoutMilliseconds)
548
552
  )
549
553
  }
550
554
 
551
- async publishEventSpectatorsMessage(eventId, css, message, messageType = PublicDisplayMessageType) {
552
- const channel = this.eventChannels[eventId].spectators
555
+ async publishEventSpectatorsMessage(anearEvent, css, message, messageType = PublicDisplayMessageType) {
556
+ const channel = this.eventChannels[anearEvent.id].spectators
553
557
  const payload = {
554
558
  css: css,
555
559
  content: message
@@ -559,7 +563,7 @@ class AnearMessaging {
559
563
  }
560
564
 
561
565
  async publishEventPrivateMessage(
562
- eventId,
566
+ anearEvent,
563
567
  participant,
564
568
  messageType,
565
569
  css,
@@ -568,10 +572,10 @@ class AnearMessaging {
568
572
  timeoutCallback=null) {
569
573
 
570
574
  const userId = participant.userId
571
- const channel = this.eventChannels[eventId].privates[userId]
575
+ const channel = this.eventChannels[anearEvent.id].privates[userId]
572
576
  if (!channel) throw new Error(`private channel not found. invalid user id ${userId}`)
573
577
 
574
- const setTimerFunction = () => this.setParticipantTimer(eventId, participant, timeoutMilliseconds)
578
+ const setTimerFunction = () => this.setParticipantTimer(anearEvent, participant, timeoutMilliseconds)
575
579
 
576
580
  await this.publishChannelMessageWithTimeout(
577
581
  channel,
@@ -612,11 +616,11 @@ class AnearMessaging {
612
616
  await timerCallback()
613
617
  }
614
618
 
615
- async publishEventTransitionMessage(eventId, newState) {
616
- const channel = this.eventChannels[eventId].events
619
+ async publishEventTransitionMessage(anearEvent, newState) {
620
+ const channel = this.eventChannels[anearEvent.id].events
617
621
  const payload = {content: {state: newState}}
618
622
 
619
- logger.debug(`publishEventTransitionMessage: event ${eventId} transitioning to ${newState}`)
623
+ logger.debug(`publishEventTransitionMessage: event ${anearEvent.id} transitioning to ${newState}`)
620
624
 
621
625
  await this.publishChannelMessage(channel, EventTransitionMessageType, payload)
622
626
  }
@@ -644,6 +648,8 @@ class AnearMessaging {
644
648
  await this.detachChannel(channel)
645
649
  }
646
650
  delete this.eventChannels[eventId]
651
+
652
+ delete this.anearEvents[eventId]
647
653
  }
648
654
 
649
655
  async detachChannel (channel) {
@@ -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.id,
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.id, this.css, message)
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.id,
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.id,
195
+ this,
196
196
  newState
197
197
  )
198
198
  }
@@ -207,13 +207,13 @@ class AnearEvent extends JsonApiResource {
207
207
 
208
208
  this.participants.add(this, participant) // update the participants entry
209
209
 
210
- this.anearStateMachine.sendRefreshEvent({participant: participant})
210
+ this.anearStateMachine.sendRefreshEvent({ participant })
211
211
 
212
212
  await participant.update()
213
213
  } else {
214
214
  this.participants.add(this, participant) // add the participants entry
215
215
 
216
- this.anearStateMachine.sendJoinEvent({participant: participant})
216
+ this.anearStateMachine.sendJoinEvent({ participant })
217
217
 
218
218
  await participant.persist()
219
219
  }
@@ -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.id, eventName)
255
+ const responseAttributes = await this.messaging.api.transitionEvent(this, eventName)
256
256
  const newState = responseAttributes.state
257
- await this.messaging.publishEventTransitionMessage(this.id, newState)
257
+ await this.messaging.publishEventTransitionMessage(this, newState)
258
258
  } catch(err) {
259
259
  logger.error(`AnearEvent: transitionEvent error: ${err}`)
260
260
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "0.3.2",
3
+ "version": "0.3.5",
4
4
  "description": "Javascript Developer API for Anear Apps",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/machvee/anear-js-api#readme",
19
19
  "dependencies": {
20
- "ably": "^1.2.15",
20
+ "ably": "^1.2.18",
21
21
  "async-mutex": "^0.3.2",
22
22
  "async-redis": "^2.0.0",
23
23
  "cross-fetch": "^3.1.5",