@@ -44,7 +44,7 @@ public final class RuleKit {
4444 }
4545 }
4646
47- var rules : [ Notification . Name : any Rule ] = [ : ]
47+ var rules : [ ( rule : any Rule , trigger : any Trigger ) ] = [ ]
4848
4949 private init ( ) { }
5050
@@ -56,24 +56,24 @@ public final class RuleKit {
5656 }
5757
5858 func triggerFulfilledRules( ) async throws {
59- for (notification , rule ) in rules {
59+ for (rule , trigger ) in rules {
6060 guard await rule. isFulfilled else {
6161 continue
6262 }
6363 let queue = rule. firstOption ( ofType: DispatchQueueOption . self) ? . queue ?? . main
6464 queue. async {
65- NotificationCenter . default . post ( name : notification , object : nil )
65+ trigger . execute ( )
6666 }
67- try await store. persist ( triggerOf: notification )
67+ try await store. persist ( triggerOf: trigger )
6868 }
6969 }
7070
7171 func donations( for event: Event ) async -> Event . Donations {
7272 ( try ? await store. donations ( for: event) ) ?? . empty
7373 }
7474
75- func lastTrigger( for notification : Notification . Name ) async -> Date ? {
76- try ? await store. lastTrigger ( of: notification )
75+ func lastTrigger( for trigger : any Trigger ) async -> Date ? {
76+ try ? await store. lastTrigger ( of: trigger )
7777 }
7878
7979 func donate( _ event: Event ) async {
@@ -113,6 +113,14 @@ extension RuleKit {
113113 }
114114
115115 public static func setRule( triggering notification: Notification . Name , options: [ any RuleKitOption ] = [ ] , _ rule: Rule ) {
116- RuleKit . internal. rules [ notification] = options. isEmpty ? rule : RuleWithOptions ( options: options, notification: notification, rule: rule)
116+ let trigger = NotificationCenterTrigger ( notification: notification)
117+ let rule = options. isEmpty ? rule : RuleWithOptions ( options: options, trigger: trigger, rule: rule)
118+ RuleKit . internal. rules. append ( ( rule, trigger) )
119+ }
120+
121+ public static func setRule( triggering callback: @escaping @Sendable ( ) -> Void , rawValue: String , options: [ any RuleKitOption ] = [ ] , _ rule: Rule ) {
122+ let trigger = CallbackTrigger ( rawValue: rawValue, callback: callback)
123+ let rule = options. isEmpty ? rule : RuleWithOptions ( options: options, trigger: trigger, rule: rule)
124+ RuleKit . internal. rules. append ( ( rule, trigger) )
117125 }
118126}
0 commit comments