convert uses of "defer" to "new Promise" in client/jsonview

RESOLVED FIXED in Firefox 58

Status

enhancement
RESOLVED FIXED
2 years ago
11 months ago

People

(Reporter: mkohler, Assigned: Oriol)

Tracking

(Blocks 1 bug)

unspecified
Firefox 58
Dependency tree / graph

Firefox Tracking Flags

(firefox58 fixed)

Details

Attachments

(1 attachment, 1 obsolete attachment)

Reporter

Description

2 years ago
No description provided.
Comment hidden (mozreview-request)
Reporter

Updated

2 years ago
Attachment #8869789 - Flags: review?(nchevobbe)

Comment 3

2 years ago
mozreview-review
Comment on attachment 8869789 [details]
Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview

https://reviewboard.mozilla.org/r/141342/#review145112

::: devtools/client/jsonview/test/head.js:30
(Diff revision 1)
>  function addJsonViewTab(url) {
>    info("Adding a new JSON tab with URL: '" + url + "'");
>  
> -  let deferred = promise.defer();
> +  return addTab(url).then(tab => {
> -  addTab(url).then(tab => {
>      let browser = tab.linkedBrowser;
>  
>      // Load devtools/shared/frame-script-utils.js
>      getFrameScript();
>  
>      // Load frame script with helpers for JSON View tests.
>      let rootDir = getRootDirectory(gTestPath);
>      let frameScriptUrl = rootDir + "doc_frame_script.js";
>      browser.messageManager.loadFrameScript(frameScriptUrl, false);
>  
>      // Resolve if the JSONView is fully loaded or wait
>      // for an initialization event.
>      if (content.window.wrappedJSObject.jsonViewInitialized) {
> -      deferred.resolve(tab);
> +      return tab;
> -    } else {
> -      waitForContentMessage("Test:JsonView:JSONViewInitialized").then(() => {
> -        deferred.resolve(tab);
> -      });
>      }
> -  });
>  
> -  return deferred.promise;
> +    return waitForContentMessage("Test:JsonView:JSONViewInitialized").then(() => {
> +      return tab;
> +    });
> +  });

I think we could turn this function into an async one.
Do you mind to try Michael ?
Comment hidden (mozreview-request)

Comment 6

2 years ago
mozreview-review
Comment on attachment 8869789 [details]
Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview

https://reviewboard.mozilla.org/r/141342/#review147430

All good, thanks !
I pushed to TRY and will land this if it's green
Attachment #8869789 - Flags: review?(nchevobbe) → review+
Reporter

Comment 7

2 years ago
(In reply to Nicolas Chevobbe [:nchevobbe] from comment #6)
> I pushed to TRY and will land this if it's green

Was it green? Can't find it somehow :(
Assignee

Comment 8

2 years ago
Michael, I'm afraid you will need to redo your patch, because in bug 1368605 I added a parameter to addJsonViewTab.
Reporter

Comment 9

2 years ago
(In reply to Oriol [:Oriol] from comment #8)
> Michael, I'm afraid you will need to redo your patch, because in bug 1368605
> I added a parameter to addJsonViewTab.

No worries. If everything goes well with my tasks before the All Hands, I will be able to do this on Saturday. Otherwise it will need to wait until after the All Hands (or I might be able to do it in the plane, we'll see).
Reporter

Comment 10

2 years ago
Unassigning myself as there has been no recent action from my side and I don't see myself doing any action on this for the next few weeks.

Sorry about this, if somebody wants to take this, go for it! :)
Assignee: me → nobody
Status: ASSIGNED → NEW
Comment hidden (mozreview-request)
Assignee

Comment 12

2 years ago
Continuing Michael's patch.
Assignee: nobody → oriol-bugzilla
Status: NEW → ASSIGNED
Attachment #8869789 - Attachment is obsolete: true

Comment 13

2 years ago
mozreview-review
Comment on attachment 8926603 [details]
Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview

https://reviewboard.mozilla.org/r/197834/#review203232

Thanks for taking it Oriol.
I have a nit and a comment, what do you think ?

::: devtools/client/jsonview/test/head.js:59
(Diff revision 1)
> -    // Add a timeout.
> +  // Otherwise wait for an initialization event, but with a time limit.
> +  return new Promise(async function (resolve, reject) {
> +    waitForContentMessage("Test:JsonView:JSONViewInitialized").then(() => {
> +      resolve(tab);
> +    });
>      if (timeout >= 0) {
> -      new Promise(resolve => {
> -        if (content.window.document.readyState === "complete") {
> +      if (content.window.document.readyState !== "complete") {
> +        await waitForContentMessage("Test:JsonView:load");
> -          resolve();
> -        } else {
> -          waitForContentMessage("Test:JsonView:load").then(resolve);
> -        }
> +      }
> -      }).then(() => {
> -        setTimeout(() => {
> +      await waitForTime(timeout);
> +      reject(new Error("JSON Viewer did not load."));
> -          deferred.reject("JSON Viewer did not load.");
> -        }, timeout);
> -      });
>      }

This is a bit hard to follow, maybe we could make this easier. What do you think of :


```
const onJSONViewInitialized = waitForContentMessage("Test:JsonView:JSONViewInitialized");
if (timeout <= 0) {
  return onJSONViewInitialized;
}

if (content.window.document.readyState !== "complete") {
  await waitForContentMessage("Test:JsonView:load");
}

let timeoutId = setTimeout(() => throw new Error("JSON Viewer did not load."), timeout);
const tab = await onJSONViewInitialized;
// Cancel throwing, or we get an uncaught exception warning.
clearTimeout(timeoutId);

return tab;
```

::: devtools/client/jsonview/test/head.js:157
(Diff revision 1)
>  
>    return executeInContent("Test:JsonView:SendString", data);
>  }
>  
>  function waitForTime(delay) {
> -  let deferred = defer();
> +  return new Promise((resolve, reject) => {

eslint might complain of unused  `reject`, I think we can remove it. 

(In case you don't know, In TRY push we now have to use  `-j eslint` to trigger eslint job. they are not added automatically like they used to be)
Assignee

Comment 14

2 years ago
(In reply to Nicolas Chevobbe [:nchevobbe] from comment #13)
> let timeoutId = setTimeout(() => throw new Error("JSON Viewer did not load."), timeout);

But throwing in a timer won't reject the promise, right?

If you run this code in the console

   var p = (async function() { 
      setTimeout(() => {throw new Error()}, 0);
      await new Promise(resolve => setTimeout(resolve, 1e3));
   })()

then you first see the error but p is not rejected. Instead, after 1 second it resolves to undefined.
(In reply to Oriol Brufau [:Oriol] from comment #14)
> (In reply to Nicolas Chevobbe [:nchevobbe] from comment #13)
> > let timeoutId = setTimeout(() => throw new Error("JSON Viewer did not load."), timeout);
> 
> But throwing in a timer won't reject the promise, right?
> 
> If you run this code in the console
> 
>    var p = (async function() { 
>       setTimeout(() => {throw new Error()}, 0);
>       await new Promise(resolve => setTimeout(resolve, 1e3));
>    })()
> 
> then you first see the error but p is not rejected. Instead, after 1 second
> it resolves to undefined.

True, something like that would work : 
```
   var p = async function(wait) { 
     let onTimeout = new Promise((_, rej) => setTimeout(rej, 1000))
     let onExpected = new Promise(resolve => setTimeout(resolve, wait));
     await Promise.race([onExpected, onTimeout]);
     return await onExpected;
   };
   // prints 10 yay
   p(10).then(() => console.log(10, "yay")).catch((e)=> console.warn(10, "nah", e))
   // prints 2000 nah
   p(2000).then(() => console.log(2000, "yay")).catch((e)=> console.warn(2000, "nah", e))
```

which could be translated to : 


```
const onJSONViewInitialized = waitForContentMessage("Test:JsonView:JSONViewInitialized");
if (timeout <= 0) {
  return onJSONViewInitialized;
}

if (content.window.document.readyState !== "complete") {
  await waitForContentMessage("Test:JsonView:load");
}

let onTimeout = new Promise((_, rej) => 
  setTimeout(() => reject(new Error("JSON Viewer did not load.")), timeout));

await Promise.race([onJSONViewInitialized, onTimeout]);
return await onJSONViewInitialized;
```


Do you think that would work ?
Comment hidden (mozreview-request)
Comment hidden (mozreview-request)
Assignee

Comment 18

2 years ago
I only added .then(() => tab) to onJSONViewInitialized, and checked !(timeout >= 0) because to handle undefined properly.

Comment 19

2 years ago
mozreview-review
Comment on attachment 8926603 [details]
Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview

https://reviewboard.mozilla.org/r/197834/#review203360

Looks good, thanks Oriol. r+ if TRY is green
Attachment #8926603 - Flags: review?(nchevobbe) → review+
Assignee

Updated

2 years ago
Keywords: checkin-needed

Comment 20

2 years ago
Pushed by ryanvm@gmail.com:
https://hg.mozilla.org/integration/autoland/rev/4c25a8edd5db
convert uses of defer to 'new Promise' in client/jsonview r=nchevobbe
Keywords: checkin-needed

Comment 21

2 years ago
bugherder
https://hg.mozilla.org/mozilla-central/rev/4c25a8edd5db
Status: ASSIGNED → RESOLVED
Last Resolved: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → Firefox 58
Assignee

Updated

2 years ago
Depends on: 1421631
Assignee

Updated

2 years ago
No longer depends on: 1421631

Updated

11 months ago
Product: Firefox → DevTools
You need to log in before you can comment on or make changes to this bug.