-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
[TM] Add spec for WebSocketModule #24893
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6f58781
[TM] Add spec for WebSocketModule
jeanregisser 6730a2d
[TM] Use NativeWebSocketModule in WebSocketInterceptor
jeanregisser 5b75b7a
[TM] Update comment
jeanregisser 05d31c1
[TM] Use 2 slightly different specs for now
jeanregisser a7dab07
Merge remote-tracking branch 'upstream/master' into spec-websocketmodule
jeanregisser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {TurboModule} from 'RCTExport'; | ||
import * as TurboModuleRegistry from 'TurboModuleRegistry'; | ||
|
||
// Native close method definition on Android | ||
declare function close(code: number, reason: string, socketID: number): void; | ||
// Native close method definition on iOS | ||
declare function close(socketID: number): void; | ||
|
||
export interface Spec extends TurboModule { | ||
+connect: ( | ||
url: string, | ||
protocols: ?Array<string>, | ||
options: ?{headers?: {origin?: string}}, | ||
socketID: number, | ||
) => void; | ||
+send: (message: string, socketID: number) => void; | ||
+sendBinary: (base64String: string, socketID: number) => void; | ||
+ping: (socketID: number) => void; | ||
+close: typeof close; | ||
|
||
// Events | ||
+addListener: (eventName: string) => void; | ||
+removeListeners: (count: number) => void; | ||
} | ||
|
||
export default TurboModuleRegistry.getEnforcing<Spec>('WebSocketModule'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky.
Approaches
specs/{ios,android}
folders. Then, have a spec for ios and a spec for android. The only thing different between the two would be the close method. Then, you could create a NativeWebSocketModule.js file that returns the correct implementation based on Platform.OS.close(socketID: number, code: number, reason: string): void;
. Then type the second and third argument as optional. It seems like the only place where we use WebSocketModule is in WebSocket.js, so this wouldn't be difficult. Also, while we're working on WebSocketModule, it might be worthwhile to actually do some cleanup. Also, with approach number one, we have a lot of duplicate code.I'm leaning more towards option 2. Even though it's probably the more difficult option, and even though it is a breaking change, I think it's probably the better solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @RSNara
I was thinking about a 3rd approach, quite similar to approach 2:
react-native/Libraries/WebSocket/RCTSRWebSocket.m
Line 558 in 7abfd23
This would actually make the iOS implementation more correct. Right now the JS interface for closing the WebSocket supports the reason and code but ignores it on iOS.
If we go this way, should the refactor be done in a different PR? I assume yes, but just checking.
Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's also a good idea! But unfortunately, I just realized that our fb-internal infra doesn't allow refactoring NativeModule methods. So for the time being, I think we should just create two different spec files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure what you mean about the fb-internal infra refactoring limitation, but I've created 2 slightly different specs for now in 05d31c1
Note that I had to suppress the flow error when calling the
close
method. Let me know if there's a better way to avoid this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright since my small refactor for the iOS close method got merged, I'm gonna update the spec! There's no more a difference between the native iOS and Android implementations.