mosquito-transport-js 0.5.2 → 0.5.3

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": "mosquito-transport-js",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Javascript web sdk for mosquito-transport (https://github.com/brainbehindx/mosquito-transport)",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -32,10 +32,8 @@ broadcaster.onmessage = async (e) => {
32
32
  };
33
33
  result.value = deserializeBSON(result.value, false)._;
34
34
 
35
- console.log('newBuilder:', newBuilder, ' result:', result);
36
35
  const { editions, pathChanges } = await syncCache(newBuilder, result);
37
36
  Scoped.broadcastedWrites[writeId] = { editions, builder: newBuilder };
38
- console.log('pathChanges:', pathChanges);
39
37
  notifyDatabaseNodeChanges(newBuilder, pathChanges);
40
38
  } else if (kind === 'database-revert') {
41
39
  // only if cache protocol is in-memory
package/src/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { BSON } from "bson";
2
+
1
3
  interface MTConfig {
2
4
  dbName?: string;
3
5
  dbUrl?: string;
@@ -75,6 +77,8 @@ export class DoNotEncrypt {
75
77
  value: any;
76
78
  }
77
79
 
80
+ export { BSON }
81
+
78
82
  interface auth_provider_id {
79
83
  GOOGLE: 'google';
80
84
  FACEBOOK: 'facebook';
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ import sendMessage from "./helpers/broadcaster";
15
15
  import cloneDeep from 'lodash/cloneDeep';
16
16
  import { Buffer } from 'buffer';
17
17
  import MTAuth, { purgePendingToken } from './products/auth';
18
+ import { BSON } from "bson";
18
19
 
19
20
  const {
20
21
  _listenCollection,
@@ -527,5 +528,6 @@ export {
527
528
  DOCUMENT_EXTRACTION,
528
529
  FIND_GEO_JSON,
529
530
  GEO_JSON,
530
- AUTH_PROVIDER_ID
531
+ AUTH_PROVIDER_ID,
532
+ BSON
531
533
  };
@@ -19,7 +19,7 @@ export const listenQueryEntry = (callback, { accessId, builder, config, processI
19
19
  const { projectUrl, dbName, dbUrl, path } = builder;
20
20
  const { episode = 0 } = config || {};
21
21
 
22
- const nodeID = `${projectUrl}${dbName}${dbUrl}${path}`;
22
+ const nodeID = `${projectUrl}_${dbName}_${dbUrl}_${path}`;
23
23
 
24
24
  if (!Scoped.ActiveDatabaseListeners[nodeID])
25
25
  Scoped.ActiveDatabaseListeners[nodeID] = {};
@@ -794,7 +794,7 @@ export const notifyDatabaseNodeChanges = (builder, changedCollections = []) => {
794
794
  const { projectUrl, dbName, dbUrl } = builder;
795
795
 
796
796
  changedCollections.forEach(path => {
797
- const nodeID = `${projectUrl}${dbName}${dbUrl}${path}`;
797
+ const nodeID = `${projectUrl}_${dbName}_${dbUrl}_${path}`;
798
798
  Object.entries(Scoped.ActiveDatabaseListeners[nodeID] || {})
799
799
  .sort((a, b) => a[1] - b[1])
800
800
  .forEach(([processId]) => {
@@ -721,43 +721,47 @@ const commitData = async (builder, value, type, config) => {
721
721
  return (await sendValue());
722
722
  };
723
723
 
724
- export const trySendPendingWrite = (projectUrl) => {
724
+ export const trySendPendingWrite = async (projectUrl) => {
725
725
  if (Scoped.dispatchingWritesPromise[projectUrl]) return;
726
726
 
727
+ let resolveCallback;
727
728
  Scoped.dispatchingWritesPromise[projectUrl] = new Promise(async resolve => {
728
- const sortedWrite = Object.entries(CacheStore.PendingWrites[projectUrl] || {})
729
- .filter(([k]) => !Scoped.OutgoingWrites[k])
730
- .sort((a, b) => a[1].addedOn - b[1].addedOn);
731
- let resolveCounts = 0;
732
-
733
- for (const [writeId, { snapshot, builder, attempts = 1 }] of sortedWrite) {
734
- try {
735
- await commitData(
736
- { ...Scoped.InitializedProject[projectUrl], ...builder, find: deserializeBSON(builder.find, true)._ },
737
- deserializeBSON(snapshot.value, true)._,
738
- snapshot.type,
739
- { ...snapshot.config, delivery: DELIVERY.NO_CACHE_NO_AWAIT }
740
- );
729
+ resolveCallback = resolve;
730
+ });
731
+
732
+ const sortedWrite = Object.entries(CacheStore.PendingWrites[projectUrl] || {})
733
+ .filter(([k]) => !Scoped.OutgoingWrites[k])
734
+ .sort((a, b) => a[1].addedOn - b[1].addedOn);
735
+ let resolveCounts = 0;
741
736
 
737
+ for (const [writeId, { snapshot, builder, attempts = 1 }] of sortedWrite) {
738
+ try {
739
+ await commitData(
740
+ { ...Scoped.InitializedProject[projectUrl], ...builder, find: deserializeBSON(builder.find, true)._ },
741
+ deserializeBSON(snapshot.value, true)._,
742
+ snapshot.type,
743
+ { ...snapshot.config, delivery: DELIVERY.NO_CACHE_NO_AWAIT }
744
+ );
745
+
746
+ delete CacheStore.PendingWrites[projectUrl][writeId];
747
+ ++resolveCounts;
748
+ } catch (err) {
749
+ const { maxRetries } = builder;
750
+ if (err?.ack || !maxRetries || attempts >= maxRetries) {
742
751
  delete CacheStore.PendingWrites[projectUrl][writeId];
743
752
  ++resolveCounts;
744
- } catch (err) {
745
- const { maxRetries } = builder;
746
- if (err?.ack || !maxRetries || attempts >= maxRetries) {
747
- delete CacheStore.PendingWrites[projectUrl][writeId];
748
- ++resolveCounts;
749
- } else if (CacheStore.PendingWrites[projectUrl]?.[writeId]) {
750
- CacheStore.PendingWrites[projectUrl][writeId].attempts = attempts + 1;
751
- }
753
+ } else if (CacheStore.PendingWrites[projectUrl]?.[writeId]) {
754
+ CacheStore.PendingWrites[projectUrl][writeId].attempts = attempts + 1;
752
755
  }
753
756
  }
754
- resolve();
755
- Scoped.dispatchingWritesPromise[projectUrl] = undefined;
756
- updateCacheStore(['PendingWrites']);
757
-
758
- if (
759
- (sortedWrite.length - resolveCounts) &&
760
- await getReachableServer(projectUrl)
761
- ) trySendPendingWrite(projectUrl);
762
- });
757
+ }
758
+ resolveCallback();
759
+ if (Scoped.dispatchingWritesPromise[projectUrl])
760
+ delete Scoped.dispatchingWritesPromise[projectUrl];
761
+ updateCacheStore(['PendingWrites']);
762
+
763
+ if (
764
+ (sortedWrite.length - resolveCounts) &&
765
+ await getReachableServer(projectUrl)
766
+ ) trySendPendingWrite(projectUrl);
763
767
  };