anear-js-api 0.3.13 → 0.3.16

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.
@@ -16,6 +16,7 @@ const EventTransitionMessageType = 'event_transition'
16
16
 
17
17
  const PRESENCE_ENTER = 'enter'
18
18
  const PRESENCE_LEAVE = 'leave'
19
+ const ALREADY_PRESENT = 'present'
19
20
 
20
21
  // any channel messages sent with 5 secs (5s) of initial attach will be delivered
21
22
  // to the subscribers
@@ -97,9 +98,8 @@ class AnearMessaging {
97
98
  if (timeoutMsecs > 0) {
98
99
  const timer = this.participantTimers[participant.id] || this.createTimer(anearEvent, participant, timeoutMsecs)
99
100
 
100
- if (timer.isPaused) {
101
- timeRemaining = this.timeRemaining
102
- timerStarter = () => timer.resume()
101
+ if (timer.isRunning) {
102
+ timeRemaining = timer.interrupt()
103
103
  } else {
104
104
  timerStarter = () => timer.start(timeoutMsecs)
105
105
  }
@@ -130,10 +130,10 @@ class AnearMessaging {
130
130
  }
131
131
  }
132
132
 
133
- pauseParticipantTimer(participantId) {
133
+ interruptParticipantTimer(participantId) {
134
134
  const timer = this.participantTimers[participantId]
135
135
 
136
- if (timer && timer.isRunning) timer.pause()
136
+ if (timer && timer.isRunning) timer.interrupt()
137
137
  }
138
138
 
139
139
  resetParticipantTimer(participantId) {
@@ -441,9 +441,7 @@ class AnearMessaging {
441
441
 
442
442
  logger.debug(`**** LEAVE PARTICIPANT **** participantLeaveMessagingCallback(participant: ${participantId})`)
443
443
 
444
- // pause the participant timer if one was active. The participant may return shortly
445
- // and we can resume this timer.
446
- this.pauseParticipantTimer(participantId)
444
+ this.interruptParticipantTimer(participantId)
447
445
  }
448
446
 
449
447
  async closeParticipant(anearEvent, participantId, callback) {
@@ -16,39 +16,54 @@ class ParticipantTimer {
16
16
  }
17
17
 
18
18
  start(timeoutMsecs, now = Date.now()) {
19
- this.startedAt = now
20
19
  logger.debug(`starting ${timeoutMsecs} msec timer for participant ${this.participantId}`)
21
- this.runTimer(timeoutMsecs)
20
+ this.runTimer(now, timeoutMsecs)
22
21
  }
23
22
 
24
- runTimer(timeoutMsecs) {
25
- const timerExpired = () => {
26
- this.state = Expired
27
- this.expireCallback()
28
- }
23
+ timerExpired() {
24
+ logger.debug(`TIMEOUT!! timer expired for participant ${this.participantId}`)
25
+ this.state = Expired
26
+ this.expireCallback()
27
+ }
28
+
29
+ runTimer(now, timeoutMsecs) {
30
+ this.startedAt = now
31
+ this.timeRemaining = timeoutMsecs
29
32
  this.state = Running
30
- this.id = setTimeout(timerExpired, timeoutMsecs)
33
+ this.id = setTimeout(() => this.timerExpired(), timeoutMsecs)
34
+ }
35
+
36
+ interrupt(now = Date.now()) {
37
+ // pause the timer to set the timeRemaining and then resume
38
+ // This is useful when a Participant does a quick LEAVE then rejoins with a REFRESH
39
+ this.pause(now)
40
+ this.resume(now)
41
+
42
+ return this.timeRemaining
31
43
  }
32
44
 
33
45
  pause(now = Date.now()) {
34
46
  // if running, stop the timer
35
- if (!this.isRunning) throw new Error("timer not running")
47
+ this.checkTimerIs(Running)
36
48
 
37
49
  clearTimeout(this.id)
38
50
  this.id = null
39
- this.timeRemaining = now - this.startedAt
51
+ this.timeRemaining -= (now - this.startedAt)
40
52
 
41
53
  logger.debug(`pausing timer for participant ${this.participantId}. Time remaining: ${this.timeRemaining}`)
42
54
  this.state = Paused
43
55
  }
44
56
 
45
- resume() {
57
+ resume(now = Date.now()) {
46
58
  // if paused, restarts the timer with the timeRemaining
47
- if (!this.isPaused) throw new Error("timer not paused")
59
+ this.checkTimerIs(Paused)
48
60
 
49
61
  if (this.timeRemaining > 0) {
50
62
  logger.debug(`resuming ${this.timeRemaining} msec timer for participant ${this.participantId}`)
51
- this.runTimer(this.timeRemaining)
63
+ this.runTimer(now, this.timeRemaining)
64
+ } else {
65
+ logger.debug(`resume() detects timeRemaining of ${this.timeRemaining} so is calling timerExpired() for participant ${this.participantId}`)
66
+ this.timerExpired()
52
67
  }
53
68
  }
54
69
 
@@ -67,6 +82,10 @@ class ParticipantTimer {
67
82
  this.startedAt = null
68
83
  }
69
84
 
85
+ checkTimerIs(state) {
86
+ if (this.state !== state) throw new Error(`timer is not ${state}`)
87
+ }
88
+
70
89
  get isRunning() {
71
90
  return this.state === Running
72
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "0.3.13",
3
+ "version": "0.3.16",
4
4
  "description": "Javascript Developer API for Anear Apps",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -47,3 +47,20 @@ test('start, pause, then resume', () => {
47
47
  jest.advanceTimersByTime(501);
48
48
  expect(callback).toHaveBeenCalledTimes(1)
49
49
  })
50
+
51
+ test('start and interrupt multiple times', () => {
52
+ const callback = jest.fn()
53
+ const t = new ParticipantTimer(ParticipantId, callback)
54
+
55
+ t.start(1000, Now)
56
+ jest.advanceTimersByTime(500);
57
+ t.interrupt(Now + 500)
58
+ expect(t.timeRemaining).toEqual(500)
59
+ expect(t.isRunning).toBe(true)
60
+ jest.advanceTimersByTime(250);
61
+ t.interrupt(Now + 750)
62
+ expect(t.timeRemaining).toEqual(250)
63
+ expect(t.isRunning).toBe(true)
64
+ jest.advanceTimersByTime(501);
65
+ expect(callback).toHaveBeenCalledTimes(1)
66
+ })