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

@uppy/utils: better fallbacks for the drag & drop API #4260

Merged
merged 1 commit into from
Jan 26, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ function getAsFileSystemHandleFromEntry (entry, logDropError) {
}
}

async function* createPromiseToAddFileOrParseDirectory (entry, relativePath) {
async function* createPromiseToAddFileOrParseDirectory (entry, relativePath, lastResortFile = undefined) {
// For each dropped item, - make sure it's a file/directory, and start deepening in!
if (entry.kind === 'file') {
const file = await entry.getFile()
if (file !== null) {
file.relativePath = relativePath ? `${relativePath}/${entry.name}` : null
yield file
}
} else if (lastResortFile != null) yield lastResortFile
} else if (entry.kind === 'directory') {
for await (const handle of entry.values()) {
yield* createPromiseToAddFileOrParseDirectory(handle, `${relativePath}/${entry.name}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we using generator and yield here? Not saying we shouldn't, but it’s hard to read and I think this is the only place we use them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"hard to read" is subjective, I kinda doubt this would be more readable without using generators – not saying that this code is the most readable, but rather than we could (should?) make it way more readable while keeping generators. In my experience using async generators is the "best" way to iterate through a file tree, as in it's probably the most memory efficient while being the more elegant solution.

}
}
} else if (lastResortFile != null) yield lastResortFile
}

export default async function* getFilesFromDataTransfer (dataTransfer, logDropError) {
Expand All @@ -53,14 +53,14 @@ export default async function* getFilesFromDataTransfer (dataTransfer, logDropEr
// :entry can be null when we drop the url e.g.
if (entry != null) {
try {
yield* createPromiseToAddFileOrParseDirectory(entry, '')
yield* createPromiseToAddFileOrParseDirectory(entry, '', lastResortFile)
} catch (err) {
if (lastResortFile) {
if (lastResortFile != null) {
yield lastResortFile
} else {
logDropError(err)
}
}
}
} else if (lastResortFile != null) yield lastResortFile
}
}