Swiftpack.co -  MaximBazarov/Decore as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Decore pre-SwiftUI apps.
.package(url: "https://github.com/MaximBazarov/Decore.git", from: "0.1.0")


The state management library that built with modularisation in mind.

How to use

There are two main features Decore gives you:

  • State declaration
  • Read/Write or both, access to the declared state.

To declare the state, you need to define its name and its structure using one of the containers:

struct Count: Atom {
    typealias Value = Int
    static var initial = { 0 }

Where Count is the name of your state and Value is a structure of the data. If you want to build a tree-like state you can nest the state declaration into other enum or struct like with other types you want to namespace.

You also provide the initial value for cases when this state is read before it's written. Note that you provide it as a function that returns that value, it will be called only if reading happened before writing.

Now when you have your state declared, you can access it as read-only using StateOf accessor:

@StateOf(Count.self) var count

Use it like any other read-only variable e.g. print(item) or you can be notified about every change in the state:

@StateOf(Count.self, onChange: { read in
    let value = read()
}) var count;

Mutable state

When you need to mutate the state you need to mark your state declaration as Mutable and use BindingTo instead of StateTo

struct Count: Atom, Mutable {
    static var initial = { 0 }

Done. Use it like any other variable through BindingTo.

@BindingTo(Count.self) var item

or observing:

@BindingTo(Count.self, onChange: { read in
    let value = read()
}) var count;

P.S. I'm very sorry that you have to put ; after a property wrapper declaration with closure, I'm thinking on solving it.

Thread-safety not included.

Warning: Decore is synchronous storage, so when you access the values you need to make sure that this is happening from a single thread. I have ideas on how to make it automatic, but I didn't want to include it for now.



Stars: 0
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Related Packages

Release Notes

1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics