Skip to content

Instantly share code, notes, and snippets.

@hardvain
Last active July 22, 2021 06:45
Show Gist options
  • Save hardvain/4ff0b2c0c705953722905453a7aec792 to your computer and use it in GitHub Desktop.
Save hardvain/4ff0b2c0c705953722905453a7aec792 to your computer and use it in GitHub Desktop.
zio dom env
import org.scalajs.dom._
import org.scalajs.dom.document._
import zio._
import org.scalajs.dom.raw._
import scala.scalajs.js
import scala.scalajs.js.annotation._
import scala.language.postfixOps
trait DomService:
def implementation: UIO[DOMImplementation]
def characterSet: UIO[String]
def doctype: UIO[DocumentType]
def documentElement: UIO[Element]
def documentURI: UIO[String]
def styleSheets: UIO[StyleSheetList]
def lastModified: UIO[String]
def getElementById(elementId: String): UIO[Element]
def getElementsByName(elementName: String): UIO[NodeList]
def getElementsByTagName(name: String): UIO[HTMLCollection]
def getElementsByTagNameNS(
namespaceURI: String,
localName: String
): UIO[HTMLCollection]
def getElementsByClassName(classNames: String): UIO[HTMLCollection]
def elementFromPoint(x: Double, y: Double): UIO[Element]
def adoptNode(source: Node): UIO[Node]
def evaluate(
xpathExpression: String,
contextNode: Node,
namespaceResolver: XPathNSResolver,
resultType: Int,
result: XPathResult
): UIO[XPathResult]
def evaluate(
xpathExpression: String,
contextNode: Node,
namespaceResolver: js.Function1[String, String],
resultType: Int,
result: XPathResult
): UIO[XPathResult]
def createNSResolver(node: Node): UIO[XPathNSResolver]
def importNode(importedNode: Node, deep: Boolean): UIO[Node]
def createElement(tagName: String): UIO[Element]
def createElementNS(
namespaceURI: String,
qualifiedName: String
): UIO[Element]
def createAttribute(name: String): UIO[Attr]
def createAttributeNS(namespaceURI: String, qualifiedName: String): UIO[Attr]
def createProcessingInstruction(
target: String,
data: String
): UIO[ProcessingInstruction]
def createCDATASection(data: String): UIO[CDATASection]
def createRange(): UIO[Range]
def createComment(data: String): UIO[Comment]
def createDocumentFragment(): UIO[DocumentFragment]
def createStyleSheet(
href: String,
index: Int
): UIO[CSSStyleSheet]
def createTextNode(data: String): UIO[Text]
def createNodeIterator(
root: Node,
whatToShow: Int,
filter: NodeFilter,
entityReferenceExpansion: Boolean
): UIO[NodeIterator]
def createTreeWalker(
root: Node,
whatToShow: Int,
filter: NodeFilter,
entityReferenceExpansion: Boolean
): UIO[TreeWalker]
def exitFullscreen(): UIO[js.Promise[Unit]]
def fullscreenElement: UIO[Element]
def fullscreenEnabled: UIO[Boolean]
object DomService {
val domServiceImpl = new DomService :
def implementation: UIO[DOMImplementation] =
IO.effectTotal(document.implementation)
def characterSet: UIO[String] = IO.effectTotal(document.characterSet)
def doctype: UIO[DocumentType] = IO.effectTotal(document.doctype)
def documentElement: UIO[Element] = IO.effectTotal(document.documentElement)
def documentURI: UIO[String] = IO.effectTotal(document.documentURI)
def styleSheets: UIO[StyleSheetList] = IO.effectTotal(document.styleSheets)
def lastModified: UIO[String] = IO.effectTotal(document.lastModified)
def getElementById(elementId: String): UIO[Element] =
IO.effectTotal(document.getElementById(elementId))
def getElementsByName(elementName: String): UIO[NodeList] =
IO.effectTotal(document.getElementsByName(elementName))
def getElementsByTagName(name: String): UIO[HTMLCollection] =
IO.effectTotal(document.getElementsByTagName(name))
def getElementsByTagNameNS(
namespaceURI: String,
localName: String
): UIO[HTMLCollection] =
IO.effectTotal(document.getElementsByTagNameNS(namespaceURI, localName))
def getElementsByClassName(classNames: String): UIO[HTMLCollection] =
IO.effectTotal(document.getElementsByClassName(classNames))
def elementFromPoint(x: Double, y: Double): UIO[Element] =
IO.effectTotal(document.elementFromPoint(x, y))
def adoptNode(source: Node): UIO[Node] =
IO.effectTotal(document.adoptNode(source))
def evaluate(
xpathExpression: String,
contextNode: Node,
namespaceResolver: XPathNSResolver,
resultType: Int,
result: XPathResult
): UIO[XPathResult] = IO.effectTotal(
document.evaluate(
xpathExpression,
contextNode,
namespaceResolver,
resultType,
result
)
)
def evaluate(
xpathExpression: String,
contextNode: Node,
namespaceResolver: js.Function1[String, String],
resultType: Int,
result: XPathResult
): UIO[XPathResult] = IO.effectTotal(
document.evaluate(
xpathExpression,
contextNode,
namespaceResolver,
resultType,
result
)
)
def createNSResolver(node: Node): UIO[XPathNSResolver] =
IO.effectTotal(document.createNSResolver(node))
def importNode(importedNode: Node, deep: Boolean): UIO[Node] =
IO.effectTotal(document.importNode(importedNode, deep))
def createElement(tagName: String): UIO[Element] =
IO.effectTotal(document.createElement(tagName))
def createElementNS(
namespaceURI: String,
qualifiedName: String
): UIO[Element] =
IO.effectTotal(document.createElementNS(namespaceURI, qualifiedName))
def createAttribute(name: String): UIO[Attr] =
IO.effectTotal(document.createAttribute(name))
def createAttributeNS(
namespaceURI: String,
qualifiedName: String
): UIO[Attr] =
IO.effectTotal(document.createAttributeNS(namespaceURI, qualifiedName))
def createProcessingInstruction(
target: String,
data: String
): UIO[ProcessingInstruction] =
IO.effectTotal(document.createProcessingInstruction(target, data))
def createCDATASection(data: String): UIO[CDATASection] =
IO.effectTotal(document.createCDATASection(data))
def createRange(): UIO[Range] = IO.effectTotal(document.createRange())
def createComment(data: String): UIO[Comment] =
IO.effectTotal(document.createComment(data))
def createDocumentFragment(): UIO[DocumentFragment] =
IO.effectTotal(document.createDocumentFragment())
def createStyleSheet(
href: String,
index: Int
): UIO[CSSStyleSheet] = IO.effectTotal(document.createStyleSheet(href, index))
def createTextNode(data: String): UIO[Text] =
IO.effectTotal(document.createTextNode(data))
def createNodeIterator(
root: Node,
whatToShow: Int,
filter: NodeFilter,
entityReferenceExpansion: Boolean
): UIO[NodeIterator] = IO.effectTotal(
document.createNodeIterator(
root,
whatToShow,
filter,
entityReferenceExpansion
)
)
def createTreeWalker(
root: Node,
whatToShow: Int,
filter: NodeFilter,
entityReferenceExpansion: Boolean
): UIO[TreeWalker] = IO.effectTotal(
document.createTreeWalker(
root,
whatToShow,
filter,
entityReferenceExpansion
)
)
def exitFullscreen(): UIO[js.Promise[Unit]] =
IO.effectTotal(document.exitFullscreen())
def fullscreenElement: UIO[Element] =
IO.effectTotal(document.fullscreenElement)
def fullscreenEnabled: UIO[Boolean] =
IO.effectTotal(document.fullscreenEnabled)
val live: ZLayer[Any, Nothing, Has[DomService]] = ZLayer.succeed(
domServiceImpl
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment