fix: read forum attribute lazily inside extend callback
This commit is contained in:
parent
4709b9d1db
commit
bf37e01eb0
File diff suppressed because one or more lines are too long
|
|
@ -9,16 +9,14 @@ import DiscussionsSearchSource from './SearchSources/DiscussionsSearchSource';
|
|||
import extendDiscussionState from './PaginatedListStates/extendDiscussionState';
|
||||
|
||||
app.initializers.add('blomstra-search', () => {
|
||||
const minLength = (app.forum.attribute('blomstraSearchMinLength') as number) || Search.MIN_SEARCH_LEN;
|
||||
if (minLength !== Search.MIN_SEARCH_LEN) {
|
||||
// Flarum provides no extension point for MIN_SEARCH_LEN, so we overwrite the
|
||||
// static property directly. TypeScript `readonly` is compile-time only — at
|
||||
// runtime this is a plain property assignment and is safe as long as no code
|
||||
// reads MIN_SEARCH_LEN before this initializer runs.
|
||||
(Search as any).MIN_SEARCH_LEN = minLength;
|
||||
}
|
||||
|
||||
extend(Search.prototype, 'sourceItems', function (this: Search<SearchAttrs>, items: ItemList<SearchSource>) {
|
||||
// app.forum is not available during initializers (it is set after they run),
|
||||
// so read the setting lazily here, at first render time.
|
||||
const minLength = (app.forum.attribute('blomstraSearchMinLength') as number) || Search.MIN_SEARCH_LEN;
|
||||
if (minLength !== Search.MIN_SEARCH_LEN) {
|
||||
(Search as any).MIN_SEARCH_LEN = minLength;
|
||||
}
|
||||
|
||||
items.replace('discussions', new DiscussionsSearchSource());
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue