anear-js-api 0.3.0 → 0.3.3

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.
@@ -69,8 +69,8 @@ class AnearMessaging {
69
69
  async () => {
70
70
  await this.getAppInfo(AppId)
71
71
  logger.info("Ably connected!")
72
- await this.reloadAnyEventsInProgress(AppId)
73
72
  await this.setupCreateEventChannel()
73
+ await this.reloadAnyEventsInProgress(AppId)
74
74
  }
75
75
  )
76
76
  }
@@ -181,18 +181,24 @@ class AnearMessaging {
181
181
 
182
182
  logger.info(`Event ${anearEvent.id} ${eventExists ? "already exists" : "does not exist"} in Storage`)
183
183
 
184
+ let loadedEvent = anearEvent
185
+
184
186
  if (!eventExists) {
185
187
  await this.runExclusive("createEventCallback", async () => {
186
188
  await anearEvent.createdEventCallback()
187
189
  await anearEvent.persist()
190
+ anearEvent.startStateMachine()
191
+ await this.initEventRealtimeMessaging(anearEvent)
188
192
  })
193
+ } else {
194
+ loadedEvent = await this.getAnearEventFromStorage(anearEvent.id)
195
+ await this.initEventRealtimeMessaging(loadedEvent)
196
+ loadedEvent.startStateMachine()
189
197
  }
190
198
 
191
- logger.info(`New ${anearEvent.constructor.name} Event: `, anearEvent.toJSON())
192
-
193
- anearEvent.startStateMachine()
199
+ logger.info(`New ${loadedEvent.constructor.name} Event: `, loadedEvent.toJSON())
194
200
 
195
- await this.initEventRealtimeMessaging(anearEvent)
201
+ return loadedEvent
196
202
  }
197
203
 
198
204
  async reloadAnyEventsInProgress(appId) {
@@ -205,7 +211,8 @@ class AnearMessaging {
205
211
  for (const eventData of events) {
206
212
  const eventJson = await this.api.getEvent(eventData.id)
207
213
  const anearEvent = new this.AnearEventClass(eventJson, this)
208
- await this.loadOrPersistEventAndInitialize(anearEvent)
214
+ // const loadedEvent = await this.loadOrPersistEventAndInitialize(anearEvent)
215
+ // await this.refreshActiveParticipants(loadedEvent) DOES NOT WORK YET
209
216
  }
210
217
  }
211
218
  } catch (err) {
@@ -213,6 +220,20 @@ class AnearMessaging {
213
220
  }
214
221
  }
215
222
 
223
+ async refreshActiveParticipants(anearEvent) {
224
+ const allParticipants = anearEvent.participants.active(false)
225
+
226
+ return Promise.all(
227
+ allParticipants.map(
228
+ async participant => await this.processParticipantEnter(
229
+ anearEvent,
230
+ participant.id,
231
+ participant.geoLocation
232
+ )
233
+ )
234
+ )
235
+ }
236
+
216
237
  async setupCreateEventChannel() {
217
238
  logger.info(`attaching to channel ${AnearCreateEventChannelName}`)
218
239
 
@@ -328,21 +349,23 @@ class AnearMessaging {
328
349
 
329
350
  logger.debug(`**** ENTER PARTICIPANT **** event: ${anearEvent.id}, participant: ${participantId}`)
330
351
 
352
+ await this.processParticipantEnter(anearEvent, participantId, geoLocation)
353
+ }
354
+
355
+ async processParticipantEnter(anearEvent, participantId, geoLocation) {
356
+
357
+ logger.debug(`processing Participant Enter for event: ${anearEvent.id}, participant: ${participantId}`)
331
358
  //
332
359
  // get the participant data from the API (this will also validate the participant).
333
360
  // check if the participant is already in storage, and if so, instantiate, else
334
361
  // instantiate from API response
335
362
  //
336
363
  try {
337
- logger.debug(`API fetch participant info for ${participantId}`)
338
-
339
364
  const participantJson = await this.api.getEventParticipantJson(participantId)
340
365
  const participant = new this.AnearParticipantClass(participantJson)
341
366
 
342
367
  participant.geoLocation = geoLocation
343
368
 
344
- await this.setupPrivatePublishingChannel(participant)
345
-
346
369
  const persistedAnearParticipant = await this.AnearParticipantClass.getFromStorage(participantId)
347
370
 
348
371
  if (persistedAnearParticipant) {
@@ -350,6 +373,7 @@ class AnearMessaging {
350
373
  }
351
374
 
352
375
  await this.runExclusive("participantEnterCallback", async () => {
376
+ await this.setupPrivatePublishingChannel(participant)
353
377
  await anearEvent.participantEnter(participant)
354
378
  await anearEvent.update()
355
379
  })
@@ -357,7 +381,7 @@ class AnearMessaging {
357
381
  // participant not found or is not currently marked active at the API service
358
382
  // don't allow participation. FIX: we need to publish to the private channel
359
383
  // with an error message type.
360
- logger.error(`participantEnterMessagingCallback(${anearEvent.id}, ${participantId}) error: `, error)
384
+ logger.error(`processParticipanEnter(${anearEvent.id}, ${participantId}) error: `, error)
361
385
  }
362
386
  }
363
387
 
@@ -74,7 +74,7 @@ class AnearEvent extends JsonApiResource {
74
74
  async clonedEventContext() {
75
75
  const clonedEvent = await this.getClonedEvent()
76
76
 
77
- return clonedEvent?.context
77
+ return clonedEvent ? clonedEvent.context : null
78
78
  }
79
79
 
80
80
  get stateMachineContext() {
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "0.3.0",
3
+ "version": "0.3.3",
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",