anear-js-api 0.4.6 → 0.4.7

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.
@@ -8,38 +8,34 @@ class AnearApi extends ApiService {
8
8
  super(apiKey, apiVersion)
9
9
  }
10
10
 
11
- async getAccount() {
11
+ getAccount() {
12
12
  logger.debug("API: GET /accounts")
13
13
 
14
- return await this.get("accounts")
14
+ return this.get("accounts")
15
15
  }
16
16
 
17
- async getEvent(eventId) {
17
+ getEvent(eventId) {
18
18
  logger.debug(`API: GET event ${eventId}`)
19
19
 
20
- const json = await this.get("events", {id: eventId})
21
- return json
20
+ return this.get("events", {id: eventId})
22
21
  }
23
22
 
24
- async getAppZones(appId) {
23
+ getAppZones(appId) {
25
24
  logger.debug(`API: GET app_zones ${appId}`)
26
25
 
27
- const json = await this.get("app_zones", {id: appId})
28
- return json
26
+ return this.get("app_zones", {id: appId})
29
27
  }
30
28
 
31
- async getApp(appId) {
29
+ getApp(appId) {
32
30
  logger.debug(`API: GET app ${appId}`)
33
31
 
34
- const json = await this.get("apps", {id: appId})
35
- return json
32
+ return this.get("apps", {id: appId})
36
33
  }
37
34
 
38
- async getZoneEvents(zoneId) {
35
+ getZoneEvents(zoneId) {
39
36
  logger.debug(`API: GET zone_events ${zoneId}`)
40
37
 
41
- const json = await this.get("zone_events", {id: zoneId})
42
- return json
38
+ return this.get("zone_events", {id: zoneId})
43
39
  }
44
40
 
45
41
  async transitionEvent(eventId, eventName='next') {
@@ -52,11 +48,10 @@ class AnearApi extends ApiService {
52
48
  return attrs
53
49
  }
54
50
 
55
- async getEventParticipantJson(participantId, geoLocation) {
51
+ getEventParticipantJson(participantId, geoLocation) {
56
52
  logger.debug(`API: GET event_participant ${participantId}`)
57
53
 
58
- const json = await this.get("event_participants", {id: participantId})
59
- return json
54
+ return this.get("event_participants", {id: participantId})
60
55
  }
61
56
  }
62
57
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  class AnearApi {
4
4
 
5
- async getAccount() {
5
+ getAccount() {
6
6
  }
7
7
 
8
8
  async transitionEvent(eventId, eventName='next') {
@@ -61,7 +61,7 @@ class AnearMessaging {
61
61
  initRealtime(clientOptions) {
62
62
  logger.debug("new Ably.Realtime connection..., log level: ", AblyLogLevel)
63
63
 
64
- this.realtime = new Ably.Realtime(clientOptions)
64
+ this.realtime = new Ably.Realtime.Promise(clientOptions)
65
65
 
66
66
  const connectedCallback = async () => {
67
67
  await this.getAppInfo(AppId)
@@ -218,21 +218,31 @@ class AnearEvent extends JsonApiResource {
218
218
  async participantEnter(participant) {
219
219
  // Called each time a participant ENTERs (attaches to) the Event's Action Channel.
220
220
  // This could be when joining an event for the first time, rejoining, or after a browser
221
- // refresh/reconnect. If the participant exists in this.participants, we call Refresh,
222
- // else Enter Invoke Enter/Refresh callbacks
223
- if (this.participants.exists(participant)) {
224
- logger.info(`AnearEvent: participant ${participant.id} exists. Refreshing...`)
225
-
226
- this.participants.add(participant) // update the participants record
227
-
228
- this.anearStateMachine.sendRefreshEvent({ participant })
229
-
230
- await participant.update()
221
+ // refresh/reconnect. If the participant exists in Storage and in this.participants,
222
+ // its probably due to a browser refresh, and we call Refresh. Else, its either a brand
223
+ // new participant or an anearEvent recovery after crash, and so we invoke new participant enter
224
+ //
225
+ if (participant.exists()) { // persisted in storage?
226
+ if (this.participants.exists(participant)) {
227
+ logger.debug(`AnearEvent: participant ${participant.id} exists. Refreshing...`)
228
+
229
+ // Likely here due to participant browser refresh
230
+ // get the existing participant, turn off his timer and send refresh event to StateMachine
231
+ const existingParticipant = this.participants.get(participant)
232
+ existingParticipant.interruptTimer()
233
+ this.anearStateMachine.sendRefreshEvent({participant: existingParticipant})
234
+ await existingParticipant.update()
235
+ } else {
236
+ // Likely here due to prior AnearEvent error and this is a event reload and restart
237
+ // add the participants record back into Participants and update persisted copy
238
+ this.participants.add(participant)
239
+ this.anearStateMachine.sendJoinEvent({ participant })
240
+ await participant.update()
241
+ }
231
242
  } else {
243
+ // New Participant first-time join this AnearEvent
232
244
  this.participants.add(participant) // add the participants record
233
-
234
245
  this.anearStateMachine.sendJoinEvent({ participant })
235
-
236
246
  await participant.persist()
237
247
  }
238
248
  }
@@ -360,8 +370,8 @@ class AnearEvent extends JsonApiResource {
360
370
  await this.messaging.detachAll(this.id)
361
371
  }
362
372
 
363
- logMessage(...args) {
364
- logger.info(...args)
373
+ logDebugMessage(...args) {
374
+ logger.debug(...args)
365
375
  }
366
376
 
367
377
  logError(context, event) {
@@ -46,7 +46,15 @@ class Participants {
46
46
  }
47
47
 
48
48
  exists({id}) {
49
- return this.getById(id) != undefined
49
+ const emptyObject = (obj) => {
50
+ return Object.keys(obj).length === 0 && Object.getPrototypeOf(obj) === Object.prototype
51
+ }
52
+
53
+ const participant = this.getById(id)
54
+
55
+ if (!participant) { return false } // id not found
56
+
57
+ return !emptyObject(participant)
50
58
  }
51
59
 
52
60
  get({id}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "0.4.6",
3
+ "version": "0.4.7",
4
4
  "description": "Javascript Developer API for Anear Apps",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {