@ouroboros/mouth-mui 1.2.0 → 1.2.1

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": "@ouroboros/mouth-mui",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Components for use with the Mouth service",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -26,7 +26,6 @@
26
26
  "url": "https://github.com/ouroboroscoding/mouth-mui/issues"
27
27
  },
28
28
  "devDependencies": {
29
- "@ouroboros/body": "^1.0.2",
30
29
  "@types/node": "^18.14.0",
31
30
  "@types/react": "^18.0.28",
32
31
  "@types/uuid": "^9.0.0",
@@ -47,9 +46,10 @@
47
46
  "@emotion/react": "^11.8.2",
48
47
  "@emotion/styled": "^11.8.1",
49
48
  "@mui/material": "^5.5.1",
50
- "@ouroboros/body": "^1.0.2",
51
- "@ouroboros/brain-react": "^1.0.0",
52
- "@ouroboros/mouth": "^1.1.0",
49
+ "@ouroboros/body": "^1.0.3",
50
+ "@ouroboros/brain-react": "^1.0.2",
51
+ "@ouroboros/events": "^1.0.1",
52
+ "@ouroboros/mouth": "^1.1.1",
53
53
  "react": "^17.0.2",
54
54
  "react-dom": "^17.0.2"
55
55
  }
package/src/locales.d.ts CHANGED
@@ -22,7 +22,8 @@ export type Options = Record<string, Option[]>;
22
22
  * @extends Subscribe
23
23
  */
24
24
  declare class Locales extends Subscribe {
25
- private running;
25
+ private failed;
26
+ private fetching;
26
27
  /**
27
28
  * Constructor
28
29
  *
@@ -58,6 +59,17 @@ declare class Locales extends Subscribe {
58
59
  * text
59
60
  */
60
61
  static objectSort(records: Record<string, Record<string, string>>): Options;
62
+ /**
63
+ * Fetch
64
+ *
65
+ * Private method to fetch the locales from the server which loops if
66
+ * there's network issues
67
+ *
68
+ * @name fetch
69
+ * @access private
70
+ * @returns void
71
+ */
72
+ private fetch;
61
73
  /**
62
74
  * Subscribe
63
75
  *
@@ -65,6 +77,9 @@ declare class Locales extends Subscribe {
65
77
  *
66
78
  * @name subscribe
67
79
  * @access public
80
+ * @param callback The function to call when locales change
81
+ * @returns an object with the current available `data` and `unsubscribe`
82
+ * to remove the callback from the list when we're done
68
83
  */
69
84
  subscribe(callback: SubscribeCallback): SubscribeReturn;
70
85
  }
package/src/locales.js CHANGED
@@ -8,6 +8,7 @@
8
8
  * @created 2021-03-17
9
9
  */
10
10
  // Ouroboros modules
11
+ import events from '@ouroboros/events';
11
12
  import Subscribe from '@ouroboros/subscribe';
12
13
  // Local modules
13
14
  import mouth from '@ouroboros/mouth';
@@ -20,8 +21,10 @@ import mouth from '@ouroboros/mouth';
20
21
  * @extends Subscribe
21
22
  */
22
23
  class Locales extends Subscribe {
24
+ // Keep track of failed attempts
25
+ failed;
23
26
  // Keeps the state of if the rest request is taking place
24
- running;
27
+ fetching;
25
28
  /**
26
29
  * Constructor
27
30
  *
@@ -35,7 +38,7 @@ class Locales extends Subscribe {
35
38
  // Call the Subscribe constructor with empty data
36
39
  super([]);
37
40
  // Init the running flag
38
- this.running = false;
41
+ this.fetching = false;
39
42
  }
40
43
  /**
41
44
  * Array Sort
@@ -122,6 +125,47 @@ class Locales extends Subscribe {
122
125
  // Return the new structure
123
126
  return oRet;
124
127
  }
128
+ /**
129
+ * Fetch
130
+ *
131
+ * Private method to fetch the locales from the server which loops if
132
+ * there's network issues
133
+ *
134
+ * @name fetch
135
+ * @access private
136
+ * @returns void
137
+ */
138
+ fetch() {
139
+ // Mark us as running
140
+ this.fetching = true;
141
+ // Fetch the data from the server
142
+ mouth.read('locales').then((list) => {
143
+ // If there's data
144
+ if (list) {
145
+ // Trigger all callbacks
146
+ this.set(list);
147
+ }
148
+ // Finish running
149
+ this.fetching = false;
150
+ // Reset the count
151
+ this.failed = 0;
152
+ }, error => {
153
+ // If we haven't hit the limit
154
+ if (this.failed < 3) {
155
+ // Increase the failed count
156
+ ++this.failed;
157
+ // Fetch again
158
+ this.fetch();
159
+ }
160
+ // Else, we can't keep trying, notify the user
161
+ else {
162
+ events.get('error').trigger(error);
163
+ }
164
+ }).finally(() => {
165
+ // Regardless of the outcome, we're done fetching
166
+ this.fetching = false;
167
+ });
168
+ }
125
169
  /**
126
170
  * Subscribe
127
171
  *
@@ -129,24 +173,17 @@ class Locales extends Subscribe {
129
173
  *
130
174
  * @name subscribe
131
175
  * @access public
176
+ * @param callback The function to call when locales change
177
+ * @returns an object with the current available `data` and `unsubscribe`
178
+ * to remove the callback from the list when we're done
132
179
  */
133
180
  subscribe(callback) {
134
181
  // Call the Subscribe subscribe
135
182
  const oReturn = super.subscribe(callback);
136
183
  // If we don't have a value yet and it's not running
137
- if (oReturn.data.length === 0 && this.running === false) {
138
- // Mark us as running
139
- this.running = true;
140
- // Fetch the data from the server
141
- mouth.read('locales').then((list) => {
142
- // If there's data
143
- if (list) {
144
- // Trigger all callbacks
145
- this.set(list);
146
- }
147
- // Finish running
148
- this.running = false;
149
- });
184
+ if (oReturn.data.length === 0 && this.fetching === false) {
185
+ // Fetch the data
186
+ this.fetch();
150
187
  // Overwrite the data
151
188
  oReturn.data = [];
152
189
  }
package/src/locales.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  */
10
10
 
11
11
  // Ouroboros modules
12
+ import events from '@ouroboros/events';
12
13
  import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
13
14
 
14
15
  // Local modules
@@ -31,8 +32,11 @@ export type Options = Record<string, Option[]>;
31
32
  */
32
33
  class Locales extends Subscribe {
33
34
 
35
+ // Keep track of failed attempts
36
+ private failed: number;
37
+
34
38
  // Keeps the state of if the rest request is taking place
35
- private running: boolean;
39
+ private fetching: boolean;
36
40
 
37
41
  /**
38
42
  * Constructor
@@ -49,7 +53,7 @@ class Locales extends Subscribe {
49
53
  super([]);
50
54
 
51
55
  // Init the running flag
52
- this.running = false;
56
+ this.fetching = false;
53
57
  }
54
58
 
55
59
  /**
@@ -151,6 +155,61 @@ class Locales extends Subscribe {
151
155
  return oRet;
152
156
  }
153
157
 
158
+ /**
159
+ * Fetch
160
+ *
161
+ * Private method to fetch the locales from the server which loops if
162
+ * there's network issues
163
+ *
164
+ * @name fetch
165
+ * @access private
166
+ * @returns void
167
+ */
168
+ private fetch(): void {
169
+
170
+ // Mark us as running
171
+ this.fetching = true;
172
+
173
+ // Fetch the data from the server
174
+ mouth.read('locales').then((list: any) => {
175
+
176
+ // If there's data
177
+ if(list) {
178
+
179
+ // Trigger all callbacks
180
+ this.set(list);
181
+ }
182
+
183
+ // Finish running
184
+ this.fetching = false;
185
+
186
+ // Reset the count
187
+ this.failed = 0;
188
+
189
+ }, error => {
190
+
191
+ // If we haven't hit the limit
192
+ if(this.failed < 3) {
193
+
194
+ // Increase the failed count
195
+ ++this.failed;
196
+
197
+ // Fetch again
198
+ this.fetch();
199
+ }
200
+
201
+ // Else, we can't keep trying, notify the user
202
+ else {
203
+ events.get('error').trigger(error);
204
+ }
205
+
206
+ }).finally(() => {
207
+
208
+ // Regardless of the outcome, we're done fetching
209
+ this.fetching = false;
210
+ })
211
+ }
212
+
154
213
  /**
155
214
  * Subscribe
156
215
  *
@@ -158,6 +217,9 @@ class Locales extends Subscribe {
158
217
  *
159
218
  * @name subscribe
160
219
  * @access public
220
+ * @param callback The function to call when locales change
221
+ * @returns an object with the current available `data` and `unsubscribe`
222
+ * to remove the callback from the list when we're done
161
223
  */
162
224
  subscribe(callback: SubscribeCallback): SubscribeReturn {
163
225
 
@@ -165,24 +227,10 @@ class Locales extends Subscribe {
165
227
  const oReturn = super.subscribe(callback);
166
228
 
167
229
  // If we don't have a value yet and it's not running
168
- if(oReturn.data.length === 0 && this.running === false) {
169
-
170
- // Mark us as running
171
- this.running = true;
172
-
173
- // Fetch the data from the server
174
- mouth.read('locales').then((list: any) => {
175
-
176
- // If there's data
177
- if(list) {
230
+ if(oReturn.data.length === 0 && this.fetching === false) {
178
231
 
179
- // Trigger all callbacks
180
- this.set(list);
181
- }
182
-
183
- // Finish running
184
- this.running = false;
185
- });
232
+ // Fetch the data
233
+ this.fetch();
186
234
 
187
235
  // Overwrite the data
188
236
  oReturn.data = [];