Git Product home page Git Product logo

stackninja's Introduction

St/\ck Ninj/\ - App constructor framework

                                   
                               Architecture overview

             ┌────────────────────────────────────────────────────────┐
             │                                                        │
             │                         ASSET                          │
             │                                                        │
             └────────────────────────────────────────────────────────┘
                      ▼                                       ▼
                 ┌─────────┐                             ┌─────────┐
                 │ SERVICE │                             │ DESIGN  │
                 └─────────┘                             └─────────┘
                      ▼                                       ▼
             ┌────────────────┐   ┌───────────────┐   ┌───────────────┐
             │                │   │               │   │               │
             │   ASYNC WORKS  │◄──┤   SCENARIO    │◄──┤     SCENE     │
             │                │   │               │   │               │
             ├────────────────┤   ├───────────────┤   ├───────────────┤
             │                │   │               │   │               │
             │                │   │  ┌─────────┐  │   │  ┌─────────┐  │  ┌────────┐
             │                │   │  │ EVENT A │◄─┼───┼──┤         ├──┼─►│        │
             │                │   │  └────┬────┘  │   │  │  VIEW   │  │  │ VIEW A │
             │                │   │       │       │   │  │ MODEL 1 │  │  │        │
             │                │   │       ▼       │   │  │         │  │  │        │
             │   ┌────────┐ ◄─┼───┼──── DOWORK    │   │  │         │◄─┼──┤        │
             │   │ WORK 1 │   │   │               │   │  │         │  │  └────────┘
             │   └────────┘ ──┼───┼───────┐       │   │  │         │  │            
             │                │   │       ▼       │   │  │         │  │  ┌────────┐
┌─────────┐  │   ┌────────┐ ◄─┼───┼──── DOWORK    │   │  │         ├──┼─►│        │
│ SERVICE │◄─┼──►│ WORK 2 │   │   │               │   │  │         │  │  │ VIEW B │
│   API   │  │   └────────┘ ──┼───┼───────┐       │   │  │         │  │  │        │
└─────────┘  │                │   │       ▼       │   │  │         │  │  │        │
             │                │   │    SETSTATE ──┼───┼─►│         │◄─┼──┤        │
             │                │   │               │   │  └─────────┘  │  └────────┘
             │                │   │               │   │               │            
             │                │   │  ┌─────────┐  │   │  ┌─────────┐  │  ┌────────┐
             │                │   │  │ EVENT B │◄─┼───┼──┤         ├──┼─►│        │
             │                │   │  └────┬────┘  │   │  │  VIEW   │  │  │ VIEW C │
             │                │   │       │       │   │  │ MODEL 2 │  │  │        │
             │                │   │       ▼       │   │  │         │  │  └────────┘
             │   ┌────────┐ ◄─┼───┼──── DOWORK    │   │  │         │  │            
             │   │ WORK 3 │   │   │               │   │  │         │  │  ┌────────┐
             │   └────────┘ ──┼───┼───────┐       │   │  │         ├──┼─►│        │
             │                │   │       ▼       │   │  │         │  │  │ VIEW D │
             │                │   │    SETSTATE ──┼───┼─►│         │◄─┼──┤        │
             │                │   │       │       │   │  └─────────┘  │  └────────┘
             │                │   │       ▼       │   │               │            
┌─────────┐  │   ┌────────┐ ◄─┼───┼──── DOWORK    │   │  ┌─────────┐  │  ┌────────┐
│         │◄─┼── │ WORK 4 │   │   │               │   │  │         ├──┼─►│        │
│ SERVICE │  │   └────────┘ ──┼───┼───────┐       │   │  │  VIEW   │  │  │ VIEW E │
│ STORAGE │  │                │   │       ▼       │   │  │ MODEL 3 │  │  │        │
│         │  │   ┌────────┐ ◄─┼───┼──── DOWORK    │   │  │         │  │  │        │
│         ├──┼─► │ WORK 5 │   │   │               │   │  │         │  │  └────────┘
└─────────┘  │   └────────┘ ──┼───┼───────┐       │   │  │         │  │            
             │                │   │       ▼       │   │  │         │  │  ┌────────┐
             │                │   │    SETSTATE ──┼───┼─►│         ├──┼─►│        │
             │                │   │               │   │  │         │  │  │ VIEW F │
             │                │   │               │   │  │         │  │  │        │
             │                │   │               │   │  └─────────┘  │  └────────┘
             │                │   │               │   │               │            
             └────────────────┘   └───────────────┘   └───────────────┘             

Async Scenario example:

      
import StackNinja

struct BenefitBasketScenarioScenarioInput: ScenarioEvents {
   let deleteItemPressed: Out
   let countPlussPressed: Out
   let countMinusPressed: Out

   let checkMarkPressed: Out<(Bool, index: Int)>
   let tableItemPressed: Out
   let buyButtonPressed: Out

   let confirmDelete: Out
   let cancelDelete: Out
}

struct BenefitBasketScenarioParams: ScenarioParams {
   typealias ScenarioInputEvents = BenefitBasketScenarioScenarioInput
   typealias ScenarioModelState = BenefitBasketState
   typealias ScenarioWorks = BenefitBasketWorks
}

final class BenefitBasketScenario: BaseParamsScenario> {
   override func configure() {
      super.configure()

      // MARK: - load items at start

      start
         .doNext(works.loadCartItemsFromService)
         .doVoidNext(works.getBasketItems)
         .onSuccess(setState) { .presentBasketItems($0) }
         .onFail(setState, .presentLoadingError)

      // MARK: - quantity count change

      events.countPlussPressed
         .doNext(works.increaseItemAmount)
         .onSuccess(setState) { .updateItemAtIndex($0.0, $0.1) }
         .onFail(setState) { (item: CartItem, ind: Int) in [.updateItemAtIndex(item, ind), .connectionError] }
         .doVoidNext(works.getBasketItems)
         .onSuccess(setState) { .updateSummaAndButton($0) }

      events.countMinusPressed
         .doNext(works.decreaseItemAmount)
         .onSuccess(setState) { .updateItemAtIndex($0.0, $0.1) }
         .onFail(setState) { (item: CartItem, ind: Int) in [.updateItemAtIndex(item, ind), .connectionError] }
         .doVoidNext(works.getBasketItems)
         .onSuccess(setState) { .updateSummaAndButton($0) }

      // MARK: - checkmark works

      events.checkMarkPressed
         .doNext(works.updateCheckbox)
         .onSuccess(setState) { .updateItemAtIndex($0.0, $0.1) }
         .onFail(setState) { (item: CartItem, ind: Int) in [.updateItemAtIndex(item, ind), .connectionError] }
         .doVoidNext(works.getBasketItems)
         .onSuccess(setState) { .updateSummaAndButton($0) }

      // MARK: - item select and push details

      events.tableItemPressed
         .doSaveResult()
         .doVoidNext(works.getBasketItems)
         .onSuccessMixSaved(setState) { .presentBenefitDetails(id: $0[$1].offerId) }

      // MARK: - buying

      events.buyButtonPressed
         .onSuccess(setState, .presentFullScreenDarkLoader)
         .doNext(works.postOrders)
         .onSuccess(setState, .finishBuyOffers)
         .onFail(setState, .presenBuyError)

      // MARK: - deleting

      events.deleteItemPressed
         .onSuccess(setState, .presentDeleteAlert)

      Work.startVoid.retainBy(works.retainer)
         // catch index at "deleteItemPressed" and await "confirmDelete"
         .doCombine(events.deleteItemPressed, events.confirmDelete)
         .doMap { $0.0 }
         .doNext(works.deleteCartItemAtIndex)
         .onFail(setState, .deleteItemError)
         .doNext(works.getBasketItems)
         .onSuccess(setState) { .presentBasketItems($0) }

      Work.startVoid.retainBy(works.retainer)
         // catch index at "deleteItemPressed" and await "cancelDelete"
         .doCombine(events.deleteItemPressed, events.cancelDelete)
         .doVoidNext(works.getBasketItems)
         .onSuccess(setState) { .presentBasketItems($0) }
   }
}

stackninja's People

Contributors

setixela avatar gapurinov avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.