Skip to content

Commit

Permalink
feat: start and stop syncing immediately after switch click
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemSBulgakov committed Jul 19, 2024
1 parent 9f158b9 commit 0ed45db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/entrypoints/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { applyUserAgentRule } from '@/entrypoints/background/net-rules'
import { autoLogIn } from '@/features/autologin/background'
import { fetchCourses } from '@/features/courses/background'
import { syncCourses } from '@/features/search-sync/background'
import { sendSyncProgress, stopSync, syncCourses } from '@/features/search-sync/background'
import { onMessage, sendMessageToMoodleTabs } from '@/shared/messages'
import { refreshToken } from '@/shared/moodle-ws-api/token-store'
import { getStored } from '@/shared/storage'
Expand All @@ -17,6 +17,7 @@ onMessage('POPUP_OPEN', () => {
})

onMessage('MOODLE_LOAD', () => {
console.log('Background has received a message MOODLE_LOAD')
getStored('autologinLastSuccessMS').then((autologinLastSuccessMS) => {
sendMessageToMoodleTabs('AUTOLOGIN_LAST_SUCCESS', autologinLastSuccessMS)
})
Expand All @@ -42,8 +43,24 @@ onMessage('MOODLE_LOAD', () => {
})

onMessage('REQUEST_AUTOLOGIN', () => {
console.log('Background has received a message REQUEST_AUTOLOGIN')
autoLogIn().then((success) => {
// Send message to all content scripts
sendMessageToMoodleTabs(success ? 'AUTOLOGIN_SUCCEEDED' : 'AUTOLOGIN_FAILED')
})
})

onMessage('REQUEST_SYNC', () => {
console.log('Background has received a message REQUEST_SYNC')
syncCourses() // Run the worker (if allowed)
})

onMessage('STOP_SYNC', () => {
console.log('Background has received a message STOP_SYNC')
stopSync() // Clear the pending requests queue
})

onMessage('REQUEST_SYNC_PROGRESS', () => {
console.log('Background has received a message REQUEST_SYNC_PROGRESS')
sendSyncProgress() // Send 'SYNCING_PROGRESS' message
})
5 changes: 4 additions & 1 deletion src/features/search-sync/SearchAutoSyncToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sendMessage } from '@/shared/messages'
import { removeStored, setStored, useStorage } from '@/shared/storage'
import { cn } from '@/shared/ui/utils'

Expand All @@ -16,8 +17,10 @@ export function SearchAutoSyncToggle() {
allowSendingCourses === false ? 'bg-red-900' : 'bg-[#9747FF] hover:bg-[#6600CC]',
)}
onClick={() => {
setStored('allowSyncingCourses', !allowSendingCourses)
const shouldAllow = !allowSendingCourses
setStored('allowSyncingCourses', shouldAllow)
removeStored('syncCoursesLastUpdateMS')
sendMessage(shouldAllow ? 'REQUEST_SYNC' : 'STOP_SYNC')
}}
>
{allowSendingCourses === false ? 'OFF' : 'ON'}
Expand Down

0 comments on commit 0ed45db

Please # to comment.