Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions contentcuration/contentcuration/frontend/shared/data/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Dexie from 'dexie';
import mapValues from 'lodash/mapValues';
import { createLeaderElection } from './leaderElection';
import { createLeaderElection } from 'broadcast-channel';
import channel from './broadcastChannel';
import { CHANGE_LOCKS_TABLE, CHANGES_TABLE, IGNORED_SOURCE, TABLE_NAMES } from './constants';
import db from './db';
Expand Down Expand Up @@ -45,11 +45,11 @@ if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
function runElection() {
const elector = createLeaderElection(channel);

elector.awaitLeadership({
success: startSyncing,
cleanup: stopSyncing,
});
return elector.waitForLeader();
elector.awaitLeadership().then(startSyncing);
elector.onduplicate = () => {
stopSyncing();
elector.die.then(runElection);
};
}

export function initializeDB() {
Expand Down
306 changes: 0 additions & 306 deletions contentcuration/contentcuration/frontend/shared/data/leaderElection.js

This file was deleted.

35 changes: 0 additions & 35 deletions contentcuration/contentcuration/frontend/shared/data/serverSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ const SYNC_IF_NO_CHANGES_FOR = 2;
// already instantiated in the broadcastChannel module.
const channel = createChannel();

// Stores last setTimeout in polling so we may clear it when we want
let unsyncedPollingTimeoutId;

// Flag to check if a sync is currently active.
let syncActive = false;

Expand Down Expand Up @@ -301,9 +298,6 @@ const debouncedSyncChanges = debounce(() => {

if (process.env.NODE_ENV !== 'production' && typeof window !== 'undefined') {
window.forceServerSync = forceServerSync;

window.stopPollingUnsyncedChanges = stopPollingUnsyncedChanges;
window.pollUnsyncedChanges = pollUnsyncedChanges;
}

async function handleChanges(changes) {
Expand Down Expand Up @@ -332,47 +326,18 @@ async function handleChanges(changes) {
}
}

async function checkAndSyncChanges() {
// Get count of changes that we care about
const changes = await db[CHANGES_TABLE].toCollection()
// Only try to sync if we have at least one change that has
// not already errored on the backend.
.filter(c => !c.errors)
.count();

// If more than 0, sync the changes
if (changes > 0) {
debouncedSyncChanges();
}
}

async function pollUnsyncedChanges() {
await checkAndSyncChanges();
unsyncedPollingTimeoutId = setTimeout(() => pollUnsyncedChanges(), SYNC_IF_NO_CHANGES_FOR * 1000);
}

function stopPollingUnsyncedChanges() {
if (unsyncedPollingTimeoutId) {
clearTimeout(unsyncedPollingTimeoutId);
}
}

export function startSyncing() {
startChannelFetchListener();
cleanupLocks();
// Initiate a sync immediately in case any data
// is left over in the database.
debouncedSyncChanges();
// Begin polling our CHANGES_TABLE
pollUnsyncedChanges();
db.on('changes', handleChanges);
}

export function stopSyncing() {
stopChannelFetchListener();
debouncedSyncChanges.cancel();
// Stop pollUnsyncedChanges
stopPollingUnsyncedChanges();
// Dexie's slightly counterintuitive method for unsubscribing from events
db.on('changes').unsubscribe(handleChanges);
}
Expand Down
Loading