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

Broadcasting of requested resolution #8

Merged
merged 8 commits into from
Jan 8, 2021

Conversation

owenpearson
Copy link
Member

If merged, this PR will allow broadcasting of requested resolution in channel presence data via a configuration option, and updating requested resolution from a new public API method, sendChangeRequest.

src/lib/AssetSubscriber.ts Show resolved Hide resolved
src/lib/AssetSubscriber.ts Outdated Show resolved Hide resolved
@@ -3,3 +3,22 @@ export type GeoJsonMessage = unknown;
export type LocationListener = (geoJsonMsg: GeoJsonMessage) => unknown;

export type StatusListener = (isOnline: boolean) => unknown;

export enum Accuracy {
Copy link
Contributor

Choose a reason for hiding this comment

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

Take a brief look around the place in terms of TypeScript enum definitions, some places seem to prefer CamelCase. Equally I would have expected this to be picked up by the linter and/or prettier if it's non-standard and that doesn't seem to be the case. Is there a convention we're aligning with here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am aware that camelCase is often used for enums but I'm aligning with the TypeScript documentation which to me feels more like the right thing to do.

Copy link
Contributor

Choose a reason for hiding this comment

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

Were you going to address this comment in the scope of this PR? It felt like you said you might elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, forgot to do that - done now in 9abeb51.

@owenpearson owenpearson force-pushed the feature/requested-resolution branch from 71c466b to a34aa8a Compare January 6, 2021 17:14
sendChangeRequest(resolution: Resolution, onSuccess: () => unknown, onError: (err: Error) => unknown): void {
this.resolution = resolution;
if (!this.assetConnection) {
onError(new Error('Cannot change resolution; no asset is currently being tracked.'));
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for making this change, but this now brings up another issue for me...

By calling onError in the way you are now, you are making it a blocking call - as in the app's onError implementation must finish executing (return) before this "method" can return.

My expectation would be that a less surprising result for app developers would be for you to delay the call to onError until after this method has returned. I believe that can be accomplished using setTimeout.

Are there any other locations within this codebase where we have methods that accept callback functions and end up calling them synchronously, as I would imagine those should change too (assuming you agree with me).

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds reasonable to me - I've fixed this and other instances where we call user provided callbacks in b1da3ed.

Copy link
Member

Choose a reason for hiding this comment

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

What does ably-js do? (cc @SimonWoolf )

Copy link
Member

Choose a reason for hiding this comment

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

ably-js does appear to call callbacks synchronously in some error cases. I agree it wouldn't be a bad idea to chuck in a setImmediate for those. But no-one's complained yet so probably not a huge deal in practice

Copy link
Member

Choose a reason for hiding this comment

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

@SimonWoolf My question was more in relation to the other issue, which is what do we think is the appropriate best practice for setTimeout/setImmediate/nextTick, and how to make it portable?

Copy link
Member

@SimonWoolf SimonWoolf Jan 8, 2021

Choose a reason for hiding this comment

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

ably-js has Utils.nextTick which does setTimeout(f, 0) on browsers (which is what owen's done here) and process.nextTick in node.

AIUI node's setImmediate is basically just an alias for setTimeout(f, 0), ie adds it as a timer action in the next eventloop. Vs process.nextTick which despite the name requeues the action in the current event loop at the end of the queue. For delaying a callback, 'end of current event' loop is arguably semantically neater, but there's no way to do that in browsers afaik, and for that purpose isn't going to make a difference.

(for batch methods that want to move things to the next event loop the difference starts to matter - nextTick is not sufficient, as we've discovered - but that's not relevant to this)

Copy link
Contributor

@QuintinWillison QuintinWillison left a comment

Choose a reason for hiding this comment

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

I'm happy to approve this for today, in order to get our next preview release out, but I would appreciate it if you could ensure that any deferred fixes are either raised immediately as new PRs or have issues created for them so they don't get forgotten. Thanks.

@@ -0,0 +1,3 @@
export const nextTick = (fn: () => unknown): void => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this, however...

Feel free to defer it to another PR, but this would be a perfect opportunity to add some commentary to this method to explain what its purpose is. We know right now, looking at it from the context of this PR, but later on the meaning will become less clear to those browsing the library.

Also, while I can imagine your reasons for using these words to form this function name, it's not a very obvious name. "tick" is an overloaded word at the best of times, while nextTick as a function name suggests it might be some kind of "getter", not a "doer". Something like emitSoon might be better, perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

I chose nextTick since it's effectively the same thing as Node's process.nextTick. I've changed it to setImmediate in 7b9bf35 since that's what Simon called it and is also the name of a non-standard browser global that does the same thing. I also added a comment to explain what it's there for.

@@ -3,3 +3,22 @@ export type GeoJsonMessage = unknown;
export type LocationListener = (geoJsonMsg: GeoJsonMessage) => unknown;

export type StatusListener = (isOnline: boolean) => unknown;

export enum Accuracy {
Copy link
Contributor

Choose a reason for hiding this comment

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

Were you going to address this comment in the scope of this PR? It felt like you said you might elsewhere.

@owenpearson owenpearson force-pushed the feature/requested-resolution branch from 153dae7 to 7b9bf35 Compare January 8, 2021 11:17
@owenpearson owenpearson merged commit 1845b5a into main Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants