@@ -25,20 +25,40 @@ export function nextAutoSwitchThreshold(current: number, lastEnabled: number): n
2525 : DEFAULT_AUTO_SWITCH_THRESHOLD ;
2626}
2727
28+ export type AutoSwitchThresholdReadDisposition = "apply" | "defer" | "ignore" ;
29+
30+ export function autoSwitchThresholdReadDisposition (
31+ editing : boolean ,
32+ saving : boolean ,
33+ startedRevision : number ,
34+ currentRevision : number ,
35+ ) : AutoSwitchThresholdReadDisposition {
36+ if ( startedRevision !== currentRevision ) return "ignore" ;
37+ return editing || saving ? "defer" : "apply" ;
38+ }
39+
40+ export interface AutoSwitchTogglePlan {
41+ threshold : number ;
42+ lastEnabled : number ;
43+ }
44+
2845/**
29- * A toggle click keeps focus on the toggle so input blur cannot race a second
30- * write. If the enabled draft changed, persist it first so a later re-enable
31- * restores the last successfully saved positive threshold .
46+ * Disabling is one server write. A valid dirty draft becomes the page-lifetime
47+ * restore value, so re-enabling can persist it without a partial two-write
48+ * failure state .
3249 */
33- export function planAutoSwitchToggleWrites (
50+ export function planAutoSwitchToggleWrite (
3451 current : number ,
3552 draft : string ,
3653 lastEnabled : number ,
37- ) : number [ ] | null {
38- if ( current <= 0 ) return [ nextAutoSwitchThreshold ( current , lastEnabled ) ] ;
54+ ) : AutoSwitchTogglePlan | null {
55+ if ( current <= 0 ) {
56+ const threshold = nextAutoSwitchThreshold ( current , lastEnabled ) ;
57+ return { threshold, lastEnabled : threshold } ;
58+ }
3959 const parsedDraft = parseEnabledAutoSwitchThreshold ( draft ) ;
4060 if ( parsedDraft === null ) return null ;
41- return parsedDraft === current ? [ 0 ] : [ parsedDraft , 0 ] ;
61+ return { threshold : 0 , lastEnabled : parsedDraft } ;
4262}
4363
4464export async function putAutoSwitchThreshold (
0 commit comments