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

Add higher-level finishCallback closure #46

Merged
merged 2 commits into from
Apr 7, 2024

Conversation

pcbeard
Copy link
Contributor

@pcbeard pcbeard commented Apr 7, 2024

Also fixes a bug in .load(path:) that was returning false when file was successfully loaded.

@pcbeard
Copy link
Contributor Author

pcbeard commented Apr 7, 2024

Not sure how to fix the Lint issues. Also, I re-pushed the branch, after rebasing locally. Don't like seeing merge commits in topic branches.

Also fixes a bug in .load(path:) that was returning false when
file was successfully loaded.
@pcbeard pcbeard force-pushed the FinishCallbackClosure branch from 73eb6c9 to 216899b Compare April 7, 2024 17:41
Copy link
Owner

@finnvoor finnvoor left a comment

Choose a reason for hiding this comment

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

Just needs two small changes, otherwise looks good!

Comment on lines 38 to 45
@discardableResult public func load(path: StaticString) -> Bool {
fileplayer.loadIntoPlayer(pointer, path.utf8Start) == 0
fileplayer.loadIntoPlayer(pointer, path.utf8Start) == 1
}

/// Prepares player to stream the file at path. Returns `true` if the file exists, otherwise `false`.
@discardableResult public func load(path: UnsafePointer<CChar>) -> Bool {
fileplayer.loadIntoPlayer(pointer, path) == 0
fileplayer.loadIntoPlayer(pointer, path) == 1
}
Copy link
Owner

Choose a reason for hiding this comment

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

I think it would be nice to eventually make these throw an error if the file doesn't exist instead of returning false, but this is a good fix for now

Sources/PlaydateKit/Core/Sound.swift Outdated Show resolved Hide resolved
Sources/PlaydateKit/Core/Sound.swift Outdated Show resolved Hide resolved
Comment on lines +62 to +83
public var finishCallback: (() -> Void)? {
get {
_callbackData?.pointee.callback
}
set {
if let callbackData = _callbackData {
callbackData.pointee.callback = newValue
} else if let newValue {
let callbackData: UnsafeMutablePointer<CallbackData> = .allocate(capacity: 1)
callbackData.initialize(to: .init(callback: newValue))
_callbackData = callbackData
setFinishCallback(
callback: { sourceSource, userdata in
if let callback = userdata?.assumingMemoryBound(to: CallbackData.self).pointee.callback {
callback()
}
},
soundUserdata: callbackData
)
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

This seems like a nice approach for this. Alternatives would be to mark FilePlayer as open, set the userdata to a pointer to the file player, and have it call an open didFinish() method that people can override (similar to what I'm doing in #32). Another option might be to allow setting a delegate on the FilePlayer, which would align with iOS APIs.

Don't really have much of a preference here, just thinking out loud.

Copy link
Contributor Author

@pcbeard pcbeard Apr 7, 2024

Choose a reason for hiding this comment

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

Another approach might pass the FilePlayer as a parameter to the closure. This may be preferable, to avoid creating cycles, since weak is unavailable in embedded Swift (which is a shame). The only tricky bit is that the FilePlayer pointer would have to be stored as well in the callback, as you mentioned above. And sub-classing seems less flexible to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One other tiny change might be to reorder the parameters of setFinishCallback() so a trailing closure can be used.

@finnvoor
Copy link
Owner

finnvoor commented Apr 7, 2024

Not sure how to fix the Lint issues. Also, I re-pushed the branch, after rebasing locally. Don't like seeing merge commits in topic branches.

Formatting is done using swiftformat. Still trying to figure out what the best way to enforce that is, I'll probably add an action so you can request a format with a comment or something. I can just format after merging for now.

@pcbeard pcbeard requested a review from finnvoor April 7, 2024 18:42
@finnvoor finnvoor merged commit 5dfc852 into finnvoor:main Apr 7, 2024
1 of 2 checks passed
@pcbeard pcbeard deleted the FinishCallbackClosure branch April 11, 2024 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants