react-native-mosquito-transport 0.0.39 → 0.0.40

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/TODO CHANGED
@@ -22,4 +22,5 @@
22
22
  - serverTimeOffset
23
23
  - avoid sending cookies ✅
24
24
  - add timeout in fetchHttp
25
+ - transform undefined to void instead of null
25
26
  <!-- - error: "refreshToken retry limit exceeded" <--- no need -->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-mosquito-transport",
3
- "version": "0.0.39",
3
+ "version": "0.0.40",
4
4
  "description": "React native javascript sdk for mosquito-transport (https://github.com/brainbehindx/mosquito-transport)",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.d.ts CHANGED
@@ -230,6 +230,7 @@ interface RNMTCollection {
230
230
  });
231
231
  count: (config?: CountConfig) => Promise<number>;
232
232
  get: (config?: GetConfig) => Promise<DocumentResult[]>;
233
+ // TODO: change GetConfig for this listen
233
234
  listen: (callback: (snapshot?: DocumentResult[]) => void, onError?: (error?: DocumentError) => void, config?: GetConfig) => void;
234
235
  findOne: (findOne?: DocumentFind) => ({
235
236
  get: (config?: GetConfig) => Promise<DocumentResult>;
@@ -380,6 +381,12 @@ interface GetConfig {
380
381
  * To learn and see more examples on this, Please visit https://brainbehindx.com/mosquito-transport/docs/reading_data/retrieval
381
382
  */
382
383
  disableMinimizer?: boolean;
384
+ onWaiting: (intruder: WaitingIntruder) => void | Promise<void>;
385
+ }
386
+
387
+ interface WaitingIntruder {
388
+ resolve?: undefined | ((data: any) => void);
389
+ reject?: undefined | ((err: any) => void);
383
390
  }
384
391
 
385
392
  interface GetConfigExtraction {
@@ -455,15 +455,17 @@ const transformBSON = (d, castBSON) => {
455
455
  return basicClone(d);
456
456
  };
457
457
 
458
- const findObject = async (builder, config) => {
458
+ const findObject = async (builder, initConfig) => {
459
459
  builder = basicClone(builder);
460
- config = basicClone(config);
461
460
  const { projectUrl, serverE2E_PublicKey, dbUrl, dbName, maxRetries = 1, path, disableCache = false, uglify, extraHeaders, command, castBSON } = builder;
462
- const pureConfig = stripRequestConfig(config);
461
+
462
+ const pureConfig = stripRequestConfig(initConfig);
463
463
  validateFindObject(command);
464
- validateFindConfig(config);
464
+ validateFindConfig(initConfig);
465
465
  validateCollectionName(path);
466
466
 
467
+ let { onWaiting, ...config } = basicClone(initConfig) || {};
468
+
467
469
  const { find, findOne, sort, direction, limit, random } = command;
468
470
  const { retrieval = RETRIEVAL.DEFAULT, episode = 0, disableAuth, disableMinimizer } = config || {};
469
471
  const enableMinimizer = !disableMinimizer;
@@ -475,6 +477,7 @@ const findObject = async (builder, config) => {
475
477
 
476
478
  await awaitStore();
477
479
 
480
+ let intruder = {};
478
481
  let retries = 0, hasFinalize;
479
482
 
480
483
  const readValue = () => new Promise(async (resolve, reject) => {
@@ -482,11 +485,12 @@ const findObject = async (builder, config) => {
482
485
  instantProcess = retryProcess === 1;
483
486
 
484
487
  const finalize = (a, b) => {
485
- const res = (instantProcess && a) ? transformBSON(a[0] || undefined, castBSON) : a;
488
+ const res = (instantProcess && a) ? intruder ? transformBSON(a[0] || undefined, castBSON) : a[0] : a;
489
+ const doClone = v => intruder ? basicClone(v) : v;
486
490
 
487
491
  if (a) {
488
- resolve(instantProcess ? basicClone(res) : a);
489
- } else reject(instantProcess ? basicClone(b) : b);
492
+ resolve(instantProcess ? doClone(res) : a);
493
+ } else reject(instantProcess ? doClone(b) : b);
490
494
  if (hasFinalize || !instantProcess) return;
491
495
  hasFinalize = true;
492
496
 
@@ -497,7 +501,7 @@ const findObject = async (builder, config) => {
497
501
  delete Scoped.PendingDbReadCollective[processAccessId];
498
502
 
499
503
  resolutionList.forEach(e => {
500
- e(a ? { result: res } : undefined, b);
504
+ e(a ? { result: doClone(res) } : undefined, doClone(b));
501
505
  });
502
506
  }
503
507
  };
@@ -507,8 +511,8 @@ const findObject = async (builder, config) => {
507
511
  if (enableMinimizer) {
508
512
  if (Scoped.PendingDbReadCollective[processAccessId]) {
509
513
  Scoped.PendingDbReadCollective[processAccessId].push((a, b) => {
510
- if (a) resolve(basicClone(a.result));
511
- else reject(basicClone(b));
514
+ if (a) resolve(a.result);
515
+ else reject(b);
512
516
  });
513
517
  return;
514
518
  }
@@ -585,6 +589,8 @@ const findObject = async (builder, config) => {
585
589
  } else {
586
590
  const onlineListener = listenReachableServer(connected => {
587
591
  if (connected) {
592
+ intruder.resolve = undefined;
593
+ intruder.reject = undefined;
588
594
  onlineListener();
589
595
  readValue().then(
590
596
  e => { finalize(e); },
@@ -592,6 +598,22 @@ const findObject = async (builder, config) => {
592
598
  );
593
599
  }
594
600
  }, projectUrl);
601
+
602
+ const cleanseIntruder = () => {
603
+ onlineListener?.();
604
+ intruder = undefined;
605
+ }
606
+
607
+ intruder.resolve = (data) => {
608
+ cleanseIntruder();
609
+ finalize([data]);
610
+ };
611
+ intruder.reject = (err) => {
612
+ cleanseIntruder();
613
+ finalize(undefined, err);
614
+ };
615
+ onWaiting?.(intruder);
616
+ onWaiting = undefined;
595
617
  }
596
618
  }
597
619
  });
@@ -31,7 +31,7 @@ const FindConfig = {
31
31
  ).length,
32
32
  returnOnly: ReturnAndExcludeFootprint,
33
33
  excludeFields: ReturnAndExcludeFootprint,
34
-
34
+ onWaiting: t => t === undefined || typeof t === 'function',
35
35
  episode: t => [undefined, 0, 1].includes(t),
36
36
  retrieval: t => t === undefined || Object.values(RETRIEVAL).includes(t),
37
37
  disableAuth: t => t === undefined || typeof t === 'boolean',