a27d6fa36d380158aef9b528f0ee94da.json 15.8 KB
{"ast":null,"code":"var _interopRequireDefault=require(\"@babel/runtime/helpers/interopRequireDefault\");Object.defineProperty(exports,\"__esModule\",{value:true});exports.default=getChildEventSubscriber;var _objectSpread2=_interopRequireDefault(require(\"@babel/runtime/helpers/objectSpread\"));function getChildEventSubscriber(addListener,key){var initialLastFocusEvent=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'didBlur';var actionSubscribers=new Set();var willFocusSubscribers=new Set();var didFocusSubscribers=new Set();var willBlurSubscribers=new Set();var didBlurSubscribers=new Set();var refocusSubscribers=new Set();var removeAll=function removeAll(){[actionSubscribers,willFocusSubscribers,didFocusSubscribers,willBlurSubscribers,didBlurSubscribers,refocusSubscribers].forEach(function(set){return set.clear();});upstreamSubscribers.forEach(function(subs){return subs&&subs.remove();});};var getChildSubscribers=function getChildSubscribers(evtName){switch(evtName){case'action':return actionSubscribers;case'willFocus':return willFocusSubscribers;case'didFocus':return didFocusSubscribers;case'willBlur':return willBlurSubscribers;case'didBlur':return didBlurSubscribers;case'refocus':return refocusSubscribers;default:return null;}};var _emit=function emit(type,payload){var payloadWithType=(0,_objectSpread2.default)({},payload,{type:type});var subscribers=getChildSubscribers(type);subscribers&&subscribers.forEach(function(subs){subs(payloadWithType);});};var lastFocusEvent=initialLastFocusEvent;var upstreamEvents=['willFocus','didFocus','willBlur','didBlur','refocus','action'];var upstreamSubscribers=upstreamEvents.map(function(eventName){return addListener(eventName,function(payload){if(eventName==='refocus'){_emit(eventName,payload);return;}var state=payload.state,lastState=payload.lastState,action=payload.action;var lastRoutes=lastState&&lastState.routes;var routes=state&&state.routes;var focusKey=routes&&routes[state.index].key;var isChildFocused=focusKey===key;var lastRoute=lastRoutes&&lastRoutes.find(function(route){return route.key===key;});var newRoute=routes&&routes.find(function(route){return route.key===key;});var childPayload={context:key+\":\"+action.type+\"_\"+(payload.context||'Root'),state:newRoute,lastState:lastRoute,action:action,type:eventName};var isTransitioning=!!state&&state.isTransitioning;var previouslylastFocusEvent=lastFocusEvent;if(lastFocusEvent==='didBlur'){if(eventName==='willFocus'&&isChildFocused){_emit(lastFocusEvent='willFocus',childPayload);}else if(eventName==='action'&&isChildFocused){_emit(lastFocusEvent='willFocus',childPayload);}}if(lastFocusEvent==='willFocus'){if(eventName==='didFocus'&&isChildFocused&&!isTransitioning){_emit(lastFocusEvent='didFocus',childPayload);}else if(eventName==='action'&&isChildFocused&&!isTransitioning){_emit(lastFocusEvent='didFocus',childPayload);}}if(lastFocusEvent==='didFocus'){if(!isChildFocused){_emit(lastFocusEvent='willBlur',childPayload);}else if(eventName==='willBlur'){_emit(lastFocusEvent='willBlur',childPayload);}else if(eventName==='action'&&previouslylastFocusEvent==='didFocus'){_emit('action',childPayload);}}if(lastFocusEvent==='willBlur'){if(eventName==='action'&&!isChildFocused&&!isTransitioning){_emit(lastFocusEvent='didBlur',childPayload);}else if(eventName==='didBlur'){_emit(lastFocusEvent='didBlur',childPayload);}else if(eventName==='action'&&isChildFocused&&!isTransitioning){_emit(lastFocusEvent='didFocus',childPayload);}else if(eventName==='action'&&isChildFocused&&isTransitioning){_emit(lastFocusEvent='willFocus',childPayload);}}if(lastFocusEvent==='didBlur'&&!newRoute){removeAll();}});});return{addListener:function addListener(eventName,eventHandler){var subscribers=getChildSubscribers(eventName);if(!subscribers){throw new Error(\"Invalid event name \\\"\"+eventName+\"\\\"\");}subscribers.add(eventHandler);var remove=function remove(){subscribers.delete(eventHandler);};return{remove:remove};},emit:function emit(eventName,payload){if(eventName!=='refocus'){console.error(\"navigation.emit only supports the 'refocus' event currently.\");return;}_emit(eventName,payload);}};}","map":{"version":3,"sources":["getChildEventSubscriber.js"],"names":["initialLastFocusEvent","actionSubscribers","willFocusSubscribers","didFocusSubscribers","willBlurSubscribers","didBlurSubscribers","refocusSubscribers","removeAll","set","upstreamSubscribers","subs","getChildSubscribers","emit","payloadWithType","type","subscribers","lastFocusEvent","upstreamEvents","addListener","eventName","state","lastState","action","payload","lastRoutes","routes","focusKey","isChildFocused","lastRoute","route","newRoute","childPayload","context","key","isTransitioning","previouslylastFocusEvent","remove","console"],"mappings":"oLAMA,0FAAe,QAAA,CAAA,uBAAA,CAAA,WAAA,CAAA,GAAA,CAIb,CADAA,GAAAA,CAAAA,qBACA,CAAA,SAAA,CAAA,MAAA,CAAA,CAAA,EAAA,SAAA,CAAA,CAAA,CAAA,GAAA,SAAA,CAAA,SAAA,CAAA,CAAA,CAAA,CADwB,SAAxBA,CAEA,GAAMC,CAAAA,iBAAiB,CAAG,GAA1B,CAAA,GAA0B,EAA1B,CACA,GAAMC,CAAAA,oBAAoB,CAAG,GAA7B,CAAA,GAA6B,EAA7B,CACA,GAAMC,CAAAA,mBAAmB,CAAG,GAA5B,CAAA,GAA4B,EAA5B,CACA,GAAMC,CAAAA,mBAAmB,CAAG,GAA5B,CAAA,GAA4B,EAA5B,CACA,GAAMC,CAAAA,kBAAkB,CAAG,GAA3B,CAAA,GAA2B,EAA3B,CACA,GAAMC,CAAAA,kBAAkB,CAAG,GAA3B,CAAA,GAA2B,EAA3B,CAEA,GAAMC,CAAAA,SAAS,CAATA,QAAAA,CAAAA,SAAAA,EAAkB,CACtB,CAAA,iBAAA,CAAA,oBAAA,CAAA,mBAAA,CAAA,mBAAA,CAAA,kBAAA,CAAA,kBAAA,EAAA,OAAA,CAOU,SAAA,GAAA,CAAG,CAAA,MAAIC,CAAAA,GAAG,CAAP,KAAIA,EAAJ,CAPb,CAAA,EASAC,mBAAmB,CAAnBA,OAAAA,CAA4B,SAAA,IAAA,CAAI,CAAA,MAAIC,CAAAA,IAAI,EAAIA,IAAI,CAAhB,MAAYA,EAAZ,CAAhCD,CAAAA,EAVF,CAAA,CAaA,GAAME,CAAAA,mBAAmB,CAAnBA,QAAAA,CAAAA,mBAAAA,CAAsB,OAAtBA,CAAiC,CACrC,OAAA,OAAA,EACE,IAAA,QAAA,CACE,MAAA,CAAA,iBAAA,CACF,IAAA,WAAA,CACE,MAAA,CAAA,oB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AAc,GAAdA,SAAAA,EAAgC,CAApC,QAAA,CAA+C,CAC7CT,SAAS,GAEZ,CAhGqD,CAC3C,CAD2C,CAAxD,CAA4BU,CAA5B,CAmGA,MAAO,CACLC,WADK,CAAA,QAAA,CAAA,WAAA,CAAA,SAAA,CAAA,YAAA,CACgC,CACnC,GAAMH,CAAAA,WAAW,CAAGJ,mBAAmB,CAAvC,SAAuC,CAAvC,CACA,GAAI,CAAJ,WAAA,CAAkB,CAChB,KAAM,IAAA,CAAA,KAAA,CAAA,wBAAA,SAAA,CAAN,IAAM,CAAN,CAEFI,CAAAA,WAAW,CAAXA,GAAAA,CAAAA,YAAAA,EACA,GAAMqB,CAAAA,MAAM,CAANA,QAAAA,CAAAA,MAAAA,EAAe,CACnBrB,WAAW,CAAXA,MAAAA,CAAAA,YAAAA,EADF,CAAA,CAGA,MAAO,CAAEqB,MAAM,CAAf,MAAO,CAAP,CAVG,CAAA,CAYLxB,IAZK,CAAA,QAAA,CAAA,IAAA,CAAA,SAAA,CAAA,OAAA,CAYoB,CACvB,GAAIO,SAAS,GAAb,SAAA,CAA6B,CAC3BkB,OAAO,CAAPA,KAAAA,CAAAA,8DAAAA,EAGA,OAEFzB,CAAAA,KAAI,CAAA,SAAA,CAAJA,OAAI,CAAJA,CAnBJ,CAAO,CAAP,CAsBD","sourcesContent":["/*\n * This is used to extract one children's worth of events from a stream of navigation action events\n *\n * Based on the 'action' events that get fired for this navigation state, this utility will fire\n * focus and blur events for this child\n */\nexport default function getChildEventSubscriber(\n  addListener,\n  key,\n  initialLastFocusEvent = 'didBlur'\n) {\n  const actionSubscribers = new Set();\n  const willFocusSubscribers = new Set();\n  const didFocusSubscribers = new Set();\n  const willBlurSubscribers = new Set();\n  const didBlurSubscribers = new Set();\n  const refocusSubscribers = new Set();\n\n  const removeAll = () => {\n    [\n      actionSubscribers,\n      willFocusSubscribers,\n      didFocusSubscribers,\n      willBlurSubscribers,\n      didBlurSubscribers,\n      refocusSubscribers,\n    ].forEach(set => set.clear());\n\n    upstreamSubscribers.forEach(subs => subs && subs.remove());\n  };\n\n  const getChildSubscribers = evtName => {\n    switch (evtName) {\n      case 'action':\n        return actionSubscribers;\n      case 'willFocus':\n        return willFocusSubscribers;\n      case 'didFocus':\n        return didFocusSubscribers;\n      case 'willBlur':\n        return willBlurSubscribers;\n      case 'didBlur':\n        return didBlurSubscribers;\n      case 'refocus':\n        return refocusSubscribers;\n      default:\n        return null;\n    }\n  };\n\n  const emit = (type, payload) => {\n    const payloadWithType = { ...payload, type };\n    const subscribers = getChildSubscribers(type);\n    subscribers &&\n      subscribers.forEach(subs => {\n        subs(payloadWithType);\n      });\n  };\n\n  // lastFocusEvent keeps track of focus state for one route. First we assume\n  // we are blurred. If we are focused on initialization, the first 'action'\n  // event will cause onFocus+willFocus events because we had previously been\n  // considered blurred\n  let lastFocusEvent = initialLastFocusEvent;\n\n  const upstreamEvents = [\n    'willFocus',\n    'didFocus',\n    'willBlur',\n    'didBlur',\n    'refocus',\n    'action',\n  ];\n\n  const upstreamSubscribers = upstreamEvents.map(eventName =>\n    addListener(eventName, payload => {\n      if (eventName === 'refocus') {\n        emit(eventName, payload);\n        return;\n      }\n\n      const { state, lastState, action } = payload;\n      const lastRoutes = lastState && lastState.routes;\n      const routes = state && state.routes;\n\n      // const lastFocusKey =\n      //   lastState && lastState.routes && lastState.routes[lastState.index].key;\n      const focusKey = routes && routes[state.index].key;\n\n      const isChildFocused = focusKey === key;\n      const lastRoute =\n        lastRoutes && lastRoutes.find(route => route.key === key);\n      const newRoute = routes && routes.find(route => route.key === key);\n      const childPayload = {\n        context: `${key}:${action.type}_${payload.context || 'Root'}`,\n        state: newRoute,\n        lastState: lastRoute,\n        action,\n        type: eventName,\n      };\n      const isTransitioning = !!state && state.isTransitioning;\n\n      const previouslylastFocusEvent = lastFocusEvent;\n\n      if (lastFocusEvent === 'didBlur') {\n        // The child is currently blurred. Look for willFocus conditions\n        if (eventName === 'willFocus' && isChildFocused) {\n          emit((lastFocusEvent = 'willFocus'), childPayload);\n        } else if (eventName === 'action' && isChildFocused) {\n          emit((lastFocusEvent = 'willFocus'), childPayload);\n        }\n      }\n      if (lastFocusEvent === 'willFocus') {\n        // We are currently mid-focus. Look for didFocus conditions.\n        // If state.isTransitioning is false, this child event happens immediately after willFocus\n        if (eventName === 'didFocus' && isChildFocused && !isTransitioning) {\n          emit((lastFocusEvent = 'didFocus'), childPayload);\n        } else if (\n          eventName === 'action' &&\n          isChildFocused &&\n          !isTransitioning\n        ) {\n          emit((lastFocusEvent = 'didFocus'), childPayload);\n        }\n      }\n\n      if (lastFocusEvent === 'didFocus') {\n        // The child is currently focused. Look for blurring events\n        if (!isChildFocused) {\n          // The child is no longer focused within this navigation state\n          emit((lastFocusEvent = 'willBlur'), childPayload);\n        } else if (eventName === 'willBlur') {\n          // The parent is getting a willBlur event\n          emit((lastFocusEvent = 'willBlur'), childPayload);\n        } else if (\n          eventName === 'action' &&\n          previouslylastFocusEvent === 'didFocus'\n        ) {\n          // While focused, pass action events to children for grandchildren focus\n          emit('action', childPayload);\n        }\n      }\n\n      if (lastFocusEvent === 'willBlur') {\n        // The child is mid-blur. Wait for transition to end\n        if (eventName === 'action' && !isChildFocused && !isTransitioning) {\n          // The child is done blurring because transitioning is over, or isTransitioning\n          // never began and didBlur fires immediately after willBlur\n          emit((lastFocusEvent = 'didBlur'), childPayload);\n        } else if (eventName === 'didBlur') {\n          // Pass through the parent didBlur event if it happens\n          emit((lastFocusEvent = 'didBlur'), childPayload);\n        } else if (\n          eventName === 'action' &&\n          isChildFocused &&\n          !isTransitioning\n        ) {\n          emit((lastFocusEvent = 'didFocus'), childPayload);\n        } else if (\n          eventName === 'action' &&\n          isChildFocused &&\n          isTransitioning\n        ) {\n          emit((lastFocusEvent = 'willFocus'), childPayload);\n        }\n      }\n\n      if (lastFocusEvent === 'didBlur' && !newRoute) {\n        removeAll();\n      }\n    })\n  );\n\n  return {\n    addListener(eventName, eventHandler) {\n      const subscribers = getChildSubscribers(eventName);\n      if (!subscribers) {\n        throw new Error(`Invalid event name \"${eventName}\"`);\n      }\n      subscribers.add(eventHandler);\n      const remove = () => {\n        subscribers.delete(eventHandler);\n      };\n      return { remove };\n    },\n    emit(eventName, payload) {\n      if (eventName !== 'refocus') {\n        console.error(\n          `navigation.emit only supports the 'refocus' event currently.`\n        );\n        return;\n      }\n      emit(eventName, payload);\n    },\n  };\n}\n"]},"metadata":{},"sourceType":"script"}