All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gwen.web.eval.driver.event.WebSessionEventDispatcher.scala Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
/*
 * Copyright 2022 Branko Juric, Brady Wood
 * 
 * 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 gwen.web.eval.driver.event

import scala.collection.mutable
import scala.util.chaining._

import com.typesafe.scalalogging.LazyLogging

import org.openqa.selenium.WebDriver

class WebSessionEventDispatcher extends LazyLogging {

  private val listeners = new mutable.Queue[WebSessionEventListener]()

  def addListener(listener: WebSessionEventListener): Unit = {
    listeners += listener
  }

  def removeListener(listener: WebSessionEventListener): Unit = {
    listeners -= listener
  }

  def sessionOpened(driver: WebDriver): Unit = {
    dispatchEvent(WebSessionPhase.opened, driver)
  }

  def sessionClosed(driver: WebDriver): Unit = {
    dispatchEvent(WebSessionPhase.closed, driver)
  }

  private def dispatchEvent(phase: WebSessionPhase, driver: WebDriver): Unit = {
    listeners foreach { listener =>
      WebSessionEvent(phase, driver) tap { event =>
        phase match {
          case WebSessionPhase.opened => listener.sessionOpened(event)
          case WebSessionPhase.closed => listener.sessionClosed(event)
        }
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy