Contents

space-code/atomic

## Description

Description

Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.

Features

πŸ”’ Thread-Safe - Automatic synchronization for concurrent access ⚑ Simple API - Just add @Atomic to any property 🎯 Type-Safe - Works with any Swift type πŸ“± Cross-Platform - Works on iOS, macOS, tvOS, watchOS, and visionOS ⚑ Lightweight - Minimal footprint with zero dependencies πŸ§ͺ Well Tested - Comprehensive test coverage

Table of Contents

Requirements

| Platform | Minimum Version | |-----------|----------------| | iOS | 13.0+ | | macOS | 10.15+ | | tvOS | 13.0+ | | watchOS | 6.0+ | | visionOS | 1.0+ | | Xcode | 15.3+ | | Swift | 5.10+ |

Installation

Swift Package Manager

Add the following dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/space-code/atomic.git", from: "1.1.1")
]

Or add it through Xcode:

  1. File > Add Package Dependencies
  2. Enter package URL: https://github.com/space-code/atomic.git
  3. Select version requirements

Quick Start

import Atomic

@Atomic var counter = 0

// Thread-safe increment from multiple threads
DispatchQueue.concurrentPerform(iterations: 1000) { _ in
    counter += 1
}

print("Final count: \(counter)") // Always 1000

Usage

Basic Usage

Simply add the @Atomic property wrapper to any property that needs thread-safe access:

import Atomic

class UserSession {
    @Atomic var user: User?
    @Atomic var loginAttempts = 0
    
    func login(username: String, password: String) async throws {
        // Thread-safe write
        _loginAttempts.write { $0 += 1 }
        
        // Perform authentication
        let authenticatedUser = try await authenticate(username, password)
        
        // Thread-safe write with new value
        _user.write(authenticatedUser)
    }
    
    func getCurrentUsername() -> String? {
        // Thread-safe read
        _user.read { $0?.username }
    }
}

Thread-Safe Collections

import Atomic

class DataCache {
    @Atomic private var cache: [String: Data] = [:]
    
    func store(_ data: Data, forKey key: String) {
        _cache.write { cache in
            cache[key] = data
        }
    }
    
    func retrieve(forKey key: String) -> Data? {
        _cache.read { $0[key] }
    }
    
    func removeExpired(before date: Date) {
        _cache.write { cache in
            cache = cache.filter { $0.value.timestamp > date }
        }
    }
}

Communication

Contributing

We love contributions! Please feel free to help out with this project. If you see something that could be made better or want a new feature, open up an issue or send a Pull Request.

Development Setup

Bootstrap the development environment:

mise install

Author

Nikita Vasilev

License

Atomic is released under the MIT license. See LICENSE for details.


<div align="center">

⬆ back to top

Made with ❀️ by space-code

</div>

Package Metadata

Repository: space-code/atomic

Default branch: main

README: README.md