Skip to content

Commit c2c7ea4

Browse files
authored
Added an interval to prevent browser tab hibernation while a replication is running (#6675)
1 parent 235494b commit c2c7ea4

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

‎docs-src/docs/releases/16.0.0.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ I completely rewrote them and improved performance especially on initial load wh
8787
- Lazily process bulkWrite() results for less CPU usage.
8888
- Only run interval cleanup on the storage of a collection if there actually have been writes to it.
8989
- Schema validation errors (code: 422) now include the `RxJsonSchema` for easier debugging.
90+
- Added an interval to prevent browser tab hibernation while a replication is running.
9091

9192
## You can help!
9293

‎src/plugins/replication/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ import {
5555
awaitRetry,
5656
DEFAULT_MODIFIER,
5757
swapDefaultDeletedTodeletedField,
58-
handlePulledDocuments
58+
handlePulledDocuments,
59+
preventHibernateBrowserTab
5960
} from './replication-helper.ts';
6061
import {
6162
addConnectedStorageToCollection, removeConnectedStorageFromCollection
@@ -155,6 +156,9 @@ export class RxReplicationState<RxDocType, CheckpointType> {
155156
return;
156157
}
157158

159+
preventHibernateBrowserTab(this);
160+
161+
158162
// fill in defaults for pull & push
159163
const pullModifier = this.pull && this.pull.modifier ? this.pull.modifier : DEFAULT_MODIFIER;
160164
const pushModifier = this.push && this.push.modifier ? this.push.modifier : DEFAULT_MODIFIER;

‎src/plugins/replication/replication-helper.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
} from '../../types/index.d.ts';
55
import { flatClone } from '../../plugins/utils/index.ts';
66
import { getComposedPrimaryKeyOfDocumentData } from '../../rx-schema-helper.ts';
7+
import type { RxReplicationState } from './index.ts';
78

89
// does nothing
910
export const DEFAULT_MODIFIER = (d: any) => Promise.resolve(d);
@@ -95,3 +96,27 @@ export function awaitRetry(
9596
window.removeEventListener('online', listener);
9697
});
9798
}
99+
100+
101+
/**
102+
* When a replication is running and the leading tab get hibernated
103+
* by the browser, the replication will be stuck.
104+
* To prevent this, we fire a mouseeven each X seconds while the replication is not canceled.
105+
*
106+
* If you find a better way to prevent hibernation, please make a pull request.
107+
*/
108+
export function preventHibernateBrowserTab(replicationState: RxReplicationState<any, any>) {
109+
function simulateActivity() {
110+
if (
111+
typeof document === 'undefined' ||
112+
typeof document.dispatchEvent !== 'function'
113+
) {
114+
return;
115+
}
116+
const event = new Event('mousemove');
117+
document.dispatchEvent(event);
118+
}
119+
120+
const intervalId = setInterval(simulateActivity, 20 * 1000); // Simulate activity every 20 seconds
121+
replicationState.onCancel.push(() => clearInterval(intervalId));
122+
}

0 commit comments

Comments
 (0)