# executors.js

    1// Copyright 2013 Software Freedom Conservancy. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415/**16 * @fileoverview Various utilities for working with17 * {@link webdriver.CommandExecutor} implementations.18 */1920var HttpClient = require('./http').HttpClient,21    HttpExecutor = require('./http').Executor,22    promise = require('./_base').require('webdriver.promise');23242526/**27 * Wraps a promised {@link webdriver.CommandExecutor}, ensuring no commands28 * are executed until the wrapped executor has been fully built.29 * @param {!webdriver.promise.Promise.<!webdriver.CommandExecutor>} delegate The30 *     promised delegate.31 * @constructor32 * @implements {webdriver.CommandExecutor}33 */34var DeferredExecutor = function(delegate) {3536  /** @override */37  this.execute = function(command, callback) {38    delegate.then(function(executor) {39      executor.execute(command, callback);40    }, callback);41  };42};434445// PUBLIC API464748/**49 * Creates a command executor that uses WebDriver's JSON wire protocol.50 * @param {(string|!webdriver.promise.Promise.<string>)} url The server's URL,51 *     or a promise that will resolve to that URL.52 * @returns {!webdriver.CommandExecutor} The new command executor.53 */54exports.createExecutor = function(url) {55  return new DeferredExecutor(promise.when(url, function(url) {56    var client = new HttpClient(url);57    return new HttpExecutor(client);58  }));59};