selenium-webdriver
Version:
The official WebDriver JavaScript bindings from the Selenium project
1 lines • 11.8 kB
HTML
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"><meta http-equiv="Content-Language" content="en"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title>thenable.js</title><link href="../../../../dossier.css" rel="stylesheet" type="text/css"><header><div><form><div><input type="search" placeholder="Search" tabindex="1"></div></form></div></header><main><article class="srcfile"><h1>lib/goog/promise/thenable.js</h1><div><table><tr><td><a id="l1"></a><a href="#l1">1</a><td>// Copyright 2013 The Closure Library Authors. All Rights Reserved.<tr><td><a id="l2"></a><a href="#l2">2</a><td>//<tr><td><a id="l3"></a><a href="#l3">3</a><td>// Licensed under the Apache License, Version 2.0 (the "License");<tr><td><a id="l4"></a><a href="#l4">4</a><td>// you may not use this file except in compliance with the License.<tr><td><a id="l5"></a><a href="#l5">5</a><td>// You may obtain a copy of the License at<tr><td><a id="l6"></a><a href="#l6">6</a><td>//<tr><td><a id="l7"></a><a href="#l7">7</a><td>// http://www.apache.org/licenses/LICENSE-2.0<tr><td><a id="l8"></a><a href="#l8">8</a><td>//<tr><td><a id="l9"></a><a href="#l9">9</a><td>// Unless required by applicable law or agreed to in writing, software<tr><td><a id="l10"></a><a href="#l10">10</a><td>// distributed under the License is distributed on an "AS-IS" BASIS,<tr><td><a id="l11"></a><a href="#l11">11</a><td>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<tr><td><a id="l12"></a><a href="#l12">12</a><td>// See the License for the specific language governing permissions and<tr><td><a id="l13"></a><a href="#l13">13</a><td>// limitations under the License.<tr><td><a id="l14"></a><a href="#l14">14</a><td><tr><td><a id="l15"></a><a href="#l15">15</a><td>goog.provide('goog.Thenable');<tr><td><a id="l16"></a><a href="#l16">16</a><td><tr><td><a id="l17"></a><a href="#l17">17</a><td><tr><td><a id="l18"></a><a href="#l18">18</a><td><tr><td><a id="l19"></a><a href="#l19">19</a><td>/**<tr><td><a id="l20"></a><a href="#l20">20</a><td> * Provides a more strict interface for Thenables in terms of<tr><td><a id="l21"></a><a href="#l21">21</a><td> * http://promisesaplus.com for interop with {@see goog.Promise}.<tr><td><a id="l22"></a><a href="#l22">22</a><td> *<tr><td><a id="l23"></a><a href="#l23">23</a><td> * @interface<tr><td><a id="l24"></a><a href="#l24">24</a><td> * @extends {IThenable<TYPE>}<tr><td><a id="l25"></a><a href="#l25">25</a><td> * @template TYPE<tr><td><a id="l26"></a><a href="#l26">26</a><td> */<tr><td><a id="l27"></a><a href="#l27">27</a><td>goog.Thenable = function() {};<tr><td><a id="l28"></a><a href="#l28">28</a><td><tr><td><a id="l29"></a><a href="#l29">29</a><td><tr><td><a id="l30"></a><a href="#l30">30</a><td>/**<tr><td><a id="l31"></a><a href="#l31">31</a><td> * Adds callbacks that will operate on the result of the Thenable, returning a<tr><td><a id="l32"></a><a href="#l32">32</a><td> * new child Promise.<tr><td><a id="l33"></a><a href="#l33">33</a><td> *<tr><td><a id="l34"></a><a href="#l34">34</a><td> * If the Thenable is fulfilled, the {@code onFulfilled} callback will be<tr><td><a id="l35"></a><a href="#l35">35</a><td> * invoked with the fulfillment value as argument, and the child Promise will<tr><td><a id="l36"></a><a href="#l36">36</a><td> * be fulfilled with the return value of the callback. If the callback throws<tr><td><a id="l37"></a><a href="#l37">37</a><td> * an exception, the child Promise will be rejected with the thrown value<tr><td><a id="l38"></a><a href="#l38">38</a><td> * instead.<tr><td><a id="l39"></a><a href="#l39">39</a><td> *<tr><td><a id="l40"></a><a href="#l40">40</a><td> * If the Thenable is rejected, the {@code onRejected} callback will be invoked<tr><td><a id="l41"></a><a href="#l41">41</a><td> * with the rejection reason as argument, and the child Promise will be rejected<tr><td><a id="l42"></a><a href="#l42">42</a><td> * with the return value of the callback or thrown value.<tr><td><a id="l43"></a><a href="#l43">43</a><td> *<tr><td><a id="l44"></a><a href="#l44">44</a><td> * @param {?(function(this:THIS, TYPE): VALUE)=} opt_onFulfilled A<tr><td><a id="l45"></a><a href="#l45">45</a><td> * function that will be invoked with the fulfillment value if the Promise<tr><td><a id="l46"></a><a href="#l46">46</a><td> * is fullfilled.<tr><td><a id="l47"></a><a href="#l47">47</a><td> * @param {?(function(this:THIS, *): *)=} opt_onRejected A function that will<tr><td><a id="l48"></a><a href="#l48">48</a><td> * be invoked with the rejection reason if the Promise is rejected.<tr><td><a id="l49"></a><a href="#l49">49</a><td> * @param {THIS=} opt_context An optional context object that will be the<tr><td><a id="l50"></a><a href="#l50">50</a><td> * execution context for the callbacks. By default, functions are executed<tr><td><a id="l51"></a><a href="#l51">51</a><td> * with the default this.<tr><td><a id="l52"></a><a href="#l52">52</a><td> *<tr><td><a id="l53"></a><a href="#l53">53</a><td> * @return {RESULT} A new Promise that will receive the result<tr><td><a id="l54"></a><a href="#l54">54</a><td> * of the fulfillment or rejection callback.<tr><td><a id="l55"></a><a href="#l55">55</a><td> * @template VALUE<tr><td><a id="l56"></a><a href="#l56">56</a><td> * @template THIS<tr><td><a id="l57"></a><a href="#l57">57</a><td> *<tr><td><a id="l58"></a><a href="#l58">58</a><td> * When a Promise (or thenable) is returned from the fulfilled callback,<tr><td><a id="l59"></a><a href="#l59">59</a><td> * the result is the payload of that promise, not the promise itself.<tr><td><a id="l60"></a><a href="#l60">60</a><td> *<tr><td><a id="l61"></a><a href="#l61">61</a><td> * @template RESULT := type('goog.Promise',<tr><td><a id="l62"></a><a href="#l62">62</a><td> * cond(isUnknown(VALUE), unknown(),<tr><td><a id="l63"></a><a href="#l63">63</a><td> * mapunion(VALUE, (V) =><tr><td><a id="l64"></a><a href="#l64">64</a><td> * cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'),<tr><td><a id="l65"></a><a href="#l65">65</a><td> * templateTypeOf(V, 0),<tr><td><a id="l66"></a><a href="#l66">66</a><td> * cond(sub(V, 'Thenable'),<tr><td><a id="l67"></a><a href="#l67">67</a><td> * unknown(),<tr><td><a id="l68"></a><a href="#l68">68</a><td> * V)))))<tr><td><a id="l69"></a><a href="#l69">69</a><td> * =:<tr><td><a id="l70"></a><a href="#l70">70</a><td> *<tr><td><a id="l71"></a><a href="#l71">71</a><td> */<tr><td><a id="l72"></a><a href="#l72">72</a><td>goog.Thenable.prototype.then = function(opt_onFulfilled, opt_onRejected,<tr><td><a id="l73"></a><a href="#l73">73</a><td> opt_context) {};<tr><td><a id="l74"></a><a href="#l74">74</a><td><tr><td><a id="l75"></a><a href="#l75">75</a><td><tr><td><a id="l76"></a><a href="#l76">76</a><td>/**<tr><td><a id="l77"></a><a href="#l77">77</a><td> * An expando property to indicate that an object implements<tr><td><a id="l78"></a><a href="#l78">78</a><td> * {@code goog.Thenable}.<tr><td><a id="l79"></a><a href="#l79">79</a><td> *<tr><td><a id="l80"></a><a href="#l80">80</a><td> * {@see addImplementation}.<tr><td><a id="l81"></a><a href="#l81">81</a><td> *<tr><td><a id="l82"></a><a href="#l82">82</a><td> * @const<tr><td><a id="l83"></a><a href="#l83">83</a><td> */<tr><td><a id="l84"></a><a href="#l84">84</a><td>goog.Thenable.IMPLEMENTED_BY_PROP = '$goog_Thenable';<tr><td><a id="l85"></a><a href="#l85">85</a><td><tr><td><a id="l86"></a><a href="#l86">86</a><td><tr><td><a id="l87"></a><a href="#l87">87</a><td>/**<tr><td><a id="l88"></a><a href="#l88">88</a><td> * Marks a given class (constructor) as an implementation of Thenable, so<tr><td><a id="l89"></a><a href="#l89">89</a><td> * that we can query that fact at runtime. The class must have already<tr><td><a id="l90"></a><a href="#l90">90</a><td> * implemented the interface.<tr><td><a id="l91"></a><a href="#l91">91</a><td> * Exports a 'then' method on the constructor prototype, so that the objects<tr><td><a id="l92"></a><a href="#l92">92</a><td> * also implement the extern {@see goog.Thenable} interface for interop with<tr><td><a id="l93"></a><a href="#l93">93</a><td> * other Promise implementations.<tr><td><a id="l94"></a><a href="#l94">94</a><td> * @param {function(new:goog.Thenable,...?)} ctor The class constructor. The<tr><td><a id="l95"></a><a href="#l95">95</a><td> * corresponding class must have already implemented the interface.<tr><td><a id="l96"></a><a href="#l96">96</a><td> */<tr><td><a id="l97"></a><a href="#l97">97</a><td>goog.Thenable.addImplementation = function(ctor) {<tr><td><a id="l98"></a><a href="#l98">98</a><td> goog.exportProperty(ctor.prototype, 'then', ctor.prototype.then);<tr><td><a id="l99"></a><a href="#l99">99</a><td> if (COMPILED) {<tr><td><a id="l100"></a><a href="#l100">100</a><td> ctor.prototype[goog.Thenable.IMPLEMENTED_BY_PROP] = true;<tr><td><a id="l101"></a><a href="#l101">101</a><td> } else {<tr><td><a id="l102"></a><a href="#l102">102</a><td> // Avoids dictionary access in uncompiled mode.<tr><td><a id="l103"></a><a href="#l103">103</a><td> ctor.prototype.$goog_Thenable = true;<tr><td><a id="l104"></a><a href="#l104">104</a><td> }<tr><td><a id="l105"></a><a href="#l105">105</a><td>};<tr><td><a id="l106"></a><a href="#l106">106</a><td><tr><td><a id="l107"></a><a href="#l107">107</a><td><tr><td><a id="l108"></a><a href="#l108">108</a><td>/**<tr><td><a id="l109"></a><a href="#l109">109</a><td> * @param {*} object<tr><td><a id="l110"></a><a href="#l110">110</a><td> * @return {boolean} Whether a given instance implements {@code goog.Thenable}.<tr><td><a id="l111"></a><a href="#l111">111</a><td> * The class/superclass of the instance must call {@code addImplementation}.<tr><td><a id="l112"></a><a href="#l112">112</a><td> */<tr><td><a id="l113"></a><a href="#l113">113</a><td>goog.Thenable.isImplementedBy = function(object) {<tr><td><a id="l114"></a><a href="#l114">114</a><td> if (!object) {<tr><td><a id="l115"></a><a href="#l115">115</a><td> return false;<tr><td><a id="l116"></a><a href="#l116">116</a><td> }<tr><td><a id="l117"></a><a href="#l117">117</a><td> try {<tr><td><a id="l118"></a><a href="#l118">118</a><td> if (COMPILED) {<tr><td><a id="l119"></a><a href="#l119">119</a><td> return !!object[goog.Thenable.IMPLEMENTED_BY_PROP];<tr><td><a id="l120"></a><a href="#l120">120</a><td> }<tr><td><a id="l121"></a><a href="#l121">121</a><td> return !!object.$goog_Thenable;<tr><td><a id="l122"></a><a href="#l122">122</a><td> } catch (e) {<tr><td><a id="l123"></a><a href="#l123">123</a><td> // Property access seems to be forbidden.<tr><td><a id="l124"></a><a href="#l124">124</a><td> return false;<tr><td><a id="l125"></a><a href="#l125">125</a><td> }<tr><td><a id="l126"></a><a href="#l126">126</a><td>};</table></div></article><nav><h3><a href="../../../../index.html" tabindex="2">Overview</a></h3><div><input type="checkbox" id="nav-modules" checked/><label for="nav-modules"><h3><span class="selectable" tabindex="2">Modules</span></h3></label><div id="nav-modules-view"></div></div><div><input type="checkbox" id="nav-types" checked/><label for="nav-types"><h3><span class="selectable" tabindex="2">Types</span></h3></label><div id="nav-types-view"></div></div><h3><a href="../../../../Changes.html" tabindex="2">Changes</a></h3></nav></main><footer><div><a href="https://github.com/jleyba/js-dossier">Generated by dossier</a></div></footer><script src="../../../../types.js"></script><script src="../../../../dossier.js"></script>