Skip to content

Convert FIRGetOptions.initWithSource to factory method. (#655) #711

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

Merged
merged 3 commits into from
Feb 1, 2018

Conversation

rsgowman
Copy link
Member

No description provided.

@@ -62,7 +65,7 @@ typedef NS_ENUM(NSUInteger, FIRGetSource) {
/**
* Initializes the get options with the specified source.
*/
- (instancetype)initWithSource:(FIRGetSource)source NS_SWIFT_NAME(init(source:));
+ (instancetype)source:(FIRGetSource)source;
Copy link
Contributor

Choose a reason for hiding this comment

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

Methods should lead with their return type in Objective-C

This should be optionsWithSource:

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@rsgowman rsgowman requested a review from ryanwilson January 26, 2018 19:57
Copy link
Member

@ryanwilson ryanwilson left a comment

Choose a reason for hiding this comment

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

Sorry about the delay in review!

@@ -62,7 +65,7 @@ typedef NS_ENUM(NSUInteger, FIRGetSource) {
/**
* Initializes the get options with the specified source.
*/
- (instancetype)initWithSource:(FIRGetSource)source NS_SWIFT_NAME(init(source:));
+ (instancetype)optionsWithSource:(FIRGetSource)source NS_SWIFT_NAME(source(source:));
Copy link
Member

Choose a reason for hiding this comment

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

I think leaving this as a constructor would be good instead of fighting against the translator: GetOptions(source: .cache).

Copy link
Contributor

Choose a reason for hiding this comment

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

So the issue is that we're going to add a timeout too.

In the other languages you'll be able to do things like this:

GetOptions.source(GetOptions.Source.CACHE).timeout(2, TimeUnit.MINUTES);

GetOptions.timeout(2, TimeUnit.MINUTES).source(GetOptions.Source.CACHE);

Making this an initializer only thing means that the same flexibility would require us to define all the permutations of options which doesn't seem like what we want to do at all.

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh okay, that makes sense. This wouldn't solve it entirely though, would it? You'd be left with an instance instead and unable to call the next class method. Unless each option was a constructor and a function:

class func source(_ source: GetSource) -> GetOptions
class func timeout(_ value: Int, _ unit: TimeUnit) -> GetOptions

func source(_ source: GetSource) -> GetOptions
func timeout(_ value: Int, _ unit: TimeUnit) -> GetOptions

Do you mean to have this as an instance method, or am I missing something?

It's worth noting that this could potentially go away once we have the mechanism to ship native Swift files:

public func init(source: GetOptions.Source = .default, 
                 timeout: (Int, TimeUnit) = (2, .minutes)) 
{                   // (or have Timeout as its own type)
  // Implementation
}

// Usage:
GetOptions()
GetOptions(source: .cache)
GetOptions(timeout: (5, .seconds))
GetOptions(source: .cache, timeout: (8, .seconds))

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think the idea would be to add instance methods as appropriate to allow chaining.

I like the idea of (eventually) using named parameters. (There's a concern if we ever have a very large number of options, but I don't think that's the case here.) Is there an approx timeline for being able to do this?

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with the API with one minor change to match SnapshotOptions: remove the argument label to make it source(_:), which turns the usage into: GetSource.source(.cache). I would be interested to see how it's going to expand with multiple parameters though - class and instance functions?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done; ptal.

(And yeah; expected path forward is to expand with class and instance functions.)

@rsgowman rsgowman merged commit 5a68eda into firestore-api-changes Feb 1, 2018
@rsgowman rsgowman deleted the rsgowman/get_options_factory branch February 1, 2018 16:37
@firebase firebase locked and limited conversation to collaborators Nov 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants