Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:algolia/instantsearch-core-swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément Le Provost committed Dec 16, 2016
2 parents 8d1aefb + 65cb317 commit 6317863
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Sources/Searcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ import Foundation
/// All currently ongoing requests.
private var pendingRequests: [Int: Operation] = [:]

/// Maximum number of pending requests allowed.
/// If many requests are made in a short time, this will keep only the N most recent and cancel the older ones.
/// This helps to avoid filling up the request queue when the network is slow.
///
@objc public var maxPendingRequests: Int = 3

// MARK: - Initialization, termination

/// Create a new searcher targeting the specified index.
Expand Down Expand Up @@ -339,6 +345,12 @@ import Foundation
Searcher.notificationSeqNoKey: currentSeqNo
]

// Cancel too old requests.
let tooOldRequests = pendingRequests.filter({ $0.0 <= currentSeqNo - maxPendingRequests })
for (seqNo, _) in tooOldRequests {
cancelRequest(seqNo: seqNo)
}

// Run request
// -----------
var operation: Operation!
Expand Down Expand Up @@ -416,6 +428,37 @@ import Foundation
}
}

// MARK: - Search for facet values

/// Search for values of a given facet.
///
/// This is a convenience shortcut for Algolia Search's `Index.searchForFacetValues(...)` method that will
/// automatically use the current search `params` as the refining query, also taking into account the
/// conjunctive/disjunctive status of the targeted facet (i.e. refinements for the targeted facet will be discarded
/// when the facet is disjunctive).
///
/// Unlike a regular `search()`:
///
/// - The searched text has to be passed as an argument to the method, as it differs in purpose from `params.query`.
/// - Result handlers or the delegate are not called; instead, the provided completion handler is called.
/// Notifications are not issued either.
///
/// - parameter facetName: Name of the facet to search. It must have been declared in the index's
/// `attributesForFaceting` setting with the `searchable()` modifier.
/// - parameter text: Text to search for in the facet's values.
/// - parameter completionHandler: Completion handler to be notified of the request's outcome.
/// - returns: A cancellable operation.
///
@objc @discardableResult
public func searchForFacetValues(of facetName: String, matching text: String, completionHandler: @escaping CompletionHandler) -> Operation {
let facetSearchParams = SearchParameters(from: self.params)
// If the searched facet is disjunctive, clear any refinements that it may have.
if facetSearchParams.isDisjunctiveFacet(name: facetName) {
facetSearchParams.clearFacetRefinements(name: facetName)
}
return index.searchForFacetValues(of: facetName, matching: text, query: facetSearchParams, completionHandler: completionHandler)
}

// MARK: - Managing requests

/// Indicates whether there are any pending requests.
Expand Down

0 comments on commit 6317863

Please sign in to comment.