Skip to content
This repository was archived by the owner on Jan 14, 2021. It is now read-only.

cxa/Reducer.swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reducer.swift

A small Swift lib inspired by React.useReducer.

import Reducer

struct State {
  var count = 0
}

enum Action {
  case increment
  case decrement
}

func reducer(state: Reducer.Updater<State>, action: Action) {
  switch action {
  case .increment: state[keyPath: \.count] += 1
  case .decrement: state[keyPath: \.count] -= 1
  }
}

// state is a Reducer.Observer<State>
let (state, dispatch) = Reducer.use(initialState: State(), reducer: reducer)

// Return a `Observation` which can be invalidated later
let observation = state.observeKeyPath(\.count) { change in
  print(change.old)
  print(change.new)
  print(change.isInitial)
}

// Use action to notify state change
dispatch(.increment)

// If you want to disable the observation
observation.invalidate()

About

An small Swift lib inspired by React.useReducer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors