Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
woodycatliu / CombineToAsync.swift
Created February 28, 2023 14:03
Combine to async
extension Publisher {
func awaitOutput() async throws -> Output {
return try await withCheckedThrowingContinuation { continuation in
let lock = NSRecursiveLock()
var nillableContinuation: CheckedContinuation<Self.Output, Error>? = continuation
var cancelable: AnyCancellable?
cancelable = first().sink(receiveCompletion: {
lock.lock(); defer { lock.unlock() }
switch $0 {
@woodycatliu
woodycatliu / XCTestCase+Extension.swift
Last active March 2, 2023 17:47
Easy to do Combine test
import Foundation
import Combine
import XCTest
extension XCTestCase {
func awaitPublisher<P: Publisher>(
_ publisher: P,
timeout: TimeInterval,
description: String,
trigger: (() -> Void)? = nil,
@woodycatliu
woodycatliu / PublishersCombine.swift
Last active August 29, 2022 08:23
Combine Publishers Zip
//
// ViewController.swift
// CombineCombine
//
// Created by Woody Liu on 2021/8/16.
//
import UIKit
import Combine
@woodycatliu
woodycatliu / ReadMe.md
Last active March 7, 2022 07:28
Countdown Timer Publisher (Swift5.2)

Countdown Timer Publisher (Swift5.2)

In the past we had to set the countdown by Timer.TimePublisher.

We maybe coding to like bellow.

let timer = Timer.publish(every: 0.01, on: .main, in: .common)
    .autoconnect()
 .map { _ in return 0.01 }
@woodycatliu
woodycatliu / ReactiveProgramming.md
Last active March 1, 2022 07:12
What is Reactive Programming in Swift ?

響應式編程 Reactive Programming

此篇將以 Swift 的角度去簡化 RP 理論的解釋

對於 iOS/OS 開發前端,這兩年很頻繁的會聽到這個名詞。
尤其是 Apple 推出 SwiftUI 與 Combine 以後,似乎 RxSwift 已經變成不是選修條件,越來越多公司在職缺條件中 Combine 或 RxSwift 是必要條件。
註:Combine/ RXSwift 是 RP 的高階封裝框架。

什麼是響應式編程(RP) ?

相信很多人 Google 之後會更加模糊,尤其是閱讀完 Wikipedia 艱深又空洞的理論介紹後,只看到一堆專業術語跟更多沒見過的名詞。

@woodycatliu
woodycatliu / ParseTCA.md
Last active February 23, 2022 08:02
How does implement of Store and ViewStore in TCA

Redux and TCA Learning Record 1


We can use Store and compose a new ViewStore to binding a smaller UI Block in subView or subViewController. How does TCA implements in binding each other ?

I crate some code to reproduce it.

Implement

@woodycatliu
woodycatliu / ParseTCA2.md
Last active February 23, 2022 08:00
Refactor to make it a more complete and llke TCA

Redux and TCA Learning Record 2


Pervious ParseTCA, I created some encapsulation to reproduce how TCA to implement in binding to ViewStore and Store.

Today, I will try to refactor them to more complete, like composable action and flexibility.

Step 1.

Refocator Reducer

// refactor after: 
@woodycatliu
woodycatliu / OptionalNotified.swift
Last active February 15, 2022 01:58
Observing optional value change allow NoticationCenter post
//
// OptionalNotified.swift
//
// Created by Woody Liu on 2021/7/24.
//
import Foundation
@propertyWrapper
public class OptionalNotified<Value: Equatable> {
//
// KitProviderLayout.swift
//
// Created by Woody on 2022/2/11.
//
import UIKit
#if canImport(SwiftUI) && DEBUG
class ViewLayout {
@woodycatliu
woodycatliu / KitProviderLayout.swift
Last active February 14, 2022 08:42
UIKit_UIViewPreviewProvider
//
// KitProviderLayout.swift
//
// Created by Woody on 2022/2/11.
//
import UIKit
#if canImport(SwiftUI) && DEBUG
class ViewLayout {