ttlStampHook function
HiHook
ttlStampHook({})
Creates a TTL stamp hook that sets creation timestamp on write.
Should run in post phase after successful writes.
Sets created_at in meta to current timestamp.
Parameters
uid: Unique hook identifier, defaults to 'ttl:stamp'events: Events to listen for, defaults to'write', 'put'metaKey: Key in ctx.data for metadata, defaults to 'meta'createdAtKey: Key in meta for creation timestamp, defaults to 'created_at'nowProvider: Function to get current time, defaults to DateTime.now()
Example
engine.register(ttlStampHook());
Implementation
HiHook<dynamic, dynamic> ttlStampHook({
String uid = 'ttl:stamp',
List<String> events = const ['write', 'put'],
String metaKey = 'meta',
String createdAtKey = 'created_at',
int Function()? nowProvider,
HiPhase phase = HiPhase.post,
int priority = 0,
}) {
return HiHook<dynamic, dynamic>(
uid: uid,
events: events,
phase: phase,
priority: priority,
handler: (payload, ctx) {
final context = ctx as HiContext;
final meta = Map<String, dynamic>.from(
context.dataTracked[metaKey] as Map<String, dynamic>? ?? {},
);
meta[createdAtKey] = nowProvider?.call() ??
DateTime.now().millisecondsSinceEpoch;
context.dataTracked[metaKey] = meta;
return const HiContinue();
},
);
}