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

com.github.stormbit.playwright.impl.WorkerImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) Microsoft Corporation.
 *
 * 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 com.github.stormbit.playwright.impl;

import com.github.stormbit.playwright.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import static com.github.stormbit.playwright.impl.Serialization.*;

class WorkerImpl extends ChannelOwner implements Worker {
  private final ListenerCollection listeners = new ListenerCollection<>();
  PageImpl page;

  WorkerImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
    super(parent, type, guid, initializer);
  }

  @Override
  public void addListener(EventType type, Listener listener) {
    listeners.add(type, listener);
  }

  @Override
  public void removeListener(EventType type, Listener listener) {
    listeners.remove(type, listener);
  }

  @Override
  public Object evaluate(String pageFunction, Object arg) {
    JsonObject params = new JsonObject();
    params.addProperty("expression", pageFunction);
    params.addProperty("isFunction", Utils.isFunctionBody(pageFunction));
    params.add("arg", gson().toJsonTree(serializeArgument(arg)));
    JsonElement json = sendMessage("evaluateExpression", params);
    SerializedValue value = gson().fromJson(json.getAsJsonObject().get("value"), SerializedValue.class);
    return deserialize(value);
  }

  @Override
  public JSHandle evaluateHandle(String pageFunction, Object arg) {
    JsonObject params = new JsonObject();
    params.addProperty("expression", pageFunction);
    params.addProperty("isFunction", Utils.isFunctionBody(pageFunction));
    params.add("arg", gson().toJsonTree(serializeArgument(arg)));
    JsonElement json = sendMessage("evaluateExpressionHandle", params);
    return connection.getExistingObject(json.getAsJsonObject().getAsJsonObject("handle").get("guid").getAsString());
  }

  @Override
  public String url() {
    return initializer.get("url").getAsString();
  }

  @Override
  public Deferred> waitForEvent(EventType event) {
    return toDeferred(new WaitableEvent<>(listeners, event));
  }

  @Override
  protected void handleEvent(String event, JsonObject params) {
    if ("close".equals(event)) {
      if (page != null) {
        page.workers.remove(this);
      }
      listeners.notify(EventType.CLOSE, this);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy