Skip to content

Commit

Permalink
Added an interval to prevent browser tab hibernation while a replicat…
Browse files Browse the repository at this point in the history
…ion is running (#6675)
  • Loading branch information
pubkey authored Dec 10, 2024
1 parent 235494b commit c2c7ea4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs-src/docs/releases/16.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ I completely rewrote them and improved performance especially on initial load wh
- Lazily process bulkWrite() results for less CPU usage.
- Only run interval cleanup on the storage of a collection if there actually have been writes to it.
- Schema validation errors (code: 422) now include the `RxJsonSchema` for easier debugging.
- Added an interval to prevent browser tab hibernation while a replication is running.

## You can help!

Expand Down
6 changes: 5 additions & 1 deletion src/plugins/replication/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import {
awaitRetry,
DEFAULT_MODIFIER,
swapDefaultDeletedTodeletedField,
handlePulledDocuments
handlePulledDocuments,
preventHibernateBrowserTab
} from './replication-helper.ts';
import {
addConnectedStorageToCollection, removeConnectedStorageFromCollection
Expand Down Expand Up @@ -155,6 +156,9 @@ export class RxReplicationState<RxDocType, CheckpointType> {
return;
}

preventHibernateBrowserTab(this);


// fill in defaults for pull & push
const pullModifier = this.pull && this.pull.modifier ? this.pull.modifier : DEFAULT_MODIFIER;
const pushModifier = this.push && this.push.modifier ? this.push.modifier : DEFAULT_MODIFIER;
Expand Down
25 changes: 25 additions & 0 deletions src/plugins/replication/replication-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
} from '../../types/index.d.ts';
import { flatClone } from '../../plugins/utils/index.ts';
import { getComposedPrimaryKeyOfDocumentData } from '../../rx-schema-helper.ts';
import type { RxReplicationState } from './index.ts';

// does nothing
export const DEFAULT_MODIFIER = (d: any) => Promise.resolve(d);
Expand Down Expand Up @@ -95,3 +96,27 @@ export function awaitRetry(
window.removeEventListener('online', listener);
});
}


/**
* When a replication is running and the leading tab get hibernated
* by the browser, the replication will be stuck.
* To prevent this, we fire a mouseeven each X seconds while the replication is not canceled.
*
* If you find a better way to prevent hibernation, please make a pull request.
*/
export function preventHibernateBrowserTab(replicationState: RxReplicationState<any, any>) {
function simulateActivity() {
if (
typeof document === 'undefined' ||
typeof document.dispatchEvent !== 'function'
) {
return;
}
const event = new Event('mousemove');
document.dispatchEvent(event);
}

const intervalId = setInterval(simulateActivity, 20 * 1000); // Simulate activity every 20 seconds
replicationState.onCancel.push(() => clearInterval(intervalId));
}

0 comments on commit c2c7ea4

Please sign in to comment.