1(function(){"use strict";if(window.MrMapleOptimistic)return;const DEFAULT_MIN_PENDING_MS=180,DEFAULT_DEBOUNCE_MS=400,TOGGLE_JOURNAL_STORAGE_KEY="mrmaple_optimistic_toggle_journal_v1",DEFAULT_RETRY_DELAYS_MS=[800,1800,3600],flushableRegistry=new Set;let lifecycleBound=!1,toggleJournalCache=null;function canUseLocalStorage(){try{return!!window.localStorage}catch{return!1}}function loadToggleJournal(){if(toggleJournalCache&&typeof toggleJournalCache=="object"||(toggleJournalCache={},!canUseLocalStorage()))return toggleJournalCache;try{const raw=window.localStorage.getItem(TOGGLE_JOURNAL_STORAGE_KEY);if(!raw)return toggleJournalCache;const parsed=JSON.parse(raw);if(!parsed||typeof parsed!="object"||Array.isArray(parsed))return toggleJournalCache;Object.keys(parsed).forEach(key=>{const entry=parsed[key];!entry||typeof entry!="object"||typeof entry.value=="boolean"&&(toggleJournalCache[key]={value:entry.value,updatedAt:Number(entry.updatedAt)||Date.now()})})}catch{}return toggleJournalCache}function persistToggleJournal(nextJournal){if(toggleJournalCache=nextJournal&&typeof nextJournal=="object"?nextJournal:{},!canUseLocalStorage())return toggleJournalCache;try{Object.keys(toggleJournalCache).length?window.localStorage.setItem(TOGGLE_JOURNAL_STORAGE_KEY,JSON.stringify(toggleJournalCache)):window.localStorage.removeItem(TOGGLE_JOURNAL_STORAGE_KEY)}catch{}return toggleJournalCache}function getPersistedToggleIntent(key){if(!key)return null;const entry=loadToggleJournal()[String(key)];return!entry||typeof entry.value!="boolean"?null:{key:String(key),value:entry.value,updatedAt:Number(entry.updatedAt)||0}}function setPersistedToggleIntent(key,value){if(!key)return null;const nextJournal={...loadToggleJournal(),[String(key)]:{value:!!value,updatedAt:Date.now()}};return persistToggleJournal(nextJournal),getPersistedToggleIntent(key)}function clearPersistedToggleIntent(key){if(!key)return;const nextJournal={...loadToggleJournal()};delete nextJournal[String(key)],persistToggleJournal(nextJournal)}function listPersistedToggleIntents(prefix=""){const normalizedPrefix=String(prefix||"");return Object.keys(loadToggleJournal()).filter(key=>normalizedPrefix?key.startsWith(normalizedPrefix):!0).map(key=>getPersistedToggleIntent(key)).filter(Boolean)}function ensureLifecycleBindings(){if(lifecycleBound||typeof window>"u"||typeof document>"u")return;lifecycleBound=!0;const flushForLifecycle=()=>{flushAllPending({reason:"pagehide",keepalive:!0})};window.addEventListener("pagehide",flushForLifecycle,{passive:!0}),document.addEventListener("visibilitychange",()=>{document.visibilityState==="hidden"&&flushForLifecycle()})}function registerFlushable(entry){return!entry||typeof entry.flush!="function"?()=>{}:(flushableRegistry.add(entry),ensureLifecycleBindings(),()=>{flushableRegistry.delete(entry)})}async function flushAllPending(options={}){const tasks=Array.from(flushableRegistry).map(entry=>{try{return Promise.resolve(entry.flush(options))}catch(error){return Promise.reject(error)}});return Promise.allSettled(tasks)}function createOptimisticToggle(opts){const{render,commit,onError,minPendingMs=DEFAULT_MIN_PENDING_MS,debounceMs=0,initialCommitted=!1,persistenceKey=null,registryKey=null,retryDelaysMs=DEFAULT_RETRY_DELAYS_MS}=opts,persistedIntent=persistenceKey?getPersistedToggleIntent(persistenceKey):null;let desired=persistedIntent?!!persistedIntent.value:!!initialCommitted,committed=!!initialCommitted,syncPromise=null,destroyed=!1,debounceTimer=null,debouncePromise=null,debounceResolve=null,retryTimer=null,retryAttempt=0,pendingReason=persistedIntent&&desired!==committed?"replay":null;const unregisterFlushable=registerFlushable({key:registryKey||persistenceKey||null,flush:options=>api.flush(options)});
1function renderState(value,pending){destroyed||render(value,{pending})}function clearRetry(){retryTimer&&(clearTimeout(retryTimer),retryTimer=null)}function flushDebounceWaiter(){if(debounceTimer&&(clearTimeout(debounceTimer),debounceTimer=null),debounceResolve){const resolve=debounceResolve;debounceResolve=null,debouncePromise=null,resolve()}}function ensureDebouncePromise(){return debounceMs<=0?Promise.resolve():(debouncePromise||(debouncePromise=new Promise(resolve=>{debounceResolve=resolve})),debounceTimer&&clearTimeout(debounceTimer),debounceTimer=setTimeout(()=>{flushDebounceWaiter()},debounceMs),debouncePromise)}function persistDesiredState(value){if(persistenceKey){if(!!value===committed){clearPersistedToggleIntent(persistenceKey);return}setPersistedToggleIntent(persistenceKey,value)}}function scheduleRetry(error){if(destroyed||!persistenceKey||error?.unauthorized||error?.status===401)return;const nextDelay=retryDelaysMs[Math.min(retryAttempt,retryDelaysMs.length-1)]||0;retryAttempt+=1,pendingReason="retry",clearRetry(),retryTimer=setTimeout(()=>{retryTimer=null,!(destroyed||desired===committed)&&syncDesired({reason:"retry",immediate:!0})},nextDelay)}function isPending(){return!!(syncPromise||debounceTimer||retryTimer)}async function flushDesiredState(options={}){for(;!destroyed&&desired!==committed;){const target=desired,pendingStart=Date.now();if(pendingReason=options.reason||pendingReason||"debounce",renderState(target,!0),!options.immediate&&debounceMs>0){if(await ensureDebouncePromise(),destroyed)return;if(desired!==target)continue}try{await commit(target,{reason:pendingReason,keepalive:options.keepalive===!0,persistenceKey,registryKey}),committed=target,retryAttempt=0,desired===committed&&clearPersistedToggleIntent(persistenceKey);const elapsed=Date.now()-pendingStart;if(!options.skipMinPending&&elapsed<minPendingMs&&await new Promise(resolve=>setTimeout(resolve,minPendingMs-elapsed)),desired!==target){persistDesiredState(desired);continue}pendingReason=null,renderState(target,!1)}catch(error){const shouldRevert=error?.unauthorized===!0||error?.status===401;if(desired===target&&(shouldRevert?(desired=committed,clearRetry(),clearPersistedToggleIntent(persistenceKey),pendingReason=null,renderState(committed,!1)):(persistDesiredState(desired),scheduleRetry(error),renderState(desired,!1))),typeof onError=="function"&&onError(error,{reverted:shouldRevert,desired,committed,pending:isPending(),persistenceKey,registryKey}),shouldRevert)throw error;return}}}function syncDesired(options={}){return destroyed?Promise.resolve():desired===committed?(clearRetry(),clearPersistedToggleIntent(persistenceKey),pendingReason=null,renderState(committed,!1),Promise.resolve()):(persistDesiredState(desired),options.immediate?flushDebounceWaiter():debounceMs>0&&ensureDebouncePromise(),syncPromise?(pendingReason=options.reason||pendingReason||"debounce",syncPromise):(syncPromise=flushDesiredState(options).catch(()=>{}).finally(()=>{syncPromise=null,!destroyed&&desired!==committed&&!retryTimer&&syncDesired({reason:"debounce"})}),syncPromise))}const api={toggle(options={}){api.setDesired(!desired,options)},setDesired(value,options={}){if(desired=!!value,pendingReason=options.reason||pendingReason||"debounce",desired===committed){clearRetry(),clearPersistedToggleIntent(persistenceKey),pendingReason=null,renderState(committed,!1);return}persistDesiredState(desired),renderState(desired,!0),syncDesired({reason:options.reason||"debounce",immediate:options.immediate===!0,keepalive:options.keepalive===!0})},setCommitted(value,options={}){committed=!!value;const persisted=persistenceKey?getPersistedToggleIntent(persistenceKey):null;if(options.forceDesired!==!0&&persisted&&persisted.value!==committed){desired=!!persisted.value,pendingReason="replay",renderState(desired,!0),syncDesired({reason:"replay",immediate:!0});return}if((!isPending()||options.forceDesired===!0)&&(desired=committed),desired===committed){clearRetry(),clearPersistedToggleIntent(persistenceKey),pendingReason=null,renderState(committed,!1);return}persistDesiredState(desired),renderState(desired,!0)},getDesired(){return desired},getCommitted(){return committed},isPending,getSnapshot(){return{desired,committed,pending:isPending(),pendingReason,persistedIntent:persistenceKey?getPersistedToggleIntent
1(persistenceKey):null,key:registryKey||persistenceKey||null}},async flush(options={}){if(!destroyed){if(desired===committed&&!syncPromise){clearPersistedToggleIntent(persistenceKey);return}if(flushDebounceWaiter(),syncPromise){await syncPromise;return}await syncDesired({reason:options.reason||"flush",immediate:!0,keepalive:options.keepalive===!0,skipMinPending:options.skipMinPending===!0})}},destroy(){destroyed=!0,flushDebounceWaiter(),clearRetry(),unregisterFlushable()},dispose(){api.destroy()}};return persistedIntent&&desired!==committed&&(renderState(desired,!0),syncDesired({reason:"replay",immediate:!0})),api}function createDebouncedMutation(opts){const{render,commit,onError,debounceMs=DEFAULT_DEBOUNCE_MS}=opts;let timer=null,lastCommitted,inFlight=null,destroyed=!1,pendingValue,replayRequested=!1;const flushable={key:opts.registryKey||null,flush:options=>api.flush(options)},unregisterFlushable=registerFlushable(flushable);function doFlush(value,meta={}){if(destroyed||typeof value>"u"||value===lastCommitted)return Promise.resolve();if(inFlight)return replayRequested=!0,inFlight;const target=value;return inFlight=Promise.resolve(commit(target,meta)).then(()=>{lastCommitted=target}).catch(error=>{!destroyed&&typeof onError=="function"&&(onError(error,lastCommitted),lastCommitted!==void 0&&render(lastCommitted))}).finally(()=>{if(inFlight=null,replayRequested&&pendingValue!==lastCommitted)return replayRequested=!1,doFlush(pendingValue,{reason:"replay"});replayRequested=!1}),inFlight}const api={setValue(value){pendingValue=value,destroyed||render(value),timer&&clearTimeout(timer),timer=setTimeout(()=>{timer=null,doFlush(pendingValue,{reason:"debounce"})},debounceMs)},async flush(options={}){timer&&(clearTimeout(timer),timer=null),await doFlush(pendingValue,{reason:options.reason||"flush",keepalive:options.keepalive===!0})},setCommitted(value){lastCommitted=value},destroy(){destroyed=!0,timer&&clearTimeout(timer),unregisterFlushable()},dispose(){api.destroy()}};return api}function createActionLock(opts){const{action,render,onError}=opts;let locked=!1,destroyed=!1;return{async execute(...args){if(!(locked||destroyed)){locked=!0,render(!0);try{return await action(...args)}catch(error){throw typeof onError=="function"&&onError(error),error}finally{locked=!1,destroyed||render(!1)}}},isLocked(){return locked},destroy(){destroyed=!0}}}window.MrMapleOptimistic={createOptimisticToggle,createDebouncedMutation,createActionLock,flushAllPending,getPersistedToggleIntent,setPersistedToggleIntent,clearPersistedToggleIntent,listPersistedToggleIntents}})(); 2//# sourceMappingURL=/cdn/shop/t/68/assets/mm-optimistic-service.js.map?v=145590146744365136351781628948
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.