queue method

Future<void> queue(
  1. List<PVCacheHook> hooks
)

Execute all hooks through the EventFlow lifecycle.

Groups hooks by stage, executes in order, handles storage I/O, and catches BreakHook exceptions.

Implementation

Future<void> queue(List<PVCacheHook> hooks) async {
  try {
    // Group hooks by EventFlow
    final hooksByFlow = <EventFlow, List<PVCacheHook>>{};
    for (final hook in hooks) {
      hooksByFlow.putIfAbsent(hook.eventFlow, () => []).add(hook);
    }

    // preProcess
    await _runHooks(hooksByFlow[EventFlow.preProcess]);

    // metaRead - automatically read metadata FIRST, then run hooks
    if (resolvedKey != null) {
      final metaData = await meta.get(resolvedKey!);
      if (metaData != null) {
        // Create a mutable copy of the metadata
        runtimeMeta = Map<String, dynamic>.from(metaData);
      }
    }
    await _runHooks(hooksByFlow[EventFlow.metaRead]);

    // metaUpdatePriorEntry - update metadata before entry operation
    await _runHooks(hooksByFlow[EventFlow.metaUpdatePriorEntry]);

    // storageRead - automatically read entry (for get operations)
    if (actionType == ActionType.get || actionType == ActionType.exists) {
      await _runHooks(hooksByFlow[EventFlow.storageRead]);
      if (resolvedKey != null) {
        final entryData = await entry.get(resolvedKey!);
        if (entryData != null) {
          entryValue = entryData['value'];
        }
      }
    }

    // storageUpdate - automatically write entry (for put operations)
    if (actionType == ActionType.put) {
      await _runHooks(hooksByFlow[EventFlow.storageUpdate]);
      if (resolvedKey != null && entryValue != null) {
        await entry.put(resolvedKey!, {'value': entryValue});
      }
    }

    // delete operation
    if (actionType == ActionType.delete) {
      await _runHooks(hooksByFlow[EventFlow.storageUpdate]);
      if (resolvedKey != null) {
        await entry.delete(resolvedKey!);
        await meta.delete(resolvedKey!);
      }
    }

    // clear operation
    if (actionType == ActionType.clear) {
      await _runHooks(hooksByFlow[EventFlow.storageUpdate]);
      await entry.clear();
      await meta.clear();
    }

    // metaUpdatePostEntry - update metadata after entry operation
    await _runHooks(hooksByFlow[EventFlow.metaUpdatePostEntry]);
    if (resolvedKey != null && runtimeMeta.isNotEmpty) {
      await meta.put(resolvedKey!, runtimeMeta);
    }

    // postProcess
    await _runHooks(hooksByFlow[EventFlow.postProcess]);
  } on BreakHook catch (e) {
    if (e.returnType == BreakReturnType.initial) {
      returnValue = initialEntryValue;
    } else if (e.returnType == BreakReturnType.resolved) {
      returnValue = entryValue;
    } else {
      returnValue = null;
    }
    return;
  }
  returnValue = entryValue;
}