( initialValue: string, initialOptions: PromptDraftControllerOptions, )
| 24 | const noopSubscribe = () => () => {} |
| 25 | |
| 26 | export function createPromptDraftController( |
| 27 | initialValue: string, |
| 28 | initialOptions: PromptDraftControllerOptions, |
| 29 | ): PromptDraftController { |
| 30 | let value = initialValue |
| 31 | let options = initialOptions |
| 32 | let suppressionTimer: ReturnType<typeof setTimeout> | null = null |
| 33 | const listeners = new Set<PromptDraftListener>() |
| 34 | |
| 35 | const clearSuppressionTimer = () => { |
| 36 | if (suppressionTimer !== null) { |
| 37 | clearTimeout(suppressionTimer) |
| 38 | suppressionTimer = null |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const notify = () => { |
| 43 | for (const listener of listeners) { |
| 44 | listener() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const setPromptSuppression = (nextValue: string) => { |
| 49 | const hasPromptText = nextValue.trim().length > 0 |
| 50 | options.setPromptInputActive(hasPromptText) |
| 51 | clearSuppressionTimer() |
| 52 | if (!hasPromptText) { |
| 53 | return |
| 54 | } |
| 55 | suppressionTimer = setTimeout(() => { |
| 56 | options.setPromptInputActive(false) |
| 57 | suppressionTimer = null |
| 58 | }, options.promptSuppressionMs) |
| 59 | } |
| 60 | |
| 61 | return { |
| 62 | destroy: () => { |
| 63 | clearSuppressionTimer() |
| 64 | listeners.clear() |
| 65 | }, |
| 66 | getValue: () => value, |
| 67 | setOptions: nextOptions => { |
| 68 | options = nextOptions |
| 69 | }, |
| 70 | setValue: nextValue => { |
| 71 | const prevValue = value |
| 72 | if (options.tryIntercept(prevValue, nextValue)) { |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | if ( |
| 77 | prevValue === '' && |
| 78 | nextValue !== '' && |
| 79 | (options.shouldRepinScroll?.() ?? true) && |
| 80 | Date.now() - options.getLastUserScrollTs() >= |
| 81 | options.recentScrollRepinWindowMs |
| 82 | ) { |
| 83 | options.repinScroll() |
no test coverage detected