Skip to content
Back to skills

1489 Publishers 3f8d3505

ASecurity

| Publisher | Use Case | |-----------|----------| | `Just` | Single synchronous value, placeholders | | `Future` | Converting callback-based APIs (executes once, caches result) | | `Deferred` | Lazy publisher creation, wrap Future for retry support | | `Empty` | No-op placeholder, completing immediately | | `Fail` | Immediate error emission, testing error paths | | `Sequence.publisher` | `[1,2,3].publisher` emits each element | | `Timer.Publisher` | Periodic events (requires `autoconnect()`) | |

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
developmentgoswifttestingapi

Works with

  • api

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 1489-publishers_3f8d3505 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 1489 Publishers 3f8d3505?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 1489 Publishers 3f8d3505
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-1489-publishers-3f8d3505/badge)](https://www.skillsdirectory.com/skills/tools-only-1489-publishers-3f8d3505)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Combine Publishers

## Built-in Publishers

| Publisher | Use Case |
|-----------|----------|
| `Just` | Single synchronous value, placeholders |
| `Future` | Converting callback-based APIs (executes once, caches result) |
| `Deferred` | Lazy publisher creation, wrap Future for retry support |
| `Empty` | No-op placeholder, completing immediately |
| `Fail` | Immediate error emission, testing error paths |
| `Sequence.publisher` | `[1,2,3].publisher` emits each element |
| `Timer.Publisher` | Periodic events (requires `autoconnect()`) |
| `DataTaskPublisher` | Network requests via URLSession |

## Subject Types

### PassthroughSubject - Use for Events

```swift
let buttonTaps = PassthroughSubject<Void, Never>()
buttonTaps.send(())  // Subscribers only notified if already subscribed
```

- No initial value required
- No `.value` property
- New subscribers receive only future values
- Best for: button taps, user actions, transient events

### CurrentValueSubject - Use for State

```swift
let loadingState = CurrentValueSubject<LoadingState, Never>(.idle)
print(loadingState.value)  // Can query current state
```

- Initial value required
- `.value` property for direct access
- New subscribers receive current value immediately
- Best for: settings, loading state, toggles

## Critical Anti-Patterns

### 1. Exposing Subjects Publicly

```swift
// BAD: External code can call loginSubject.send(...)
class AuthManager {
    let loginSubject = PassthroughSubject<User, Error>()
}

// GOOD: Expose as read-only publisher
class AuthManager {
    private let loginSubject = PassthroughSubject<User, Error>()
    var loginPublisher: AnyPublisher<User, Error> {
        loginSubject.eraseToAnyPublisher()
    }
}
```

### 2. Using Just for Arrays When Sequence Intended

```swift
// BAD: Emits entire array as one value
Just([1, 2, 3]).sink { print($0) }  // prints: [1, 2, 3]

// GOOD: Emits each element
[1, 2, 3].publisher.sink { print($0) }  // prints: 1, 2, 3
```

### 3. Future Without Deferred for Retry

```swift
// BAD: Retry reuses cached failure
Future { promise in networkCall(completion: promise) }
    .retry(3)  // Same cached result retried!

// GOOD: Wrap in Deferred
Deferred {
    Future { promise in networkCall(completion: promise) }
}.retry(3)  // New Future created each retry
```

### 4. Wrong Subject Type for Use Case

```swift
// BAD: PassthroughSubject for state (late subscribers miss value)
let isLoggedIn = PassthroughSubject<Bool, Never>()

// GOOD: CurrentValueSubject for state
let isLoggedIn = CurrentValueSubject<Bool, Never>(false)
```

## Review Questions

1. Are Subjects exposed publicly or converted to AnyPublisher?
2. Is the correct Subject type used (events vs state)?
3. Is Future used with retry without Deferred wrapper?
4. Are built-in publishers preferred over custom implementations?
5. Is `.value` access needed? (Requires CurrentValueSubject)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.