@onehat/data 1.13.6 → 1.14.0

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.
@@ -358,6 +358,22 @@ describe('OneHatData', function() {
358
358
  })();
359
359
  });
360
360
 
361
+ it('setIsOnline', function() {
362
+ (async function() {
363
+ await beforeEach();
364
+
365
+ const oneHatData = this.oneHatData;
366
+
367
+ oneHatData.setIsOnline(true);
368
+ expect(oneHatData.isOnline).to.be.true;
369
+
370
+ oneHatData.setIsOnline(false);
371
+ expect(oneHatData.isOnline).to.be.false;
372
+
373
+ afterEach();
374
+ })();
375
+ });
376
+
361
377
  it('isEntity', async function() {
362
378
  (async function() {
363
379
  await beforeEach();
@@ -112,7 +112,7 @@ describe('OneBuildRepository', function() {
112
112
  expect(_.size(this.repository.entities)).to.be.eq(1);
113
113
  });
114
114
 
115
- it.only('sortInMemory', function() {
115
+ it('sortInMemory', function() {
116
116
 
117
117
  const repository = this.repository;
118
118
 
@@ -138,6 +138,17 @@ describe('OneBuildRepository', function() {
138
138
  expect(entities[3].key).to.be.eq(4);
139
139
  expect(entities[4].key).to.be.eq(5);
140
140
  });
141
+
142
+ it('setIsOnline', function() {
143
+
144
+ const repository = this.repository;
145
+
146
+ repository.setIsOnline(true);
147
+ expect(repository.isOnline).to.be.true;
148
+
149
+ repository.setIsOnline(false);
150
+ expect(repository.isOnline).to.be.false;
151
+ });
141
152
 
142
153
  });
143
154
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.13.6",
3
+ "version": "1.14.0",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/OneHatData.js CHANGED
@@ -67,6 +67,14 @@ export class OneHatData extends EventEmitter {
67
67
  */
68
68
  this.isDestroyed = false;
69
69
 
70
+ /**
71
+ * @member {boolean} isOnline - Whether the remote Internet connection is active.
72
+ * This must be managed by outside software, calling setIsOnline at appropriate times.
73
+ * @private
74
+ */
75
+ this.isOnline = true;
76
+
77
+
70
78
  this.registerEvents([
71
79
  'createRepository',
72
80
  'deleteRepository',
@@ -605,6 +613,18 @@ export class OneHatData extends EventEmitter {
605
613
  return this.repositories && this.repositories.hasOwnProperty(id);
606
614
  }
607
615
 
616
+ /**
617
+ * Sets isOnline for all remote repositories.
618
+ * Remote repositories won't submit queries if !isOnline
619
+ */
620
+ setIsOnline = (isOnline) => {
621
+ this.isOnline = !!isOnline;
622
+ const remoteRepositories = oneHatData.getRepositoriesBy((repository) => !!repository.setIsOnline);
623
+ _.each(remoteRepositories, (repository) => {
624
+ repository.setIsOnline(isOnline);
625
+ });
626
+ }
627
+
608
628
 
609
629
 
610
630
  // ____ __ __
@@ -93,6 +93,13 @@ class AjaxRepository extends Repository {
93
93
  * @member {object} _baseParams - Params that will be applied to every request
94
94
  */
95
95
  _baseParams: {},
96
+
97
+ /**
98
+ * @member {boolean} isOnline - Whether the remote storage medium is available.
99
+ * This must be managed by outside software, calling setIsOnline at appropriate times.
100
+ * @private
101
+ */
102
+ isOnline: true,
96
103
 
97
104
  };
98
105
  _.merge(this, defaults, config);
@@ -800,6 +807,11 @@ class AjaxRepository extends Repository {
800
807
  return;
801
808
  }
802
809
 
810
+ if (!this.isOnline) {
811
+ this.throwError('Offline');
812
+ return;
813
+ }
814
+
803
815
  const options = {
804
816
  url,
805
817
  method,
@@ -884,6 +896,10 @@ class AjaxRepository extends Repository {
884
896
  }));
885
897
  }
886
898
 
899
+ setIsOnline = (isOnline) => {
900
+ this.isOnline = !!isOnline; // force convert type to boolean
901
+ }
902
+
887
903
  }
888
904
 
889
905
  AjaxRepository.className = 'Ajax';
@@ -99,6 +99,11 @@ class OneBuildRepository extends AjaxRepository {
99
99
  return;
100
100
  }
101
101
 
102
+ if (!this.isOnline) {
103
+ this.throwError('Offline');
104
+ return;
105
+ }
106
+
102
107
  const options = {
103
108
  url,
104
109
  method,
@@ -242,6 +247,12 @@ class OneBuildRepository extends AjaxRepository {
242
247
  * @return {Promise}
243
248
  */
244
249
  reorder = (dragRecord, dropRecord, dropPosition) => {
250
+
251
+ if (!this.isOnline) {
252
+ this.throwError('Offline');
253
+ return;
254
+ }
255
+
245
256
  const data = {
246
257
  url: 'reorder',
247
258
  data: qs.stringify({
@@ -284,6 +295,11 @@ class OneBuildRepository extends AjaxRepository {
284
295
  */
285
296
  login = (creds) => {
286
297
 
298
+ if (!this.isOnline) {
299
+ this.throwError('Offline');
300
+ return;
301
+ }
302
+
287
303
  const data = {
288
304
  url: 'apiLogin',
289
305
  data: qs.stringify(creds),
@@ -317,9 +333,16 @@ class OneBuildRepository extends AjaxRepository {
317
333
  * @return {Promise}
318
334
  */
319
335
  logout = () => {
336
+
337
+ if (!this.isOnline) {
338
+ this.throwError('Offline');
339
+ return;
340
+ }
341
+
320
342
  if (this.debugMode) {
321
343
  console.log('logout');
322
344
  }
345
+
323
346
  return this.axios({
324
347
  url: 'Users/apiLogout',
325
348
  method: 'POST',
@@ -341,6 +364,12 @@ class OneBuildRepository extends AjaxRepository {
341
364
  }
342
365
 
343
366
  forgotPassword = (email = null, username = null) => {
367
+
368
+ if (!this.isOnline) {
369
+ this.throwError('Offline');
370
+ return;
371
+ }
372
+
344
373
  const data = {
345
374
  url: 'forgotPassword',
346
375
  data: qs.stringify({