Skip to content
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

Added support for EachDisposer #15

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Each Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import UIKit
import Each

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

Expand Down
41 changes: 29 additions & 12 deletions Each.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
0F41C47A1DF37BC20076BB6B /* Each.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F41C43A1DF373B40076BB6B /* Each.h */; settings = {ATTRIBUTES = (Public, ); }; };
0F41C47B1DF37BC20076BB6B /* Each.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F41C43A1DF373B40076BB6B /* Each.h */; settings = {ATTRIBUTES = (Public, ); }; };
0F41C4981DF37D2D0076BB6B /* Each-macOS.h in Headers */ = {isa = PBXBuildFile; fileRef = 0F41C4971DF37D2D0076BB6B /* Each-macOS.h */; settings = {ATTRIBUTES = (Public, ); }; };
496258C61FB8897100445EA8 /* EachDisposerTestCases.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496258C51FB8897100445EA8 /* EachDisposerTestCases.swift */; };
49B261011FB6FDAC002AEC8E /* EachDisposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B261001FB6FDAC002AEC8E /* EachDisposer.swift */; };
49B261021FB6FDAC002AEC8E /* EachDisposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B261001FB6FDAC002AEC8E /* EachDisposer.swift */; };
49B261031FB6FDAC002AEC8E /* EachDisposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B261001FB6FDAC002AEC8E /* EachDisposer.swift */; };
49B261041FB6FDAC002AEC8E /* EachDisposer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B261001FB6FDAC002AEC8E /* EachDisposer.swift */; };
817D34741DBB6A4A0059E633 /* EachTimeIntervalTestCases.swift in Sources */ = {isa = PBXBuildFile; fileRef = 817D34731DBB6A4A0059E633 /* EachTimeIntervalTestCases.swift */; };
81C8F80D1DB0C487009FE6D3 /* EachTestCases.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C8F80C1DB0C487009FE6D3 /* EachTestCases.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -75,6 +80,8 @@
0F41C46D1DF379C70076BB6B /* Each.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Each.framework; sourceTree = BUILT_PRODUCTS_DIR; };
0F41C4751DF379FF0076BB6B /* Info-macOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Info-macOS.plist"; path = "Sources/Info-macOS.plist"; sourceTree = "<group>"; };
0F41C4971DF37D2D0076BB6B /* Each-macOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "Each-macOS.h"; path = "Sources/Each-macOS.h"; sourceTree = "<group>"; };
496258C51FB8897100445EA8 /* EachDisposerTestCases.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EachDisposerTestCases.swift; sourceTree = "<group>"; };
49B261001FB6FDAC002AEC8E /* EachDisposer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = EachDisposer.swift; path = Sources/EachDisposer.swift; sourceTree = "<group>"; };
817D34731DBB6A4A0059E633 /* EachTimeIntervalTestCases.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EachTimeIntervalTestCases.swift; sourceTree = "<group>"; };
81A194441DB0C3A100985EC0 /* Each Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Each Example.app"; sourceTree = BUILT_PRODUCTS_DIR; };
81A194581DB0C3A200985EC0 /* EachTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = EachTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -180,6 +187,7 @@
children = (
81C8F80C1DB0C487009FE6D3 /* EachTestCases.swift */,
817D34731DBB6A4A0059E633 /* EachTimeIntervalTestCases.swift */,
496258C51FB8897100445EA8 /* EachDisposerTestCases.swift */,
81A1945E1DB0C3A200985EC0 /* Info.plist */,
);
path = EachTests;
Expand All @@ -189,6 +197,7 @@
isa = PBXGroup;
children = (
81A194681DB0C3EC00985EC0 /* Each.swift */,
49B261001FB6FDAC002AEC8E /* EachDisposer.swift */,
0F41C43A1DF373B40076BB6B /* Each.h */,
0F41C4971DF37D2D0076BB6B /* Each-macOS.h */,
0F41C4651DF379600076BB6B /* Info-iOS.plist */,
Expand Down Expand Up @@ -460,6 +469,7 @@
buildActionMask = 2147483647;
files = (
0F41C4341DF373470076BB6B /* Each.swift in Sources */,
49B261011FB6FDAC002AEC8E /* EachDisposer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -468,6 +478,7 @@
buildActionMask = 2147483647;
files = (
0F41C4771DF37AEF0076BB6B /* Each.swift in Sources */,
49B261031FB6FDAC002AEC8E /* EachDisposer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -476,6 +487,7 @@
buildActionMask = 2147483647;
files = (
0F41C4761DF37AEE0076BB6B /* Each.swift in Sources */,
49B261021FB6FDAC002AEC8E /* EachDisposer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -484,6 +496,7 @@
buildActionMask = 2147483647;
files = (
0F41C4781DF37AEF0076BB6B /* Each.swift in Sources */,
49B261041FB6FDAC002AEC8E /* EachDisposer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -500,6 +513,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
496258C61FB8897100445EA8 /* EachDisposerTestCases.swift in Sources */,
817D34741DBB6A4A0059E633 /* EachTimeIntervalTestCases.swift in Sources */,
81C8F80D1DB0C487009FE6D3 /* EachTestCases.swift in Sources */,
);
Expand Down Expand Up @@ -557,7 +571,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.dalu93.Each-iOS";
PRODUCT_NAME = Each;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -580,7 +594,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.dalu93.Each-iOS";
PRODUCT_NAME = Each;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -603,7 +617,7 @@
PRODUCT_NAME = Each;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 4;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -628,7 +642,7 @@
PRODUCT_NAME = Each;
SDKROOT = watchos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 4;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -653,7 +667,7 @@
PRODUCT_NAME = Each;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -678,7 +692,7 @@
PRODUCT_NAME = Each;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 9.0;
VERSIONING_SYSTEM = "apple-generic";
Expand Down Expand Up @@ -706,7 +720,7 @@
PRODUCT_NAME = Each;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -732,7 +746,7 @@
PRODUCT_NAME = Each;
SDKROOT = macosx;
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down Expand Up @@ -842,7 +856,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.dalu93.EachExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -855,7 +869,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.dalu93.EachExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -868,7 +882,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.dalu93.EachTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -881,7 +895,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.dalu93.EachTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -904,6 +918,7 @@
0F41C4571DF378400076BB6B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
0F41C4621DF378E00076BB6B /* Build configuration list for PBXNativeTarget "Each tvOS" */ = {
isa = XCConfigurationList;
Expand All @@ -912,6 +927,7 @@
0F41C4641DF378E00076BB6B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
0F41C4721DF379C70076BB6B /* Build configuration list for PBXNativeTarget "Each macOS" */ = {
isa = XCConfigurationList;
Expand All @@ -920,6 +936,7 @@
0F41C4741DF379C70076BB6B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
81A1943F1DB0C3A100985EC0 /* Build configuration list for PBXProject "Each" */ = {
isa = XCConfigurationList;
Expand Down
63 changes: 63 additions & 0 deletions EachTests/EachDisposerTestCases.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// EachDisposerTestCases.swift
// EachTests
//
// Created by D'Alberti, Luca on 11/12/17.
// Copyright © 2017 dalu93. All rights reserved.
//

@testable import Each
import XCTest

class EachDisposerTestCases: XCTestCase {

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

func testAddDisposable() {
// GIVEN
let timer = Each(1).seconds
let disposer = EachDisposer()

// WHEN
disposer.add(timer)

// THEN
XCTAssertTrue(disposer.timers.count == 1)
}

func testAddDisposableTwice() {
// GIVEN
let timer = Each(1).seconds
let disposer = EachDisposer()

// WHEN
disposer.add(timer)
disposer.add(timer)

// THEN
XCTAssertTrue(disposer.timers.count == 2)
}

func testDispose() {
// GIVEN
let timer = Each(1).seconds
let disposer = EachDisposer()
disposer.add(timer)
timer.perform {
// do something
return .continue
}

// WHEN
disposer.dispose()

// THEN
XCTAssertTrue(timer.isStopped == true)
}
}
15 changes: 15 additions & 0 deletions EachTests/EachTestCases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ class EachTestCases: XCTestCase {
print(error)
}
}

func testEachStop() {
// GIVEN
let timer = Each(2).seconds
timer.perform {
// do something
return .continue
}

// WHEN
timer.stop()

// THEN
XCTAssertTrue(timer.isStopped == true)
}

func testEachStopAndStartAgain() {
let exp = expectation(description: "Timer waiting")
Expand Down
44 changes: 44 additions & 0 deletions Sources/EachDisposer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// EachDisposer.swift
// Each
//
// Created by D'Alberti, Luca on 11/11/17.
// Copyright © 2017 dalu93. All rights reserved.
//

import Foundation

// MARK: - EachDisposer declaration
/// `EachDisposer` can dispose a set of `Each` objects.
///
/// The main feature of this object is that it provides an automatic disposition
/// of every `Each` instance when the disposer is deinitialized
open class EachDisposer {

/// List of `Each`s added to the `Disposer`
private(set) var timers: [Each] = []

public init() {}

/// Add the `Each` instance as `Disposable`
///
/// The `Each` instance is retained and it will be deinitialized and stopped
/// together with the `EachDisposer` instance
///
/// - Parameter disposable: The `Each` instance
public func add(_ disposable: Each) {
timers.append(disposable)
}

/// Forces the dispose of all the `Each`s added
public func dispose() {
timers.forEach { $0.stop() }
timers = []
}

deinit {
dispose()
}
}