storePut method

  1. @override
Future<void> storePut(
  1. String key,
  2. dynamic value
)
override

Implementation

@override
Future<void> storePut(String key, dynamic value) async {
  // Apply serialization hooks first
  Map<String, dynamic> wrappedValue = await ctx.control.emit(
    TriggerType.onValueSerialize.name,
    action: (ctx) async {
      String? hookId;
      dynamic result = value;

      for (var hook in this.ctx.config.storeSerializationHooks) {
        if (hook.canHandle != null && !await hook.canHandle!(ctx)) {
          continue;
        }
        try {
          // Update payload so hook can read the current value
          ctx.payload = ctx.payload.copyWith(value: result);
          result = await hook.serialize(ctx);
          hookId = hook.id; // Track which hook serialized this
          break; // Only use the first matching hook
        } catch (e) {
          if (!hook.silentOnError) rethrow;
          if (hook.onError != null) await hook.onError!(ctx);
        }
      }

      // Wrap the result with ID
      return {'_hivehook__id_': hookId, 'value': result};
    },
  );

  // Convert wrapped value to JSON string
  String serializedValue = jsonEncode(wrappedValue);

  // Then apply terminal serialization
  String finalValue = await ctx.control.emit(
    TriggerType.onValueTSerialize.name,
    action: (ctx) async {
      String result = serializedValue;
      for (var hook in this.ctx.config.storeTerminalSerializationHooks) {
        result = await hook.serialize(result, ctx);
      }
      return result;
    },
  );

  final box = await store;
  await box.put(key, finalValue);
}