@@ -138,43 +138,58 @@ class DirectoryEventStream {
138138
139139 for (index, dictionary) in eventDictionaries. enumerated ( ) {
140140 // Get get file id use dictionary[kFSEventStreamEventExtendedFileIDKey] as? UInt64
141- guard let path = dictionary [ kFSEventStreamEventExtendedDataPathKey] as? String ,
142- let event = getEventFromFlags ( eventFlags [ index] )
141+ guard let path = dictionary [ kFSEventStreamEventExtendedDataPathKey] as? String
143142 else {
144143 continue
145144 }
146145
147- events. append ( . init( path: path, eventType: event) )
146+ let fsEvents = getEventsFromFlags ( eventFlags [ index] )
147+
148+ for event in fsEvents {
149+ events. append ( . init( path: path, eventType: event) )
150+ }
148151 }
149152
150153 callback ( events)
151154 }
152155
153- /// Parses an ``FSEvent`` from the raw flag value.
156+ /// Parses ``FSEvent`` from the raw flag value.
157+ ///
158+ /// There can be multiple events in the raw flag value,
159+ /// bacause of how OS processes almost simlutaneous actions – thus this functions returns a `Set` of `FSEvent`.
154160 ///
155- /// Often returns ``FSEvent/changeInDirectory`` as `FSEventStream` returns
161+ /// Often returns ``[ FSEvent/changeInDirectory] `` as `FSEventStream` returns
156162 /// `kFSEventStreamEventFlagNone (0x00000000)` frequently without more information.
157163 /// - Parameter raw: The int value received from the FSEventStream
158- /// - Returns: An ``FSEvent`` if a valid one was found, or `nil` otherwise.
159- func getEventFromFlags( _ raw: FSEventStreamEventFlags ) -> FSEvent ? {
164+ /// - Returns: A `Set` of ``FSEvent``'s if at least one valid was found, or `[]` otherwise.
165+ private func getEventsFromFlags( _ raw: FSEventStreamEventFlags ) -> Set < FSEvent > {
166+ var events : Set < FSEvent > = [ ]
167+
160168 if raw == 0 {
161- return . changeInDirectory
162- } else if raw & UInt32 ( kFSEventStreamEventFlagRootChanged) > 0 {
163- return . rootChanged
164- } else if raw & UInt32 ( kFSEventStreamEventFlagItemChangeOwner) > 0 {
165- return . itemChangedOwner
166- } else if raw & UInt32 ( kFSEventStreamEventFlagItemCreated) > 0 {
167- return . itemCreated
168- } else if raw & UInt32 ( kFSEventStreamEventFlagItemCloned) > 0 {
169- return . itemCloned
170- } else if raw & UInt32 ( kFSEventStreamEventFlagItemModified) > 0 {
171- return . itemModified
172- } else if raw & UInt32 ( kFSEventStreamEventFlagItemRemoved) > 0 {
173- return . itemRemoved
174- } else if raw & UInt32 ( kFSEventStreamEventFlagItemRenamed) > 0 {
175- return . itemRenamed
176- } else {
177- return nil
169+ events. insert ( . changeInDirectory)
170+ }
171+ if raw & UInt32 ( kFSEventStreamEventFlagRootChanged) > 0 {
172+ events. insert ( . rootChanged)
173+ }
174+ if raw & UInt32 ( kFSEventStreamEventFlagItemChangeOwner) > 0 {
175+ events. insert ( . itemChangedOwner)
178176 }
177+ if raw & UInt32 ( kFSEventStreamEventFlagItemCreated) > 0 {
178+ events. insert ( . itemCreated)
179+ }
180+ if raw & UInt32 ( kFSEventStreamEventFlagItemCloned) > 0 {
181+ events. insert ( . itemCloned)
182+ }
183+ if raw & UInt32 ( kFSEventStreamEventFlagItemModified) > 0 {
184+ events. insert ( . itemModified)
185+ }
186+ if raw & UInt32 ( kFSEventStreamEventFlagItemRemoved) > 0 {
187+ events. insert ( . itemRemoved)
188+ }
189+ if raw & UInt32 ( kFSEventStreamEventFlagItemRenamed) > 0 {
190+ events. insert ( . itemRenamed)
191+ }
192+
193+ return events
179194 }
180195}
0 commit comments