PeriodicSyncStrategy class

A reusable periodic synchronization strategy that separates technical implementation from business logic.

This strategy implements a periodic timer that orchestrates the sync process:

  1. Push pending local events to remote
  2. Pull remote events and apply them locally

Business logic (API calls, repository-specific rules) is provided through callbacks.

Example usage:

final strategy = PeriodicSyncStrategy(
  syncInterval: Duration(seconds: 5),
  repositoryNames: ['user', 'counter_log'],
  onFetchEvents: (repositoryName) async {
    // Fetch events from your API
    final response = await api.fetchEvents(repositoryName);
    return response.events;
  },
  onPushEvents: (repositoryName, events) async {
    // Push events to your API
    return await api.pushEvents(repositoryName, events);
  },
  onBuildSyncFilter: (repositoryName) async {
    // Return filter parameters based on last sync state
    final lastSeq = await storage.getLastSequence(repositoryName);
    return lastSeq != null ? {'afterSequence': lastSeq} : null;
  },
  onSaveSyncState: (repositoryName, events) async {
    // Save sync state after applying events
    if (events.isNotEmpty) {
      final maxSeq = events.map((e) => e['sequence']).reduce(max);
      await storage.saveLastSequence(repositoryName, maxSeq);
    }
  },
);

final client = LocalFirstClient(
  repositories: [userRepository, counterLogRepository],
  localStorage: SqliteLocalFirstStorage(),
  syncStrategies: [strategy],
);

await client.initialize();
await strategy.start();
Inheritance
  • Object
  • DataSyncStrategy
  • PeriodicSyncStrategy

Constructors

PeriodicSyncStrategy({required Duration syncInterval, required List<String> repositoryNames, required FetchEventsCallback onFetchEvents, required PushEventsCallback onPushEvents, required BuildSyncFilterCallback onBuildSyncFilter, required SaveSyncStateCallback onSaveSyncState, PingCallback? onPing})

Properties

client → LocalFirstClient
no setterinherited
connectionChanges Stream<bool>
Exposes the connection state stream maintained by the client.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
latestConnectionState bool?
Latest known connection state.
no setterinherited
onBuildSyncFilter BuildSyncFilterCallback
Callback to build sync filter parameters
final
onFetchEvents FetchEventsCallback
Callback to fetch remote events
final
onPing PingCallback?
Optional callback to check connection health
final
onPushEvents PushEventsCallback
Callback to push local events
final
onSaveSyncState SaveSyncStateCallback
Callback to save sync state after applying events
final
repositoryNames List<String>
List of repository names to synchronize
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
syncInterval Duration
Interval between sync cycles
final

Methods

attach(LocalFirstClient client) → void
Attaches this strategy to a LocalFirstClient.
inherited
dispose() → void
Disposes of all resources and stops synchronization.
forceSync() Future<void>
Forces an immediate sync cycle outside the regular timer.
getPendingEvents({required String repositoryName}) Future<LocalFirstEvents>
Fetches pending events for a repository to be pushed upstream.
inherited
handlesRepository(String repositoryName) bool
Returns true if this strategy handles the given repository.
inherited
markEventsAsSynced(LocalFirstEvents events) Future<void>
Marks the provided events as successfully synchronized.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onPushToRemote(LocalFirstEvent localData) Future<SyncStatus>
Queues local event for push during the next sync cycle.
override
pullChangesToLocal({required String repositoryName, required List<JsonMap> remoteChanges}) Future<void>
Applies remote changes for a repository by delegating to the client.
inherited
reportConnectionState(bool connected) → void
Notifies listeners about connection state changes (e.g. connectivity loss).
inherited
start() Future<void>
Starts the periodic synchronization timer.
stop() → void
Stops the periodic synchronization timer.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

logTag → const String