GithubHelp home page GithubHelp logo

pexavc / granite Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 1.0 3.32 MB

A SwiftUI Architecture that merges Redux to the functional world of Swift

License: GNU General Public License v3.0

Swift 100.00%
clean-architecture design-patterns redux swift swiftui

granite's People

Contributors

pexavc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

cryptomacedonia

granite's Issues

Non-Main thread Reducer Side Effect causes State inconsistency

2023-05-15

struct AskResponse: GraniteReducer {
    typealias Center = SandService.Center
    
    @Payload var response: SandGPT.Response?
    
    @Event var processDocument: AskDocumentResponse.Reducer
    
    func reduce(state: inout Center.State) {
        guard response?.isComplete == false else {
            print("[SandService] AskIsComplete")
            
            if response?.isStream == false {
                if let payload = response {
                    processDocument.send(payload) // <-------- Becomes priority stateful update (1)
                } else {
                    state.response = response?.data ?? ""
                }
            }
            
            state.isResponding = false // <---------- This won't be final (2)
            return
        }
        print("[SandService] test")
        
        state.response = response?.data ?? ""
    }
}

struct AskDocumentResponse: GraniteReducer {
    typealias Center = SandService.Center
    
    @Relay var environment: EnvironmentService
    
    @Payload var response: SandGPT.Response?
    
    func reduce(state: inout Center.State) {
        guard let response = response?.data else { return }
        let doc = Document(parsing: response, source: nil)
        let lineCount = Int(doc.range?.upperBound.line ?? 0)
        
        environment
            .center
            .responseWindowSize
            .send(
                EnvironmentService
                    .ResponseWindowSizeUpdated
                    .Meta(lineCount: lineCount))
        
        state.response = response
        state.isResponding = false // <------------- this is final (3)
    }
    
    var thread: DispatchQueue? {
        .global(qos: .utility)
    }
}

Comments have marked order of operations. Second reducer is using utility thread.

state.isResponding was not set to false in comment (3) then it would persist true even if (2) marked it false.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.