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';
|
import extendDiscussionState from './PaginatedListStates/extendDiscussionState';
|
||||||
|
|
||||||
app.initializers.add('blomstra-search', () => {
|
app.initializers.add('blomstra-search', () => {
|
||||||
|
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;
|
const minLength = (app.forum.attribute('blomstraSearchMinLength') as number) || Search.MIN_SEARCH_LEN;
|
||||||
if (minLength !== 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;
|
(Search as any).MIN_SEARCH_LEN = minLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
extend(Search.prototype, 'sourceItems', function (this: Search<SearchAttrs>, items: ItemList<SearchSource>) {
|
|
||||||
items.replace('discussions', new DiscussionsSearchSource());
|
items.replace('discussions', new DiscussionsSearchSource());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue