From f5c78b3df535bcdca946108cd198aed26e4e9563 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 31 Jan 2023 04:02:52 +0000 Subject: [PATCH] Expose `Window#document` --- .../scala-2/fs2/dom/WindowCrossCompat.scala | 21 ++++++++++ dom/src/main/scala-3/fs2/dom/Dom.scala | 2 + .../scala-3/fs2/dom/WindowCrossCompat.scala | 38 +++++++++++++++++++ dom/src/main/scala/fs2/dom/Window.scala | 8 ++-- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 dom/src/main/scala-2/fs2/dom/WindowCrossCompat.scala create mode 100644 dom/src/main/scala-3/fs2/dom/WindowCrossCompat.scala diff --git a/dom/src/main/scala-2/fs2/dom/WindowCrossCompat.scala b/dom/src/main/scala-2/fs2/dom/WindowCrossCompat.scala new file mode 100644 index 0000000..9dcdfcd --- /dev/null +++ b/dom/src/main/scala-2/fs2/dom/WindowCrossCompat.scala @@ -0,0 +1,21 @@ +/* + * Copyright 2022 Arman Bilge + * + * 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 fs2.dom + +private[dom] trait WindowCrossCompat[F[_]] + +private trait WindowImplCrossCompat[F[_]] extends WindowCrossCompat[F] diff --git a/dom/src/main/scala-3/fs2/dom/Dom.scala b/dom/src/main/scala-3/fs2/dom/Dom.scala index 0173ca0..b7bdacc 100644 --- a/dom/src/main/scala-3/fs2/dom/Dom.scala +++ b/dom/src/main/scala-3/fs2/dom/Dom.scala @@ -66,6 +66,8 @@ object Document { } } +opaque type HtmlDocument[F[_]] <: Document[F] = dom.HTMLDocument + opaque type Element[F[_]] <: Node[F] = dom.Element opaque type HtmlElement[F[_]] <: Element[F] = dom.HTMLElement opaque type HtmlAnchorElement[F[_]] <: HtmlElement[F] = dom.HTMLAnchorElement diff --git a/dom/src/main/scala-3/fs2/dom/WindowCrossCompat.scala b/dom/src/main/scala-3/fs2/dom/WindowCrossCompat.scala new file mode 100644 index 0000000..1c0a748 --- /dev/null +++ b/dom/src/main/scala-3/fs2/dom/WindowCrossCompat.scala @@ -0,0 +1,38 @@ +/* + * Copyright 2022 Arman Bilge + * + * 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 fs2.dom + +import cats.effect.kernel.Async +import org.scalajs.dom + +private trait WindowCrossCompat[F[_]] { + + implicit def given_Dom_F: Dom[F] + + def document: HtmlDocument[F] + +} + +private trait WindowImplCrossCompat[F[_]](using Async[F]) extends WindowCrossCompat[F] { + + private[dom] def window: dom.Window + + implicit def given_Dom_F = Dom.forAsync + + def document = window.document.asInstanceOf[HtmlDocument[F]] + +} diff --git a/dom/src/main/scala/fs2/dom/Window.scala b/dom/src/main/scala/fs2/dom/Window.scala index bb0a406..ebd172c 100644 --- a/dom/src/main/scala/fs2/dom/Window.scala +++ b/dom/src/main/scala/fs2/dom/Window.scala @@ -20,7 +20,7 @@ import cats.effect.kernel.Async import fs2.Stream import org.scalajs.dom -abstract class Window[F[_]] private { +abstract class Window[F[_]] private extends WindowCrossCompat[F] { def history[S](implicit serializer: Serializer[F, S]): History[F, S] @@ -41,8 +41,10 @@ object Window { def apply[F[_]](implicit F: Async[F]): Window[F] = apply(dom.window) - def apply[F[_]](window: dom.Window)(implicit F: Async[F]): Window[F] = - new Window[F] { + private def apply[F[_]](_window: dom.Window)(implicit F: Async[F]): Window[F] = + new Window[F] with WindowImplCrossCompat[F] { + + private[dom] def window = _window def history[S](implicit serializer: Serializer[F, S]) = History(window, window.history)