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

When a route matches multiple handlers, make it FIFO #121

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion Sources/URLNavigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ open class Navigator: NavigatorType {
open weak var delegate: NavigatorDelegate?

private var viewControllerFactories = [URLPattern: ViewControllerFactory]()
private var handlerFactories = [URLPattern: URLOpenHandlerFactory]()
private var handlerFactories = OrderedDictionary<URLPattern, URLOpenHandlerFactory>()

public init() {
// ⛵ I'm a Navigator!
Expand Down
79 changes: 79 additions & 0 deletions Sources/URLNavigator/OrderedDictionary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// OrderedDictionary.swift
// URLNavigator
//
// Created by Jerome Pasquier on 28/01/2019.
//

import Foundation

struct OrderedDictionary<Key: Hashable, Value> {
var keys = [Key]()
var values = [Key: Value]()

var count: Int {
return self.keys.count
}

subscript(index: Int) -> Value? {
get {
let key = self.keys[index]
return self.values[key]
}
set(newValue) {
let key = self.keys[index]
if newValue != nil {
self.values[key] = newValue
} else {
self.values.removeValue(forKey: key)
self.keys.remove(at: index)
}
}
}

subscript(key: Key) -> Value? {
get {
return self.values[key]
}
set(newValue) {
if let newVal = newValue {
let oldValue = self.values.updateValue(newVal, forKey: key)
if oldValue == nil {
self.keys.append(key)
}
} else {
self.values.removeValue(forKey: key)
self.keys = self.keys.filter {$0 != key}
}
}
}

}

extension OrderedDictionary: CustomStringConvertible {
var description: String {
let isString = type(of: self.keys[0]) == String.self
var result = "["
for key in keys {
result += isString ? "\"\(key)\"" : "\(key)"
result += ": \(self[key]!), "
}
result = String(result.dropLast(2))
result += "]"
return result
}
}

extension OrderedDictionary: Sequence {
func makeIterator() -> AnyIterator<Value> {
var counter = 0
return AnyIterator {
guard counter<self.keys.count else {
return nil
}
let next = self.values[self.keys[counter]]
counter += 1
return next
}
}
}