- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 156
Remove global clients #1014
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
base: main
Are you sure you want to change the base?
Remove global clients #1014
Conversation
…OME and HOME directories
| Codecov ReportAttention: Patch coverage is  
 
 Additional details and impacted files@@            Coverage Diff             @@
##             main    #1014      +/-   ##
==========================================
+ Coverage   20.24%   20.81%   +0.57%     
==========================================
  Files          42       42              
  Lines        3241     3291      +50     
==========================================
+ Hits          656      685      +29     
- Misses       2498     2515      +17     
- Partials       87       91       +4     
 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
 | 
…serial, and update files
…ating logAction method
| Tested on linux with MKR 1010 
 | 
        
          
                hub.go
              
                Outdated
          
        
      | p.OnClose = func(port *serport) { | ||
| hub.serialPortList.MarkPortAsClosed(p.portName) | ||
| hub.serialPortList.List() | ||
| } | 
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 think you can remove the OnClose callback and move those two lines of code in the spClose method below after calling p.Close(). AFAIK this struct is the only one closing ports, so moving the code there should cover all the cases. The following should be the updated spClose function:
func (hub *hub) spClose(portname string) {
	if myport, ok := hub.serialHub.FindPortByName(portname); ok {
		hub.broadcastSys <- []byte("Closing serial port " + portname)
		myport.Close()
		hub.serialPortList.MarkPortAsClosed(myport.portName)
		hub.serialPortList.List()
	} else {
		hub.spErr("We could not find the serial port " + portname + " that you were trying to close.")
	}
}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.
uhm I think it is also called here
arduino-create-agent/serialport.go
Line 171 in b717430
| p.Close() | 
…b for cleaner code
…o hub.go for better organization
| Tested again like #1014 (comment) | 
| Test: It can download a tool. 
 This hte was logs  | 
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 would try to reduce callbacks if possible. They are usually difficult to follow.
        
          
                hub.go
              
                Outdated
          
        
      | onRegister := func(port *serport) { | ||
| broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}") | ||
| } | ||
| onUnregister := func(port *serport) { | ||
| broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}") | ||
| } | ||
| serialHubub := newSerialHub(onRegister, onUnregister) | ||
|  | ||
| onList := func(data []byte) { | ||
| broadcastSys <- data | ||
| } | ||
| onErr := func(err string) { | ||
| broadcastSys <- []byte("{\"Error\":\"" + err + "\"}") | ||
| } | ||
| serialPortList := newSerialPortList(tools, onList, onErr) | 
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 would simplify both of this two with the ChanWriter
| onRegister := func(port *serport) { | |
| broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}") | |
| } | |
| onUnregister := func(port *serport) { | |
| broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}") | |
| } | |
| serialHubub := newSerialHub(onRegister, onUnregister) | |
| onList := func(data []byte) { | |
| broadcastSys <- data | |
| } | |
| onErr := func(err string) { | |
| broadcastSys <- []byte("{\"Error\":\"" + err + "\"}") | |
| } | |
| serialPortList := newSerialPortList(tools, onList, onErr) | |
| serialHubub := newSerialHub(ChanWriter{Ch:broadcastSys}) | |
| serialPortList := newSerialPortList(tools, ChanWriter{Ch:broadcastSys}) | 
| OnList func([]byte) `json:"-"` | ||
| OnErr func(string) `json:"-"` | 
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.
| OnList func([]byte) `json:"-"` | |
| OnErr func(string) `json:"-"` | |
| writer io.Writer `json:"-"` | 
        
          
                serial.go
              
                Outdated
          
        
      | sh.mu.Lock() | ||
| //log.Print("Registering a port: ", p.portConf.Name) | ||
| h.broadcastSys <- []byte("{\"Cmd\":\"Open\",\"Desc\":\"Got register/open on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + ",\"BufferType\":\"" + port.BufferType + "\"}") | ||
| sh.onRegister(port) | 
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.
| sh.onRegister(port) | |
| fmt.Fprintf(sh.writer, `{"Cmd":"Open","Desc":"Got register/open on port.","Port":%q,"Baud":%d,"BufferType":%q}`, port.portConf.Name , port.portConf.Baud, port.BufferType) | 
| sh.mu.Lock() | ||
| //log.Print("Unregistering a port: ", p.portConf.Name) | ||
| h.broadcastSys <- []byte("{\"Cmd\":\"Close\",\"Desc\":\"Got unregister/close on port.\",\"Port\":\"" + port.portConf.Name + "\",\"Baud\":" + strconv.Itoa(port.portConf.Baud) + "}") | ||
| sh.onUnregister(port) | 
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.
| sh.onUnregister(port) | |
| fmt.Fprintf(sh.writer, `{"Cmd":"Close","Desc":"Got unregister/close on port.","Port":%q,"Baud":%d}`, port.portConf.Name, port.portConf.Baud) | 
| OnList: onList, | ||
| OnErr: onErr, | 
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.
| OnList: onList, | |
| OnErr: onErr, | |
| writer: io.Writer, | 
| //log.Println(err) | ||
| h.broadcastSys <- []byte("Error creating json on port list " + | ||
| err.Error()) | ||
| sp.OnErr("Error creating json on port list " + err.Error()) | 
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.
| sp.OnErr("Error creating json on port list " + err.Error()) | |
| fmt.Fprintf(sp.writer,"Error creating json on port list %s", err.Error()) | 
| sp.OnErr("Error creating json on port list " + err.Error()) | ||
| } else { | ||
| h.broadcastSys <- ls | ||
| sp.OnList(ls) | 
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.
| sp.OnList(ls) | |
| sp.Write(ls) | 
I added some comments but I don't want to block the PR
| I've merged #1032 here. | 
Please check if the PR fulfills these requirements
before creating one)
The purpose of this PR is to improve the maintainability, testability, and clarity of the codebase by removing global variables and explicitly passing dependencies as arguments to functions or methods. Global variables can lead to tightly coupled code, making it harder to understand, test, and maintain.
remove global variables, they are now created in the main function and passed explicitly to the components or functions that need them.
move the
go hub.serialPortList.Run()from themaingointo thehub.run()because the hub is in charge of connecting serial events to websocket clients.No.