@@ -16,8 +16,6 @@ import org.typelevel.ci.CIStringSyntax
1616
1717import fs2 .*
1818import fs2 .concurrent .Topic
19- import fs2 .io .Watcher
20- import fs2 .io .Watcher .Event
2119import fs2 .io .file .Files
2220
2321import scribe .Scribe
@@ -29,7 +27,6 @@ import cats.effect.IO
2927import cats .effect .kernel .Ref
3028import cats .effect .kernel .Resource
3129import cats .effect .std .*
32- import cats .effect .std .MapRef
3330import cats .syntax .all .*
3431
3532import _root_ .io .circe .syntax .EncoderOps
@@ -89,7 +86,7 @@ end ETagMiddleware
8986
9087def routes (
9188 stringPath : String ,
92- refreshTopic : Topic [IO , String ],
89+ refreshTopic : Topic [IO , Unit ],
9390 stylesPath : Option [String ],
9491 proxyRoutes : HttpRoutes [IO ],
9592 indexHtmlTemplate : String ,
@@ -162,7 +159,7 @@ def seedMapOnStart(stringPath: String, mr: MapRef[IO, String, Option[String]])(l
162159 .isRegularFile(f)
163160 .ifM(
164161 // logger.trace(s"hashing $f") >>
165- fielHash (f).flatMap(
162+ fileHash (f).flatMap(
166163 h =>
167164 val key = asFs2.relativize(f)
168165 logger.trace(s " hashing $f to put at $key with hash : $h" ) >>
@@ -179,46 +176,33 @@ end seedMapOnStart
179176
180177private def fileWatcher (
181178 stringPath : fs2.io.file.Path ,
182- mr : MapRef [IO , String , Option [String ]]
183- )(logger : Scribe [IO ]): ResourceIO [IO [OutcomeIO [Unit ]]] =
184- fs2
185- .Stream
186- .resource(Watcher .default[IO ].evalTap(_.watch(stringPath.toNioPath)))
187- .flatMap {
188- w =>
189- w.events()
190- .evalTap(
191- (e : Event ) =>
192- e match
193- case Event .Created (path, i) =>
194- // if path.endsWith(".js") then
195- logger.trace(s " created $path, calculating hash " ) >>
196- fielHash(fs2.io.file.Path (path.toString())).flatMap(
197- h =>
198- val serveAt = stringPath.relativize(fs2.io.file.Path (path.toString()))
199- logger.trace(s " $serveAt :: hash -> $h" ) >>
200- mr.setKeyValue(serveAt.toString(), h)
201- )
202- // else IO.unit
203- case Event .Modified (path, i) =>
204- // if path.endsWith(".js") then
205- logger.trace(s " modified $path, calculating hash " ) >>
206- fielHash(fs2.io.file.Path (path.toString())).flatMap(
207- h =>
208- val serveAt = stringPath.relativize(fs2.io.file.Path (path.toString()))
209- logger.trace(s " $serveAt :: hash -> $h" ) >>
210- mr.setKeyValue(serveAt.toString(), h)
211- )
212- // else IO.unit
213- case Event .Deleted (path, i) =>
179+ mr : MapRef [IO , String , Option [String ]],
180+ linkingTopic : Topic [IO , Unit ],
181+ refreshTopic : Topic [IO , Unit ]
182+ )(logger : Scribe [IO ]): ResourceIO [Unit ] =
183+ linkingTopic
184+ .subscribe(10 )
185+ .evalTap {
186+ _ =>
187+ fs2
188+ .io
189+ .file
190+ .Files [IO ]
191+ .list(stringPath)
192+ .evalTap {
193+ path =>
194+ fileHash(path).flatMap(
195+ h =>
214196 val serveAt = stringPath.relativize(fs2.io.file.Path (path.toString()))
215- logger.trace(s " deleted $path, removing key $serveAt" ) >>
216- mr.unsetKey(serveAt.toString())
217- case e : Event .Overflow => logger.info(" overflow" )
218- case e : Event .NonStandard => logger.info(" non-standard" )
219- )
197+ logger.trace(s " $serveAt :: hash -> $h" ) >>
198+ mr.setKeyValue(serveAt.toString(), h)
199+ )
200+ }
201+ .compile
202+ .drain >> refreshTopic.publish1(())
220203 }
221204 .compile
222205 .drain
223206 .background
207+ .void
224208end fileWatcher
0 commit comments