This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Repository navigation
Improve the typings for goog.Promise. #689
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/java/com/google/javascript/clutz/goog_promise_with_platform.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| declare namespace ಠ_ಠ.clutz.goog { | ||
| class Promise < TYPE , RESOLVER_CONTEXT > extends Promise_Instance < TYPE , RESOLVER_CONTEXT > { | ||
| static all < TYPE > (promises : any [] ) : ಠ_ಠ.clutz.goog.Promise < TYPE [] , any > ; | ||
| static race < TYPE > (promises : any [] ) : ಠ_ಠ.clutz.goog.Promise < TYPE , any > ; | ||
| static resolve < T >(value: ಠ_ಠ.clutz.goog.Promise < T , any > | T): any; | ||
| } | ||
| class Promise_Instance < TYPE , RESOLVER_CONTEXT > implements ಠ_ಠ.clutz.goog.Thenable < TYPE > { | ||
| private noStructuralTyping_: any; | ||
| constructor (resolver : (a : (a ? : TYPE | PromiseLike < TYPE > | null | { then : any } ) => any , b : (a ? : any ) => any ) => void , opt_context ? : RESOLVER_CONTEXT ) ; | ||
| then < RESULT > (opt_onFulfilled ? : ( (a : TYPE ) => any | RESULT ) | null , opt_onRejected ? : ( (a : any ) => any ) | null) : any ; | ||
| } | ||
| } | ||
| declare module 'goog:goog.Promise' { | ||
| import alias = ಠ_ಠ.clutz.goog.Promise; | ||
| export default alias; | ||
| } | ||
| declare namespace ಠ_ಠ.clutz.goog { | ||
| interface Thenable < TYPE > extends PromiseLike < TYPE > { | ||
| then < RESULT > (opt_onFulfilled ? : ( (a : TYPE ) => ಠ_ಠ.clutz.goog.Thenable < RESULT > | RESULT ) | null , opt_onRejected ? : ( (a : any ) => any ) | null) : ಠ_ಠ.clutz.goog.Thenable < RESULT > ; | ||
| } | ||
| } | ||
| declare namespace ಠ_ಠ.clutz.goog.Thenable { | ||
| var IMPLEMENTED_BY_PROP : string ; | ||
| function addImplementation (ctor : { new ( ...a : any [] ) : ಠ_ಠ.clutz.goog.Thenable < any > } ) : void ; | ||
| function isImplementedBy (object : any ) : boolean ; | ||
| } | ||
| declare module 'goog:goog.Thenable' { | ||
| import alias = ಠ_ಠ.clutz.goog.Thenable; | ||
| export default alias; | ||
| } |
132 changes: 132 additions & 0 deletions
132
src/test/java/com/google/javascript/clutz/goog_promise_with_platform.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| //!! goog.Promise (https://github.com/google/closure-library/blob/master/closure/goog/promise/promise.js), | ||
| //!! unlike other implementations of Promise, has 2 template params this test ensures | ||
| //!! that the specially handled TTE methods work for it as well | ||
| goog.provide('goog.Promise'); | ||
| goog.provide('goog.Thenable'); | ||
|
|
||
| /** | ||
| * @interface | ||
| * @extends {IThenable<TYPE>} | ||
| * @template TYPE | ||
| */ | ||
| goog.Thenable = function() {}; | ||
|
|
||
|
|
||
| /** | ||
| * @param {?(function(this:THIS, TYPE): VALUE)=} opt_onFulfilled A | ||
| * function that will be invoked with the fulfillment value if the Promise | ||
| * is fulfilled. | ||
| * @param {?(function(this:THIS, *): *)=} opt_onRejected A function that will | ||
| * be invoked with the rejection reason if the Promise is rejected. | ||
| * @param {THIS=} opt_context An optional context object that will be the | ||
| * execution context for the callbacks. By default, functions are executed | ||
| * with the default this. | ||
| * | ||
| * @return {RESULT} A new Promise that will receive the result | ||
| * of the fulfillment or rejection callback. | ||
| * @template VALUE | ||
| * @template THIS | ||
| * | ||
| * When a Promise (or thenable) is returned from the fulfilled callback, | ||
| * the result is the payload of that promise, not the promise itself. | ||
| * | ||
| * @template RESULT := type('goog.Promise', | ||
| * cond(isUnknown(VALUE), unknown(), | ||
| * mapunion(VALUE, (V) => | ||
| * cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'), | ||
| * templateTypeOf(V, 0), | ||
| * cond(sub(V, 'Thenable'), | ||
| * unknown(), | ||
| * V))))) | ||
| * =: | ||
| * | ||
| */ | ||
| goog.Thenable.prototype.then = function( | ||
| opt_onFulfilled, opt_onRejected, opt_context) {}; | ||
|
|
||
|
|
||
| /** | ||
| * @const | ||
| */ | ||
| goog.Thenable.IMPLEMENTED_BY_PROP = '$goog_Thenable'; | ||
|
|
||
|
|
||
| /** | ||
| * @param {function(new:goog.Thenable,...?)} ctor The class constructor. The | ||
| * corresponding class must have already implemented the interface. | ||
| */ | ||
| goog.Thenable.addImplementation = function(ctor) {}; | ||
|
|
||
|
|
||
| /** | ||
| * @param {?} object | ||
| * @return {boolean} Whether a given instance implements `goog.Thenable`. | ||
| * The class/superclass of the instance must call `addImplementation`. | ||
| */ | ||
| goog.Thenable.isImplementedBy = function(object) {}; | ||
|
|
||
| /** | ||
| * @param {function( | ||
| * this:RESOLVER_CONTEXT, | ||
| * function((TYPE|IThenable<TYPE>|Thenable)=), | ||
| * function(*=)): void} resolver | ||
| * Initialization function that is invoked immediately with `resolve` | ||
| * and `reject` functions as arguments. The Promise is resolved or | ||
| * rejected with the first argument passed to either function. | ||
| * @param {RESOLVER_CONTEXT=} opt_context An optional context for executing the | ||
| * resolver function. If unspecified, the resolver function will be executed | ||
| * in the default scope. | ||
| * @constructor | ||
| * @struct | ||
| * @final | ||
| * @implements {goog.Thenable<TYPE>} | ||
| * @template TYPE,RESOLVER_CONTEXT | ||
| */ | ||
| goog.Promise = function(resolver, opt_context) {}; | ||
|
|
||
| /** | ||
| * @param {VALUE=} opt_value | ||
| * @return {RESULT} A new Promise that is immediately resolved | ||
| * with the given value. If the input value is already a goog.Promise, it | ||
| * will be returned immediately without creating a new instance. | ||
| * @template VALUE | ||
| * @template RESULT := type('goog.Promise', | ||
| * cond(isUnknown(VALUE), unknown(), | ||
| * mapunion(VALUE, (V) => | ||
| * cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'), | ||
| * templateTypeOf(V, 0), | ||
| * cond(sub(V, 'Thenable'), | ||
| * unknown(), | ||
| * V))))) | ||
| * =: | ||
| */ | ||
| goog.Promise.resolve = function(opt_value) {}; | ||
|
|
||
| /** | ||
| * @param {!Array<?(goog.Promise<TYPE>|goog.Thenable<TYPE>|Thenable|*)>} | ||
| * promises | ||
| * @return {!goog.Promise<TYPE>} A Promise that receives the result of the | ||
| * first Promise (or Promise-like) input to settle immediately after it | ||
| * settles. | ||
| * @template TYPE | ||
| */ | ||
| goog.Promise.race = function(promises) {}; | ||
|
|
||
|
|
||
| /** | ||
| * @param {!Array<?(goog.Promise<TYPE>|goog.Thenable<TYPE>|Thenable|*)>} | ||
| * promises | ||
| * @return {!goog.Promise<!Array<TYPE>>} A Promise that receives a list of | ||
| * every fulfilled value once every input Promise (or Promise-like) is | ||
| * successfully fulfilled, or is rejected with the first rejection reason | ||
| * immediately after it is rejected. | ||
| * @template TYPE | ||
| */ | ||
| goog.Promise.all = function(promises) {}; | ||
|
|
||
| /** | ||
| * @override | ||
| */ | ||
| goog.Promise.prototype.then = function( | ||
| opt_onFulfilled, opt_onRejected, opt_context) {}; | ||
| goog.Thenable.addImplementation(goog.Promise); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks wrong? where is ಠ_ಠ.clutz.module$exports$tte$Promise$Partial defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right here. PartialDefered_Instance is extended by PartialDeferred, which are both in the ಠ_ಠ.clutz.module$exports$tte$Promise$Partial namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I guess I expected
PartialDeferredbut doesn't hurt to fully qualify.