Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Adds Publisher.weaklyAssign(to:on:). #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CombineExt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
78C193E0241D4D8D0001B7FD /* MaterializeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78C193DF241D4D8D0001B7FD /* MaterializeTests.swift */; };
78C193E2241D596F0001B7FD /* Dematerialize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78C193E1241D596F0001B7FD /* Dematerialize.swift */; };
78C193E4241D63620001B7FD /* DematerializeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 78C193E3241D63620001B7FD /* DematerializeTests.swift */; };
AAEAF0E32436C8A8007C35E0 /* WeakAssign.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAEAF0E22436C8A8007C35E0 /* WeakAssign.swift */; };
AAEAF0E52436C9C2007C35E0 /* WeakAssignTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAEAF0E42436C9C2007C35E0 /* WeakAssignTests.swift */; };
BF50924B241FFE8E00600DF4 /* ZipManyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF50924A241FFE8E00600DF4 /* ZipManyTests.swift */; };
BF8121BC241FF42C006A93B8 /* ZipMany.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF8121BB241FF42C006A93B8 /* ZipMany.swift */; };
BFB4EA132428256B0096E9E9 /* CombineLatestMany.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFB4EA122428256B0096E9E9 /* CombineLatestMany.swift */; };
Expand Down Expand Up @@ -83,6 +85,8 @@
78C193DF241D4D8D0001B7FD /* MaterializeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaterializeTests.swift; sourceTree = "<group>"; };
78C193E1241D596F0001B7FD /* Dematerialize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dematerialize.swift; sourceTree = "<group>"; };
78C193E3241D63620001B7FD /* DematerializeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DematerializeTests.swift; sourceTree = "<group>"; };
AAEAF0E22436C8A8007C35E0 /* WeakAssign.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakAssign.swift; sourceTree = "<group>"; };
AAEAF0E42436C9C2007C35E0 /* WeakAssignTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeakAssignTests.swift; sourceTree = "<group>"; };
BF50924A241FFE8E00600DF4 /* ZipManyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipManyTests.swift; sourceTree = "<group>"; };
BF8121BB241FF42C006A93B8 /* ZipMany.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipMany.swift; sourceTree = "<group>"; };
BFB4EA122428256B0096E9E9 /* CombineLatestMany.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineLatestMany.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -166,6 +170,7 @@
BF50924A241FFE8E00600DF4 /* ZipManyTests.swift */,
BFB4EA1424283ECF0096E9E9 /* CombineLatestManyTests.swift */,
DC1691112428228200B234C4 /* MapManyTests.swift */,
AAEAF0E42436C9C2007C35E0 /* WeakAssignTests.swift */,
);
path = Tests;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -215,6 +220,7 @@
BF8121BB241FF42C006A93B8 /* ZipMany.swift */,
BFB4EA122428256B0096E9E9 /* CombineLatestMany.swift */,
DC16910E24281A1800B234C4 /* MapMany.swift */,
AAEAF0E22436C8A8007C35E0 /* WeakAssign.swift */,
);
path = Operators;
sourceTree = "<group>";
Expand Down Expand Up @@ -331,6 +337,7 @@
78C193E2241D596F0001B7FD /* Dematerialize.swift in Sources */,
78002BB7241E915E0018AA28 /* CurrentValueRelay.swift in Sources */,
78C193DE241D46F40001B7FD /* Materialize.swift in Sources */,
AAEAF0E32436C8A8007C35E0 /* WeakAssign.swift in Sources */,
78988A23241FFE2400F3A4AF /* Partition.swift in Sources */,
BF8121BC241FF42C006A93B8 /* ZipMany.swift in Sources */,
78C193D4241C2DE00001B7FD /* Create.swift in Sources */,
Expand Down Expand Up @@ -362,6 +369,7 @@
78C193E0241D4D8D0001B7FD /* MaterializeTests.swift in Sources */,
78002BBB241E97350018AA28 /* CurrentValueRelayTests.swift in Sources */,
78C193E4241D63620001B7FD /* DematerializeTests.swift in Sources */,
AAEAF0E52436C9C2007C35E0 /* WeakAssignTests.swift in Sources */,
BF50924B241FFE8E00600DF4 /* ZipManyTests.swift in Sources */,
78988A1E241EAFDD00F3A4AF /* PassthroughRelayTests.swift in Sources */,
DC1691122428228200B234C4 /* MapManyTests.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ All operators, utilities and helpers respect Combine's publisher contract, inclu
* [zip(with:) and Collection.zip](#ZipMany)
* [combineLatest(with:) and Collection.combineLatest](#CombineLatestMany)
* [mapMany(_:)](#MapMany)
* [weaklyAssign(to:on:)](#weaklyAssign)

### Publishers
* [AnyPublisher.create](#anypublisher.create)
Expand Down Expand Up @@ -362,6 +363,10 @@ intArrayPublisher.send([10, 2, 2, 4, 3, 8])
["10", "2", "2", "4", "3", "8"]
```

### weaklyAssign

Like Apple’s [`Publisher.assign(to:on)`](https://developer.apple.com/documentation/combine/publisher/3235801-assign), except it only `weak`ly captures `on` instead of `strong`ly.

## Publishers

This section outlines some of the custom Combine publishers CombineExt provides
Expand Down
25 changes: 25 additions & 0 deletions Sources/Operators/WeakAssign.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// WeakAssign.swift
// CombineExt
//
// Created by Jasdev Singh on 2/04/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//

import Combine

public extension Publisher where Failure == Never {
/// `weak`ly assigns onto an object at a specified key path.
///
/// - Parameters:
/// - keyPath: The key path to assign onto on the referenced object.
/// - object: The object reference to assign onto.
///
/// - Returns: A subscription cancellation token.
func weaklyAssign<Root: AnyObject>(to keyPath: ReferenceWritableKeyPath<Root, Output>,
on object: Root) -> AnyCancellable {
sink { [weak object] value in
object?[keyPath: keyPath] = value
}
}
}
46 changes: 46 additions & 0 deletions Tests/WeakAssignTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// WeakAssignTests.swift
// CombineExtTests
//
// Created by Jasdev Singh on 2/04/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//

import XCTest
import Combine
import CombineExt

final class WeakAssignTests: XCTestCase {
private var subscription: AnyCancellable!

func testWeakAssignment() {
let source = PassthroughSubject<Int, Never>()
var destination = Fake(property: 1)

XCTAssertEqual(destination.property, 1)

subscription = source
.assign(to: \.property, on: destination)

XCTAssertFalse(isKnownUniquelyReferenced(&destination))

subscription = source
.weaklyAssign(to: \.property, on: destination)

XCTAssertTrue(isKnownUniquelyReferenced(&destination))

source.send(2)
XCTAssertEqual(destination.property, 2)
}
}

// MARK: - Private Helpers

private final class Fake<PropertyType> {
var property: PropertyType

init(property: PropertyType) {
self.property = property
}
}