-
Notifications
You must be signed in to change notification settings - Fork 525
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
refactor(core): move options diff to core #5908
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,7 @@ export type InstantSearchStatus = 'idle' | 'loading' | 'stalled' | 'error'; | |
export const INSTANTSEARCH_FUTURE_DEFAULTS: Required< | ||
InstantSearchOptions['future'] | ||
> = { preserveSharedStateOnUnmount: false }; | ||
export const INSTANTSEARCH_STALLED_SEARCH_DEFAULTS = 200; | ||
|
||
/** | ||
* The actual implementation of the InstantSearch. This is | ||
|
@@ -250,7 +251,7 @@ Use \`InstantSearch.status === "stalled"\` instead.` | |
routing = null, | ||
insights = undefined, | ||
searchFunction, | ||
stalledSearchDelay = 200, | ||
stalledSearchDelay = INSTANTSEARCH_STALLED_SEARCH_DEFAULTS, | ||
searchClient = null, | ||
insightsClient = null, | ||
onStateChange = null, | ||
|
@@ -364,8 +365,9 @@ See documentation: ${createDocumentationLink({ | |
// This is the default Insights middleware, | ||
// added when `insights` is set to true by the user. | ||
// Any user-provided middleware will be added later and override this one. | ||
if (insights) { | ||
const insightsOptions = typeof insights === 'boolean' ? {} : insights; | ||
if (this._insights) { | ||
const insightsOptions = | ||
typeof this._insights === 'boolean' ? {} : this._insights; | ||
insightsOptions.$$internal = true; | ||
this.use(createInsightsMiddleware(insightsOptions)); | ||
} | ||
|
@@ -840,6 +842,100 @@ See documentation: ${createDocumentationLink({ | |
|
||
this.mainHelper.clearCache().search(); | ||
} | ||
|
||
/** | ||
* Update the parameters passed to the InstantSearch constructor. | ||
*/ | ||
public update( | ||
options: Partial<InstantSearchOptions<TUiState, TRouteState>> | ||
): void { | ||
if (this.indexName !== options.indexName) { | ||
this.helper!.setIndex(options.indexName || '').search(); | ||
} | ||
|
||
if (this.client !== options.searchClient) { | ||
warning( | ||
false, | ||
// TODO: flavor-specific warning (search._flavor = 'vue') URL | ||
'The `searchClient` parameter changed, which may cause more search requests than necessary. If this is an unwanted behavior, please provide a stable reference.' | ||
); | ||
|
||
if (!options.searchClient) { | ||
throw new Error(withUsage('The `searchClient` option is required.')); | ||
} | ||
|
||
if (typeof options.searchClient.search !== 'function') { | ||
throw new Error( | ||
`The \`searchClient\` must implement a \`search\` method. | ||
|
||
See: https://www.algolia.com/doc/guides/building-search-ui/going-further/backend-search/in-depth/backend-instantsearch/js/` | ||
); | ||
} | ||
|
||
// TODO: proper API for this too | ||
// InstantSearch._algoliaAgents (or constructor) | ||
// addAlgoliaAgents(props.searchClient, [ | ||
// ...defaultUserAgents, | ||
// serverContext && serverUserAgent, | ||
// ]); | ||
|
||
this.mainHelper!.setClient(options.searchClient).search(); | ||
} | ||
|
||
if (this.onStateChange !== options.onStateChange) { | ||
this.onStateChange = options.onStateChange; | ||
} | ||
|
||
if (this._searchFunction !== options.searchFunction) { | ||
this._searchFunction = | ||
options.searchFunction ?? | ||
((helper) => { | ||
helper.search(); | ||
}); | ||
} | ||
|
||
if (this._stalledSearchDelay !== options.stalledSearchDelay) { | ||
this._stalledSearchDelay = | ||
options.stalledSearchDelay ?? INSTANTSEARCH_STALLED_SEARCH_DEFAULTS; | ||
} | ||
|
||
// TODO: move dequal to a shared package? | ||
// if (!dequal(this.future, props.future)) { | ||
// this.future = { | ||
// ...INSTANTSEARCH_FUTURE_DEFAULTS, | ||
// ...props.future, | ||
// }; | ||
// } | ||
|
||
// TODO: implement update in middleware | ||
// TODO: can this be simplified? | ||
// if (!dequal(this._insights, options.insights)) { | ||
// this._insights = options.insights; | ||
// const existing = this.middleware.find( | ||
// (m) => m.instance.$$type === 'ais.insights' && m.instance.$$internal | ||
// ); | ||
// if (options.insights && existing) { | ||
// // TODO: update existing middleware somehow (or maybe remount it) | ||
// } else if (options.insights && !existing) { | ||
// const insightsOptions = | ||
// typeof options.insights === 'boolean' ? {} : options.insights; | ||
// insightsOptions.$$internal = true; | ||
// this.use(createInsightsMiddleware(insightsOptions)); | ||
// } else if (!options.insights && existing) { | ||
// this.unuse(existing.creator); | ||
// } | ||
// } | ||
|
||
// TODO: validate other options (onStateChange, routing, give a warning for all non-updated values?) | ||
|
||
// Updating the `routing` prop is not supported because InstantSearch.js | ||
// doesn't let us change it. This might not be a problem though, because `routing` | ||
// shouldn't need to be dynamic. | ||
// If we find scenarios where `routing` needs to change, we can always expose | ||
// it privately on the InstantSearch instance. Another way would be to | ||
// manually inject the routing middleware in this library, and not rely | ||
// on the provided `routing` prop. | ||
Comment on lines
+931
to
+937
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still can't see why routing would need to change during the lifecycle of an InstantSearch app 😅. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I'd rather ignore or show an error than handling it |
||
} | ||
} | ||
|
||
export default InstantSearch; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love for middlewares to remove some of the load from this method. Maybe we could iterate over middlewares, call their
update()
method, and get a returned value (boolean? object?) which could tellInstantSearch.update()
to unuse them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case probably
existing.update(options.insights)
and handling that somehow in the middleware indeed was best, wasn't sure whether the api was worth the coals