@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
@@ -1620,9 +1620,14 @@ class MessageReceiver {
1620
1620
  // Apply update
1621
1621
  const syncMessageType = readSyncMessage(message.decoder, message.encoder, provider.document, provider);
1622
1622
  // Synced once we receive Step2
1623
- if (emitSynced && syncMessageType === messageYjsSyncStep2) {
1623
+ if (emitSynced && (syncMessageType === messageYjsSyncStep2)) {
1624
1624
  provider.synced = true;
1625
1625
  }
1626
+ if (syncMessageType === messageYjsUpdate || syncMessageType === messageYjsSyncStep2) {
1627
+ if (provider.unsyncedChanges > 0) {
1628
+ provider.unsyncedChanges -= 1;
1629
+ }
1630
+ }
1626
1631
  }
1627
1632
  applyAwarenessMessage(provider) {
1628
1633
  const { message } = this;
@@ -1808,6 +1813,7 @@ class HocuspocusProvider extends EventEmitter {
1808
1813
  this.shouldConnect = true;
1809
1814
  this.status = exports.WebSocketStatus.Disconnected;
1810
1815
  this.isSynced = false;
1816
+ this.unsyncedChanges = 0;
1811
1817
  this.isAuthenticated = false;
1812
1818
  this.lastMessageReceived = 0;
1813
1819
  this.mux = createMutex();
@@ -1816,6 +1822,9 @@ class HocuspocusProvider extends EventEmitter {
1816
1822
  connectionChecker: null,
1817
1823
  };
1818
1824
  this.connectionAttempt = null;
1825
+ this.boundConnect = this.connect.bind(this);
1826
+ this.boundBeforeUnload = this.beforeUnload.bind(this);
1827
+ this.boundBroadcastChannelSubscriber = this.broadcastChannelSubscriber.bind(this);
1819
1828
  this.setConfiguration(configuration);
1820
1829
  this.configuration.document = configuration.document ? configuration.document : new Y__namespace.Doc();
1821
1830
  this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
@@ -1861,6 +1870,7 @@ class HocuspocusProvider extends EventEmitter {
1861
1870
  if (this.status === exports.WebSocketStatus.Connected) {
1862
1871
  return;
1863
1872
  }
1873
+ this.unsyncedChanges = 0; // set to 0 in case we got reconnected
1864
1874
  this.shouldConnect = true;
1865
1875
  this.subscribeToBroadcastChannel();
1866
1876
  try {
@@ -1933,6 +1943,9 @@ class HocuspocusProvider extends EventEmitter {
1933
1943
  get awareness() {
1934
1944
  return this.configuration.awareness;
1935
1945
  }
1946
+ get hasUnsyncedChanges() {
1947
+ return this.unsyncedChanges > 0;
1948
+ }
1936
1949
  checkConnection() {
1937
1950
  var _a;
1938
1951
  // Don’t check the connection when it’s not even established
@@ -1957,19 +1970,21 @@ class HocuspocusProvider extends EventEmitter {
1957
1970
  }
1958
1971
  this.send(SyncStepOneMessage, { document: this.document });
1959
1972
  }
1973
+ beforeUnload() {
1974
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
1975
+ }
1960
1976
  registerEventListeners() {
1961
1977
  if (typeof window === 'undefined') {
1962
1978
  return;
1963
1979
  }
1964
- window.addEventListener('online', this.connect.bind(this));
1965
- window.addEventListener('beforeunload', () => {
1966
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
1967
- });
1980
+ window.addEventListener('online', this.boundConnect);
1981
+ window.addEventListener('beforeunload', this.boundBeforeUnload);
1968
1982
  }
1969
1983
  documentUpdateHandler(update, origin) {
1970
1984
  if (origin === this) {
1971
1985
  return;
1972
1986
  }
1987
+ this.unsyncedChanges += 1;
1973
1988
  this.send(UpdateMessage, { update }, true);
1974
1989
  }
1975
1990
  awarenessUpdateHandler({ added, updated, removed }, origin) {
@@ -2133,7 +2148,8 @@ class HocuspocusProvider extends EventEmitter {
2133
2148
  if (typeof window === 'undefined') {
2134
2149
  return;
2135
2150
  }
2136
- window.removeEventListener('online', this.connect.bind(this));
2151
+ window.removeEventListener('online', this.boundConnect);
2152
+ window.removeEventListener('beforeunload', this.boundBeforeUnload);
2137
2153
  }
2138
2154
  get broadcastChannel() {
2139
2155
  return `${this.serverUrl}/${this.configuration.name}`;
@@ -2148,7 +2164,7 @@ class HocuspocusProvider extends EventEmitter {
2148
2164
  }
2149
2165
  subscribeToBroadcastChannel() {
2150
2166
  if (!this.subscribedToBroadcastChannel) {
2151
- subscribe(this.broadcastChannel, this.broadcastChannelSubscriber.bind(this));
2167
+ subscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2152
2168
  this.subscribedToBroadcastChannel = true;
2153
2169
  }
2154
2170
  this.mux(() => {
@@ -2166,7 +2182,7 @@ class HocuspocusProvider extends EventEmitter {
2166
2182
  states: new Map(),
2167
2183
  }, true);
2168
2184
  if (this.subscribedToBroadcastChannel) {
2169
- unsubscribe(this.broadcastChannel, this.broadcastChannelSubscriber.bind(this));
2185
+ unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2170
2186
  this.subscribedToBroadcastChannel = false;
2171
2187
  }
2172
2188
  }