Last active
August 29, 2015 14:06
-
-
Save hiddenmemory/0a9069dd276c7fca263f to your computer and use it in GitHub Desktop.
API naive example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class API { | |
private var firstCompletionHandlers: [() -> ()] = [] | |
private var hasCompletedFirst = false | |
private func completeFirst() { | |
hasCompletedFirst = true | |
// API CALL RESPONSE | |
if firstCompletionHandlers.count > 0 { | |
for value in firstCompletionHandlers { | |
value() | |
} | |
firstCompletionHandlers = [] | |
} | |
} | |
private func checkFirst(completion: () -> ()) { | |
if hasCompletedFirst { | |
completion() | |
} | |
else { | |
firstCompletionHandlers.append(completion) | |
first() | |
} | |
} | |
func first() { | |
completeFirst() | |
} | |
func second() { | |
checkFirst { | |
println("Second API Call") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment