Files
sim/stores/sync-registry.ts

83 lines
1.9 KiB
TypeScript

'use client'
import { SyncManager } from './sync'
import { isLocalStorageMode } from './sync-core'
import { fetchWorkflowsFromDB, workflowSync } from './workflows/sync'
// Initialize managers lazily
let initialized = false
let initializing = false
let managers: SyncManager[] = []
/**
* Initialize sync managers and fetch data from DB
* Returns a promise that resolves when initialization is complete
*
* Note: Workflow scheduling is handled automatically by the workflowSync manager
* when workflows are synced to the database. The scheduling logic checks if a
* workflow has scheduling enabled in its starter block and updates the schedule
* accordingly.
*/
export async function initializeSyncManagers(): Promise<boolean> {
// Skip if already initialized or initializing
if (initialized || initializing) {
return initialized
}
initializing = true
try {
// Skip DB sync in local storage mode
if (isLocalStorageMode()) {
managers = [workflowSync]
initialized = true
return true
}
// Initialize sync managers
managers = [workflowSync]
// Fetch data from DB
try {
// Remove environment variables fetch
await fetchWorkflowsFromDB()
} catch (error) {
console.error('Error fetching data from DB:', error)
}
initialized = true
return true
} catch (error) {
console.error('Error initializing sync managers:', error)
return false
} finally {
initializing = false
}
}
/**
* Check if sync managers are initialized
*/
export function isSyncInitialized(): boolean {
return initialized
}
/**
* Get all sync managers
*/
export function getSyncManagers(): SyncManager[] {
return managers
}
/**
* Reset all sync managers
*/
export function resetSyncManagers(): void {
initialized = false
initializing = false
managers = []
}
// Export individual sync managers for direct use
export { workflowSync }