Skip to content

Commit

Permalink
Added Observable
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrommcarrasco committed Apr 1, 2019
1 parent 9d9bee8 commit 38e490a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sucrose.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.swift_version = "5.0"
s.name = "Sucrose"
s.version = "3.2.0"
s.version = "3.3.0"
s.summary = "🍬 Everyday sugar"
s.description = "Collection of handy methods & objects"

Expand Down
5 changes: 5 additions & 0 deletions Sucrose.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
209692162252C57A007EF9DB /* Observable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 209692152252C57A007EF9DB /* Observable.swift */; };
20D4D24021E67CEF00A3F80A /* Sucrose.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 20D4D23621E67CEF00A3F80A /* Sucrose.framework */; };
20D4D25121E67E5600A3F80A /* Collection+SafeIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D4D25021E67E5600A3F80A /* Collection+SafeIndex.swift */; };
20D4D25721E67ED000A3F80A /* NSObject+Create.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20D4D25621E67ED000A3F80A /* NSObject+Create.swift */; };
Expand Down Expand Up @@ -44,6 +45,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
209692152252C57A007EF9DB /* Observable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Observable.swift; sourceTree = "<group>"; };
20D4D23621E67CEF00A3F80A /* Sucrose.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sucrose.framework; sourceTree = BUILT_PRODUCTS_DIR; };
20D4D23A21E67CEF00A3F80A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
20D4D23F21E67CEF00A3F80A /* SucroseTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SucroseTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -155,6 +157,7 @@
isa = PBXGroup;
children = (
20D4D26121E6800900A3F80A /* Weak.swift */,
209692152252C57A007EF9DB /* Observable.swift */,
);
path = Objects;
sourceTree = "<group>";
Expand Down Expand Up @@ -255,6 +258,7 @@
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 20D4D22C21E67CEF00A3F80A;
productRefGroup = 20D4D23721E67CEF00A3F80A /* Products */;
Expand Down Expand Up @@ -301,6 +305,7 @@
20D4D25921E67F0600A3F80A /* UIButton+Init.swift in Sources */,
20D4D26221E6800900A3F80A /* Weak.swift in Sources */,
20D4D25F21E67FAA00A3F80A /* UIViewController+Hierarchy.swift in Sources */,
209692162252C57A007EF9DB /* Observable.swift in Sources */,
531857832215A5D700009D11 /* UIView+Nameable.swift in Sources */,
20D4D25B21E67F3D00A3F80A /* UIStackView+Hierarchy.swift in Sources */,
531857872215B5BC00009D11 /* UIView+Shape.swift in Sources */,
Expand Down
31 changes: 31 additions & 0 deletions Sucrose/Objects/Observable.swift
@@ -0,0 +1,31 @@
//
// Observable.swift
// Sucrose
//
// Created by Pedro Carrasco on 01/04/2019.
// Copyright © 2019 Pedro Carrasco. All rights reserved.
//

import Foundation

public final class Observable<T> {
public typealias Observer = (T) -> ()

public var value: T {
didSet { observer?(value) }
}

private var observer: Observer?

public init(_ value: T) {
self.value = value
}
}

extension Observable {

public func subscribe(_ observer: Observer?) {
self.observer = observer
observer?(value)
}
}

0 comments on commit 38e490a

Please sign in to comment.