anear-js-api 0.4.22 → 0.4.23

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "0.4.22",
3
+ "version": "0.4.23",
4
4
  "description": "Javascript Developer API for Anear Apps",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -18,8 +18,6 @@
18
18
  "homepage": "https://github.com/machvee/anear-js-api#readme",
19
19
  "dependencies": {
20
20
  "ably": "^2.7.0",
21
- "async-mutex": "^0.3.2",
22
- "async-redis": "^2.0.0",
23
21
  "clean-css": "^5.3.3",
24
22
  "cross-fetch": "^3.1.5",
25
23
  "pug": "^3.0.2",
@@ -30,8 +28,7 @@
30
28
  },
31
29
  "devDependencies": {
32
30
  "dotenv": "^16.0.3",
33
- "fakeredis": "^2.0.0",
34
- "jest": "^26.6.3",
31
+ "jest": "^29.7.0",
35
32
  "node-notifier": "^10.0.1"
36
33
  }
37
34
  }
@@ -1,181 +0,0 @@
1
- "use strict"
2
-
3
- const ActiveState = "active"
4
- const IdleState = "idle"
5
-
6
- const MINUTES = (60 * 1000)
7
- const HOURS = (60 * MINUTES)
8
- const DefaultIdleMsecs = (30 * MINUTES)
9
- const DefaultPurgeMsecs = (2 * HOURS) // after Idle
10
-
11
- class Participants {
12
-
13
- constructor(anearEvent, {idleMsecs = DefaultIdleMsecs, purgeMsecs = DefaultPurgeMsecs, ids = []} = {}) {
14
- this.anearEvent = anearEvent
15
- this.idleMsecs = idleMsecs
16
- this.purgeMsecs = purgeMsecs
17
- this._participants = {}
18
- for (const id of ids) {
19
- // app restart logic ...
20
- // seeds from ids with empty objects awaiting full
21
- // anearParticipant rehydration from redis
22
- this._participants[id] = {}
23
- }
24
- this._host = null
25
- }
26
-
27
- toJSON() {
28
- return {
29
- ids: this.all.map(p => p.id),
30
- idleMsecs: this.idleMsecs,
31
- purgeMsecs: this.purgeMsecs
32
- }
33
- }
34
-
35
- get ids() {
36
- return Object.keys(this._participants)
37
- }
38
-
39
- indexedById() {
40
- // returns an object that has AnearParticipant.id as key
41
- return this._participants
42
- }
43
-
44
- getById(participantId) {
45
- return this._participants[participantId]
46
- }
47
-
48
- exists({id}) {
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)
58
- }
59
-
60
- get({id}) {
61
- return this.getById(id)
62
- }
63
-
64
- get host() {
65
- return this._host
66
- }
67
-
68
- set host(h) {
69
- this._host = h
70
- }
71
-
72
- get all() {
73
- return Object.values(this._participants).
74
- sort((ca, cb) => ca.timestamp - cb.timestamp)
75
- }
76
-
77
- get active() {
78
- return Object.values(this._participants).filter(c => c.state === ActiveState)
79
- }
80
-
81
- get idle() {
82
- return Object.values(this._participants).filter(c => c.state === IdleState)
83
- }
84
-
85
- get count() {
86
- return this.ids.length
87
- }
88
-
89
- get numActive() {
90
- return this.active.length
91
- }
92
-
93
- get numIdle() {
94
- return this.idle.length
95
- }
96
-
97
- isIdle(c, currentTimestamp) {
98
- if (!this.idleMsecs) return false
99
- return (currentTimestamp - c.timestamp) >= this.idleMsecs
100
- }
101
-
102
- isActive(c, currentTimestamp) {
103
- if (!this.idleMsecs) return true
104
- return (currentTimestamp - c.timestamp) < this.idleMsecs
105
- }
106
-
107
- isPurge(c, currentTimestamp) {
108
- if (!this.purgeMsecs) return false
109
- return (currentTimestamp - c.timestamp) >= this.purgeMsecs
110
- }
111
-
112
- updateState(currentTimestamp) {
113
- const sweeper = participant => {
114
- if (participant.state === ActiveState) {
115
- if (this.isIdle(participant, currentTimestamp)) {
116
- participant.state = IdleState
117
- }
118
- } else if (participant.state === IdleState) {
119
- if (this.isPurge(participant, currentTimestamp)) {
120
- this.purge(participant)
121
- }
122
- }
123
- }
124
-
125
- this.ids.forEach(
126
- k => {
127
- const c = this._participants[k]
128
- sweeper(c)
129
- }
130
- )
131
- }
132
-
133
- add(anearParticipant, withTimestamp = this.currentTimestamp) {
134
- if (anearParticipant.isHost() && this.anearEvent.hosted) {
135
- // the host is not an eligible participant and isn't active nor idle
136
- this.host = anearParticipant
137
- } else {
138
- this._participants[anearParticipant.id] = anearParticipant
139
- this.markActive(anearParticipant, withTimestamp)
140
- }
141
- return anearParticipant
142
- }
143
-
144
- markActive(anearParticipant, withTimestamp = this.currentTimestamp) {
145
- anearParticipant.timestamp = withTimestamp
146
- anearParticipant.state = ActiveState
147
- }
148
-
149
- purge(participant) {
150
- if (!participant) return
151
-
152
- const {id} = participant
153
-
154
- if (this.host && (id === this.host.id)) {
155
- this.host = null
156
- } else {
157
- if (this._participants[id]) {
158
- delete this._participants[id]
159
- }
160
- }
161
- }
162
-
163
- get currentTimestamp() {
164
- return new Date().getTime()
165
- }
166
-
167
- load(anearParticipants) {
168
- // used for tests only
169
- anearParticipants.forEach(
170
- p => {
171
- if (p.isHost() && this.anearEvent.hosted) {
172
- this.host = p
173
- } else {
174
- this._participants[p.id] = p
175
- }
176
- }
177
- )
178
- }
179
- }
180
-
181
- module.exports = Participants