@hocuspocus/provider 1.0.0-alpha.35 → 1.0.0-alpha.38

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.
Files changed (29) hide show
  1. package/dist/hocuspocus-provider.cjs +24 -8
  2. package/dist/hocuspocus-provider.cjs.map +1 -1
  3. package/dist/hocuspocus-provider.esm.js +24 -8
  4. package/dist/hocuspocus-provider.esm.js.map +1 -1
  5. package/dist/packages/extension-database/src/Database.d.ts +3 -5
  6. package/dist/packages/extension-monitor/src/Collector.d.ts +2 -1
  7. package/dist/packages/extension-redis/src/Redis.d.ts +91 -9
  8. package/dist/packages/extension-redis/src/index.d.ts +0 -1
  9. package/dist/packages/provider/src/HocuspocusProvider.d.ts +6 -0
  10. package/dist/packages/server/src/Document.d.ts +1 -1
  11. package/dist/packages/server/src/Hocuspocus.d.ts +6 -2
  12. package/dist/packages/server/src/MessageReceiver.d.ts +1 -1
  13. package/dist/packages/server/src/OutgoingMessage.d.ts +1 -0
  14. package/dist/packages/server/src/index.d.ts +3 -4
  15. package/dist/packages/server/src/types.d.ts +26 -6
  16. package/dist/tests/{extension-redis-rewrite/closeConnections.d.ts → extension-database/fetch.d.ts} +0 -0
  17. package/dist/tests/{extension-redis-rewrite/getConnectionCount.d.ts → extension-redis/closeConnections.d.ts} +0 -0
  18. package/dist/tests/{extension-redis-rewrite/getDocumentsCount.d.ts → extension-redis/getConnectionCount.d.ts} +0 -0
  19. package/dist/tests/{extension-redis-rewrite/onAwarenessChange.d.ts → extension-redis/getDocumentsCount.d.ts} +0 -0
  20. package/dist/tests/{extension-redis-rewrite/onChange.d.ts → extension-redis/onAwarenessChange.d.ts} +0 -0
  21. package/dist/tests/{extension-redis-rewrite/onStoreDocument.d.ts → extension-redis/onChange.d.ts} +0 -0
  22. package/dist/tests/extension-redis/{onLoadDocument.d.ts → onStoreDocument.d.ts} +0 -0
  23. package/dist/tests/utils/index.d.ts +1 -0
  24. package/dist/tests/utils/randomInteger.d.ts +1 -0
  25. package/package.json +4 -4
  26. package/src/HocuspocusProvider.ts +24 -7
  27. package/src/MessageReceiver.ts +8 -2
  28. package/dist/packages/extension-redis/src/RedisCluster.d.ts +0 -4
  29. package/dist/tests/extension-redis/onSynced.d.ts +0 -1
@@ -1596,9 +1596,14 @@ class MessageReceiver {
1596
1596
  // Apply update
1597
1597
  const syncMessageType = readSyncMessage(message.decoder, message.encoder, provider.document, provider);
1598
1598
  // Synced once we receive Step2
1599
- if (emitSynced && syncMessageType === messageYjsSyncStep2) {
1599
+ if (emitSynced && (syncMessageType === messageYjsSyncStep2)) {
1600
1600
  provider.synced = true;
1601
1601
  }
1602
+ if (syncMessageType === messageYjsUpdate || syncMessageType === messageYjsSyncStep2) {
1603
+ if (provider.unsyncedChanges > 0) {
1604
+ provider.unsyncedChanges -= 1;
1605
+ }
1606
+ }
1602
1607
  }
1603
1608
  applyAwarenessMessage(provider) {
1604
1609
  const { message } = this;
@@ -1784,6 +1789,7 @@ class HocuspocusProvider extends EventEmitter {
1784
1789
  this.shouldConnect = true;
1785
1790
  this.status = WebSocketStatus.Disconnected;
1786
1791
  this.isSynced = false;
1792
+ this.unsyncedChanges = 0;
1787
1793
  this.isAuthenticated = false;
1788
1794
  this.lastMessageReceived = 0;
1789
1795
  this.mux = createMutex();
@@ -1792,6 +1798,9 @@ class HocuspocusProvider extends EventEmitter {
1792
1798
  connectionChecker: null,
1793
1799
  };
1794
1800
  this.connectionAttempt = null;
1801
+ this.boundConnect = this.connect.bind(this);
1802
+ this.boundBeforeUnload = this.beforeUnload.bind(this);
1803
+ this.boundBroadcastChannelSubscriber = this.broadcastChannelSubscriber.bind(this);
1795
1804
  this.setConfiguration(configuration);
1796
1805
  this.configuration.document = configuration.document ? configuration.document : new Y.Doc();
1797
1806
  this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
@@ -1837,6 +1846,7 @@ class HocuspocusProvider extends EventEmitter {
1837
1846
  if (this.status === WebSocketStatus.Connected) {
1838
1847
  return;
1839
1848
  }
1849
+ this.unsyncedChanges = 0; // set to 0 in case we got reconnected
1840
1850
  this.shouldConnect = true;
1841
1851
  this.subscribeToBroadcastChannel();
1842
1852
  try {
@@ -1909,6 +1919,9 @@ class HocuspocusProvider extends EventEmitter {
1909
1919
  get awareness() {
1910
1920
  return this.configuration.awareness;
1911
1921
  }
1922
+ get hasUnsyncedChanges() {
1923
+ return this.unsyncedChanges > 0;
1924
+ }
1912
1925
  checkConnection() {
1913
1926
  var _a;
1914
1927
  // Don’t check the connection when it’s not even established
@@ -1933,19 +1946,21 @@ class HocuspocusProvider extends EventEmitter {
1933
1946
  }
1934
1947
  this.send(SyncStepOneMessage, { document: this.document });
1935
1948
  }
1949
+ beforeUnload() {
1950
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
1951
+ }
1936
1952
  registerEventListeners() {
1937
1953
  if (typeof window === 'undefined') {
1938
1954
  return;
1939
1955
  }
1940
- window.addEventListener('online', this.connect.bind(this));
1941
- window.addEventListener('beforeunload', () => {
1942
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
1943
- });
1956
+ window.addEventListener('online', this.boundConnect);
1957
+ window.addEventListener('beforeunload', this.boundBeforeUnload);
1944
1958
  }
1945
1959
  documentUpdateHandler(update, origin) {
1946
1960
  if (origin === this) {
1947
1961
  return;
1948
1962
  }
1963
+ this.unsyncedChanges += 1;
1949
1964
  this.send(UpdateMessage, { update }, true);
1950
1965
  }
1951
1966
  awarenessUpdateHandler({ added, updated, removed }, origin) {
@@ -2109,7 +2124,8 @@ class HocuspocusProvider extends EventEmitter {
2109
2124
  if (typeof window === 'undefined') {
2110
2125
  return;
2111
2126
  }
2112
- window.removeEventListener('online', this.connect.bind(this));
2127
+ window.removeEventListener('online', this.boundConnect);
2128
+ window.removeEventListener('beforeunload', this.boundBeforeUnload);
2113
2129
  }
2114
2130
  get broadcastChannel() {
2115
2131
  return `${this.serverUrl}/${this.configuration.name}`;
@@ -2124,7 +2140,7 @@ class HocuspocusProvider extends EventEmitter {
2124
2140
  }
2125
2141
  subscribeToBroadcastChannel() {
2126
2142
  if (!this.subscribedToBroadcastChannel) {
2127
- subscribe(this.broadcastChannel, this.broadcastChannelSubscriber.bind(this));
2143
+ subscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2128
2144
  this.subscribedToBroadcastChannel = true;
2129
2145
  }
2130
2146
  this.mux(() => {
@@ -2142,7 +2158,7 @@ class HocuspocusProvider extends EventEmitter {
2142
2158
  states: new Map(),
2143
2159
  }, true);
2144
2160
  if (this.subscribedToBroadcastChannel) {
2145
- unsubscribe(this.broadcastChannel, this.broadcastChannelSubscriber.bind(this));
2161
+ unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2146
2162
  this.subscribedToBroadcastChannel = false;
2147
2163
  }
2148
2164
  }