From 1fdc2ac10c0a98f85daf6cd8ff2cca90182e10bb Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Sun, 18 Feb 2024 14:21:17 -0800 Subject: [PATCH] chore: rename contract_cache to contract_snapshot --- packages/cli/lib/cache/contract_cache.dart | 79 ---------------------- 1 file changed, 79 deletions(-) delete mode 100644 packages/cli/lib/cache/contract_cache.dart diff --git a/packages/cli/lib/cache/contract_cache.dart b/packages/cli/lib/cache/contract_cache.dart deleted file mode 100644 index b81cfbf8..00000000 --- a/packages/cli/lib/cache/contract_cache.dart +++ /dev/null @@ -1,79 +0,0 @@ -import 'package:cli/api.dart'; -import 'package:cli/compare.dart'; -import 'package:cli/logger.dart'; -import 'package:cli/net/queries.dart'; -import 'package:collection/collection.dart'; -import 'package:db/db.dart'; -import 'package:types/types.dart'; - -/// Snapshot of contracts in the database. -class ContractSnapshot { - /// Creates a new contract snapshot. - ContractSnapshot(this.contracts); - - /// Load the ContractSnapshot from the database. - static Future load(Database db) async { - final contracts = await db.allContracts(); - return ContractSnapshot(contracts.toList()); - } - - /// Contracts in the cache. - final List contracts; - - /// Number of requests between checks to ensure ships are up to date. - final int requestsBetweenChecks = 100; - - int _requestsSinceLastCheck = 0; - - /// Returns a list of all completed contracts. - List get completedContracts => - contracts.where((c) => c.fulfilled).toList(); - - /// Returns a list of all expired contracts. - List get expiredContracts => - contracts.where((c) => c.isExpired && !c.fulfilled).toList(); - - /// Returns a list of all active (not fulfilled or expired) contracts. - List get activeContracts => - contracts.where((c) => !c.fulfilled && !c.isExpired).toList(); - - /// Returns a list of all unaccepted contracts. - List get unacceptedContracts => - contracts.where((c) => !c.accepted).toList(); - - /// Looks up the contract by id. - Contract? contract(String id) => - contracts.firstWhereOrNull((c) => c.id == id); - - /// Fetches a new snapshot and logs if different from this one. - // TODO(eseidel): This does not belong in this class. - Future ensureUpToDate(Database db, Api api) async { - _requestsSinceLastCheck++; - if (_requestsSinceLastCheck < requestsBetweenChecks) { - return this; - } - _requestsSinceLastCheck = 0; - - final newContracts = await fetchContracts(db, api); - final newContractsJson = - newContracts.contracts.map((c) => c.toOpenApi().toJson()).toList(); - final oldContractsJson = - contracts.map((c) => c.toOpenApi().toJson()).toList(); - // Our contracts class has a timestamp which we don't want to compare, so - // compare the OpenAPI JSON instead. - if (jsonMatches(newContractsJson, oldContractsJson)) { - logger.warn('Contracts changed, updating cache.'); - return newContracts; - } - return this; - } -} - -/// Fetches all of the user's contracts. -Future fetchContracts(Database db, Api api) async { - final contracts = await allMyContracts(api).toList(); - for (final contract in contracts) { - await db.upsertContract(contract); - } - return ContractSnapshot(contracts); -}