Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(filelist): rename promise -> lastCompletedRefresh and remove… #3060

Merged
merged 1 commit into from
Jul 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class FileList {

this.buckets = new Map()

this._refreshing = Promise.resolve()
// A promise that is pending if and only if we are active in this.refresh_()
this._refreshing = null

const emit = () => {
this._emitter.emit('file_list_modified', this.files)
Expand Down Expand Up @@ -65,8 +66,8 @@ class FileList {
_refresh () {
const matchedFiles = new Set()

let promise
promise = Promise.map(this._patterns, (patternObject) => {
let lastCompletedRefresh = this._refreshing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read the comment below ;-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's possible to write unit tests that fails when you replace let lastCompletedRefresh = this._refreshing with let lastCompletedRefresh - like it was before ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well maybe I'm confused. To me

let lastCompletedRefresh = Promise.map(this._patterns, (patternObject) => {...})
   .then(() => { 
        if (this._refreshing !== lastCompletedRefresh) {
          return this._refreshing
        }
     })
...

means that once this statement executes, lastCompletedRefresh is set to the promise returned by the RHS. In the promise callback the value is unambiguous. However this line will cause the test to fail with ReferenceError: lastCompletedRefresh is not defined.
If we write

let lastCompletedRefresh = null
lastCompletedRefresh = Promise.map(this._patterns, (patternObject) => {...})
   .then(() => { 
        if (lastCompletedRefresh === null) {
          throw new Error('how can this be')
        }
     })
...

Then one of the unit tests throws "how can this be". I don't understand how the then call back can run before the promise assignment.

So, no, but I also don't understand the unit tests behavior now.

(This long chain of promises at different levels is to hard to read. IMO it should be refactored to make the chains clearer).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then one of the unit tests throws "how can this be". I don't understand how the then call back can run before the promise assignment.

Because of something like this, which causes promise callbacks to be executed synchronously (and therefore before the assignment).

I'm working on simplifying this piece now.

lastCompletedRefresh = Promise.map(this._patterns, (patternObject) => {
const pattern = patternObject.pattern
const type = patternObject.type

Expand Down Expand Up @@ -113,14 +114,18 @@ class FileList {
})
})
.then(() => {
if (this._refreshing !== promise) {
// When we return from this function the file processing chain will be
// complete. In the case of two fast refresh() calls, the second call
// will overwrite this._refreshing, and we want the status to reflect
// the second call and skip the modification event from the first call.
if (this._refreshing !== lastCompletedRefresh) {
return this._refreshing
}
this._emitModified(true)
return this.files
})

return promise
return lastCompletedRefresh
}

get files () {
Expand Down