-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1393 from RaasAhsan/fiber-local
Fiber locals
- Loading branch information
Showing
5 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect | ||
|
||
trait FiberLocal[F[_], A] { | ||
|
||
def get: F[A] | ||
|
||
def set(value: A): F[Unit] | ||
|
||
def clear: F[Unit] | ||
|
||
def update(f: A => A): F[Unit] | ||
|
||
def modify[B](f: A => (A, B)): F[B] | ||
|
||
def getAndSet(value: A): F[A] | ||
|
||
def getAndClear: F[A] | ||
|
||
} | ||
|
||
object FiberLocal { | ||
|
||
def apply[A](default: A): IO[FiberLocal[IO, A]] = | ||
IO { | ||
new FiberLocal[IO, A] { self => | ||
override def get: IO[A] = | ||
IO.Local(state => (state, state.get(self).map(_.asInstanceOf[A]).getOrElse(default))) | ||
|
||
override def set(value: A): IO[Unit] = | ||
IO.Local(state => (state + (self -> value), ())) | ||
|
||
override def clear: IO[Unit] = | ||
IO.Local(state => (state - self, ())) | ||
|
||
override def update(f: A => A): IO[Unit] = | ||
get.flatMap(a => set(f(a))) | ||
|
||
override def modify[B](f: A => (A, B)): IO[B] = | ||
get.flatMap { a => | ||
val (a2, b) = f(a) | ||
set(a2).as(b) | ||
} | ||
|
||
override def getAndSet(value: A): IO[A] = | ||
get <* set(value) | ||
|
||
override def getAndClear: IO[A] = | ||
get <* clear | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/shared/src/test/scala/cats/effect/FiberLocalSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats | ||
package effect | ||
|
||
class FiberLocalSpec extends BaseSpec { | ||
|
||
"Local" should { | ||
"return a default value" in ticked { implicit ticker => | ||
val io = FiberLocal(0).flatMap(_.get) | ||
|
||
io must completeAs(0) | ||
} | ||
|
||
"set and get a value" in ticked { implicit ticker => | ||
val io = for { | ||
local <- FiberLocal(0) | ||
_ <- local.set(10) | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"preserve locals across async boundaries" in ticked { implicit ticker => | ||
val io = for { | ||
local <- FiberLocal(0) | ||
_ <- local.set(10) | ||
_ <- IO.cede | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"copy locals to children fibers" in ticked { implicit ticker => | ||
val io = for { | ||
local <- FiberLocal(0) | ||
_ <- local.set(10) | ||
f <- local.get.start | ||
value <- f.joinWithNever | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"child local manipulation is invisible to parents" in ticked { implicit ticker => | ||
val io = for { | ||
local <- FiberLocal(10) | ||
f <- local.set(20).start | ||
_ <- f.join | ||
value <- local.get | ||
} yield value | ||
|
||
io must completeAs(10) | ||
} | ||
|
||
"parent local manipulation is invisible to children" in ticked { implicit ticker => | ||
val io = for { | ||
local <- FiberLocal(0) | ||
d1 <- Deferred[IO, Unit] | ||
f <- (d1.get *> local.get).start | ||
_ <- local.set(10) | ||
_ <- d1.complete(()) | ||
value <- f.joinWithNever | ||
} yield value | ||
|
||
io must completeAs(0) | ||
} | ||
} | ||
|
||
} |