Skip to content

Commit

Permalink
refactor(plc4go/spi): improve code flow of read and write request
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jun 13, 2023
1 parent f75dd21 commit ee99a37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
11 changes: 7 additions & 4 deletions plc4go/spi/model/DefaultPlcReadRequest.go
Expand Up @@ -115,13 +115,17 @@ func (d *DefaultPlcReadRequest) Execute() <-chan apiModel.PlcReadRequestResult {
}

func (d *DefaultPlcReadRequest) ExecuteWithContext(ctx context.Context) <-chan apiModel.PlcReadRequestResult {
// Shortcut, if no interceptor is defined
if d.readRequestInterceptor == nil {
return d.reader.Read(ctx, d)
if d.readRequestInterceptor != nil {
return d.ExecuteWithContextAndInterceptor(ctx)
}

return d.reader.Read(ctx, d)
}

func (d *DefaultPlcReadRequest) ExecuteWithContextAndInterceptor(ctx context.Context) <-chan apiModel.PlcReadRequestResult {
// Split the requests up into multiple ones.
readRequests := d.readRequestInterceptor.InterceptReadRequest(ctx, d)

// Shortcut for single-request-requests
if len(readRequests) == 1 {
return d.reader.Read(ctx, readRequests[0])
Expand Down Expand Up @@ -160,6 +164,5 @@ func (d *DefaultPlcReadRequest) ExecuteWithContext(ctx context.Context) <-chan a
// Return the final result
resultChannel <- result
}()

return resultChannel
}
10 changes: 6 additions & 4 deletions plc4go/spi/model/DefaultPlcWriteRequest.go
Expand Up @@ -137,11 +137,14 @@ func (d *DefaultPlcWriteRequest) Execute() <-chan apiModel.PlcWriteRequestResult
}

func (d *DefaultPlcWriteRequest) ExecuteWithContext(ctx context.Context) <-chan apiModel.PlcWriteRequestResult {
// Shortcut, if no interceptor is defined
if d.writeRequestInterceptor == nil {
return d.writer.Write(ctx, d)
if d.writeRequestInterceptor != nil {
return d.ExecuteWithContextAndInterceptor(ctx)
}

return d.writer.Write(ctx, d)
}

func (d *DefaultPlcWriteRequest) ExecuteWithContextAndInterceptor(ctx context.Context) <-chan apiModel.PlcWriteRequestResult {
// Split the requests up into multiple ones.
writeRequests := d.writeRequestInterceptor.InterceptWriteRequest(ctx, d)
// Shortcut for single-request-requests
Expand Down Expand Up @@ -182,7 +185,6 @@ func (d *DefaultPlcWriteRequest) ExecuteWithContext(ctx context.Context) <-chan
// Return the final result
resultChannel <- result
}()

return resultChannel
}

Expand Down

0 comments on commit ee99a37

Please sign in to comment.