storeGet method

  1. @override
Future storeGet(
  1. String key
)
override

Implementation

@override
Future<dynamic> storeGet(String key) async {
  final box = await store;
  final rawResult = await box.get(key);

  if (rawResult == null) return null;

  // Terminal deserialization first
  String valueStr = await ctx.control.emit(
    TriggerType.onValueTDeserialize.name,
    action: (ctx) async {
      String result = rawResult;
      for (var hook in this.ctx.config.storeTerminalSerializationHooks) {
        result = await hook.deserialize(result, ctx);
      }
      return result;
    },
  );

  // Parse the wrapped value to extract ID and value
  final parsed = jsonDecode(valueStr) as Map<String, dynamic>;
  final hookId = parsed['_hivehook__id_'] as String?;
  final wrappedValue = parsed['value'];

  // Then apply serialization hooks
  final deserializedValue = await ctx.control.emit(
    TriggerType.onValueDeserialize.name,
    action: (ctx) async {
      dynamic result = wrappedValue;

      if (hookId != null) {
        // Find the specific hook by ID
        final hook = this.ctx.config.storeSerializationHooks
            .where((h) => h.id == hookId)
            .firstOrNull;

        if (hook != null) {
          if (hook.canHandle == null || await hook.canHandle!(ctx)) {
            try {
              // Update payload so hook can read the current value
              ctx.payload = ctx.payload.copyWith(value: result);
              result = await hook.deserialize(ctx);
            } catch (e) {
              if (!hook.silentOnError) rethrow;
              if (hook.onError != null) await hook.onError!(ctx);
            }
          }
        }
      }

      return result;
    },
  );

  return deserializedValue;
}