CombineLatest.cs 431 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, Func<T1, T2, TResult> resultSelector)
  9. {
  10. Error.ThrowArgumentNullException(source1, nameof(source1));
  11. Error.ThrowArgumentNullException(source2, nameof(source2));
  12. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  13. return new CombineLatest<T1, T2, TResult>(source1, source2, resultSelector);
  14. }
  15. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, Func<T1, T2, T3, TResult> resultSelector)
  16. {
  17. Error.ThrowArgumentNullException(source1, nameof(source1));
  18. Error.ThrowArgumentNullException(source2, nameof(source2));
  19. Error.ThrowArgumentNullException(source3, nameof(source3));
  20. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  21. return new CombineLatest<T1, T2, T3, TResult>(source1, source2, source3, resultSelector);
  22. }
  23. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, Func<T1, T2, T3, T4, TResult> resultSelector)
  24. {
  25. Error.ThrowArgumentNullException(source1, nameof(source1));
  26. Error.ThrowArgumentNullException(source2, nameof(source2));
  27. Error.ThrowArgumentNullException(source3, nameof(source3));
  28. Error.ThrowArgumentNullException(source4, nameof(source4));
  29. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  30. return new CombineLatest<T1, T2, T3, T4, TResult>(source1, source2, source3, source4, resultSelector);
  31. }
  32. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, Func<T1, T2, T3, T4, T5, TResult> resultSelector)
  33. {
  34. Error.ThrowArgumentNullException(source1, nameof(source1));
  35. Error.ThrowArgumentNullException(source2, nameof(source2));
  36. Error.ThrowArgumentNullException(source3, nameof(source3));
  37. Error.ThrowArgumentNullException(source4, nameof(source4));
  38. Error.ThrowArgumentNullException(source5, nameof(source5));
  39. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  40. return new CombineLatest<T1, T2, T3, T4, T5, TResult>(source1, source2, source3, source4, source5, resultSelector);
  41. }
  42. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, Func<T1, T2, T3, T4, T5, T6, TResult> resultSelector)
  43. {
  44. Error.ThrowArgumentNullException(source1, nameof(source1));
  45. Error.ThrowArgumentNullException(source2, nameof(source2));
  46. Error.ThrowArgumentNullException(source3, nameof(source3));
  47. Error.ThrowArgumentNullException(source4, nameof(source4));
  48. Error.ThrowArgumentNullException(source5, nameof(source5));
  49. Error.ThrowArgumentNullException(source6, nameof(source6));
  50. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  51. return new CombineLatest<T1, T2, T3, T4, T5, T6, TResult>(source1, source2, source3, source4, source5, source6, resultSelector);
  52. }
  53. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, Func<T1, T2, T3, T4, T5, T6, T7, TResult> resultSelector)
  54. {
  55. Error.ThrowArgumentNullException(source1, nameof(source1));
  56. Error.ThrowArgumentNullException(source2, nameof(source2));
  57. Error.ThrowArgumentNullException(source3, nameof(source3));
  58. Error.ThrowArgumentNullException(source4, nameof(source4));
  59. Error.ThrowArgumentNullException(source5, nameof(source5));
  60. Error.ThrowArgumentNullException(source6, nameof(source6));
  61. Error.ThrowArgumentNullException(source7, nameof(source7));
  62. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  63. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, TResult>(source1, source2, source3, source4, source5, source6, source7, resultSelector);
  64. }
  65. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> resultSelector)
  66. {
  67. Error.ThrowArgumentNullException(source1, nameof(source1));
  68. Error.ThrowArgumentNullException(source2, nameof(source2));
  69. Error.ThrowArgumentNullException(source3, nameof(source3));
  70. Error.ThrowArgumentNullException(source4, nameof(source4));
  71. Error.ThrowArgumentNullException(source5, nameof(source5));
  72. Error.ThrowArgumentNullException(source6, nameof(source6));
  73. Error.ThrowArgumentNullException(source7, nameof(source7));
  74. Error.ThrowArgumentNullException(source8, nameof(source8));
  75. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  76. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, resultSelector);
  77. }
  78. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> resultSelector)
  79. {
  80. Error.ThrowArgumentNullException(source1, nameof(source1));
  81. Error.ThrowArgumentNullException(source2, nameof(source2));
  82. Error.ThrowArgumentNullException(source3, nameof(source3));
  83. Error.ThrowArgumentNullException(source4, nameof(source4));
  84. Error.ThrowArgumentNullException(source5, nameof(source5));
  85. Error.ThrowArgumentNullException(source6, nameof(source6));
  86. Error.ThrowArgumentNullException(source7, nameof(source7));
  87. Error.ThrowArgumentNullException(source8, nameof(source8));
  88. Error.ThrowArgumentNullException(source9, nameof(source9));
  89. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  90. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, resultSelector);
  91. }
  92. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> resultSelector)
  93. {
  94. Error.ThrowArgumentNullException(source1, nameof(source1));
  95. Error.ThrowArgumentNullException(source2, nameof(source2));
  96. Error.ThrowArgumentNullException(source3, nameof(source3));
  97. Error.ThrowArgumentNullException(source4, nameof(source4));
  98. Error.ThrowArgumentNullException(source5, nameof(source5));
  99. Error.ThrowArgumentNullException(source6, nameof(source6));
  100. Error.ThrowArgumentNullException(source7, nameof(source7));
  101. Error.ThrowArgumentNullException(source8, nameof(source8));
  102. Error.ThrowArgumentNullException(source9, nameof(source9));
  103. Error.ThrowArgumentNullException(source10, nameof(source10));
  104. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  105. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, resultSelector);
  106. }
  107. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> resultSelector)
  108. {
  109. Error.ThrowArgumentNullException(source1, nameof(source1));
  110. Error.ThrowArgumentNullException(source2, nameof(source2));
  111. Error.ThrowArgumentNullException(source3, nameof(source3));
  112. Error.ThrowArgumentNullException(source4, nameof(source4));
  113. Error.ThrowArgumentNullException(source5, nameof(source5));
  114. Error.ThrowArgumentNullException(source6, nameof(source6));
  115. Error.ThrowArgumentNullException(source7, nameof(source7));
  116. Error.ThrowArgumentNullException(source8, nameof(source8));
  117. Error.ThrowArgumentNullException(source9, nameof(source9));
  118. Error.ThrowArgumentNullException(source10, nameof(source10));
  119. Error.ThrowArgumentNullException(source11, nameof(source11));
  120. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  121. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, resultSelector);
  122. }
  123. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> resultSelector)
  124. {
  125. Error.ThrowArgumentNullException(source1, nameof(source1));
  126. Error.ThrowArgumentNullException(source2, nameof(source2));
  127. Error.ThrowArgumentNullException(source3, nameof(source3));
  128. Error.ThrowArgumentNullException(source4, nameof(source4));
  129. Error.ThrowArgumentNullException(source5, nameof(source5));
  130. Error.ThrowArgumentNullException(source6, nameof(source6));
  131. Error.ThrowArgumentNullException(source7, nameof(source7));
  132. Error.ThrowArgumentNullException(source8, nameof(source8));
  133. Error.ThrowArgumentNullException(source9, nameof(source9));
  134. Error.ThrowArgumentNullException(source10, nameof(source10));
  135. Error.ThrowArgumentNullException(source11, nameof(source11));
  136. Error.ThrowArgumentNullException(source12, nameof(source12));
  137. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  138. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, resultSelector);
  139. }
  140. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> resultSelector)
  141. {
  142. Error.ThrowArgumentNullException(source1, nameof(source1));
  143. Error.ThrowArgumentNullException(source2, nameof(source2));
  144. Error.ThrowArgumentNullException(source3, nameof(source3));
  145. Error.ThrowArgumentNullException(source4, nameof(source4));
  146. Error.ThrowArgumentNullException(source5, nameof(source5));
  147. Error.ThrowArgumentNullException(source6, nameof(source6));
  148. Error.ThrowArgumentNullException(source7, nameof(source7));
  149. Error.ThrowArgumentNullException(source8, nameof(source8));
  150. Error.ThrowArgumentNullException(source9, nameof(source9));
  151. Error.ThrowArgumentNullException(source10, nameof(source10));
  152. Error.ThrowArgumentNullException(source11, nameof(source11));
  153. Error.ThrowArgumentNullException(source12, nameof(source12));
  154. Error.ThrowArgumentNullException(source13, nameof(source13));
  155. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  156. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, resultSelector);
  157. }
  158. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, IUniTaskAsyncEnumerable<T14> source14, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> resultSelector)
  159. {
  160. Error.ThrowArgumentNullException(source1, nameof(source1));
  161. Error.ThrowArgumentNullException(source2, nameof(source2));
  162. Error.ThrowArgumentNullException(source3, nameof(source3));
  163. Error.ThrowArgumentNullException(source4, nameof(source4));
  164. Error.ThrowArgumentNullException(source5, nameof(source5));
  165. Error.ThrowArgumentNullException(source6, nameof(source6));
  166. Error.ThrowArgumentNullException(source7, nameof(source7));
  167. Error.ThrowArgumentNullException(source8, nameof(source8));
  168. Error.ThrowArgumentNullException(source9, nameof(source9));
  169. Error.ThrowArgumentNullException(source10, nameof(source10));
  170. Error.ThrowArgumentNullException(source11, nameof(source11));
  171. Error.ThrowArgumentNullException(source12, nameof(source12));
  172. Error.ThrowArgumentNullException(source13, nameof(source13));
  173. Error.ThrowArgumentNullException(source14, nameof(source14));
  174. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  175. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, resultSelector);
  176. }
  177. public static IUniTaskAsyncEnumerable<TResult> CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>(this IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, IUniTaskAsyncEnumerable<T14> source14, IUniTaskAsyncEnumerable<T15> source15, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> resultSelector)
  178. {
  179. Error.ThrowArgumentNullException(source1, nameof(source1));
  180. Error.ThrowArgumentNullException(source2, nameof(source2));
  181. Error.ThrowArgumentNullException(source3, nameof(source3));
  182. Error.ThrowArgumentNullException(source4, nameof(source4));
  183. Error.ThrowArgumentNullException(source5, nameof(source5));
  184. Error.ThrowArgumentNullException(source6, nameof(source6));
  185. Error.ThrowArgumentNullException(source7, nameof(source7));
  186. Error.ThrowArgumentNullException(source8, nameof(source8));
  187. Error.ThrowArgumentNullException(source9, nameof(source9));
  188. Error.ThrowArgumentNullException(source10, nameof(source10));
  189. Error.ThrowArgumentNullException(source11, nameof(source11));
  190. Error.ThrowArgumentNullException(source12, nameof(source12));
  191. Error.ThrowArgumentNullException(source13, nameof(source13));
  192. Error.ThrowArgumentNullException(source14, nameof(source14));
  193. Error.ThrowArgumentNullException(source15, nameof(source15));
  194. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  195. return new CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult>(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, source15, resultSelector);
  196. }
  197. }
  198. internal class CombineLatest<T1, T2, TResult> : IUniTaskAsyncEnumerable<TResult>
  199. {
  200. readonly IUniTaskAsyncEnumerable<T1> source1;
  201. readonly IUniTaskAsyncEnumerable<T2> source2;
  202. readonly Func<T1, T2, TResult> resultSelector;
  203. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, Func<T1, T2, TResult> resultSelector)
  204. {
  205. this.source1 = source1;
  206. this.source2 = source2;
  207. this.resultSelector = resultSelector;
  208. }
  209. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  210. {
  211. return new _CombineLatest(source1, source2, resultSelector, cancellationToken);
  212. }
  213. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  214. {
  215. static readonly Action<object> Completed1Delegate = Completed1;
  216. static readonly Action<object> Completed2Delegate = Completed2;
  217. const int CompleteCount = 2;
  218. readonly IUniTaskAsyncEnumerable<T1> source1;
  219. readonly IUniTaskAsyncEnumerable<T2> source2;
  220. readonly Func<T1, T2, TResult> resultSelector;
  221. CancellationToken cancellationToken;
  222. IUniTaskAsyncEnumerator<T1> enumerator1;
  223. UniTask<bool>.Awaiter awaiter1;
  224. bool hasCurrent1;
  225. bool running1;
  226. T1 current1;
  227. IUniTaskAsyncEnumerator<T2> enumerator2;
  228. UniTask<bool>.Awaiter awaiter2;
  229. bool hasCurrent2;
  230. bool running2;
  231. T2 current2;
  232. int completedCount;
  233. bool syncRunning;
  234. TResult result;
  235. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, Func<T1, T2, TResult> resultSelector, CancellationToken cancellationToken)
  236. {
  237. this.source1 = source1;
  238. this.source2 = source2;
  239. this.resultSelector = resultSelector;
  240. this.cancellationToken = cancellationToken;
  241. TaskTracker.TrackActiveTask(this, 3);
  242. }
  243. public TResult Current => result;
  244. public UniTask<bool> MoveNextAsync()
  245. {
  246. cancellationToken.ThrowIfCancellationRequested();
  247. if (completedCount == CompleteCount) return CompletedTasks.False;
  248. if (enumerator1 == null)
  249. {
  250. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  251. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  252. }
  253. completionSource.Reset();
  254. AGAIN:
  255. syncRunning = true;
  256. if (!running1)
  257. {
  258. running1 = true;
  259. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  260. if (awaiter1.IsCompleted)
  261. {
  262. Completed1(this);
  263. }
  264. else
  265. {
  266. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  267. }
  268. }
  269. if (!running2)
  270. {
  271. running2 = true;
  272. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  273. if (awaiter2.IsCompleted)
  274. {
  275. Completed2(this);
  276. }
  277. else
  278. {
  279. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  280. }
  281. }
  282. if (!running1 || !running2)
  283. {
  284. goto AGAIN;
  285. }
  286. syncRunning = false;
  287. return new UniTask<bool>(this, completionSource.Version);
  288. }
  289. static void Completed1(object state)
  290. {
  291. var self = (_CombineLatest)state;
  292. self.running1 = false;
  293. try
  294. {
  295. if (self.awaiter1.GetResult())
  296. {
  297. self.hasCurrent1 = true;
  298. self.current1 = self.enumerator1.Current;
  299. goto SUCCESS;
  300. }
  301. else
  302. {
  303. self.running1 = true; // as complete, no more call MoveNextAsync.
  304. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  305. {
  306. goto COMPLETE;
  307. }
  308. return;
  309. }
  310. }
  311. catch (Exception ex)
  312. {
  313. self.running1 = true; // as complete, no more call MoveNextAsync.
  314. self.completedCount = CompleteCount;
  315. self.completionSource.TrySetException(ex);
  316. return;
  317. }
  318. SUCCESS:
  319. if (!self.TrySetResult())
  320. {
  321. if (self.syncRunning) return;
  322. self.running1 = true; // as complete, no more call MoveNextAsync.
  323. try
  324. {
  325. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  326. }
  327. catch (Exception ex)
  328. {
  329. self.completedCount = CompleteCount;
  330. self.completionSource.TrySetException(ex);
  331. return;
  332. }
  333. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  334. }
  335. return;
  336. COMPLETE:
  337. self.completionSource.TrySetResult(false);
  338. return;
  339. }
  340. static void Completed2(object state)
  341. {
  342. var self = (_CombineLatest)state;
  343. self.running2 = false;
  344. try
  345. {
  346. if (self.awaiter2.GetResult())
  347. {
  348. self.hasCurrent2 = true;
  349. self.current2 = self.enumerator2.Current;
  350. goto SUCCESS;
  351. }
  352. else
  353. {
  354. self.running2 = true; // as complete, no more call MoveNextAsync.
  355. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  356. {
  357. goto COMPLETE;
  358. }
  359. return;
  360. }
  361. }
  362. catch (Exception ex)
  363. {
  364. self.running2 = true; // as complete, no more call MoveNextAsync.
  365. self.completedCount = CompleteCount;
  366. self.completionSource.TrySetException(ex);
  367. return;
  368. }
  369. SUCCESS:
  370. if (!self.TrySetResult())
  371. {
  372. if (self.syncRunning) return;
  373. self.running2 = true; // as complete, no more call MoveNextAsync.
  374. try
  375. {
  376. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  377. }
  378. catch (Exception ex)
  379. {
  380. self.completedCount = CompleteCount;
  381. self.completionSource.TrySetException(ex);
  382. return;
  383. }
  384. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  385. }
  386. return;
  387. COMPLETE:
  388. self.completionSource.TrySetResult(false);
  389. return;
  390. }
  391. bool TrySetResult()
  392. {
  393. if (hasCurrent1 && hasCurrent2)
  394. {
  395. result = resultSelector(current1, current2);
  396. completionSource.TrySetResult(true);
  397. return true;
  398. }
  399. else
  400. {
  401. return false;
  402. }
  403. }
  404. public async UniTask DisposeAsync()
  405. {
  406. TaskTracker.RemoveTracking(this);
  407. if (enumerator1 != null)
  408. {
  409. await enumerator1.DisposeAsync();
  410. }
  411. if (enumerator2 != null)
  412. {
  413. await enumerator2.DisposeAsync();
  414. }
  415. }
  416. }
  417. }
  418. internal class CombineLatest<T1, T2, T3, TResult> : IUniTaskAsyncEnumerable<TResult>
  419. {
  420. readonly IUniTaskAsyncEnumerable<T1> source1;
  421. readonly IUniTaskAsyncEnumerable<T2> source2;
  422. readonly IUniTaskAsyncEnumerable<T3> source3;
  423. readonly Func<T1, T2, T3, TResult> resultSelector;
  424. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, Func<T1, T2, T3, TResult> resultSelector)
  425. {
  426. this.source1 = source1;
  427. this.source2 = source2;
  428. this.source3 = source3;
  429. this.resultSelector = resultSelector;
  430. }
  431. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  432. {
  433. return new _CombineLatest(source1, source2, source3, resultSelector, cancellationToken);
  434. }
  435. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  436. {
  437. static readonly Action<object> Completed1Delegate = Completed1;
  438. static readonly Action<object> Completed2Delegate = Completed2;
  439. static readonly Action<object> Completed3Delegate = Completed3;
  440. const int CompleteCount = 3;
  441. readonly IUniTaskAsyncEnumerable<T1> source1;
  442. readonly IUniTaskAsyncEnumerable<T2> source2;
  443. readonly IUniTaskAsyncEnumerable<T3> source3;
  444. readonly Func<T1, T2, T3, TResult> resultSelector;
  445. CancellationToken cancellationToken;
  446. IUniTaskAsyncEnumerator<T1> enumerator1;
  447. UniTask<bool>.Awaiter awaiter1;
  448. bool hasCurrent1;
  449. bool running1;
  450. T1 current1;
  451. IUniTaskAsyncEnumerator<T2> enumerator2;
  452. UniTask<bool>.Awaiter awaiter2;
  453. bool hasCurrent2;
  454. bool running2;
  455. T2 current2;
  456. IUniTaskAsyncEnumerator<T3> enumerator3;
  457. UniTask<bool>.Awaiter awaiter3;
  458. bool hasCurrent3;
  459. bool running3;
  460. T3 current3;
  461. int completedCount;
  462. bool syncRunning;
  463. TResult result;
  464. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, Func<T1, T2, T3, TResult> resultSelector, CancellationToken cancellationToken)
  465. {
  466. this.source1 = source1;
  467. this.source2 = source2;
  468. this.source3 = source3;
  469. this.resultSelector = resultSelector;
  470. this.cancellationToken = cancellationToken;
  471. TaskTracker.TrackActiveTask(this, 3);
  472. }
  473. public TResult Current => result;
  474. public UniTask<bool> MoveNextAsync()
  475. {
  476. cancellationToken.ThrowIfCancellationRequested();
  477. if (completedCount == CompleteCount) return CompletedTasks.False;
  478. if (enumerator1 == null)
  479. {
  480. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  481. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  482. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  483. }
  484. completionSource.Reset();
  485. AGAIN:
  486. syncRunning = true;
  487. if (!running1)
  488. {
  489. running1 = true;
  490. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  491. if (awaiter1.IsCompleted)
  492. {
  493. Completed1(this);
  494. }
  495. else
  496. {
  497. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  498. }
  499. }
  500. if (!running2)
  501. {
  502. running2 = true;
  503. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  504. if (awaiter2.IsCompleted)
  505. {
  506. Completed2(this);
  507. }
  508. else
  509. {
  510. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  511. }
  512. }
  513. if (!running3)
  514. {
  515. running3 = true;
  516. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  517. if (awaiter3.IsCompleted)
  518. {
  519. Completed3(this);
  520. }
  521. else
  522. {
  523. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  524. }
  525. }
  526. if (!running1 || !running2 || !running3)
  527. {
  528. goto AGAIN;
  529. }
  530. syncRunning = false;
  531. return new UniTask<bool>(this, completionSource.Version);
  532. }
  533. static void Completed1(object state)
  534. {
  535. var self = (_CombineLatest)state;
  536. self.running1 = false;
  537. try
  538. {
  539. if (self.awaiter1.GetResult())
  540. {
  541. self.hasCurrent1 = true;
  542. self.current1 = self.enumerator1.Current;
  543. goto SUCCESS;
  544. }
  545. else
  546. {
  547. self.running1 = true; // as complete, no more call MoveNextAsync.
  548. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  549. {
  550. goto COMPLETE;
  551. }
  552. return;
  553. }
  554. }
  555. catch (Exception ex)
  556. {
  557. self.running1 = true; // as complete, no more call MoveNextAsync.
  558. self.completedCount = CompleteCount;
  559. self.completionSource.TrySetException(ex);
  560. return;
  561. }
  562. SUCCESS:
  563. if (!self.TrySetResult())
  564. {
  565. if (self.syncRunning) return;
  566. self.running1 = true; // as complete, no more call MoveNextAsync.
  567. try
  568. {
  569. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  570. }
  571. catch (Exception ex)
  572. {
  573. self.completedCount = CompleteCount;
  574. self.completionSource.TrySetException(ex);
  575. return;
  576. }
  577. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  578. }
  579. return;
  580. COMPLETE:
  581. self.completionSource.TrySetResult(false);
  582. return;
  583. }
  584. static void Completed2(object state)
  585. {
  586. var self = (_CombineLatest)state;
  587. self.running2 = false;
  588. try
  589. {
  590. if (self.awaiter2.GetResult())
  591. {
  592. self.hasCurrent2 = true;
  593. self.current2 = self.enumerator2.Current;
  594. goto SUCCESS;
  595. }
  596. else
  597. {
  598. self.running2 = true; // as complete, no more call MoveNextAsync.
  599. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  600. {
  601. goto COMPLETE;
  602. }
  603. return;
  604. }
  605. }
  606. catch (Exception ex)
  607. {
  608. self.running2 = true; // as complete, no more call MoveNextAsync.
  609. self.completedCount = CompleteCount;
  610. self.completionSource.TrySetException(ex);
  611. return;
  612. }
  613. SUCCESS:
  614. if (!self.TrySetResult())
  615. {
  616. if (self.syncRunning) return;
  617. self.running2 = true; // as complete, no more call MoveNextAsync.
  618. try
  619. {
  620. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  621. }
  622. catch (Exception ex)
  623. {
  624. self.completedCount = CompleteCount;
  625. self.completionSource.TrySetException(ex);
  626. return;
  627. }
  628. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  629. }
  630. return;
  631. COMPLETE:
  632. self.completionSource.TrySetResult(false);
  633. return;
  634. }
  635. static void Completed3(object state)
  636. {
  637. var self = (_CombineLatest)state;
  638. self.running3 = false;
  639. try
  640. {
  641. if (self.awaiter3.GetResult())
  642. {
  643. self.hasCurrent3 = true;
  644. self.current3 = self.enumerator3.Current;
  645. goto SUCCESS;
  646. }
  647. else
  648. {
  649. self.running3 = true; // as complete, no more call MoveNextAsync.
  650. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  651. {
  652. goto COMPLETE;
  653. }
  654. return;
  655. }
  656. }
  657. catch (Exception ex)
  658. {
  659. self.running3 = true; // as complete, no more call MoveNextAsync.
  660. self.completedCount = CompleteCount;
  661. self.completionSource.TrySetException(ex);
  662. return;
  663. }
  664. SUCCESS:
  665. if (!self.TrySetResult())
  666. {
  667. if (self.syncRunning) return;
  668. self.running3 = true; // as complete, no more call MoveNextAsync.
  669. try
  670. {
  671. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  672. }
  673. catch (Exception ex)
  674. {
  675. self.completedCount = CompleteCount;
  676. self.completionSource.TrySetException(ex);
  677. return;
  678. }
  679. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  680. }
  681. return;
  682. COMPLETE:
  683. self.completionSource.TrySetResult(false);
  684. return;
  685. }
  686. bool TrySetResult()
  687. {
  688. if (hasCurrent1 && hasCurrent2 && hasCurrent3)
  689. {
  690. result = resultSelector(current1, current2, current3);
  691. completionSource.TrySetResult(true);
  692. return true;
  693. }
  694. else
  695. {
  696. return false;
  697. }
  698. }
  699. public async UniTask DisposeAsync()
  700. {
  701. TaskTracker.RemoveTracking(this);
  702. if (enumerator1 != null)
  703. {
  704. await enumerator1.DisposeAsync();
  705. }
  706. if (enumerator2 != null)
  707. {
  708. await enumerator2.DisposeAsync();
  709. }
  710. if (enumerator3 != null)
  711. {
  712. await enumerator3.DisposeAsync();
  713. }
  714. }
  715. }
  716. }
  717. internal class CombineLatest<T1, T2, T3, T4, TResult> : IUniTaskAsyncEnumerable<TResult>
  718. {
  719. readonly IUniTaskAsyncEnumerable<T1> source1;
  720. readonly IUniTaskAsyncEnumerable<T2> source2;
  721. readonly IUniTaskAsyncEnumerable<T3> source3;
  722. readonly IUniTaskAsyncEnumerable<T4> source4;
  723. readonly Func<T1, T2, T3, T4, TResult> resultSelector;
  724. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, Func<T1, T2, T3, T4, TResult> resultSelector)
  725. {
  726. this.source1 = source1;
  727. this.source2 = source2;
  728. this.source3 = source3;
  729. this.source4 = source4;
  730. this.resultSelector = resultSelector;
  731. }
  732. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  733. {
  734. return new _CombineLatest(source1, source2, source3, source4, resultSelector, cancellationToken);
  735. }
  736. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  737. {
  738. static readonly Action<object> Completed1Delegate = Completed1;
  739. static readonly Action<object> Completed2Delegate = Completed2;
  740. static readonly Action<object> Completed3Delegate = Completed3;
  741. static readonly Action<object> Completed4Delegate = Completed4;
  742. const int CompleteCount = 4;
  743. readonly IUniTaskAsyncEnumerable<T1> source1;
  744. readonly IUniTaskAsyncEnumerable<T2> source2;
  745. readonly IUniTaskAsyncEnumerable<T3> source3;
  746. readonly IUniTaskAsyncEnumerable<T4> source4;
  747. readonly Func<T1, T2, T3, T4, TResult> resultSelector;
  748. CancellationToken cancellationToken;
  749. IUniTaskAsyncEnumerator<T1> enumerator1;
  750. UniTask<bool>.Awaiter awaiter1;
  751. bool hasCurrent1;
  752. bool running1;
  753. T1 current1;
  754. IUniTaskAsyncEnumerator<T2> enumerator2;
  755. UniTask<bool>.Awaiter awaiter2;
  756. bool hasCurrent2;
  757. bool running2;
  758. T2 current2;
  759. IUniTaskAsyncEnumerator<T3> enumerator3;
  760. UniTask<bool>.Awaiter awaiter3;
  761. bool hasCurrent3;
  762. bool running3;
  763. T3 current3;
  764. IUniTaskAsyncEnumerator<T4> enumerator4;
  765. UniTask<bool>.Awaiter awaiter4;
  766. bool hasCurrent4;
  767. bool running4;
  768. T4 current4;
  769. int completedCount;
  770. bool syncRunning;
  771. TResult result;
  772. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, Func<T1, T2, T3, T4, TResult> resultSelector, CancellationToken cancellationToken)
  773. {
  774. this.source1 = source1;
  775. this.source2 = source2;
  776. this.source3 = source3;
  777. this.source4 = source4;
  778. this.resultSelector = resultSelector;
  779. this.cancellationToken = cancellationToken;
  780. TaskTracker.TrackActiveTask(this, 3);
  781. }
  782. public TResult Current => result;
  783. public UniTask<bool> MoveNextAsync()
  784. {
  785. cancellationToken.ThrowIfCancellationRequested();
  786. if (completedCount == CompleteCount) return CompletedTasks.False;
  787. if (enumerator1 == null)
  788. {
  789. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  790. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  791. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  792. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  793. }
  794. completionSource.Reset();
  795. AGAIN:
  796. syncRunning = true;
  797. if (!running1)
  798. {
  799. running1 = true;
  800. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  801. if (awaiter1.IsCompleted)
  802. {
  803. Completed1(this);
  804. }
  805. else
  806. {
  807. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  808. }
  809. }
  810. if (!running2)
  811. {
  812. running2 = true;
  813. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  814. if (awaiter2.IsCompleted)
  815. {
  816. Completed2(this);
  817. }
  818. else
  819. {
  820. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  821. }
  822. }
  823. if (!running3)
  824. {
  825. running3 = true;
  826. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  827. if (awaiter3.IsCompleted)
  828. {
  829. Completed3(this);
  830. }
  831. else
  832. {
  833. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  834. }
  835. }
  836. if (!running4)
  837. {
  838. running4 = true;
  839. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  840. if (awaiter4.IsCompleted)
  841. {
  842. Completed4(this);
  843. }
  844. else
  845. {
  846. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  847. }
  848. }
  849. if (!running1 || !running2 || !running3 || !running4)
  850. {
  851. goto AGAIN;
  852. }
  853. syncRunning = false;
  854. return new UniTask<bool>(this, completionSource.Version);
  855. }
  856. static void Completed1(object state)
  857. {
  858. var self = (_CombineLatest)state;
  859. self.running1 = false;
  860. try
  861. {
  862. if (self.awaiter1.GetResult())
  863. {
  864. self.hasCurrent1 = true;
  865. self.current1 = self.enumerator1.Current;
  866. goto SUCCESS;
  867. }
  868. else
  869. {
  870. self.running1 = true; // as complete, no more call MoveNextAsync.
  871. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  872. {
  873. goto COMPLETE;
  874. }
  875. return;
  876. }
  877. }
  878. catch (Exception ex)
  879. {
  880. self.running1 = true; // as complete, no more call MoveNextAsync.
  881. self.completedCount = CompleteCount;
  882. self.completionSource.TrySetException(ex);
  883. return;
  884. }
  885. SUCCESS:
  886. if (!self.TrySetResult())
  887. {
  888. if (self.syncRunning) return;
  889. self.running1 = true; // as complete, no more call MoveNextAsync.
  890. try
  891. {
  892. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  893. }
  894. catch (Exception ex)
  895. {
  896. self.completedCount = CompleteCount;
  897. self.completionSource.TrySetException(ex);
  898. return;
  899. }
  900. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  901. }
  902. return;
  903. COMPLETE:
  904. self.completionSource.TrySetResult(false);
  905. return;
  906. }
  907. static void Completed2(object state)
  908. {
  909. var self = (_CombineLatest)state;
  910. self.running2 = false;
  911. try
  912. {
  913. if (self.awaiter2.GetResult())
  914. {
  915. self.hasCurrent2 = true;
  916. self.current2 = self.enumerator2.Current;
  917. goto SUCCESS;
  918. }
  919. else
  920. {
  921. self.running2 = true; // as complete, no more call MoveNextAsync.
  922. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  923. {
  924. goto COMPLETE;
  925. }
  926. return;
  927. }
  928. }
  929. catch (Exception ex)
  930. {
  931. self.running2 = true; // as complete, no more call MoveNextAsync.
  932. self.completedCount = CompleteCount;
  933. self.completionSource.TrySetException(ex);
  934. return;
  935. }
  936. SUCCESS:
  937. if (!self.TrySetResult())
  938. {
  939. if (self.syncRunning) return;
  940. self.running2 = true; // as complete, no more call MoveNextAsync.
  941. try
  942. {
  943. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  944. }
  945. catch (Exception ex)
  946. {
  947. self.completedCount = CompleteCount;
  948. self.completionSource.TrySetException(ex);
  949. return;
  950. }
  951. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  952. }
  953. return;
  954. COMPLETE:
  955. self.completionSource.TrySetResult(false);
  956. return;
  957. }
  958. static void Completed3(object state)
  959. {
  960. var self = (_CombineLatest)state;
  961. self.running3 = false;
  962. try
  963. {
  964. if (self.awaiter3.GetResult())
  965. {
  966. self.hasCurrent3 = true;
  967. self.current3 = self.enumerator3.Current;
  968. goto SUCCESS;
  969. }
  970. else
  971. {
  972. self.running3 = true; // as complete, no more call MoveNextAsync.
  973. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  974. {
  975. goto COMPLETE;
  976. }
  977. return;
  978. }
  979. }
  980. catch (Exception ex)
  981. {
  982. self.running3 = true; // as complete, no more call MoveNextAsync.
  983. self.completedCount = CompleteCount;
  984. self.completionSource.TrySetException(ex);
  985. return;
  986. }
  987. SUCCESS:
  988. if (!self.TrySetResult())
  989. {
  990. if (self.syncRunning) return;
  991. self.running3 = true; // as complete, no more call MoveNextAsync.
  992. try
  993. {
  994. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  995. }
  996. catch (Exception ex)
  997. {
  998. self.completedCount = CompleteCount;
  999. self.completionSource.TrySetException(ex);
  1000. return;
  1001. }
  1002. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  1003. }
  1004. return;
  1005. COMPLETE:
  1006. self.completionSource.TrySetResult(false);
  1007. return;
  1008. }
  1009. static void Completed4(object state)
  1010. {
  1011. var self = (_CombineLatest)state;
  1012. self.running4 = false;
  1013. try
  1014. {
  1015. if (self.awaiter4.GetResult())
  1016. {
  1017. self.hasCurrent4 = true;
  1018. self.current4 = self.enumerator4.Current;
  1019. goto SUCCESS;
  1020. }
  1021. else
  1022. {
  1023. self.running4 = true; // as complete, no more call MoveNextAsync.
  1024. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1025. {
  1026. goto COMPLETE;
  1027. }
  1028. return;
  1029. }
  1030. }
  1031. catch (Exception ex)
  1032. {
  1033. self.running4 = true; // as complete, no more call MoveNextAsync.
  1034. self.completedCount = CompleteCount;
  1035. self.completionSource.TrySetException(ex);
  1036. return;
  1037. }
  1038. SUCCESS:
  1039. if (!self.TrySetResult())
  1040. {
  1041. if (self.syncRunning) return;
  1042. self.running4 = true; // as complete, no more call MoveNextAsync.
  1043. try
  1044. {
  1045. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  1046. }
  1047. catch (Exception ex)
  1048. {
  1049. self.completedCount = CompleteCount;
  1050. self.completionSource.TrySetException(ex);
  1051. return;
  1052. }
  1053. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  1054. }
  1055. return;
  1056. COMPLETE:
  1057. self.completionSource.TrySetResult(false);
  1058. return;
  1059. }
  1060. bool TrySetResult()
  1061. {
  1062. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4)
  1063. {
  1064. result = resultSelector(current1, current2, current3, current4);
  1065. completionSource.TrySetResult(true);
  1066. return true;
  1067. }
  1068. else
  1069. {
  1070. return false;
  1071. }
  1072. }
  1073. public async UniTask DisposeAsync()
  1074. {
  1075. TaskTracker.RemoveTracking(this);
  1076. if (enumerator1 != null)
  1077. {
  1078. await enumerator1.DisposeAsync();
  1079. }
  1080. if (enumerator2 != null)
  1081. {
  1082. await enumerator2.DisposeAsync();
  1083. }
  1084. if (enumerator3 != null)
  1085. {
  1086. await enumerator3.DisposeAsync();
  1087. }
  1088. if (enumerator4 != null)
  1089. {
  1090. await enumerator4.DisposeAsync();
  1091. }
  1092. }
  1093. }
  1094. }
  1095. internal class CombineLatest<T1, T2, T3, T4, T5, TResult> : IUniTaskAsyncEnumerable<TResult>
  1096. {
  1097. readonly IUniTaskAsyncEnumerable<T1> source1;
  1098. readonly IUniTaskAsyncEnumerable<T2> source2;
  1099. readonly IUniTaskAsyncEnumerable<T3> source3;
  1100. readonly IUniTaskAsyncEnumerable<T4> source4;
  1101. readonly IUniTaskAsyncEnumerable<T5> source5;
  1102. readonly Func<T1, T2, T3, T4, T5, TResult> resultSelector;
  1103. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, Func<T1, T2, T3, T4, T5, TResult> resultSelector)
  1104. {
  1105. this.source1 = source1;
  1106. this.source2 = source2;
  1107. this.source3 = source3;
  1108. this.source4 = source4;
  1109. this.source5 = source5;
  1110. this.resultSelector = resultSelector;
  1111. }
  1112. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  1113. {
  1114. return new _CombineLatest(source1, source2, source3, source4, source5, resultSelector, cancellationToken);
  1115. }
  1116. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  1117. {
  1118. static readonly Action<object> Completed1Delegate = Completed1;
  1119. static readonly Action<object> Completed2Delegate = Completed2;
  1120. static readonly Action<object> Completed3Delegate = Completed3;
  1121. static readonly Action<object> Completed4Delegate = Completed4;
  1122. static readonly Action<object> Completed5Delegate = Completed5;
  1123. const int CompleteCount = 5;
  1124. readonly IUniTaskAsyncEnumerable<T1> source1;
  1125. readonly IUniTaskAsyncEnumerable<T2> source2;
  1126. readonly IUniTaskAsyncEnumerable<T3> source3;
  1127. readonly IUniTaskAsyncEnumerable<T4> source4;
  1128. readonly IUniTaskAsyncEnumerable<T5> source5;
  1129. readonly Func<T1, T2, T3, T4, T5, TResult> resultSelector;
  1130. CancellationToken cancellationToken;
  1131. IUniTaskAsyncEnumerator<T1> enumerator1;
  1132. UniTask<bool>.Awaiter awaiter1;
  1133. bool hasCurrent1;
  1134. bool running1;
  1135. T1 current1;
  1136. IUniTaskAsyncEnumerator<T2> enumerator2;
  1137. UniTask<bool>.Awaiter awaiter2;
  1138. bool hasCurrent2;
  1139. bool running2;
  1140. T2 current2;
  1141. IUniTaskAsyncEnumerator<T3> enumerator3;
  1142. UniTask<bool>.Awaiter awaiter3;
  1143. bool hasCurrent3;
  1144. bool running3;
  1145. T3 current3;
  1146. IUniTaskAsyncEnumerator<T4> enumerator4;
  1147. UniTask<bool>.Awaiter awaiter4;
  1148. bool hasCurrent4;
  1149. bool running4;
  1150. T4 current4;
  1151. IUniTaskAsyncEnumerator<T5> enumerator5;
  1152. UniTask<bool>.Awaiter awaiter5;
  1153. bool hasCurrent5;
  1154. bool running5;
  1155. T5 current5;
  1156. int completedCount;
  1157. bool syncRunning;
  1158. TResult result;
  1159. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, Func<T1, T2, T3, T4, T5, TResult> resultSelector, CancellationToken cancellationToken)
  1160. {
  1161. this.source1 = source1;
  1162. this.source2 = source2;
  1163. this.source3 = source3;
  1164. this.source4 = source4;
  1165. this.source5 = source5;
  1166. this.resultSelector = resultSelector;
  1167. this.cancellationToken = cancellationToken;
  1168. TaskTracker.TrackActiveTask(this, 3);
  1169. }
  1170. public TResult Current => result;
  1171. public UniTask<bool> MoveNextAsync()
  1172. {
  1173. cancellationToken.ThrowIfCancellationRequested();
  1174. if (completedCount == CompleteCount) return CompletedTasks.False;
  1175. if (enumerator1 == null)
  1176. {
  1177. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  1178. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  1179. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  1180. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  1181. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  1182. }
  1183. completionSource.Reset();
  1184. AGAIN:
  1185. syncRunning = true;
  1186. if (!running1)
  1187. {
  1188. running1 = true;
  1189. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  1190. if (awaiter1.IsCompleted)
  1191. {
  1192. Completed1(this);
  1193. }
  1194. else
  1195. {
  1196. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  1197. }
  1198. }
  1199. if (!running2)
  1200. {
  1201. running2 = true;
  1202. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  1203. if (awaiter2.IsCompleted)
  1204. {
  1205. Completed2(this);
  1206. }
  1207. else
  1208. {
  1209. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  1210. }
  1211. }
  1212. if (!running3)
  1213. {
  1214. running3 = true;
  1215. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  1216. if (awaiter3.IsCompleted)
  1217. {
  1218. Completed3(this);
  1219. }
  1220. else
  1221. {
  1222. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  1223. }
  1224. }
  1225. if (!running4)
  1226. {
  1227. running4 = true;
  1228. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  1229. if (awaiter4.IsCompleted)
  1230. {
  1231. Completed4(this);
  1232. }
  1233. else
  1234. {
  1235. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  1236. }
  1237. }
  1238. if (!running5)
  1239. {
  1240. running5 = true;
  1241. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  1242. if (awaiter5.IsCompleted)
  1243. {
  1244. Completed5(this);
  1245. }
  1246. else
  1247. {
  1248. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  1249. }
  1250. }
  1251. if (!running1 || !running2 || !running3 || !running4 || !running5)
  1252. {
  1253. goto AGAIN;
  1254. }
  1255. syncRunning = false;
  1256. return new UniTask<bool>(this, completionSource.Version);
  1257. }
  1258. static void Completed1(object state)
  1259. {
  1260. var self = (_CombineLatest)state;
  1261. self.running1 = false;
  1262. try
  1263. {
  1264. if (self.awaiter1.GetResult())
  1265. {
  1266. self.hasCurrent1 = true;
  1267. self.current1 = self.enumerator1.Current;
  1268. goto SUCCESS;
  1269. }
  1270. else
  1271. {
  1272. self.running1 = true; // as complete, no more call MoveNextAsync.
  1273. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1274. {
  1275. goto COMPLETE;
  1276. }
  1277. return;
  1278. }
  1279. }
  1280. catch (Exception ex)
  1281. {
  1282. self.running1 = true; // as complete, no more call MoveNextAsync.
  1283. self.completedCount = CompleteCount;
  1284. self.completionSource.TrySetException(ex);
  1285. return;
  1286. }
  1287. SUCCESS:
  1288. if (!self.TrySetResult())
  1289. {
  1290. if (self.syncRunning) return;
  1291. self.running1 = true; // as complete, no more call MoveNextAsync.
  1292. try
  1293. {
  1294. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  1295. }
  1296. catch (Exception ex)
  1297. {
  1298. self.completedCount = CompleteCount;
  1299. self.completionSource.TrySetException(ex);
  1300. return;
  1301. }
  1302. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  1303. }
  1304. return;
  1305. COMPLETE:
  1306. self.completionSource.TrySetResult(false);
  1307. return;
  1308. }
  1309. static void Completed2(object state)
  1310. {
  1311. var self = (_CombineLatest)state;
  1312. self.running2 = false;
  1313. try
  1314. {
  1315. if (self.awaiter2.GetResult())
  1316. {
  1317. self.hasCurrent2 = true;
  1318. self.current2 = self.enumerator2.Current;
  1319. goto SUCCESS;
  1320. }
  1321. else
  1322. {
  1323. self.running2 = true; // as complete, no more call MoveNextAsync.
  1324. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1325. {
  1326. goto COMPLETE;
  1327. }
  1328. return;
  1329. }
  1330. }
  1331. catch (Exception ex)
  1332. {
  1333. self.running2 = true; // as complete, no more call MoveNextAsync.
  1334. self.completedCount = CompleteCount;
  1335. self.completionSource.TrySetException(ex);
  1336. return;
  1337. }
  1338. SUCCESS:
  1339. if (!self.TrySetResult())
  1340. {
  1341. if (self.syncRunning) return;
  1342. self.running2 = true; // as complete, no more call MoveNextAsync.
  1343. try
  1344. {
  1345. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  1346. }
  1347. catch (Exception ex)
  1348. {
  1349. self.completedCount = CompleteCount;
  1350. self.completionSource.TrySetException(ex);
  1351. return;
  1352. }
  1353. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  1354. }
  1355. return;
  1356. COMPLETE:
  1357. self.completionSource.TrySetResult(false);
  1358. return;
  1359. }
  1360. static void Completed3(object state)
  1361. {
  1362. var self = (_CombineLatest)state;
  1363. self.running3 = false;
  1364. try
  1365. {
  1366. if (self.awaiter3.GetResult())
  1367. {
  1368. self.hasCurrent3 = true;
  1369. self.current3 = self.enumerator3.Current;
  1370. goto SUCCESS;
  1371. }
  1372. else
  1373. {
  1374. self.running3 = true; // as complete, no more call MoveNextAsync.
  1375. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1376. {
  1377. goto COMPLETE;
  1378. }
  1379. return;
  1380. }
  1381. }
  1382. catch (Exception ex)
  1383. {
  1384. self.running3 = true; // as complete, no more call MoveNextAsync.
  1385. self.completedCount = CompleteCount;
  1386. self.completionSource.TrySetException(ex);
  1387. return;
  1388. }
  1389. SUCCESS:
  1390. if (!self.TrySetResult())
  1391. {
  1392. if (self.syncRunning) return;
  1393. self.running3 = true; // as complete, no more call MoveNextAsync.
  1394. try
  1395. {
  1396. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  1397. }
  1398. catch (Exception ex)
  1399. {
  1400. self.completedCount = CompleteCount;
  1401. self.completionSource.TrySetException(ex);
  1402. return;
  1403. }
  1404. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  1405. }
  1406. return;
  1407. COMPLETE:
  1408. self.completionSource.TrySetResult(false);
  1409. return;
  1410. }
  1411. static void Completed4(object state)
  1412. {
  1413. var self = (_CombineLatest)state;
  1414. self.running4 = false;
  1415. try
  1416. {
  1417. if (self.awaiter4.GetResult())
  1418. {
  1419. self.hasCurrent4 = true;
  1420. self.current4 = self.enumerator4.Current;
  1421. goto SUCCESS;
  1422. }
  1423. else
  1424. {
  1425. self.running4 = true; // as complete, no more call MoveNextAsync.
  1426. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1427. {
  1428. goto COMPLETE;
  1429. }
  1430. return;
  1431. }
  1432. }
  1433. catch (Exception ex)
  1434. {
  1435. self.running4 = true; // as complete, no more call MoveNextAsync.
  1436. self.completedCount = CompleteCount;
  1437. self.completionSource.TrySetException(ex);
  1438. return;
  1439. }
  1440. SUCCESS:
  1441. if (!self.TrySetResult())
  1442. {
  1443. if (self.syncRunning) return;
  1444. self.running4 = true; // as complete, no more call MoveNextAsync.
  1445. try
  1446. {
  1447. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  1448. }
  1449. catch (Exception ex)
  1450. {
  1451. self.completedCount = CompleteCount;
  1452. self.completionSource.TrySetException(ex);
  1453. return;
  1454. }
  1455. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  1456. }
  1457. return;
  1458. COMPLETE:
  1459. self.completionSource.TrySetResult(false);
  1460. return;
  1461. }
  1462. static void Completed5(object state)
  1463. {
  1464. var self = (_CombineLatest)state;
  1465. self.running5 = false;
  1466. try
  1467. {
  1468. if (self.awaiter5.GetResult())
  1469. {
  1470. self.hasCurrent5 = true;
  1471. self.current5 = self.enumerator5.Current;
  1472. goto SUCCESS;
  1473. }
  1474. else
  1475. {
  1476. self.running5 = true; // as complete, no more call MoveNextAsync.
  1477. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1478. {
  1479. goto COMPLETE;
  1480. }
  1481. return;
  1482. }
  1483. }
  1484. catch (Exception ex)
  1485. {
  1486. self.running5 = true; // as complete, no more call MoveNextAsync.
  1487. self.completedCount = CompleteCount;
  1488. self.completionSource.TrySetException(ex);
  1489. return;
  1490. }
  1491. SUCCESS:
  1492. if (!self.TrySetResult())
  1493. {
  1494. if (self.syncRunning) return;
  1495. self.running5 = true; // as complete, no more call MoveNextAsync.
  1496. try
  1497. {
  1498. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  1499. }
  1500. catch (Exception ex)
  1501. {
  1502. self.completedCount = CompleteCount;
  1503. self.completionSource.TrySetException(ex);
  1504. return;
  1505. }
  1506. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  1507. }
  1508. return;
  1509. COMPLETE:
  1510. self.completionSource.TrySetResult(false);
  1511. return;
  1512. }
  1513. bool TrySetResult()
  1514. {
  1515. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5)
  1516. {
  1517. result = resultSelector(current1, current2, current3, current4, current5);
  1518. completionSource.TrySetResult(true);
  1519. return true;
  1520. }
  1521. else
  1522. {
  1523. return false;
  1524. }
  1525. }
  1526. public async UniTask DisposeAsync()
  1527. {
  1528. TaskTracker.RemoveTracking(this);
  1529. if (enumerator1 != null)
  1530. {
  1531. await enumerator1.DisposeAsync();
  1532. }
  1533. if (enumerator2 != null)
  1534. {
  1535. await enumerator2.DisposeAsync();
  1536. }
  1537. if (enumerator3 != null)
  1538. {
  1539. await enumerator3.DisposeAsync();
  1540. }
  1541. if (enumerator4 != null)
  1542. {
  1543. await enumerator4.DisposeAsync();
  1544. }
  1545. if (enumerator5 != null)
  1546. {
  1547. await enumerator5.DisposeAsync();
  1548. }
  1549. }
  1550. }
  1551. }
  1552. internal class CombineLatest<T1, T2, T3, T4, T5, T6, TResult> : IUniTaskAsyncEnumerable<TResult>
  1553. {
  1554. readonly IUniTaskAsyncEnumerable<T1> source1;
  1555. readonly IUniTaskAsyncEnumerable<T2> source2;
  1556. readonly IUniTaskAsyncEnumerable<T3> source3;
  1557. readonly IUniTaskAsyncEnumerable<T4> source4;
  1558. readonly IUniTaskAsyncEnumerable<T5> source5;
  1559. readonly IUniTaskAsyncEnumerable<T6> source6;
  1560. readonly Func<T1, T2, T3, T4, T5, T6, TResult> resultSelector;
  1561. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, Func<T1, T2, T3, T4, T5, T6, TResult> resultSelector)
  1562. {
  1563. this.source1 = source1;
  1564. this.source2 = source2;
  1565. this.source3 = source3;
  1566. this.source4 = source4;
  1567. this.source5 = source5;
  1568. this.source6 = source6;
  1569. this.resultSelector = resultSelector;
  1570. }
  1571. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  1572. {
  1573. return new _CombineLatest(source1, source2, source3, source4, source5, source6, resultSelector, cancellationToken);
  1574. }
  1575. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  1576. {
  1577. static readonly Action<object> Completed1Delegate = Completed1;
  1578. static readonly Action<object> Completed2Delegate = Completed2;
  1579. static readonly Action<object> Completed3Delegate = Completed3;
  1580. static readonly Action<object> Completed4Delegate = Completed4;
  1581. static readonly Action<object> Completed5Delegate = Completed5;
  1582. static readonly Action<object> Completed6Delegate = Completed6;
  1583. const int CompleteCount = 6;
  1584. readonly IUniTaskAsyncEnumerable<T1> source1;
  1585. readonly IUniTaskAsyncEnumerable<T2> source2;
  1586. readonly IUniTaskAsyncEnumerable<T3> source3;
  1587. readonly IUniTaskAsyncEnumerable<T4> source4;
  1588. readonly IUniTaskAsyncEnumerable<T5> source5;
  1589. readonly IUniTaskAsyncEnumerable<T6> source6;
  1590. readonly Func<T1, T2, T3, T4, T5, T6, TResult> resultSelector;
  1591. CancellationToken cancellationToken;
  1592. IUniTaskAsyncEnumerator<T1> enumerator1;
  1593. UniTask<bool>.Awaiter awaiter1;
  1594. bool hasCurrent1;
  1595. bool running1;
  1596. T1 current1;
  1597. IUniTaskAsyncEnumerator<T2> enumerator2;
  1598. UniTask<bool>.Awaiter awaiter2;
  1599. bool hasCurrent2;
  1600. bool running2;
  1601. T2 current2;
  1602. IUniTaskAsyncEnumerator<T3> enumerator3;
  1603. UniTask<bool>.Awaiter awaiter3;
  1604. bool hasCurrent3;
  1605. bool running3;
  1606. T3 current3;
  1607. IUniTaskAsyncEnumerator<T4> enumerator4;
  1608. UniTask<bool>.Awaiter awaiter4;
  1609. bool hasCurrent4;
  1610. bool running4;
  1611. T4 current4;
  1612. IUniTaskAsyncEnumerator<T5> enumerator5;
  1613. UniTask<bool>.Awaiter awaiter5;
  1614. bool hasCurrent5;
  1615. bool running5;
  1616. T5 current5;
  1617. IUniTaskAsyncEnumerator<T6> enumerator6;
  1618. UniTask<bool>.Awaiter awaiter6;
  1619. bool hasCurrent6;
  1620. bool running6;
  1621. T6 current6;
  1622. int completedCount;
  1623. bool syncRunning;
  1624. TResult result;
  1625. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, Func<T1, T2, T3, T4, T5, T6, TResult> resultSelector, CancellationToken cancellationToken)
  1626. {
  1627. this.source1 = source1;
  1628. this.source2 = source2;
  1629. this.source3 = source3;
  1630. this.source4 = source4;
  1631. this.source5 = source5;
  1632. this.source6 = source6;
  1633. this.resultSelector = resultSelector;
  1634. this.cancellationToken = cancellationToken;
  1635. TaskTracker.TrackActiveTask(this, 3);
  1636. }
  1637. public TResult Current => result;
  1638. public UniTask<bool> MoveNextAsync()
  1639. {
  1640. cancellationToken.ThrowIfCancellationRequested();
  1641. if (completedCount == CompleteCount) return CompletedTasks.False;
  1642. if (enumerator1 == null)
  1643. {
  1644. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  1645. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  1646. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  1647. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  1648. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  1649. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  1650. }
  1651. completionSource.Reset();
  1652. AGAIN:
  1653. syncRunning = true;
  1654. if (!running1)
  1655. {
  1656. running1 = true;
  1657. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  1658. if (awaiter1.IsCompleted)
  1659. {
  1660. Completed1(this);
  1661. }
  1662. else
  1663. {
  1664. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  1665. }
  1666. }
  1667. if (!running2)
  1668. {
  1669. running2 = true;
  1670. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  1671. if (awaiter2.IsCompleted)
  1672. {
  1673. Completed2(this);
  1674. }
  1675. else
  1676. {
  1677. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  1678. }
  1679. }
  1680. if (!running3)
  1681. {
  1682. running3 = true;
  1683. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  1684. if (awaiter3.IsCompleted)
  1685. {
  1686. Completed3(this);
  1687. }
  1688. else
  1689. {
  1690. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  1691. }
  1692. }
  1693. if (!running4)
  1694. {
  1695. running4 = true;
  1696. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  1697. if (awaiter4.IsCompleted)
  1698. {
  1699. Completed4(this);
  1700. }
  1701. else
  1702. {
  1703. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  1704. }
  1705. }
  1706. if (!running5)
  1707. {
  1708. running5 = true;
  1709. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  1710. if (awaiter5.IsCompleted)
  1711. {
  1712. Completed5(this);
  1713. }
  1714. else
  1715. {
  1716. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  1717. }
  1718. }
  1719. if (!running6)
  1720. {
  1721. running6 = true;
  1722. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  1723. if (awaiter6.IsCompleted)
  1724. {
  1725. Completed6(this);
  1726. }
  1727. else
  1728. {
  1729. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  1730. }
  1731. }
  1732. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6)
  1733. {
  1734. goto AGAIN;
  1735. }
  1736. syncRunning = false;
  1737. return new UniTask<bool>(this, completionSource.Version);
  1738. }
  1739. static void Completed1(object state)
  1740. {
  1741. var self = (_CombineLatest)state;
  1742. self.running1 = false;
  1743. try
  1744. {
  1745. if (self.awaiter1.GetResult())
  1746. {
  1747. self.hasCurrent1 = true;
  1748. self.current1 = self.enumerator1.Current;
  1749. goto SUCCESS;
  1750. }
  1751. else
  1752. {
  1753. self.running1 = true; // as complete, no more call MoveNextAsync.
  1754. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1755. {
  1756. goto COMPLETE;
  1757. }
  1758. return;
  1759. }
  1760. }
  1761. catch (Exception ex)
  1762. {
  1763. self.running1 = true; // as complete, no more call MoveNextAsync.
  1764. self.completedCount = CompleteCount;
  1765. self.completionSource.TrySetException(ex);
  1766. return;
  1767. }
  1768. SUCCESS:
  1769. if (!self.TrySetResult())
  1770. {
  1771. if (self.syncRunning) return;
  1772. self.running1 = true; // as complete, no more call MoveNextAsync.
  1773. try
  1774. {
  1775. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  1776. }
  1777. catch (Exception ex)
  1778. {
  1779. self.completedCount = CompleteCount;
  1780. self.completionSource.TrySetException(ex);
  1781. return;
  1782. }
  1783. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  1784. }
  1785. return;
  1786. COMPLETE:
  1787. self.completionSource.TrySetResult(false);
  1788. return;
  1789. }
  1790. static void Completed2(object state)
  1791. {
  1792. var self = (_CombineLatest)state;
  1793. self.running2 = false;
  1794. try
  1795. {
  1796. if (self.awaiter2.GetResult())
  1797. {
  1798. self.hasCurrent2 = true;
  1799. self.current2 = self.enumerator2.Current;
  1800. goto SUCCESS;
  1801. }
  1802. else
  1803. {
  1804. self.running2 = true; // as complete, no more call MoveNextAsync.
  1805. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1806. {
  1807. goto COMPLETE;
  1808. }
  1809. return;
  1810. }
  1811. }
  1812. catch (Exception ex)
  1813. {
  1814. self.running2 = true; // as complete, no more call MoveNextAsync.
  1815. self.completedCount = CompleteCount;
  1816. self.completionSource.TrySetException(ex);
  1817. return;
  1818. }
  1819. SUCCESS:
  1820. if (!self.TrySetResult())
  1821. {
  1822. if (self.syncRunning) return;
  1823. self.running2 = true; // as complete, no more call MoveNextAsync.
  1824. try
  1825. {
  1826. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  1827. }
  1828. catch (Exception ex)
  1829. {
  1830. self.completedCount = CompleteCount;
  1831. self.completionSource.TrySetException(ex);
  1832. return;
  1833. }
  1834. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  1835. }
  1836. return;
  1837. COMPLETE:
  1838. self.completionSource.TrySetResult(false);
  1839. return;
  1840. }
  1841. static void Completed3(object state)
  1842. {
  1843. var self = (_CombineLatest)state;
  1844. self.running3 = false;
  1845. try
  1846. {
  1847. if (self.awaiter3.GetResult())
  1848. {
  1849. self.hasCurrent3 = true;
  1850. self.current3 = self.enumerator3.Current;
  1851. goto SUCCESS;
  1852. }
  1853. else
  1854. {
  1855. self.running3 = true; // as complete, no more call MoveNextAsync.
  1856. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1857. {
  1858. goto COMPLETE;
  1859. }
  1860. return;
  1861. }
  1862. }
  1863. catch (Exception ex)
  1864. {
  1865. self.running3 = true; // as complete, no more call MoveNextAsync.
  1866. self.completedCount = CompleteCount;
  1867. self.completionSource.TrySetException(ex);
  1868. return;
  1869. }
  1870. SUCCESS:
  1871. if (!self.TrySetResult())
  1872. {
  1873. if (self.syncRunning) return;
  1874. self.running3 = true; // as complete, no more call MoveNextAsync.
  1875. try
  1876. {
  1877. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  1878. }
  1879. catch (Exception ex)
  1880. {
  1881. self.completedCount = CompleteCount;
  1882. self.completionSource.TrySetException(ex);
  1883. return;
  1884. }
  1885. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  1886. }
  1887. return;
  1888. COMPLETE:
  1889. self.completionSource.TrySetResult(false);
  1890. return;
  1891. }
  1892. static void Completed4(object state)
  1893. {
  1894. var self = (_CombineLatest)state;
  1895. self.running4 = false;
  1896. try
  1897. {
  1898. if (self.awaiter4.GetResult())
  1899. {
  1900. self.hasCurrent4 = true;
  1901. self.current4 = self.enumerator4.Current;
  1902. goto SUCCESS;
  1903. }
  1904. else
  1905. {
  1906. self.running4 = true; // as complete, no more call MoveNextAsync.
  1907. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1908. {
  1909. goto COMPLETE;
  1910. }
  1911. return;
  1912. }
  1913. }
  1914. catch (Exception ex)
  1915. {
  1916. self.running4 = true; // as complete, no more call MoveNextAsync.
  1917. self.completedCount = CompleteCount;
  1918. self.completionSource.TrySetException(ex);
  1919. return;
  1920. }
  1921. SUCCESS:
  1922. if (!self.TrySetResult())
  1923. {
  1924. if (self.syncRunning) return;
  1925. self.running4 = true; // as complete, no more call MoveNextAsync.
  1926. try
  1927. {
  1928. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  1929. }
  1930. catch (Exception ex)
  1931. {
  1932. self.completedCount = CompleteCount;
  1933. self.completionSource.TrySetException(ex);
  1934. return;
  1935. }
  1936. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  1937. }
  1938. return;
  1939. COMPLETE:
  1940. self.completionSource.TrySetResult(false);
  1941. return;
  1942. }
  1943. static void Completed5(object state)
  1944. {
  1945. var self = (_CombineLatest)state;
  1946. self.running5 = false;
  1947. try
  1948. {
  1949. if (self.awaiter5.GetResult())
  1950. {
  1951. self.hasCurrent5 = true;
  1952. self.current5 = self.enumerator5.Current;
  1953. goto SUCCESS;
  1954. }
  1955. else
  1956. {
  1957. self.running5 = true; // as complete, no more call MoveNextAsync.
  1958. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  1959. {
  1960. goto COMPLETE;
  1961. }
  1962. return;
  1963. }
  1964. }
  1965. catch (Exception ex)
  1966. {
  1967. self.running5 = true; // as complete, no more call MoveNextAsync.
  1968. self.completedCount = CompleteCount;
  1969. self.completionSource.TrySetException(ex);
  1970. return;
  1971. }
  1972. SUCCESS:
  1973. if (!self.TrySetResult())
  1974. {
  1975. if (self.syncRunning) return;
  1976. self.running5 = true; // as complete, no more call MoveNextAsync.
  1977. try
  1978. {
  1979. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  1980. }
  1981. catch (Exception ex)
  1982. {
  1983. self.completedCount = CompleteCount;
  1984. self.completionSource.TrySetException(ex);
  1985. return;
  1986. }
  1987. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  1988. }
  1989. return;
  1990. COMPLETE:
  1991. self.completionSource.TrySetResult(false);
  1992. return;
  1993. }
  1994. static void Completed6(object state)
  1995. {
  1996. var self = (_CombineLatest)state;
  1997. self.running6 = false;
  1998. try
  1999. {
  2000. if (self.awaiter6.GetResult())
  2001. {
  2002. self.hasCurrent6 = true;
  2003. self.current6 = self.enumerator6.Current;
  2004. goto SUCCESS;
  2005. }
  2006. else
  2007. {
  2008. self.running6 = true; // as complete, no more call MoveNextAsync.
  2009. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2010. {
  2011. goto COMPLETE;
  2012. }
  2013. return;
  2014. }
  2015. }
  2016. catch (Exception ex)
  2017. {
  2018. self.running6 = true; // as complete, no more call MoveNextAsync.
  2019. self.completedCount = CompleteCount;
  2020. self.completionSource.TrySetException(ex);
  2021. return;
  2022. }
  2023. SUCCESS:
  2024. if (!self.TrySetResult())
  2025. {
  2026. if (self.syncRunning) return;
  2027. self.running6 = true; // as complete, no more call MoveNextAsync.
  2028. try
  2029. {
  2030. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  2031. }
  2032. catch (Exception ex)
  2033. {
  2034. self.completedCount = CompleteCount;
  2035. self.completionSource.TrySetException(ex);
  2036. return;
  2037. }
  2038. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  2039. }
  2040. return;
  2041. COMPLETE:
  2042. self.completionSource.TrySetResult(false);
  2043. return;
  2044. }
  2045. bool TrySetResult()
  2046. {
  2047. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6)
  2048. {
  2049. result = resultSelector(current1, current2, current3, current4, current5, current6);
  2050. completionSource.TrySetResult(true);
  2051. return true;
  2052. }
  2053. else
  2054. {
  2055. return false;
  2056. }
  2057. }
  2058. public async UniTask DisposeAsync()
  2059. {
  2060. TaskTracker.RemoveTracking(this);
  2061. if (enumerator1 != null)
  2062. {
  2063. await enumerator1.DisposeAsync();
  2064. }
  2065. if (enumerator2 != null)
  2066. {
  2067. await enumerator2.DisposeAsync();
  2068. }
  2069. if (enumerator3 != null)
  2070. {
  2071. await enumerator3.DisposeAsync();
  2072. }
  2073. if (enumerator4 != null)
  2074. {
  2075. await enumerator4.DisposeAsync();
  2076. }
  2077. if (enumerator5 != null)
  2078. {
  2079. await enumerator5.DisposeAsync();
  2080. }
  2081. if (enumerator6 != null)
  2082. {
  2083. await enumerator6.DisposeAsync();
  2084. }
  2085. }
  2086. }
  2087. }
  2088. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, TResult> : IUniTaskAsyncEnumerable<TResult>
  2089. {
  2090. readonly IUniTaskAsyncEnumerable<T1> source1;
  2091. readonly IUniTaskAsyncEnumerable<T2> source2;
  2092. readonly IUniTaskAsyncEnumerable<T3> source3;
  2093. readonly IUniTaskAsyncEnumerable<T4> source4;
  2094. readonly IUniTaskAsyncEnumerable<T5> source5;
  2095. readonly IUniTaskAsyncEnumerable<T6> source6;
  2096. readonly IUniTaskAsyncEnumerable<T7> source7;
  2097. readonly Func<T1, T2, T3, T4, T5, T6, T7, TResult> resultSelector;
  2098. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, Func<T1, T2, T3, T4, T5, T6, T7, TResult> resultSelector)
  2099. {
  2100. this.source1 = source1;
  2101. this.source2 = source2;
  2102. this.source3 = source3;
  2103. this.source4 = source4;
  2104. this.source5 = source5;
  2105. this.source6 = source6;
  2106. this.source7 = source7;
  2107. this.resultSelector = resultSelector;
  2108. }
  2109. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  2110. {
  2111. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, resultSelector, cancellationToken);
  2112. }
  2113. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  2114. {
  2115. static readonly Action<object> Completed1Delegate = Completed1;
  2116. static readonly Action<object> Completed2Delegate = Completed2;
  2117. static readonly Action<object> Completed3Delegate = Completed3;
  2118. static readonly Action<object> Completed4Delegate = Completed4;
  2119. static readonly Action<object> Completed5Delegate = Completed5;
  2120. static readonly Action<object> Completed6Delegate = Completed6;
  2121. static readonly Action<object> Completed7Delegate = Completed7;
  2122. const int CompleteCount = 7;
  2123. readonly IUniTaskAsyncEnumerable<T1> source1;
  2124. readonly IUniTaskAsyncEnumerable<T2> source2;
  2125. readonly IUniTaskAsyncEnumerable<T3> source3;
  2126. readonly IUniTaskAsyncEnumerable<T4> source4;
  2127. readonly IUniTaskAsyncEnumerable<T5> source5;
  2128. readonly IUniTaskAsyncEnumerable<T6> source6;
  2129. readonly IUniTaskAsyncEnumerable<T7> source7;
  2130. readonly Func<T1, T2, T3, T4, T5, T6, T7, TResult> resultSelector;
  2131. CancellationToken cancellationToken;
  2132. IUniTaskAsyncEnumerator<T1> enumerator1;
  2133. UniTask<bool>.Awaiter awaiter1;
  2134. bool hasCurrent1;
  2135. bool running1;
  2136. T1 current1;
  2137. IUniTaskAsyncEnumerator<T2> enumerator2;
  2138. UniTask<bool>.Awaiter awaiter2;
  2139. bool hasCurrent2;
  2140. bool running2;
  2141. T2 current2;
  2142. IUniTaskAsyncEnumerator<T3> enumerator3;
  2143. UniTask<bool>.Awaiter awaiter3;
  2144. bool hasCurrent3;
  2145. bool running3;
  2146. T3 current3;
  2147. IUniTaskAsyncEnumerator<T4> enumerator4;
  2148. UniTask<bool>.Awaiter awaiter4;
  2149. bool hasCurrent4;
  2150. bool running4;
  2151. T4 current4;
  2152. IUniTaskAsyncEnumerator<T5> enumerator5;
  2153. UniTask<bool>.Awaiter awaiter5;
  2154. bool hasCurrent5;
  2155. bool running5;
  2156. T5 current5;
  2157. IUniTaskAsyncEnumerator<T6> enumerator6;
  2158. UniTask<bool>.Awaiter awaiter6;
  2159. bool hasCurrent6;
  2160. bool running6;
  2161. T6 current6;
  2162. IUniTaskAsyncEnumerator<T7> enumerator7;
  2163. UniTask<bool>.Awaiter awaiter7;
  2164. bool hasCurrent7;
  2165. bool running7;
  2166. T7 current7;
  2167. int completedCount;
  2168. bool syncRunning;
  2169. TResult result;
  2170. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, Func<T1, T2, T3, T4, T5, T6, T7, TResult> resultSelector, CancellationToken cancellationToken)
  2171. {
  2172. this.source1 = source1;
  2173. this.source2 = source2;
  2174. this.source3 = source3;
  2175. this.source4 = source4;
  2176. this.source5 = source5;
  2177. this.source6 = source6;
  2178. this.source7 = source7;
  2179. this.resultSelector = resultSelector;
  2180. this.cancellationToken = cancellationToken;
  2181. TaskTracker.TrackActiveTask(this, 3);
  2182. }
  2183. public TResult Current => result;
  2184. public UniTask<bool> MoveNextAsync()
  2185. {
  2186. cancellationToken.ThrowIfCancellationRequested();
  2187. if (completedCount == CompleteCount) return CompletedTasks.False;
  2188. if (enumerator1 == null)
  2189. {
  2190. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  2191. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  2192. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  2193. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  2194. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  2195. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  2196. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  2197. }
  2198. completionSource.Reset();
  2199. AGAIN:
  2200. syncRunning = true;
  2201. if (!running1)
  2202. {
  2203. running1 = true;
  2204. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  2205. if (awaiter1.IsCompleted)
  2206. {
  2207. Completed1(this);
  2208. }
  2209. else
  2210. {
  2211. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  2212. }
  2213. }
  2214. if (!running2)
  2215. {
  2216. running2 = true;
  2217. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  2218. if (awaiter2.IsCompleted)
  2219. {
  2220. Completed2(this);
  2221. }
  2222. else
  2223. {
  2224. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  2225. }
  2226. }
  2227. if (!running3)
  2228. {
  2229. running3 = true;
  2230. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  2231. if (awaiter3.IsCompleted)
  2232. {
  2233. Completed3(this);
  2234. }
  2235. else
  2236. {
  2237. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  2238. }
  2239. }
  2240. if (!running4)
  2241. {
  2242. running4 = true;
  2243. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  2244. if (awaiter4.IsCompleted)
  2245. {
  2246. Completed4(this);
  2247. }
  2248. else
  2249. {
  2250. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  2251. }
  2252. }
  2253. if (!running5)
  2254. {
  2255. running5 = true;
  2256. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  2257. if (awaiter5.IsCompleted)
  2258. {
  2259. Completed5(this);
  2260. }
  2261. else
  2262. {
  2263. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  2264. }
  2265. }
  2266. if (!running6)
  2267. {
  2268. running6 = true;
  2269. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  2270. if (awaiter6.IsCompleted)
  2271. {
  2272. Completed6(this);
  2273. }
  2274. else
  2275. {
  2276. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  2277. }
  2278. }
  2279. if (!running7)
  2280. {
  2281. running7 = true;
  2282. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  2283. if (awaiter7.IsCompleted)
  2284. {
  2285. Completed7(this);
  2286. }
  2287. else
  2288. {
  2289. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  2290. }
  2291. }
  2292. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7)
  2293. {
  2294. goto AGAIN;
  2295. }
  2296. syncRunning = false;
  2297. return new UniTask<bool>(this, completionSource.Version);
  2298. }
  2299. static void Completed1(object state)
  2300. {
  2301. var self = (_CombineLatest)state;
  2302. self.running1 = false;
  2303. try
  2304. {
  2305. if (self.awaiter1.GetResult())
  2306. {
  2307. self.hasCurrent1 = true;
  2308. self.current1 = self.enumerator1.Current;
  2309. goto SUCCESS;
  2310. }
  2311. else
  2312. {
  2313. self.running1 = true; // as complete, no more call MoveNextAsync.
  2314. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2315. {
  2316. goto COMPLETE;
  2317. }
  2318. return;
  2319. }
  2320. }
  2321. catch (Exception ex)
  2322. {
  2323. self.running1 = true; // as complete, no more call MoveNextAsync.
  2324. self.completedCount = CompleteCount;
  2325. self.completionSource.TrySetException(ex);
  2326. return;
  2327. }
  2328. SUCCESS:
  2329. if (!self.TrySetResult())
  2330. {
  2331. if (self.syncRunning) return;
  2332. self.running1 = true; // as complete, no more call MoveNextAsync.
  2333. try
  2334. {
  2335. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  2336. }
  2337. catch (Exception ex)
  2338. {
  2339. self.completedCount = CompleteCount;
  2340. self.completionSource.TrySetException(ex);
  2341. return;
  2342. }
  2343. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  2344. }
  2345. return;
  2346. COMPLETE:
  2347. self.completionSource.TrySetResult(false);
  2348. return;
  2349. }
  2350. static void Completed2(object state)
  2351. {
  2352. var self = (_CombineLatest)state;
  2353. self.running2 = false;
  2354. try
  2355. {
  2356. if (self.awaiter2.GetResult())
  2357. {
  2358. self.hasCurrent2 = true;
  2359. self.current2 = self.enumerator2.Current;
  2360. goto SUCCESS;
  2361. }
  2362. else
  2363. {
  2364. self.running2 = true; // as complete, no more call MoveNextAsync.
  2365. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2366. {
  2367. goto COMPLETE;
  2368. }
  2369. return;
  2370. }
  2371. }
  2372. catch (Exception ex)
  2373. {
  2374. self.running2 = true; // as complete, no more call MoveNextAsync.
  2375. self.completedCount = CompleteCount;
  2376. self.completionSource.TrySetException(ex);
  2377. return;
  2378. }
  2379. SUCCESS:
  2380. if (!self.TrySetResult())
  2381. {
  2382. if (self.syncRunning) return;
  2383. self.running2 = true; // as complete, no more call MoveNextAsync.
  2384. try
  2385. {
  2386. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  2387. }
  2388. catch (Exception ex)
  2389. {
  2390. self.completedCount = CompleteCount;
  2391. self.completionSource.TrySetException(ex);
  2392. return;
  2393. }
  2394. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  2395. }
  2396. return;
  2397. COMPLETE:
  2398. self.completionSource.TrySetResult(false);
  2399. return;
  2400. }
  2401. static void Completed3(object state)
  2402. {
  2403. var self = (_CombineLatest)state;
  2404. self.running3 = false;
  2405. try
  2406. {
  2407. if (self.awaiter3.GetResult())
  2408. {
  2409. self.hasCurrent3 = true;
  2410. self.current3 = self.enumerator3.Current;
  2411. goto SUCCESS;
  2412. }
  2413. else
  2414. {
  2415. self.running3 = true; // as complete, no more call MoveNextAsync.
  2416. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2417. {
  2418. goto COMPLETE;
  2419. }
  2420. return;
  2421. }
  2422. }
  2423. catch (Exception ex)
  2424. {
  2425. self.running3 = true; // as complete, no more call MoveNextAsync.
  2426. self.completedCount = CompleteCount;
  2427. self.completionSource.TrySetException(ex);
  2428. return;
  2429. }
  2430. SUCCESS:
  2431. if (!self.TrySetResult())
  2432. {
  2433. if (self.syncRunning) return;
  2434. self.running3 = true; // as complete, no more call MoveNextAsync.
  2435. try
  2436. {
  2437. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  2438. }
  2439. catch (Exception ex)
  2440. {
  2441. self.completedCount = CompleteCount;
  2442. self.completionSource.TrySetException(ex);
  2443. return;
  2444. }
  2445. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  2446. }
  2447. return;
  2448. COMPLETE:
  2449. self.completionSource.TrySetResult(false);
  2450. return;
  2451. }
  2452. static void Completed4(object state)
  2453. {
  2454. var self = (_CombineLatest)state;
  2455. self.running4 = false;
  2456. try
  2457. {
  2458. if (self.awaiter4.GetResult())
  2459. {
  2460. self.hasCurrent4 = true;
  2461. self.current4 = self.enumerator4.Current;
  2462. goto SUCCESS;
  2463. }
  2464. else
  2465. {
  2466. self.running4 = true; // as complete, no more call MoveNextAsync.
  2467. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2468. {
  2469. goto COMPLETE;
  2470. }
  2471. return;
  2472. }
  2473. }
  2474. catch (Exception ex)
  2475. {
  2476. self.running4 = true; // as complete, no more call MoveNextAsync.
  2477. self.completedCount = CompleteCount;
  2478. self.completionSource.TrySetException(ex);
  2479. return;
  2480. }
  2481. SUCCESS:
  2482. if (!self.TrySetResult())
  2483. {
  2484. if (self.syncRunning) return;
  2485. self.running4 = true; // as complete, no more call MoveNextAsync.
  2486. try
  2487. {
  2488. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  2489. }
  2490. catch (Exception ex)
  2491. {
  2492. self.completedCount = CompleteCount;
  2493. self.completionSource.TrySetException(ex);
  2494. return;
  2495. }
  2496. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  2497. }
  2498. return;
  2499. COMPLETE:
  2500. self.completionSource.TrySetResult(false);
  2501. return;
  2502. }
  2503. static void Completed5(object state)
  2504. {
  2505. var self = (_CombineLatest)state;
  2506. self.running5 = false;
  2507. try
  2508. {
  2509. if (self.awaiter5.GetResult())
  2510. {
  2511. self.hasCurrent5 = true;
  2512. self.current5 = self.enumerator5.Current;
  2513. goto SUCCESS;
  2514. }
  2515. else
  2516. {
  2517. self.running5 = true; // as complete, no more call MoveNextAsync.
  2518. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2519. {
  2520. goto COMPLETE;
  2521. }
  2522. return;
  2523. }
  2524. }
  2525. catch (Exception ex)
  2526. {
  2527. self.running5 = true; // as complete, no more call MoveNextAsync.
  2528. self.completedCount = CompleteCount;
  2529. self.completionSource.TrySetException(ex);
  2530. return;
  2531. }
  2532. SUCCESS:
  2533. if (!self.TrySetResult())
  2534. {
  2535. if (self.syncRunning) return;
  2536. self.running5 = true; // as complete, no more call MoveNextAsync.
  2537. try
  2538. {
  2539. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  2540. }
  2541. catch (Exception ex)
  2542. {
  2543. self.completedCount = CompleteCount;
  2544. self.completionSource.TrySetException(ex);
  2545. return;
  2546. }
  2547. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  2548. }
  2549. return;
  2550. COMPLETE:
  2551. self.completionSource.TrySetResult(false);
  2552. return;
  2553. }
  2554. static void Completed6(object state)
  2555. {
  2556. var self = (_CombineLatest)state;
  2557. self.running6 = false;
  2558. try
  2559. {
  2560. if (self.awaiter6.GetResult())
  2561. {
  2562. self.hasCurrent6 = true;
  2563. self.current6 = self.enumerator6.Current;
  2564. goto SUCCESS;
  2565. }
  2566. else
  2567. {
  2568. self.running6 = true; // as complete, no more call MoveNextAsync.
  2569. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2570. {
  2571. goto COMPLETE;
  2572. }
  2573. return;
  2574. }
  2575. }
  2576. catch (Exception ex)
  2577. {
  2578. self.running6 = true; // as complete, no more call MoveNextAsync.
  2579. self.completedCount = CompleteCount;
  2580. self.completionSource.TrySetException(ex);
  2581. return;
  2582. }
  2583. SUCCESS:
  2584. if (!self.TrySetResult())
  2585. {
  2586. if (self.syncRunning) return;
  2587. self.running6 = true; // as complete, no more call MoveNextAsync.
  2588. try
  2589. {
  2590. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  2591. }
  2592. catch (Exception ex)
  2593. {
  2594. self.completedCount = CompleteCount;
  2595. self.completionSource.TrySetException(ex);
  2596. return;
  2597. }
  2598. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  2599. }
  2600. return;
  2601. COMPLETE:
  2602. self.completionSource.TrySetResult(false);
  2603. return;
  2604. }
  2605. static void Completed7(object state)
  2606. {
  2607. var self = (_CombineLatest)state;
  2608. self.running7 = false;
  2609. try
  2610. {
  2611. if (self.awaiter7.GetResult())
  2612. {
  2613. self.hasCurrent7 = true;
  2614. self.current7 = self.enumerator7.Current;
  2615. goto SUCCESS;
  2616. }
  2617. else
  2618. {
  2619. self.running7 = true; // as complete, no more call MoveNextAsync.
  2620. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2621. {
  2622. goto COMPLETE;
  2623. }
  2624. return;
  2625. }
  2626. }
  2627. catch (Exception ex)
  2628. {
  2629. self.running7 = true; // as complete, no more call MoveNextAsync.
  2630. self.completedCount = CompleteCount;
  2631. self.completionSource.TrySetException(ex);
  2632. return;
  2633. }
  2634. SUCCESS:
  2635. if (!self.TrySetResult())
  2636. {
  2637. if (self.syncRunning) return;
  2638. self.running7 = true; // as complete, no more call MoveNextAsync.
  2639. try
  2640. {
  2641. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  2642. }
  2643. catch (Exception ex)
  2644. {
  2645. self.completedCount = CompleteCount;
  2646. self.completionSource.TrySetException(ex);
  2647. return;
  2648. }
  2649. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  2650. }
  2651. return;
  2652. COMPLETE:
  2653. self.completionSource.TrySetResult(false);
  2654. return;
  2655. }
  2656. bool TrySetResult()
  2657. {
  2658. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7)
  2659. {
  2660. result = resultSelector(current1, current2, current3, current4, current5, current6, current7);
  2661. completionSource.TrySetResult(true);
  2662. return true;
  2663. }
  2664. else
  2665. {
  2666. return false;
  2667. }
  2668. }
  2669. public async UniTask DisposeAsync()
  2670. {
  2671. TaskTracker.RemoveTracking(this);
  2672. if (enumerator1 != null)
  2673. {
  2674. await enumerator1.DisposeAsync();
  2675. }
  2676. if (enumerator2 != null)
  2677. {
  2678. await enumerator2.DisposeAsync();
  2679. }
  2680. if (enumerator3 != null)
  2681. {
  2682. await enumerator3.DisposeAsync();
  2683. }
  2684. if (enumerator4 != null)
  2685. {
  2686. await enumerator4.DisposeAsync();
  2687. }
  2688. if (enumerator5 != null)
  2689. {
  2690. await enumerator5.DisposeAsync();
  2691. }
  2692. if (enumerator6 != null)
  2693. {
  2694. await enumerator6.DisposeAsync();
  2695. }
  2696. if (enumerator7 != null)
  2697. {
  2698. await enumerator7.DisposeAsync();
  2699. }
  2700. }
  2701. }
  2702. }
  2703. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, TResult> : IUniTaskAsyncEnumerable<TResult>
  2704. {
  2705. readonly IUniTaskAsyncEnumerable<T1> source1;
  2706. readonly IUniTaskAsyncEnumerable<T2> source2;
  2707. readonly IUniTaskAsyncEnumerable<T3> source3;
  2708. readonly IUniTaskAsyncEnumerable<T4> source4;
  2709. readonly IUniTaskAsyncEnumerable<T5> source5;
  2710. readonly IUniTaskAsyncEnumerable<T6> source6;
  2711. readonly IUniTaskAsyncEnumerable<T7> source7;
  2712. readonly IUniTaskAsyncEnumerable<T8> source8;
  2713. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> resultSelector;
  2714. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> resultSelector)
  2715. {
  2716. this.source1 = source1;
  2717. this.source2 = source2;
  2718. this.source3 = source3;
  2719. this.source4 = source4;
  2720. this.source5 = source5;
  2721. this.source6 = source6;
  2722. this.source7 = source7;
  2723. this.source8 = source8;
  2724. this.resultSelector = resultSelector;
  2725. }
  2726. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  2727. {
  2728. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, resultSelector, cancellationToken);
  2729. }
  2730. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  2731. {
  2732. static readonly Action<object> Completed1Delegate = Completed1;
  2733. static readonly Action<object> Completed2Delegate = Completed2;
  2734. static readonly Action<object> Completed3Delegate = Completed3;
  2735. static readonly Action<object> Completed4Delegate = Completed4;
  2736. static readonly Action<object> Completed5Delegate = Completed5;
  2737. static readonly Action<object> Completed6Delegate = Completed6;
  2738. static readonly Action<object> Completed7Delegate = Completed7;
  2739. static readonly Action<object> Completed8Delegate = Completed8;
  2740. const int CompleteCount = 8;
  2741. readonly IUniTaskAsyncEnumerable<T1> source1;
  2742. readonly IUniTaskAsyncEnumerable<T2> source2;
  2743. readonly IUniTaskAsyncEnumerable<T3> source3;
  2744. readonly IUniTaskAsyncEnumerable<T4> source4;
  2745. readonly IUniTaskAsyncEnumerable<T5> source5;
  2746. readonly IUniTaskAsyncEnumerable<T6> source6;
  2747. readonly IUniTaskAsyncEnumerable<T7> source7;
  2748. readonly IUniTaskAsyncEnumerable<T8> source8;
  2749. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> resultSelector;
  2750. CancellationToken cancellationToken;
  2751. IUniTaskAsyncEnumerator<T1> enumerator1;
  2752. UniTask<bool>.Awaiter awaiter1;
  2753. bool hasCurrent1;
  2754. bool running1;
  2755. T1 current1;
  2756. IUniTaskAsyncEnumerator<T2> enumerator2;
  2757. UniTask<bool>.Awaiter awaiter2;
  2758. bool hasCurrent2;
  2759. bool running2;
  2760. T2 current2;
  2761. IUniTaskAsyncEnumerator<T3> enumerator3;
  2762. UniTask<bool>.Awaiter awaiter3;
  2763. bool hasCurrent3;
  2764. bool running3;
  2765. T3 current3;
  2766. IUniTaskAsyncEnumerator<T4> enumerator4;
  2767. UniTask<bool>.Awaiter awaiter4;
  2768. bool hasCurrent4;
  2769. bool running4;
  2770. T4 current4;
  2771. IUniTaskAsyncEnumerator<T5> enumerator5;
  2772. UniTask<bool>.Awaiter awaiter5;
  2773. bool hasCurrent5;
  2774. bool running5;
  2775. T5 current5;
  2776. IUniTaskAsyncEnumerator<T6> enumerator6;
  2777. UniTask<bool>.Awaiter awaiter6;
  2778. bool hasCurrent6;
  2779. bool running6;
  2780. T6 current6;
  2781. IUniTaskAsyncEnumerator<T7> enumerator7;
  2782. UniTask<bool>.Awaiter awaiter7;
  2783. bool hasCurrent7;
  2784. bool running7;
  2785. T7 current7;
  2786. IUniTaskAsyncEnumerator<T8> enumerator8;
  2787. UniTask<bool>.Awaiter awaiter8;
  2788. bool hasCurrent8;
  2789. bool running8;
  2790. T8 current8;
  2791. int completedCount;
  2792. bool syncRunning;
  2793. TResult result;
  2794. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> resultSelector, CancellationToken cancellationToken)
  2795. {
  2796. this.source1 = source1;
  2797. this.source2 = source2;
  2798. this.source3 = source3;
  2799. this.source4 = source4;
  2800. this.source5 = source5;
  2801. this.source6 = source6;
  2802. this.source7 = source7;
  2803. this.source8 = source8;
  2804. this.resultSelector = resultSelector;
  2805. this.cancellationToken = cancellationToken;
  2806. TaskTracker.TrackActiveTask(this, 3);
  2807. }
  2808. public TResult Current => result;
  2809. public UniTask<bool> MoveNextAsync()
  2810. {
  2811. cancellationToken.ThrowIfCancellationRequested();
  2812. if (completedCount == CompleteCount) return CompletedTasks.False;
  2813. if (enumerator1 == null)
  2814. {
  2815. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  2816. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  2817. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  2818. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  2819. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  2820. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  2821. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  2822. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  2823. }
  2824. completionSource.Reset();
  2825. AGAIN:
  2826. syncRunning = true;
  2827. if (!running1)
  2828. {
  2829. running1 = true;
  2830. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  2831. if (awaiter1.IsCompleted)
  2832. {
  2833. Completed1(this);
  2834. }
  2835. else
  2836. {
  2837. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  2838. }
  2839. }
  2840. if (!running2)
  2841. {
  2842. running2 = true;
  2843. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  2844. if (awaiter2.IsCompleted)
  2845. {
  2846. Completed2(this);
  2847. }
  2848. else
  2849. {
  2850. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  2851. }
  2852. }
  2853. if (!running3)
  2854. {
  2855. running3 = true;
  2856. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  2857. if (awaiter3.IsCompleted)
  2858. {
  2859. Completed3(this);
  2860. }
  2861. else
  2862. {
  2863. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  2864. }
  2865. }
  2866. if (!running4)
  2867. {
  2868. running4 = true;
  2869. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  2870. if (awaiter4.IsCompleted)
  2871. {
  2872. Completed4(this);
  2873. }
  2874. else
  2875. {
  2876. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  2877. }
  2878. }
  2879. if (!running5)
  2880. {
  2881. running5 = true;
  2882. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  2883. if (awaiter5.IsCompleted)
  2884. {
  2885. Completed5(this);
  2886. }
  2887. else
  2888. {
  2889. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  2890. }
  2891. }
  2892. if (!running6)
  2893. {
  2894. running6 = true;
  2895. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  2896. if (awaiter6.IsCompleted)
  2897. {
  2898. Completed6(this);
  2899. }
  2900. else
  2901. {
  2902. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  2903. }
  2904. }
  2905. if (!running7)
  2906. {
  2907. running7 = true;
  2908. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  2909. if (awaiter7.IsCompleted)
  2910. {
  2911. Completed7(this);
  2912. }
  2913. else
  2914. {
  2915. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  2916. }
  2917. }
  2918. if (!running8)
  2919. {
  2920. running8 = true;
  2921. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  2922. if (awaiter8.IsCompleted)
  2923. {
  2924. Completed8(this);
  2925. }
  2926. else
  2927. {
  2928. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  2929. }
  2930. }
  2931. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8)
  2932. {
  2933. goto AGAIN;
  2934. }
  2935. syncRunning = false;
  2936. return new UniTask<bool>(this, completionSource.Version);
  2937. }
  2938. static void Completed1(object state)
  2939. {
  2940. var self = (_CombineLatest)state;
  2941. self.running1 = false;
  2942. try
  2943. {
  2944. if (self.awaiter1.GetResult())
  2945. {
  2946. self.hasCurrent1 = true;
  2947. self.current1 = self.enumerator1.Current;
  2948. goto SUCCESS;
  2949. }
  2950. else
  2951. {
  2952. self.running1 = true; // as complete, no more call MoveNextAsync.
  2953. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  2954. {
  2955. goto COMPLETE;
  2956. }
  2957. return;
  2958. }
  2959. }
  2960. catch (Exception ex)
  2961. {
  2962. self.running1 = true; // as complete, no more call MoveNextAsync.
  2963. self.completedCount = CompleteCount;
  2964. self.completionSource.TrySetException(ex);
  2965. return;
  2966. }
  2967. SUCCESS:
  2968. if (!self.TrySetResult())
  2969. {
  2970. if (self.syncRunning) return;
  2971. self.running1 = true; // as complete, no more call MoveNextAsync.
  2972. try
  2973. {
  2974. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  2975. }
  2976. catch (Exception ex)
  2977. {
  2978. self.completedCount = CompleteCount;
  2979. self.completionSource.TrySetException(ex);
  2980. return;
  2981. }
  2982. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  2983. }
  2984. return;
  2985. COMPLETE:
  2986. self.completionSource.TrySetResult(false);
  2987. return;
  2988. }
  2989. static void Completed2(object state)
  2990. {
  2991. var self = (_CombineLatest)state;
  2992. self.running2 = false;
  2993. try
  2994. {
  2995. if (self.awaiter2.GetResult())
  2996. {
  2997. self.hasCurrent2 = true;
  2998. self.current2 = self.enumerator2.Current;
  2999. goto SUCCESS;
  3000. }
  3001. else
  3002. {
  3003. self.running2 = true; // as complete, no more call MoveNextAsync.
  3004. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3005. {
  3006. goto COMPLETE;
  3007. }
  3008. return;
  3009. }
  3010. }
  3011. catch (Exception ex)
  3012. {
  3013. self.running2 = true; // as complete, no more call MoveNextAsync.
  3014. self.completedCount = CompleteCount;
  3015. self.completionSource.TrySetException(ex);
  3016. return;
  3017. }
  3018. SUCCESS:
  3019. if (!self.TrySetResult())
  3020. {
  3021. if (self.syncRunning) return;
  3022. self.running2 = true; // as complete, no more call MoveNextAsync.
  3023. try
  3024. {
  3025. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  3026. }
  3027. catch (Exception ex)
  3028. {
  3029. self.completedCount = CompleteCount;
  3030. self.completionSource.TrySetException(ex);
  3031. return;
  3032. }
  3033. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  3034. }
  3035. return;
  3036. COMPLETE:
  3037. self.completionSource.TrySetResult(false);
  3038. return;
  3039. }
  3040. static void Completed3(object state)
  3041. {
  3042. var self = (_CombineLatest)state;
  3043. self.running3 = false;
  3044. try
  3045. {
  3046. if (self.awaiter3.GetResult())
  3047. {
  3048. self.hasCurrent3 = true;
  3049. self.current3 = self.enumerator3.Current;
  3050. goto SUCCESS;
  3051. }
  3052. else
  3053. {
  3054. self.running3 = true; // as complete, no more call MoveNextAsync.
  3055. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3056. {
  3057. goto COMPLETE;
  3058. }
  3059. return;
  3060. }
  3061. }
  3062. catch (Exception ex)
  3063. {
  3064. self.running3 = true; // as complete, no more call MoveNextAsync.
  3065. self.completedCount = CompleteCount;
  3066. self.completionSource.TrySetException(ex);
  3067. return;
  3068. }
  3069. SUCCESS:
  3070. if (!self.TrySetResult())
  3071. {
  3072. if (self.syncRunning) return;
  3073. self.running3 = true; // as complete, no more call MoveNextAsync.
  3074. try
  3075. {
  3076. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  3077. }
  3078. catch (Exception ex)
  3079. {
  3080. self.completedCount = CompleteCount;
  3081. self.completionSource.TrySetException(ex);
  3082. return;
  3083. }
  3084. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  3085. }
  3086. return;
  3087. COMPLETE:
  3088. self.completionSource.TrySetResult(false);
  3089. return;
  3090. }
  3091. static void Completed4(object state)
  3092. {
  3093. var self = (_CombineLatest)state;
  3094. self.running4 = false;
  3095. try
  3096. {
  3097. if (self.awaiter4.GetResult())
  3098. {
  3099. self.hasCurrent4 = true;
  3100. self.current4 = self.enumerator4.Current;
  3101. goto SUCCESS;
  3102. }
  3103. else
  3104. {
  3105. self.running4 = true; // as complete, no more call MoveNextAsync.
  3106. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3107. {
  3108. goto COMPLETE;
  3109. }
  3110. return;
  3111. }
  3112. }
  3113. catch (Exception ex)
  3114. {
  3115. self.running4 = true; // as complete, no more call MoveNextAsync.
  3116. self.completedCount = CompleteCount;
  3117. self.completionSource.TrySetException(ex);
  3118. return;
  3119. }
  3120. SUCCESS:
  3121. if (!self.TrySetResult())
  3122. {
  3123. if (self.syncRunning) return;
  3124. self.running4 = true; // as complete, no more call MoveNextAsync.
  3125. try
  3126. {
  3127. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  3128. }
  3129. catch (Exception ex)
  3130. {
  3131. self.completedCount = CompleteCount;
  3132. self.completionSource.TrySetException(ex);
  3133. return;
  3134. }
  3135. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  3136. }
  3137. return;
  3138. COMPLETE:
  3139. self.completionSource.TrySetResult(false);
  3140. return;
  3141. }
  3142. static void Completed5(object state)
  3143. {
  3144. var self = (_CombineLatest)state;
  3145. self.running5 = false;
  3146. try
  3147. {
  3148. if (self.awaiter5.GetResult())
  3149. {
  3150. self.hasCurrent5 = true;
  3151. self.current5 = self.enumerator5.Current;
  3152. goto SUCCESS;
  3153. }
  3154. else
  3155. {
  3156. self.running5 = true; // as complete, no more call MoveNextAsync.
  3157. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3158. {
  3159. goto COMPLETE;
  3160. }
  3161. return;
  3162. }
  3163. }
  3164. catch (Exception ex)
  3165. {
  3166. self.running5 = true; // as complete, no more call MoveNextAsync.
  3167. self.completedCount = CompleteCount;
  3168. self.completionSource.TrySetException(ex);
  3169. return;
  3170. }
  3171. SUCCESS:
  3172. if (!self.TrySetResult())
  3173. {
  3174. if (self.syncRunning) return;
  3175. self.running5 = true; // as complete, no more call MoveNextAsync.
  3176. try
  3177. {
  3178. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  3179. }
  3180. catch (Exception ex)
  3181. {
  3182. self.completedCount = CompleteCount;
  3183. self.completionSource.TrySetException(ex);
  3184. return;
  3185. }
  3186. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  3187. }
  3188. return;
  3189. COMPLETE:
  3190. self.completionSource.TrySetResult(false);
  3191. return;
  3192. }
  3193. static void Completed6(object state)
  3194. {
  3195. var self = (_CombineLatest)state;
  3196. self.running6 = false;
  3197. try
  3198. {
  3199. if (self.awaiter6.GetResult())
  3200. {
  3201. self.hasCurrent6 = true;
  3202. self.current6 = self.enumerator6.Current;
  3203. goto SUCCESS;
  3204. }
  3205. else
  3206. {
  3207. self.running6 = true; // as complete, no more call MoveNextAsync.
  3208. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3209. {
  3210. goto COMPLETE;
  3211. }
  3212. return;
  3213. }
  3214. }
  3215. catch (Exception ex)
  3216. {
  3217. self.running6 = true; // as complete, no more call MoveNextAsync.
  3218. self.completedCount = CompleteCount;
  3219. self.completionSource.TrySetException(ex);
  3220. return;
  3221. }
  3222. SUCCESS:
  3223. if (!self.TrySetResult())
  3224. {
  3225. if (self.syncRunning) return;
  3226. self.running6 = true; // as complete, no more call MoveNextAsync.
  3227. try
  3228. {
  3229. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  3230. }
  3231. catch (Exception ex)
  3232. {
  3233. self.completedCount = CompleteCount;
  3234. self.completionSource.TrySetException(ex);
  3235. return;
  3236. }
  3237. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  3238. }
  3239. return;
  3240. COMPLETE:
  3241. self.completionSource.TrySetResult(false);
  3242. return;
  3243. }
  3244. static void Completed7(object state)
  3245. {
  3246. var self = (_CombineLatest)state;
  3247. self.running7 = false;
  3248. try
  3249. {
  3250. if (self.awaiter7.GetResult())
  3251. {
  3252. self.hasCurrent7 = true;
  3253. self.current7 = self.enumerator7.Current;
  3254. goto SUCCESS;
  3255. }
  3256. else
  3257. {
  3258. self.running7 = true; // as complete, no more call MoveNextAsync.
  3259. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3260. {
  3261. goto COMPLETE;
  3262. }
  3263. return;
  3264. }
  3265. }
  3266. catch (Exception ex)
  3267. {
  3268. self.running7 = true; // as complete, no more call MoveNextAsync.
  3269. self.completedCount = CompleteCount;
  3270. self.completionSource.TrySetException(ex);
  3271. return;
  3272. }
  3273. SUCCESS:
  3274. if (!self.TrySetResult())
  3275. {
  3276. if (self.syncRunning) return;
  3277. self.running7 = true; // as complete, no more call MoveNextAsync.
  3278. try
  3279. {
  3280. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  3281. }
  3282. catch (Exception ex)
  3283. {
  3284. self.completedCount = CompleteCount;
  3285. self.completionSource.TrySetException(ex);
  3286. return;
  3287. }
  3288. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  3289. }
  3290. return;
  3291. COMPLETE:
  3292. self.completionSource.TrySetResult(false);
  3293. return;
  3294. }
  3295. static void Completed8(object state)
  3296. {
  3297. var self = (_CombineLatest)state;
  3298. self.running8 = false;
  3299. try
  3300. {
  3301. if (self.awaiter8.GetResult())
  3302. {
  3303. self.hasCurrent8 = true;
  3304. self.current8 = self.enumerator8.Current;
  3305. goto SUCCESS;
  3306. }
  3307. else
  3308. {
  3309. self.running8 = true; // as complete, no more call MoveNextAsync.
  3310. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3311. {
  3312. goto COMPLETE;
  3313. }
  3314. return;
  3315. }
  3316. }
  3317. catch (Exception ex)
  3318. {
  3319. self.running8 = true; // as complete, no more call MoveNextAsync.
  3320. self.completedCount = CompleteCount;
  3321. self.completionSource.TrySetException(ex);
  3322. return;
  3323. }
  3324. SUCCESS:
  3325. if (!self.TrySetResult())
  3326. {
  3327. if (self.syncRunning) return;
  3328. self.running8 = true; // as complete, no more call MoveNextAsync.
  3329. try
  3330. {
  3331. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  3332. }
  3333. catch (Exception ex)
  3334. {
  3335. self.completedCount = CompleteCount;
  3336. self.completionSource.TrySetException(ex);
  3337. return;
  3338. }
  3339. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  3340. }
  3341. return;
  3342. COMPLETE:
  3343. self.completionSource.TrySetResult(false);
  3344. return;
  3345. }
  3346. bool TrySetResult()
  3347. {
  3348. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8)
  3349. {
  3350. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8);
  3351. completionSource.TrySetResult(true);
  3352. return true;
  3353. }
  3354. else
  3355. {
  3356. return false;
  3357. }
  3358. }
  3359. public async UniTask DisposeAsync()
  3360. {
  3361. TaskTracker.RemoveTracking(this);
  3362. if (enumerator1 != null)
  3363. {
  3364. await enumerator1.DisposeAsync();
  3365. }
  3366. if (enumerator2 != null)
  3367. {
  3368. await enumerator2.DisposeAsync();
  3369. }
  3370. if (enumerator3 != null)
  3371. {
  3372. await enumerator3.DisposeAsync();
  3373. }
  3374. if (enumerator4 != null)
  3375. {
  3376. await enumerator4.DisposeAsync();
  3377. }
  3378. if (enumerator5 != null)
  3379. {
  3380. await enumerator5.DisposeAsync();
  3381. }
  3382. if (enumerator6 != null)
  3383. {
  3384. await enumerator6.DisposeAsync();
  3385. }
  3386. if (enumerator7 != null)
  3387. {
  3388. await enumerator7.DisposeAsync();
  3389. }
  3390. if (enumerator8 != null)
  3391. {
  3392. await enumerator8.DisposeAsync();
  3393. }
  3394. }
  3395. }
  3396. }
  3397. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> : IUniTaskAsyncEnumerable<TResult>
  3398. {
  3399. readonly IUniTaskAsyncEnumerable<T1> source1;
  3400. readonly IUniTaskAsyncEnumerable<T2> source2;
  3401. readonly IUniTaskAsyncEnumerable<T3> source3;
  3402. readonly IUniTaskAsyncEnumerable<T4> source4;
  3403. readonly IUniTaskAsyncEnumerable<T5> source5;
  3404. readonly IUniTaskAsyncEnumerable<T6> source6;
  3405. readonly IUniTaskAsyncEnumerable<T7> source7;
  3406. readonly IUniTaskAsyncEnumerable<T8> source8;
  3407. readonly IUniTaskAsyncEnumerable<T9> source9;
  3408. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> resultSelector;
  3409. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> resultSelector)
  3410. {
  3411. this.source1 = source1;
  3412. this.source2 = source2;
  3413. this.source3 = source3;
  3414. this.source4 = source4;
  3415. this.source5 = source5;
  3416. this.source6 = source6;
  3417. this.source7 = source7;
  3418. this.source8 = source8;
  3419. this.source9 = source9;
  3420. this.resultSelector = resultSelector;
  3421. }
  3422. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  3423. {
  3424. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, resultSelector, cancellationToken);
  3425. }
  3426. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  3427. {
  3428. static readonly Action<object> Completed1Delegate = Completed1;
  3429. static readonly Action<object> Completed2Delegate = Completed2;
  3430. static readonly Action<object> Completed3Delegate = Completed3;
  3431. static readonly Action<object> Completed4Delegate = Completed4;
  3432. static readonly Action<object> Completed5Delegate = Completed5;
  3433. static readonly Action<object> Completed6Delegate = Completed6;
  3434. static readonly Action<object> Completed7Delegate = Completed7;
  3435. static readonly Action<object> Completed8Delegate = Completed8;
  3436. static readonly Action<object> Completed9Delegate = Completed9;
  3437. const int CompleteCount = 9;
  3438. readonly IUniTaskAsyncEnumerable<T1> source1;
  3439. readonly IUniTaskAsyncEnumerable<T2> source2;
  3440. readonly IUniTaskAsyncEnumerable<T3> source3;
  3441. readonly IUniTaskAsyncEnumerable<T4> source4;
  3442. readonly IUniTaskAsyncEnumerable<T5> source5;
  3443. readonly IUniTaskAsyncEnumerable<T6> source6;
  3444. readonly IUniTaskAsyncEnumerable<T7> source7;
  3445. readonly IUniTaskAsyncEnumerable<T8> source8;
  3446. readonly IUniTaskAsyncEnumerable<T9> source9;
  3447. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> resultSelector;
  3448. CancellationToken cancellationToken;
  3449. IUniTaskAsyncEnumerator<T1> enumerator1;
  3450. UniTask<bool>.Awaiter awaiter1;
  3451. bool hasCurrent1;
  3452. bool running1;
  3453. T1 current1;
  3454. IUniTaskAsyncEnumerator<T2> enumerator2;
  3455. UniTask<bool>.Awaiter awaiter2;
  3456. bool hasCurrent2;
  3457. bool running2;
  3458. T2 current2;
  3459. IUniTaskAsyncEnumerator<T3> enumerator3;
  3460. UniTask<bool>.Awaiter awaiter3;
  3461. bool hasCurrent3;
  3462. bool running3;
  3463. T3 current3;
  3464. IUniTaskAsyncEnumerator<T4> enumerator4;
  3465. UniTask<bool>.Awaiter awaiter4;
  3466. bool hasCurrent4;
  3467. bool running4;
  3468. T4 current4;
  3469. IUniTaskAsyncEnumerator<T5> enumerator5;
  3470. UniTask<bool>.Awaiter awaiter5;
  3471. bool hasCurrent5;
  3472. bool running5;
  3473. T5 current5;
  3474. IUniTaskAsyncEnumerator<T6> enumerator6;
  3475. UniTask<bool>.Awaiter awaiter6;
  3476. bool hasCurrent6;
  3477. bool running6;
  3478. T6 current6;
  3479. IUniTaskAsyncEnumerator<T7> enumerator7;
  3480. UniTask<bool>.Awaiter awaiter7;
  3481. bool hasCurrent7;
  3482. bool running7;
  3483. T7 current7;
  3484. IUniTaskAsyncEnumerator<T8> enumerator8;
  3485. UniTask<bool>.Awaiter awaiter8;
  3486. bool hasCurrent8;
  3487. bool running8;
  3488. T8 current8;
  3489. IUniTaskAsyncEnumerator<T9> enumerator9;
  3490. UniTask<bool>.Awaiter awaiter9;
  3491. bool hasCurrent9;
  3492. bool running9;
  3493. T9 current9;
  3494. int completedCount;
  3495. bool syncRunning;
  3496. TResult result;
  3497. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> resultSelector, CancellationToken cancellationToken)
  3498. {
  3499. this.source1 = source1;
  3500. this.source2 = source2;
  3501. this.source3 = source3;
  3502. this.source4 = source4;
  3503. this.source5 = source5;
  3504. this.source6 = source6;
  3505. this.source7 = source7;
  3506. this.source8 = source8;
  3507. this.source9 = source9;
  3508. this.resultSelector = resultSelector;
  3509. this.cancellationToken = cancellationToken;
  3510. TaskTracker.TrackActiveTask(this, 3);
  3511. }
  3512. public TResult Current => result;
  3513. public UniTask<bool> MoveNextAsync()
  3514. {
  3515. cancellationToken.ThrowIfCancellationRequested();
  3516. if (completedCount == CompleteCount) return CompletedTasks.False;
  3517. if (enumerator1 == null)
  3518. {
  3519. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  3520. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  3521. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  3522. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  3523. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  3524. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  3525. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  3526. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  3527. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  3528. }
  3529. completionSource.Reset();
  3530. AGAIN:
  3531. syncRunning = true;
  3532. if (!running1)
  3533. {
  3534. running1 = true;
  3535. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  3536. if (awaiter1.IsCompleted)
  3537. {
  3538. Completed1(this);
  3539. }
  3540. else
  3541. {
  3542. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  3543. }
  3544. }
  3545. if (!running2)
  3546. {
  3547. running2 = true;
  3548. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  3549. if (awaiter2.IsCompleted)
  3550. {
  3551. Completed2(this);
  3552. }
  3553. else
  3554. {
  3555. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  3556. }
  3557. }
  3558. if (!running3)
  3559. {
  3560. running3 = true;
  3561. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  3562. if (awaiter3.IsCompleted)
  3563. {
  3564. Completed3(this);
  3565. }
  3566. else
  3567. {
  3568. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  3569. }
  3570. }
  3571. if (!running4)
  3572. {
  3573. running4 = true;
  3574. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  3575. if (awaiter4.IsCompleted)
  3576. {
  3577. Completed4(this);
  3578. }
  3579. else
  3580. {
  3581. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  3582. }
  3583. }
  3584. if (!running5)
  3585. {
  3586. running5 = true;
  3587. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  3588. if (awaiter5.IsCompleted)
  3589. {
  3590. Completed5(this);
  3591. }
  3592. else
  3593. {
  3594. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  3595. }
  3596. }
  3597. if (!running6)
  3598. {
  3599. running6 = true;
  3600. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  3601. if (awaiter6.IsCompleted)
  3602. {
  3603. Completed6(this);
  3604. }
  3605. else
  3606. {
  3607. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  3608. }
  3609. }
  3610. if (!running7)
  3611. {
  3612. running7 = true;
  3613. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  3614. if (awaiter7.IsCompleted)
  3615. {
  3616. Completed7(this);
  3617. }
  3618. else
  3619. {
  3620. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  3621. }
  3622. }
  3623. if (!running8)
  3624. {
  3625. running8 = true;
  3626. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  3627. if (awaiter8.IsCompleted)
  3628. {
  3629. Completed8(this);
  3630. }
  3631. else
  3632. {
  3633. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  3634. }
  3635. }
  3636. if (!running9)
  3637. {
  3638. running9 = true;
  3639. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  3640. if (awaiter9.IsCompleted)
  3641. {
  3642. Completed9(this);
  3643. }
  3644. else
  3645. {
  3646. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  3647. }
  3648. }
  3649. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9)
  3650. {
  3651. goto AGAIN;
  3652. }
  3653. syncRunning = false;
  3654. return new UniTask<bool>(this, completionSource.Version);
  3655. }
  3656. static void Completed1(object state)
  3657. {
  3658. var self = (_CombineLatest)state;
  3659. self.running1 = false;
  3660. try
  3661. {
  3662. if (self.awaiter1.GetResult())
  3663. {
  3664. self.hasCurrent1 = true;
  3665. self.current1 = self.enumerator1.Current;
  3666. goto SUCCESS;
  3667. }
  3668. else
  3669. {
  3670. self.running1 = true; // as complete, no more call MoveNextAsync.
  3671. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3672. {
  3673. goto COMPLETE;
  3674. }
  3675. return;
  3676. }
  3677. }
  3678. catch (Exception ex)
  3679. {
  3680. self.running1 = true; // as complete, no more call MoveNextAsync.
  3681. self.completedCount = CompleteCount;
  3682. self.completionSource.TrySetException(ex);
  3683. return;
  3684. }
  3685. SUCCESS:
  3686. if (!self.TrySetResult())
  3687. {
  3688. if (self.syncRunning) return;
  3689. self.running1 = true; // as complete, no more call MoveNextAsync.
  3690. try
  3691. {
  3692. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  3693. }
  3694. catch (Exception ex)
  3695. {
  3696. self.completedCount = CompleteCount;
  3697. self.completionSource.TrySetException(ex);
  3698. return;
  3699. }
  3700. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  3701. }
  3702. return;
  3703. COMPLETE:
  3704. self.completionSource.TrySetResult(false);
  3705. return;
  3706. }
  3707. static void Completed2(object state)
  3708. {
  3709. var self = (_CombineLatest)state;
  3710. self.running2 = false;
  3711. try
  3712. {
  3713. if (self.awaiter2.GetResult())
  3714. {
  3715. self.hasCurrent2 = true;
  3716. self.current2 = self.enumerator2.Current;
  3717. goto SUCCESS;
  3718. }
  3719. else
  3720. {
  3721. self.running2 = true; // as complete, no more call MoveNextAsync.
  3722. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3723. {
  3724. goto COMPLETE;
  3725. }
  3726. return;
  3727. }
  3728. }
  3729. catch (Exception ex)
  3730. {
  3731. self.running2 = true; // as complete, no more call MoveNextAsync.
  3732. self.completedCount = CompleteCount;
  3733. self.completionSource.TrySetException(ex);
  3734. return;
  3735. }
  3736. SUCCESS:
  3737. if (!self.TrySetResult())
  3738. {
  3739. if (self.syncRunning) return;
  3740. self.running2 = true; // as complete, no more call MoveNextAsync.
  3741. try
  3742. {
  3743. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  3744. }
  3745. catch (Exception ex)
  3746. {
  3747. self.completedCount = CompleteCount;
  3748. self.completionSource.TrySetException(ex);
  3749. return;
  3750. }
  3751. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  3752. }
  3753. return;
  3754. COMPLETE:
  3755. self.completionSource.TrySetResult(false);
  3756. return;
  3757. }
  3758. static void Completed3(object state)
  3759. {
  3760. var self = (_CombineLatest)state;
  3761. self.running3 = false;
  3762. try
  3763. {
  3764. if (self.awaiter3.GetResult())
  3765. {
  3766. self.hasCurrent3 = true;
  3767. self.current3 = self.enumerator3.Current;
  3768. goto SUCCESS;
  3769. }
  3770. else
  3771. {
  3772. self.running3 = true; // as complete, no more call MoveNextAsync.
  3773. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3774. {
  3775. goto COMPLETE;
  3776. }
  3777. return;
  3778. }
  3779. }
  3780. catch (Exception ex)
  3781. {
  3782. self.running3 = true; // as complete, no more call MoveNextAsync.
  3783. self.completedCount = CompleteCount;
  3784. self.completionSource.TrySetException(ex);
  3785. return;
  3786. }
  3787. SUCCESS:
  3788. if (!self.TrySetResult())
  3789. {
  3790. if (self.syncRunning) return;
  3791. self.running3 = true; // as complete, no more call MoveNextAsync.
  3792. try
  3793. {
  3794. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  3795. }
  3796. catch (Exception ex)
  3797. {
  3798. self.completedCount = CompleteCount;
  3799. self.completionSource.TrySetException(ex);
  3800. return;
  3801. }
  3802. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  3803. }
  3804. return;
  3805. COMPLETE:
  3806. self.completionSource.TrySetResult(false);
  3807. return;
  3808. }
  3809. static void Completed4(object state)
  3810. {
  3811. var self = (_CombineLatest)state;
  3812. self.running4 = false;
  3813. try
  3814. {
  3815. if (self.awaiter4.GetResult())
  3816. {
  3817. self.hasCurrent4 = true;
  3818. self.current4 = self.enumerator4.Current;
  3819. goto SUCCESS;
  3820. }
  3821. else
  3822. {
  3823. self.running4 = true; // as complete, no more call MoveNextAsync.
  3824. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3825. {
  3826. goto COMPLETE;
  3827. }
  3828. return;
  3829. }
  3830. }
  3831. catch (Exception ex)
  3832. {
  3833. self.running4 = true; // as complete, no more call MoveNextAsync.
  3834. self.completedCount = CompleteCount;
  3835. self.completionSource.TrySetException(ex);
  3836. return;
  3837. }
  3838. SUCCESS:
  3839. if (!self.TrySetResult())
  3840. {
  3841. if (self.syncRunning) return;
  3842. self.running4 = true; // as complete, no more call MoveNextAsync.
  3843. try
  3844. {
  3845. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  3846. }
  3847. catch (Exception ex)
  3848. {
  3849. self.completedCount = CompleteCount;
  3850. self.completionSource.TrySetException(ex);
  3851. return;
  3852. }
  3853. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  3854. }
  3855. return;
  3856. COMPLETE:
  3857. self.completionSource.TrySetResult(false);
  3858. return;
  3859. }
  3860. static void Completed5(object state)
  3861. {
  3862. var self = (_CombineLatest)state;
  3863. self.running5 = false;
  3864. try
  3865. {
  3866. if (self.awaiter5.GetResult())
  3867. {
  3868. self.hasCurrent5 = true;
  3869. self.current5 = self.enumerator5.Current;
  3870. goto SUCCESS;
  3871. }
  3872. else
  3873. {
  3874. self.running5 = true; // as complete, no more call MoveNextAsync.
  3875. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3876. {
  3877. goto COMPLETE;
  3878. }
  3879. return;
  3880. }
  3881. }
  3882. catch (Exception ex)
  3883. {
  3884. self.running5 = true; // as complete, no more call MoveNextAsync.
  3885. self.completedCount = CompleteCount;
  3886. self.completionSource.TrySetException(ex);
  3887. return;
  3888. }
  3889. SUCCESS:
  3890. if (!self.TrySetResult())
  3891. {
  3892. if (self.syncRunning) return;
  3893. self.running5 = true; // as complete, no more call MoveNextAsync.
  3894. try
  3895. {
  3896. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  3897. }
  3898. catch (Exception ex)
  3899. {
  3900. self.completedCount = CompleteCount;
  3901. self.completionSource.TrySetException(ex);
  3902. return;
  3903. }
  3904. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  3905. }
  3906. return;
  3907. COMPLETE:
  3908. self.completionSource.TrySetResult(false);
  3909. return;
  3910. }
  3911. static void Completed6(object state)
  3912. {
  3913. var self = (_CombineLatest)state;
  3914. self.running6 = false;
  3915. try
  3916. {
  3917. if (self.awaiter6.GetResult())
  3918. {
  3919. self.hasCurrent6 = true;
  3920. self.current6 = self.enumerator6.Current;
  3921. goto SUCCESS;
  3922. }
  3923. else
  3924. {
  3925. self.running6 = true; // as complete, no more call MoveNextAsync.
  3926. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3927. {
  3928. goto COMPLETE;
  3929. }
  3930. return;
  3931. }
  3932. }
  3933. catch (Exception ex)
  3934. {
  3935. self.running6 = true; // as complete, no more call MoveNextAsync.
  3936. self.completedCount = CompleteCount;
  3937. self.completionSource.TrySetException(ex);
  3938. return;
  3939. }
  3940. SUCCESS:
  3941. if (!self.TrySetResult())
  3942. {
  3943. if (self.syncRunning) return;
  3944. self.running6 = true; // as complete, no more call MoveNextAsync.
  3945. try
  3946. {
  3947. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  3948. }
  3949. catch (Exception ex)
  3950. {
  3951. self.completedCount = CompleteCount;
  3952. self.completionSource.TrySetException(ex);
  3953. return;
  3954. }
  3955. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  3956. }
  3957. return;
  3958. COMPLETE:
  3959. self.completionSource.TrySetResult(false);
  3960. return;
  3961. }
  3962. static void Completed7(object state)
  3963. {
  3964. var self = (_CombineLatest)state;
  3965. self.running7 = false;
  3966. try
  3967. {
  3968. if (self.awaiter7.GetResult())
  3969. {
  3970. self.hasCurrent7 = true;
  3971. self.current7 = self.enumerator7.Current;
  3972. goto SUCCESS;
  3973. }
  3974. else
  3975. {
  3976. self.running7 = true; // as complete, no more call MoveNextAsync.
  3977. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  3978. {
  3979. goto COMPLETE;
  3980. }
  3981. return;
  3982. }
  3983. }
  3984. catch (Exception ex)
  3985. {
  3986. self.running7 = true; // as complete, no more call MoveNextAsync.
  3987. self.completedCount = CompleteCount;
  3988. self.completionSource.TrySetException(ex);
  3989. return;
  3990. }
  3991. SUCCESS:
  3992. if (!self.TrySetResult())
  3993. {
  3994. if (self.syncRunning) return;
  3995. self.running7 = true; // as complete, no more call MoveNextAsync.
  3996. try
  3997. {
  3998. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  3999. }
  4000. catch (Exception ex)
  4001. {
  4002. self.completedCount = CompleteCount;
  4003. self.completionSource.TrySetException(ex);
  4004. return;
  4005. }
  4006. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  4007. }
  4008. return;
  4009. COMPLETE:
  4010. self.completionSource.TrySetResult(false);
  4011. return;
  4012. }
  4013. static void Completed8(object state)
  4014. {
  4015. var self = (_CombineLatest)state;
  4016. self.running8 = false;
  4017. try
  4018. {
  4019. if (self.awaiter8.GetResult())
  4020. {
  4021. self.hasCurrent8 = true;
  4022. self.current8 = self.enumerator8.Current;
  4023. goto SUCCESS;
  4024. }
  4025. else
  4026. {
  4027. self.running8 = true; // as complete, no more call MoveNextAsync.
  4028. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4029. {
  4030. goto COMPLETE;
  4031. }
  4032. return;
  4033. }
  4034. }
  4035. catch (Exception ex)
  4036. {
  4037. self.running8 = true; // as complete, no more call MoveNextAsync.
  4038. self.completedCount = CompleteCount;
  4039. self.completionSource.TrySetException(ex);
  4040. return;
  4041. }
  4042. SUCCESS:
  4043. if (!self.TrySetResult())
  4044. {
  4045. if (self.syncRunning) return;
  4046. self.running8 = true; // as complete, no more call MoveNextAsync.
  4047. try
  4048. {
  4049. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  4050. }
  4051. catch (Exception ex)
  4052. {
  4053. self.completedCount = CompleteCount;
  4054. self.completionSource.TrySetException(ex);
  4055. return;
  4056. }
  4057. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  4058. }
  4059. return;
  4060. COMPLETE:
  4061. self.completionSource.TrySetResult(false);
  4062. return;
  4063. }
  4064. static void Completed9(object state)
  4065. {
  4066. var self = (_CombineLatest)state;
  4067. self.running9 = false;
  4068. try
  4069. {
  4070. if (self.awaiter9.GetResult())
  4071. {
  4072. self.hasCurrent9 = true;
  4073. self.current9 = self.enumerator9.Current;
  4074. goto SUCCESS;
  4075. }
  4076. else
  4077. {
  4078. self.running9 = true; // as complete, no more call MoveNextAsync.
  4079. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4080. {
  4081. goto COMPLETE;
  4082. }
  4083. return;
  4084. }
  4085. }
  4086. catch (Exception ex)
  4087. {
  4088. self.running9 = true; // as complete, no more call MoveNextAsync.
  4089. self.completedCount = CompleteCount;
  4090. self.completionSource.TrySetException(ex);
  4091. return;
  4092. }
  4093. SUCCESS:
  4094. if (!self.TrySetResult())
  4095. {
  4096. if (self.syncRunning) return;
  4097. self.running9 = true; // as complete, no more call MoveNextAsync.
  4098. try
  4099. {
  4100. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  4101. }
  4102. catch (Exception ex)
  4103. {
  4104. self.completedCount = CompleteCount;
  4105. self.completionSource.TrySetException(ex);
  4106. return;
  4107. }
  4108. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  4109. }
  4110. return;
  4111. COMPLETE:
  4112. self.completionSource.TrySetResult(false);
  4113. return;
  4114. }
  4115. bool TrySetResult()
  4116. {
  4117. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9)
  4118. {
  4119. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9);
  4120. completionSource.TrySetResult(true);
  4121. return true;
  4122. }
  4123. else
  4124. {
  4125. return false;
  4126. }
  4127. }
  4128. public async UniTask DisposeAsync()
  4129. {
  4130. TaskTracker.RemoveTracking(this);
  4131. if (enumerator1 != null)
  4132. {
  4133. await enumerator1.DisposeAsync();
  4134. }
  4135. if (enumerator2 != null)
  4136. {
  4137. await enumerator2.DisposeAsync();
  4138. }
  4139. if (enumerator3 != null)
  4140. {
  4141. await enumerator3.DisposeAsync();
  4142. }
  4143. if (enumerator4 != null)
  4144. {
  4145. await enumerator4.DisposeAsync();
  4146. }
  4147. if (enumerator5 != null)
  4148. {
  4149. await enumerator5.DisposeAsync();
  4150. }
  4151. if (enumerator6 != null)
  4152. {
  4153. await enumerator6.DisposeAsync();
  4154. }
  4155. if (enumerator7 != null)
  4156. {
  4157. await enumerator7.DisposeAsync();
  4158. }
  4159. if (enumerator8 != null)
  4160. {
  4161. await enumerator8.DisposeAsync();
  4162. }
  4163. if (enumerator9 != null)
  4164. {
  4165. await enumerator9.DisposeAsync();
  4166. }
  4167. }
  4168. }
  4169. }
  4170. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> : IUniTaskAsyncEnumerable<TResult>
  4171. {
  4172. readonly IUniTaskAsyncEnumerable<T1> source1;
  4173. readonly IUniTaskAsyncEnumerable<T2> source2;
  4174. readonly IUniTaskAsyncEnumerable<T3> source3;
  4175. readonly IUniTaskAsyncEnumerable<T4> source4;
  4176. readonly IUniTaskAsyncEnumerable<T5> source5;
  4177. readonly IUniTaskAsyncEnumerable<T6> source6;
  4178. readonly IUniTaskAsyncEnumerable<T7> source7;
  4179. readonly IUniTaskAsyncEnumerable<T8> source8;
  4180. readonly IUniTaskAsyncEnumerable<T9> source9;
  4181. readonly IUniTaskAsyncEnumerable<T10> source10;
  4182. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> resultSelector;
  4183. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> resultSelector)
  4184. {
  4185. this.source1 = source1;
  4186. this.source2 = source2;
  4187. this.source3 = source3;
  4188. this.source4 = source4;
  4189. this.source5 = source5;
  4190. this.source6 = source6;
  4191. this.source7 = source7;
  4192. this.source8 = source8;
  4193. this.source9 = source9;
  4194. this.source10 = source10;
  4195. this.resultSelector = resultSelector;
  4196. }
  4197. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  4198. {
  4199. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, resultSelector, cancellationToken);
  4200. }
  4201. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  4202. {
  4203. static readonly Action<object> Completed1Delegate = Completed1;
  4204. static readonly Action<object> Completed2Delegate = Completed2;
  4205. static readonly Action<object> Completed3Delegate = Completed3;
  4206. static readonly Action<object> Completed4Delegate = Completed4;
  4207. static readonly Action<object> Completed5Delegate = Completed5;
  4208. static readonly Action<object> Completed6Delegate = Completed6;
  4209. static readonly Action<object> Completed7Delegate = Completed7;
  4210. static readonly Action<object> Completed8Delegate = Completed8;
  4211. static readonly Action<object> Completed9Delegate = Completed9;
  4212. static readonly Action<object> Completed10Delegate = Completed10;
  4213. const int CompleteCount = 10;
  4214. readonly IUniTaskAsyncEnumerable<T1> source1;
  4215. readonly IUniTaskAsyncEnumerable<T2> source2;
  4216. readonly IUniTaskAsyncEnumerable<T3> source3;
  4217. readonly IUniTaskAsyncEnumerable<T4> source4;
  4218. readonly IUniTaskAsyncEnumerable<T5> source5;
  4219. readonly IUniTaskAsyncEnumerable<T6> source6;
  4220. readonly IUniTaskAsyncEnumerable<T7> source7;
  4221. readonly IUniTaskAsyncEnumerable<T8> source8;
  4222. readonly IUniTaskAsyncEnumerable<T9> source9;
  4223. readonly IUniTaskAsyncEnumerable<T10> source10;
  4224. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> resultSelector;
  4225. CancellationToken cancellationToken;
  4226. IUniTaskAsyncEnumerator<T1> enumerator1;
  4227. UniTask<bool>.Awaiter awaiter1;
  4228. bool hasCurrent1;
  4229. bool running1;
  4230. T1 current1;
  4231. IUniTaskAsyncEnumerator<T2> enumerator2;
  4232. UniTask<bool>.Awaiter awaiter2;
  4233. bool hasCurrent2;
  4234. bool running2;
  4235. T2 current2;
  4236. IUniTaskAsyncEnumerator<T3> enumerator3;
  4237. UniTask<bool>.Awaiter awaiter3;
  4238. bool hasCurrent3;
  4239. bool running3;
  4240. T3 current3;
  4241. IUniTaskAsyncEnumerator<T4> enumerator4;
  4242. UniTask<bool>.Awaiter awaiter4;
  4243. bool hasCurrent4;
  4244. bool running4;
  4245. T4 current4;
  4246. IUniTaskAsyncEnumerator<T5> enumerator5;
  4247. UniTask<bool>.Awaiter awaiter5;
  4248. bool hasCurrent5;
  4249. bool running5;
  4250. T5 current5;
  4251. IUniTaskAsyncEnumerator<T6> enumerator6;
  4252. UniTask<bool>.Awaiter awaiter6;
  4253. bool hasCurrent6;
  4254. bool running6;
  4255. T6 current6;
  4256. IUniTaskAsyncEnumerator<T7> enumerator7;
  4257. UniTask<bool>.Awaiter awaiter7;
  4258. bool hasCurrent7;
  4259. bool running7;
  4260. T7 current7;
  4261. IUniTaskAsyncEnumerator<T8> enumerator8;
  4262. UniTask<bool>.Awaiter awaiter8;
  4263. bool hasCurrent8;
  4264. bool running8;
  4265. T8 current8;
  4266. IUniTaskAsyncEnumerator<T9> enumerator9;
  4267. UniTask<bool>.Awaiter awaiter9;
  4268. bool hasCurrent9;
  4269. bool running9;
  4270. T9 current9;
  4271. IUniTaskAsyncEnumerator<T10> enumerator10;
  4272. UniTask<bool>.Awaiter awaiter10;
  4273. bool hasCurrent10;
  4274. bool running10;
  4275. T10 current10;
  4276. int completedCount;
  4277. bool syncRunning;
  4278. TResult result;
  4279. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> resultSelector, CancellationToken cancellationToken)
  4280. {
  4281. this.source1 = source1;
  4282. this.source2 = source2;
  4283. this.source3 = source3;
  4284. this.source4 = source4;
  4285. this.source5 = source5;
  4286. this.source6 = source6;
  4287. this.source7 = source7;
  4288. this.source8 = source8;
  4289. this.source9 = source9;
  4290. this.source10 = source10;
  4291. this.resultSelector = resultSelector;
  4292. this.cancellationToken = cancellationToken;
  4293. TaskTracker.TrackActiveTask(this, 3);
  4294. }
  4295. public TResult Current => result;
  4296. public UniTask<bool> MoveNextAsync()
  4297. {
  4298. cancellationToken.ThrowIfCancellationRequested();
  4299. if (completedCount == CompleteCount) return CompletedTasks.False;
  4300. if (enumerator1 == null)
  4301. {
  4302. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  4303. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  4304. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  4305. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  4306. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  4307. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  4308. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  4309. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  4310. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  4311. enumerator10 = source10.GetAsyncEnumerator(cancellationToken);
  4312. }
  4313. completionSource.Reset();
  4314. AGAIN:
  4315. syncRunning = true;
  4316. if (!running1)
  4317. {
  4318. running1 = true;
  4319. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  4320. if (awaiter1.IsCompleted)
  4321. {
  4322. Completed1(this);
  4323. }
  4324. else
  4325. {
  4326. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  4327. }
  4328. }
  4329. if (!running2)
  4330. {
  4331. running2 = true;
  4332. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  4333. if (awaiter2.IsCompleted)
  4334. {
  4335. Completed2(this);
  4336. }
  4337. else
  4338. {
  4339. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  4340. }
  4341. }
  4342. if (!running3)
  4343. {
  4344. running3 = true;
  4345. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  4346. if (awaiter3.IsCompleted)
  4347. {
  4348. Completed3(this);
  4349. }
  4350. else
  4351. {
  4352. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  4353. }
  4354. }
  4355. if (!running4)
  4356. {
  4357. running4 = true;
  4358. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  4359. if (awaiter4.IsCompleted)
  4360. {
  4361. Completed4(this);
  4362. }
  4363. else
  4364. {
  4365. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  4366. }
  4367. }
  4368. if (!running5)
  4369. {
  4370. running5 = true;
  4371. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  4372. if (awaiter5.IsCompleted)
  4373. {
  4374. Completed5(this);
  4375. }
  4376. else
  4377. {
  4378. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  4379. }
  4380. }
  4381. if (!running6)
  4382. {
  4383. running6 = true;
  4384. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  4385. if (awaiter6.IsCompleted)
  4386. {
  4387. Completed6(this);
  4388. }
  4389. else
  4390. {
  4391. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  4392. }
  4393. }
  4394. if (!running7)
  4395. {
  4396. running7 = true;
  4397. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  4398. if (awaiter7.IsCompleted)
  4399. {
  4400. Completed7(this);
  4401. }
  4402. else
  4403. {
  4404. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  4405. }
  4406. }
  4407. if (!running8)
  4408. {
  4409. running8 = true;
  4410. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  4411. if (awaiter8.IsCompleted)
  4412. {
  4413. Completed8(this);
  4414. }
  4415. else
  4416. {
  4417. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  4418. }
  4419. }
  4420. if (!running9)
  4421. {
  4422. running9 = true;
  4423. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  4424. if (awaiter9.IsCompleted)
  4425. {
  4426. Completed9(this);
  4427. }
  4428. else
  4429. {
  4430. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  4431. }
  4432. }
  4433. if (!running10)
  4434. {
  4435. running10 = true;
  4436. awaiter10 = enumerator10.MoveNextAsync().GetAwaiter();
  4437. if (awaiter10.IsCompleted)
  4438. {
  4439. Completed10(this);
  4440. }
  4441. else
  4442. {
  4443. awaiter10.SourceOnCompleted(Completed10Delegate, this);
  4444. }
  4445. }
  4446. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10)
  4447. {
  4448. goto AGAIN;
  4449. }
  4450. syncRunning = false;
  4451. return new UniTask<bool>(this, completionSource.Version);
  4452. }
  4453. static void Completed1(object state)
  4454. {
  4455. var self = (_CombineLatest)state;
  4456. self.running1 = false;
  4457. try
  4458. {
  4459. if (self.awaiter1.GetResult())
  4460. {
  4461. self.hasCurrent1 = true;
  4462. self.current1 = self.enumerator1.Current;
  4463. goto SUCCESS;
  4464. }
  4465. else
  4466. {
  4467. self.running1 = true; // as complete, no more call MoveNextAsync.
  4468. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4469. {
  4470. goto COMPLETE;
  4471. }
  4472. return;
  4473. }
  4474. }
  4475. catch (Exception ex)
  4476. {
  4477. self.running1 = true; // as complete, no more call MoveNextAsync.
  4478. self.completedCount = CompleteCount;
  4479. self.completionSource.TrySetException(ex);
  4480. return;
  4481. }
  4482. SUCCESS:
  4483. if (!self.TrySetResult())
  4484. {
  4485. if (self.syncRunning) return;
  4486. self.running1 = true; // as complete, no more call MoveNextAsync.
  4487. try
  4488. {
  4489. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  4490. }
  4491. catch (Exception ex)
  4492. {
  4493. self.completedCount = CompleteCount;
  4494. self.completionSource.TrySetException(ex);
  4495. return;
  4496. }
  4497. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  4498. }
  4499. return;
  4500. COMPLETE:
  4501. self.completionSource.TrySetResult(false);
  4502. return;
  4503. }
  4504. static void Completed2(object state)
  4505. {
  4506. var self = (_CombineLatest)state;
  4507. self.running2 = false;
  4508. try
  4509. {
  4510. if (self.awaiter2.GetResult())
  4511. {
  4512. self.hasCurrent2 = true;
  4513. self.current2 = self.enumerator2.Current;
  4514. goto SUCCESS;
  4515. }
  4516. else
  4517. {
  4518. self.running2 = true; // as complete, no more call MoveNextAsync.
  4519. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4520. {
  4521. goto COMPLETE;
  4522. }
  4523. return;
  4524. }
  4525. }
  4526. catch (Exception ex)
  4527. {
  4528. self.running2 = true; // as complete, no more call MoveNextAsync.
  4529. self.completedCount = CompleteCount;
  4530. self.completionSource.TrySetException(ex);
  4531. return;
  4532. }
  4533. SUCCESS:
  4534. if (!self.TrySetResult())
  4535. {
  4536. if (self.syncRunning) return;
  4537. self.running2 = true; // as complete, no more call MoveNextAsync.
  4538. try
  4539. {
  4540. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  4541. }
  4542. catch (Exception ex)
  4543. {
  4544. self.completedCount = CompleteCount;
  4545. self.completionSource.TrySetException(ex);
  4546. return;
  4547. }
  4548. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  4549. }
  4550. return;
  4551. COMPLETE:
  4552. self.completionSource.TrySetResult(false);
  4553. return;
  4554. }
  4555. static void Completed3(object state)
  4556. {
  4557. var self = (_CombineLatest)state;
  4558. self.running3 = false;
  4559. try
  4560. {
  4561. if (self.awaiter3.GetResult())
  4562. {
  4563. self.hasCurrent3 = true;
  4564. self.current3 = self.enumerator3.Current;
  4565. goto SUCCESS;
  4566. }
  4567. else
  4568. {
  4569. self.running3 = true; // as complete, no more call MoveNextAsync.
  4570. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4571. {
  4572. goto COMPLETE;
  4573. }
  4574. return;
  4575. }
  4576. }
  4577. catch (Exception ex)
  4578. {
  4579. self.running3 = true; // as complete, no more call MoveNextAsync.
  4580. self.completedCount = CompleteCount;
  4581. self.completionSource.TrySetException(ex);
  4582. return;
  4583. }
  4584. SUCCESS:
  4585. if (!self.TrySetResult())
  4586. {
  4587. if (self.syncRunning) return;
  4588. self.running3 = true; // as complete, no more call MoveNextAsync.
  4589. try
  4590. {
  4591. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  4592. }
  4593. catch (Exception ex)
  4594. {
  4595. self.completedCount = CompleteCount;
  4596. self.completionSource.TrySetException(ex);
  4597. return;
  4598. }
  4599. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  4600. }
  4601. return;
  4602. COMPLETE:
  4603. self.completionSource.TrySetResult(false);
  4604. return;
  4605. }
  4606. static void Completed4(object state)
  4607. {
  4608. var self = (_CombineLatest)state;
  4609. self.running4 = false;
  4610. try
  4611. {
  4612. if (self.awaiter4.GetResult())
  4613. {
  4614. self.hasCurrent4 = true;
  4615. self.current4 = self.enumerator4.Current;
  4616. goto SUCCESS;
  4617. }
  4618. else
  4619. {
  4620. self.running4 = true; // as complete, no more call MoveNextAsync.
  4621. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4622. {
  4623. goto COMPLETE;
  4624. }
  4625. return;
  4626. }
  4627. }
  4628. catch (Exception ex)
  4629. {
  4630. self.running4 = true; // as complete, no more call MoveNextAsync.
  4631. self.completedCount = CompleteCount;
  4632. self.completionSource.TrySetException(ex);
  4633. return;
  4634. }
  4635. SUCCESS:
  4636. if (!self.TrySetResult())
  4637. {
  4638. if (self.syncRunning) return;
  4639. self.running4 = true; // as complete, no more call MoveNextAsync.
  4640. try
  4641. {
  4642. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  4643. }
  4644. catch (Exception ex)
  4645. {
  4646. self.completedCount = CompleteCount;
  4647. self.completionSource.TrySetException(ex);
  4648. return;
  4649. }
  4650. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  4651. }
  4652. return;
  4653. COMPLETE:
  4654. self.completionSource.TrySetResult(false);
  4655. return;
  4656. }
  4657. static void Completed5(object state)
  4658. {
  4659. var self = (_CombineLatest)state;
  4660. self.running5 = false;
  4661. try
  4662. {
  4663. if (self.awaiter5.GetResult())
  4664. {
  4665. self.hasCurrent5 = true;
  4666. self.current5 = self.enumerator5.Current;
  4667. goto SUCCESS;
  4668. }
  4669. else
  4670. {
  4671. self.running5 = true; // as complete, no more call MoveNextAsync.
  4672. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4673. {
  4674. goto COMPLETE;
  4675. }
  4676. return;
  4677. }
  4678. }
  4679. catch (Exception ex)
  4680. {
  4681. self.running5 = true; // as complete, no more call MoveNextAsync.
  4682. self.completedCount = CompleteCount;
  4683. self.completionSource.TrySetException(ex);
  4684. return;
  4685. }
  4686. SUCCESS:
  4687. if (!self.TrySetResult())
  4688. {
  4689. if (self.syncRunning) return;
  4690. self.running5 = true; // as complete, no more call MoveNextAsync.
  4691. try
  4692. {
  4693. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  4694. }
  4695. catch (Exception ex)
  4696. {
  4697. self.completedCount = CompleteCount;
  4698. self.completionSource.TrySetException(ex);
  4699. return;
  4700. }
  4701. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  4702. }
  4703. return;
  4704. COMPLETE:
  4705. self.completionSource.TrySetResult(false);
  4706. return;
  4707. }
  4708. static void Completed6(object state)
  4709. {
  4710. var self = (_CombineLatest)state;
  4711. self.running6 = false;
  4712. try
  4713. {
  4714. if (self.awaiter6.GetResult())
  4715. {
  4716. self.hasCurrent6 = true;
  4717. self.current6 = self.enumerator6.Current;
  4718. goto SUCCESS;
  4719. }
  4720. else
  4721. {
  4722. self.running6 = true; // as complete, no more call MoveNextAsync.
  4723. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4724. {
  4725. goto COMPLETE;
  4726. }
  4727. return;
  4728. }
  4729. }
  4730. catch (Exception ex)
  4731. {
  4732. self.running6 = true; // as complete, no more call MoveNextAsync.
  4733. self.completedCount = CompleteCount;
  4734. self.completionSource.TrySetException(ex);
  4735. return;
  4736. }
  4737. SUCCESS:
  4738. if (!self.TrySetResult())
  4739. {
  4740. if (self.syncRunning) return;
  4741. self.running6 = true; // as complete, no more call MoveNextAsync.
  4742. try
  4743. {
  4744. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  4745. }
  4746. catch (Exception ex)
  4747. {
  4748. self.completedCount = CompleteCount;
  4749. self.completionSource.TrySetException(ex);
  4750. return;
  4751. }
  4752. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  4753. }
  4754. return;
  4755. COMPLETE:
  4756. self.completionSource.TrySetResult(false);
  4757. return;
  4758. }
  4759. static void Completed7(object state)
  4760. {
  4761. var self = (_CombineLatest)state;
  4762. self.running7 = false;
  4763. try
  4764. {
  4765. if (self.awaiter7.GetResult())
  4766. {
  4767. self.hasCurrent7 = true;
  4768. self.current7 = self.enumerator7.Current;
  4769. goto SUCCESS;
  4770. }
  4771. else
  4772. {
  4773. self.running7 = true; // as complete, no more call MoveNextAsync.
  4774. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4775. {
  4776. goto COMPLETE;
  4777. }
  4778. return;
  4779. }
  4780. }
  4781. catch (Exception ex)
  4782. {
  4783. self.running7 = true; // as complete, no more call MoveNextAsync.
  4784. self.completedCount = CompleteCount;
  4785. self.completionSource.TrySetException(ex);
  4786. return;
  4787. }
  4788. SUCCESS:
  4789. if (!self.TrySetResult())
  4790. {
  4791. if (self.syncRunning) return;
  4792. self.running7 = true; // as complete, no more call MoveNextAsync.
  4793. try
  4794. {
  4795. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  4796. }
  4797. catch (Exception ex)
  4798. {
  4799. self.completedCount = CompleteCount;
  4800. self.completionSource.TrySetException(ex);
  4801. return;
  4802. }
  4803. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  4804. }
  4805. return;
  4806. COMPLETE:
  4807. self.completionSource.TrySetResult(false);
  4808. return;
  4809. }
  4810. static void Completed8(object state)
  4811. {
  4812. var self = (_CombineLatest)state;
  4813. self.running8 = false;
  4814. try
  4815. {
  4816. if (self.awaiter8.GetResult())
  4817. {
  4818. self.hasCurrent8 = true;
  4819. self.current8 = self.enumerator8.Current;
  4820. goto SUCCESS;
  4821. }
  4822. else
  4823. {
  4824. self.running8 = true; // as complete, no more call MoveNextAsync.
  4825. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4826. {
  4827. goto COMPLETE;
  4828. }
  4829. return;
  4830. }
  4831. }
  4832. catch (Exception ex)
  4833. {
  4834. self.running8 = true; // as complete, no more call MoveNextAsync.
  4835. self.completedCount = CompleteCount;
  4836. self.completionSource.TrySetException(ex);
  4837. return;
  4838. }
  4839. SUCCESS:
  4840. if (!self.TrySetResult())
  4841. {
  4842. if (self.syncRunning) return;
  4843. self.running8 = true; // as complete, no more call MoveNextAsync.
  4844. try
  4845. {
  4846. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  4847. }
  4848. catch (Exception ex)
  4849. {
  4850. self.completedCount = CompleteCount;
  4851. self.completionSource.TrySetException(ex);
  4852. return;
  4853. }
  4854. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  4855. }
  4856. return;
  4857. COMPLETE:
  4858. self.completionSource.TrySetResult(false);
  4859. return;
  4860. }
  4861. static void Completed9(object state)
  4862. {
  4863. var self = (_CombineLatest)state;
  4864. self.running9 = false;
  4865. try
  4866. {
  4867. if (self.awaiter9.GetResult())
  4868. {
  4869. self.hasCurrent9 = true;
  4870. self.current9 = self.enumerator9.Current;
  4871. goto SUCCESS;
  4872. }
  4873. else
  4874. {
  4875. self.running9 = true; // as complete, no more call MoveNextAsync.
  4876. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4877. {
  4878. goto COMPLETE;
  4879. }
  4880. return;
  4881. }
  4882. }
  4883. catch (Exception ex)
  4884. {
  4885. self.running9 = true; // as complete, no more call MoveNextAsync.
  4886. self.completedCount = CompleteCount;
  4887. self.completionSource.TrySetException(ex);
  4888. return;
  4889. }
  4890. SUCCESS:
  4891. if (!self.TrySetResult())
  4892. {
  4893. if (self.syncRunning) return;
  4894. self.running9 = true; // as complete, no more call MoveNextAsync.
  4895. try
  4896. {
  4897. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  4898. }
  4899. catch (Exception ex)
  4900. {
  4901. self.completedCount = CompleteCount;
  4902. self.completionSource.TrySetException(ex);
  4903. return;
  4904. }
  4905. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  4906. }
  4907. return;
  4908. COMPLETE:
  4909. self.completionSource.TrySetResult(false);
  4910. return;
  4911. }
  4912. static void Completed10(object state)
  4913. {
  4914. var self = (_CombineLatest)state;
  4915. self.running10 = false;
  4916. try
  4917. {
  4918. if (self.awaiter10.GetResult())
  4919. {
  4920. self.hasCurrent10 = true;
  4921. self.current10 = self.enumerator10.Current;
  4922. goto SUCCESS;
  4923. }
  4924. else
  4925. {
  4926. self.running10 = true; // as complete, no more call MoveNextAsync.
  4927. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  4928. {
  4929. goto COMPLETE;
  4930. }
  4931. return;
  4932. }
  4933. }
  4934. catch (Exception ex)
  4935. {
  4936. self.running10 = true; // as complete, no more call MoveNextAsync.
  4937. self.completedCount = CompleteCount;
  4938. self.completionSource.TrySetException(ex);
  4939. return;
  4940. }
  4941. SUCCESS:
  4942. if (!self.TrySetResult())
  4943. {
  4944. if (self.syncRunning) return;
  4945. self.running10 = true; // as complete, no more call MoveNextAsync.
  4946. try
  4947. {
  4948. self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter();
  4949. }
  4950. catch (Exception ex)
  4951. {
  4952. self.completedCount = CompleteCount;
  4953. self.completionSource.TrySetException(ex);
  4954. return;
  4955. }
  4956. self.awaiter10.SourceOnCompleted(Completed10Delegate, self);
  4957. }
  4958. return;
  4959. COMPLETE:
  4960. self.completionSource.TrySetResult(false);
  4961. return;
  4962. }
  4963. bool TrySetResult()
  4964. {
  4965. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10)
  4966. {
  4967. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10);
  4968. completionSource.TrySetResult(true);
  4969. return true;
  4970. }
  4971. else
  4972. {
  4973. return false;
  4974. }
  4975. }
  4976. public async UniTask DisposeAsync()
  4977. {
  4978. TaskTracker.RemoveTracking(this);
  4979. if (enumerator1 != null)
  4980. {
  4981. await enumerator1.DisposeAsync();
  4982. }
  4983. if (enumerator2 != null)
  4984. {
  4985. await enumerator2.DisposeAsync();
  4986. }
  4987. if (enumerator3 != null)
  4988. {
  4989. await enumerator3.DisposeAsync();
  4990. }
  4991. if (enumerator4 != null)
  4992. {
  4993. await enumerator4.DisposeAsync();
  4994. }
  4995. if (enumerator5 != null)
  4996. {
  4997. await enumerator5.DisposeAsync();
  4998. }
  4999. if (enumerator6 != null)
  5000. {
  5001. await enumerator6.DisposeAsync();
  5002. }
  5003. if (enumerator7 != null)
  5004. {
  5005. await enumerator7.DisposeAsync();
  5006. }
  5007. if (enumerator8 != null)
  5008. {
  5009. await enumerator8.DisposeAsync();
  5010. }
  5011. if (enumerator9 != null)
  5012. {
  5013. await enumerator9.DisposeAsync();
  5014. }
  5015. if (enumerator10 != null)
  5016. {
  5017. await enumerator10.DisposeAsync();
  5018. }
  5019. }
  5020. }
  5021. }
  5022. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> : IUniTaskAsyncEnumerable<TResult>
  5023. {
  5024. readonly IUniTaskAsyncEnumerable<T1> source1;
  5025. readonly IUniTaskAsyncEnumerable<T2> source2;
  5026. readonly IUniTaskAsyncEnumerable<T3> source3;
  5027. readonly IUniTaskAsyncEnumerable<T4> source4;
  5028. readonly IUniTaskAsyncEnumerable<T5> source5;
  5029. readonly IUniTaskAsyncEnumerable<T6> source6;
  5030. readonly IUniTaskAsyncEnumerable<T7> source7;
  5031. readonly IUniTaskAsyncEnumerable<T8> source8;
  5032. readonly IUniTaskAsyncEnumerable<T9> source9;
  5033. readonly IUniTaskAsyncEnumerable<T10> source10;
  5034. readonly IUniTaskAsyncEnumerable<T11> source11;
  5035. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> resultSelector;
  5036. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> resultSelector)
  5037. {
  5038. this.source1 = source1;
  5039. this.source2 = source2;
  5040. this.source3 = source3;
  5041. this.source4 = source4;
  5042. this.source5 = source5;
  5043. this.source6 = source6;
  5044. this.source7 = source7;
  5045. this.source8 = source8;
  5046. this.source9 = source9;
  5047. this.source10 = source10;
  5048. this.source11 = source11;
  5049. this.resultSelector = resultSelector;
  5050. }
  5051. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  5052. {
  5053. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, resultSelector, cancellationToken);
  5054. }
  5055. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  5056. {
  5057. static readonly Action<object> Completed1Delegate = Completed1;
  5058. static readonly Action<object> Completed2Delegate = Completed2;
  5059. static readonly Action<object> Completed3Delegate = Completed3;
  5060. static readonly Action<object> Completed4Delegate = Completed4;
  5061. static readonly Action<object> Completed5Delegate = Completed5;
  5062. static readonly Action<object> Completed6Delegate = Completed6;
  5063. static readonly Action<object> Completed7Delegate = Completed7;
  5064. static readonly Action<object> Completed8Delegate = Completed8;
  5065. static readonly Action<object> Completed9Delegate = Completed9;
  5066. static readonly Action<object> Completed10Delegate = Completed10;
  5067. static readonly Action<object> Completed11Delegate = Completed11;
  5068. const int CompleteCount = 11;
  5069. readonly IUniTaskAsyncEnumerable<T1> source1;
  5070. readonly IUniTaskAsyncEnumerable<T2> source2;
  5071. readonly IUniTaskAsyncEnumerable<T3> source3;
  5072. readonly IUniTaskAsyncEnumerable<T4> source4;
  5073. readonly IUniTaskAsyncEnumerable<T5> source5;
  5074. readonly IUniTaskAsyncEnumerable<T6> source6;
  5075. readonly IUniTaskAsyncEnumerable<T7> source7;
  5076. readonly IUniTaskAsyncEnumerable<T8> source8;
  5077. readonly IUniTaskAsyncEnumerable<T9> source9;
  5078. readonly IUniTaskAsyncEnumerable<T10> source10;
  5079. readonly IUniTaskAsyncEnumerable<T11> source11;
  5080. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> resultSelector;
  5081. CancellationToken cancellationToken;
  5082. IUniTaskAsyncEnumerator<T1> enumerator1;
  5083. UniTask<bool>.Awaiter awaiter1;
  5084. bool hasCurrent1;
  5085. bool running1;
  5086. T1 current1;
  5087. IUniTaskAsyncEnumerator<T2> enumerator2;
  5088. UniTask<bool>.Awaiter awaiter2;
  5089. bool hasCurrent2;
  5090. bool running2;
  5091. T2 current2;
  5092. IUniTaskAsyncEnumerator<T3> enumerator3;
  5093. UniTask<bool>.Awaiter awaiter3;
  5094. bool hasCurrent3;
  5095. bool running3;
  5096. T3 current3;
  5097. IUniTaskAsyncEnumerator<T4> enumerator4;
  5098. UniTask<bool>.Awaiter awaiter4;
  5099. bool hasCurrent4;
  5100. bool running4;
  5101. T4 current4;
  5102. IUniTaskAsyncEnumerator<T5> enumerator5;
  5103. UniTask<bool>.Awaiter awaiter5;
  5104. bool hasCurrent5;
  5105. bool running5;
  5106. T5 current5;
  5107. IUniTaskAsyncEnumerator<T6> enumerator6;
  5108. UniTask<bool>.Awaiter awaiter6;
  5109. bool hasCurrent6;
  5110. bool running6;
  5111. T6 current6;
  5112. IUniTaskAsyncEnumerator<T7> enumerator7;
  5113. UniTask<bool>.Awaiter awaiter7;
  5114. bool hasCurrent7;
  5115. bool running7;
  5116. T7 current7;
  5117. IUniTaskAsyncEnumerator<T8> enumerator8;
  5118. UniTask<bool>.Awaiter awaiter8;
  5119. bool hasCurrent8;
  5120. bool running8;
  5121. T8 current8;
  5122. IUniTaskAsyncEnumerator<T9> enumerator9;
  5123. UniTask<bool>.Awaiter awaiter9;
  5124. bool hasCurrent9;
  5125. bool running9;
  5126. T9 current9;
  5127. IUniTaskAsyncEnumerator<T10> enumerator10;
  5128. UniTask<bool>.Awaiter awaiter10;
  5129. bool hasCurrent10;
  5130. bool running10;
  5131. T10 current10;
  5132. IUniTaskAsyncEnumerator<T11> enumerator11;
  5133. UniTask<bool>.Awaiter awaiter11;
  5134. bool hasCurrent11;
  5135. bool running11;
  5136. T11 current11;
  5137. int completedCount;
  5138. bool syncRunning;
  5139. TResult result;
  5140. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> resultSelector, CancellationToken cancellationToken)
  5141. {
  5142. this.source1 = source1;
  5143. this.source2 = source2;
  5144. this.source3 = source3;
  5145. this.source4 = source4;
  5146. this.source5 = source5;
  5147. this.source6 = source6;
  5148. this.source7 = source7;
  5149. this.source8 = source8;
  5150. this.source9 = source9;
  5151. this.source10 = source10;
  5152. this.source11 = source11;
  5153. this.resultSelector = resultSelector;
  5154. this.cancellationToken = cancellationToken;
  5155. TaskTracker.TrackActiveTask(this, 3);
  5156. }
  5157. public TResult Current => result;
  5158. public UniTask<bool> MoveNextAsync()
  5159. {
  5160. cancellationToken.ThrowIfCancellationRequested();
  5161. if (completedCount == CompleteCount) return CompletedTasks.False;
  5162. if (enumerator1 == null)
  5163. {
  5164. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  5165. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  5166. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  5167. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  5168. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  5169. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  5170. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  5171. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  5172. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  5173. enumerator10 = source10.GetAsyncEnumerator(cancellationToken);
  5174. enumerator11 = source11.GetAsyncEnumerator(cancellationToken);
  5175. }
  5176. completionSource.Reset();
  5177. AGAIN:
  5178. syncRunning = true;
  5179. if (!running1)
  5180. {
  5181. running1 = true;
  5182. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  5183. if (awaiter1.IsCompleted)
  5184. {
  5185. Completed1(this);
  5186. }
  5187. else
  5188. {
  5189. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  5190. }
  5191. }
  5192. if (!running2)
  5193. {
  5194. running2 = true;
  5195. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  5196. if (awaiter2.IsCompleted)
  5197. {
  5198. Completed2(this);
  5199. }
  5200. else
  5201. {
  5202. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  5203. }
  5204. }
  5205. if (!running3)
  5206. {
  5207. running3 = true;
  5208. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  5209. if (awaiter3.IsCompleted)
  5210. {
  5211. Completed3(this);
  5212. }
  5213. else
  5214. {
  5215. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  5216. }
  5217. }
  5218. if (!running4)
  5219. {
  5220. running4 = true;
  5221. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  5222. if (awaiter4.IsCompleted)
  5223. {
  5224. Completed4(this);
  5225. }
  5226. else
  5227. {
  5228. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  5229. }
  5230. }
  5231. if (!running5)
  5232. {
  5233. running5 = true;
  5234. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  5235. if (awaiter5.IsCompleted)
  5236. {
  5237. Completed5(this);
  5238. }
  5239. else
  5240. {
  5241. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  5242. }
  5243. }
  5244. if (!running6)
  5245. {
  5246. running6 = true;
  5247. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  5248. if (awaiter6.IsCompleted)
  5249. {
  5250. Completed6(this);
  5251. }
  5252. else
  5253. {
  5254. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  5255. }
  5256. }
  5257. if (!running7)
  5258. {
  5259. running7 = true;
  5260. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  5261. if (awaiter7.IsCompleted)
  5262. {
  5263. Completed7(this);
  5264. }
  5265. else
  5266. {
  5267. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  5268. }
  5269. }
  5270. if (!running8)
  5271. {
  5272. running8 = true;
  5273. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  5274. if (awaiter8.IsCompleted)
  5275. {
  5276. Completed8(this);
  5277. }
  5278. else
  5279. {
  5280. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  5281. }
  5282. }
  5283. if (!running9)
  5284. {
  5285. running9 = true;
  5286. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  5287. if (awaiter9.IsCompleted)
  5288. {
  5289. Completed9(this);
  5290. }
  5291. else
  5292. {
  5293. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  5294. }
  5295. }
  5296. if (!running10)
  5297. {
  5298. running10 = true;
  5299. awaiter10 = enumerator10.MoveNextAsync().GetAwaiter();
  5300. if (awaiter10.IsCompleted)
  5301. {
  5302. Completed10(this);
  5303. }
  5304. else
  5305. {
  5306. awaiter10.SourceOnCompleted(Completed10Delegate, this);
  5307. }
  5308. }
  5309. if (!running11)
  5310. {
  5311. running11 = true;
  5312. awaiter11 = enumerator11.MoveNextAsync().GetAwaiter();
  5313. if (awaiter11.IsCompleted)
  5314. {
  5315. Completed11(this);
  5316. }
  5317. else
  5318. {
  5319. awaiter11.SourceOnCompleted(Completed11Delegate, this);
  5320. }
  5321. }
  5322. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11)
  5323. {
  5324. goto AGAIN;
  5325. }
  5326. syncRunning = false;
  5327. return new UniTask<bool>(this, completionSource.Version);
  5328. }
  5329. static void Completed1(object state)
  5330. {
  5331. var self = (_CombineLatest)state;
  5332. self.running1 = false;
  5333. try
  5334. {
  5335. if (self.awaiter1.GetResult())
  5336. {
  5337. self.hasCurrent1 = true;
  5338. self.current1 = self.enumerator1.Current;
  5339. goto SUCCESS;
  5340. }
  5341. else
  5342. {
  5343. self.running1 = true; // as complete, no more call MoveNextAsync.
  5344. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5345. {
  5346. goto COMPLETE;
  5347. }
  5348. return;
  5349. }
  5350. }
  5351. catch (Exception ex)
  5352. {
  5353. self.running1 = true; // as complete, no more call MoveNextAsync.
  5354. self.completedCount = CompleteCount;
  5355. self.completionSource.TrySetException(ex);
  5356. return;
  5357. }
  5358. SUCCESS:
  5359. if (!self.TrySetResult())
  5360. {
  5361. if (self.syncRunning) return;
  5362. self.running1 = true; // as complete, no more call MoveNextAsync.
  5363. try
  5364. {
  5365. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  5366. }
  5367. catch (Exception ex)
  5368. {
  5369. self.completedCount = CompleteCount;
  5370. self.completionSource.TrySetException(ex);
  5371. return;
  5372. }
  5373. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  5374. }
  5375. return;
  5376. COMPLETE:
  5377. self.completionSource.TrySetResult(false);
  5378. return;
  5379. }
  5380. static void Completed2(object state)
  5381. {
  5382. var self = (_CombineLatest)state;
  5383. self.running2 = false;
  5384. try
  5385. {
  5386. if (self.awaiter2.GetResult())
  5387. {
  5388. self.hasCurrent2 = true;
  5389. self.current2 = self.enumerator2.Current;
  5390. goto SUCCESS;
  5391. }
  5392. else
  5393. {
  5394. self.running2 = true; // as complete, no more call MoveNextAsync.
  5395. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5396. {
  5397. goto COMPLETE;
  5398. }
  5399. return;
  5400. }
  5401. }
  5402. catch (Exception ex)
  5403. {
  5404. self.running2 = true; // as complete, no more call MoveNextAsync.
  5405. self.completedCount = CompleteCount;
  5406. self.completionSource.TrySetException(ex);
  5407. return;
  5408. }
  5409. SUCCESS:
  5410. if (!self.TrySetResult())
  5411. {
  5412. if (self.syncRunning) return;
  5413. self.running2 = true; // as complete, no more call MoveNextAsync.
  5414. try
  5415. {
  5416. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  5417. }
  5418. catch (Exception ex)
  5419. {
  5420. self.completedCount = CompleteCount;
  5421. self.completionSource.TrySetException(ex);
  5422. return;
  5423. }
  5424. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  5425. }
  5426. return;
  5427. COMPLETE:
  5428. self.completionSource.TrySetResult(false);
  5429. return;
  5430. }
  5431. static void Completed3(object state)
  5432. {
  5433. var self = (_CombineLatest)state;
  5434. self.running3 = false;
  5435. try
  5436. {
  5437. if (self.awaiter3.GetResult())
  5438. {
  5439. self.hasCurrent3 = true;
  5440. self.current3 = self.enumerator3.Current;
  5441. goto SUCCESS;
  5442. }
  5443. else
  5444. {
  5445. self.running3 = true; // as complete, no more call MoveNextAsync.
  5446. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5447. {
  5448. goto COMPLETE;
  5449. }
  5450. return;
  5451. }
  5452. }
  5453. catch (Exception ex)
  5454. {
  5455. self.running3 = true; // as complete, no more call MoveNextAsync.
  5456. self.completedCount = CompleteCount;
  5457. self.completionSource.TrySetException(ex);
  5458. return;
  5459. }
  5460. SUCCESS:
  5461. if (!self.TrySetResult())
  5462. {
  5463. if (self.syncRunning) return;
  5464. self.running3 = true; // as complete, no more call MoveNextAsync.
  5465. try
  5466. {
  5467. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  5468. }
  5469. catch (Exception ex)
  5470. {
  5471. self.completedCount = CompleteCount;
  5472. self.completionSource.TrySetException(ex);
  5473. return;
  5474. }
  5475. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  5476. }
  5477. return;
  5478. COMPLETE:
  5479. self.completionSource.TrySetResult(false);
  5480. return;
  5481. }
  5482. static void Completed4(object state)
  5483. {
  5484. var self = (_CombineLatest)state;
  5485. self.running4 = false;
  5486. try
  5487. {
  5488. if (self.awaiter4.GetResult())
  5489. {
  5490. self.hasCurrent4 = true;
  5491. self.current4 = self.enumerator4.Current;
  5492. goto SUCCESS;
  5493. }
  5494. else
  5495. {
  5496. self.running4 = true; // as complete, no more call MoveNextAsync.
  5497. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5498. {
  5499. goto COMPLETE;
  5500. }
  5501. return;
  5502. }
  5503. }
  5504. catch (Exception ex)
  5505. {
  5506. self.running4 = true; // as complete, no more call MoveNextAsync.
  5507. self.completedCount = CompleteCount;
  5508. self.completionSource.TrySetException(ex);
  5509. return;
  5510. }
  5511. SUCCESS:
  5512. if (!self.TrySetResult())
  5513. {
  5514. if (self.syncRunning) return;
  5515. self.running4 = true; // as complete, no more call MoveNextAsync.
  5516. try
  5517. {
  5518. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  5519. }
  5520. catch (Exception ex)
  5521. {
  5522. self.completedCount = CompleteCount;
  5523. self.completionSource.TrySetException(ex);
  5524. return;
  5525. }
  5526. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  5527. }
  5528. return;
  5529. COMPLETE:
  5530. self.completionSource.TrySetResult(false);
  5531. return;
  5532. }
  5533. static void Completed5(object state)
  5534. {
  5535. var self = (_CombineLatest)state;
  5536. self.running5 = false;
  5537. try
  5538. {
  5539. if (self.awaiter5.GetResult())
  5540. {
  5541. self.hasCurrent5 = true;
  5542. self.current5 = self.enumerator5.Current;
  5543. goto SUCCESS;
  5544. }
  5545. else
  5546. {
  5547. self.running5 = true; // as complete, no more call MoveNextAsync.
  5548. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5549. {
  5550. goto COMPLETE;
  5551. }
  5552. return;
  5553. }
  5554. }
  5555. catch (Exception ex)
  5556. {
  5557. self.running5 = true; // as complete, no more call MoveNextAsync.
  5558. self.completedCount = CompleteCount;
  5559. self.completionSource.TrySetException(ex);
  5560. return;
  5561. }
  5562. SUCCESS:
  5563. if (!self.TrySetResult())
  5564. {
  5565. if (self.syncRunning) return;
  5566. self.running5 = true; // as complete, no more call MoveNextAsync.
  5567. try
  5568. {
  5569. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  5570. }
  5571. catch (Exception ex)
  5572. {
  5573. self.completedCount = CompleteCount;
  5574. self.completionSource.TrySetException(ex);
  5575. return;
  5576. }
  5577. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  5578. }
  5579. return;
  5580. COMPLETE:
  5581. self.completionSource.TrySetResult(false);
  5582. return;
  5583. }
  5584. static void Completed6(object state)
  5585. {
  5586. var self = (_CombineLatest)state;
  5587. self.running6 = false;
  5588. try
  5589. {
  5590. if (self.awaiter6.GetResult())
  5591. {
  5592. self.hasCurrent6 = true;
  5593. self.current6 = self.enumerator6.Current;
  5594. goto SUCCESS;
  5595. }
  5596. else
  5597. {
  5598. self.running6 = true; // as complete, no more call MoveNextAsync.
  5599. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5600. {
  5601. goto COMPLETE;
  5602. }
  5603. return;
  5604. }
  5605. }
  5606. catch (Exception ex)
  5607. {
  5608. self.running6 = true; // as complete, no more call MoveNextAsync.
  5609. self.completedCount = CompleteCount;
  5610. self.completionSource.TrySetException(ex);
  5611. return;
  5612. }
  5613. SUCCESS:
  5614. if (!self.TrySetResult())
  5615. {
  5616. if (self.syncRunning) return;
  5617. self.running6 = true; // as complete, no more call MoveNextAsync.
  5618. try
  5619. {
  5620. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  5621. }
  5622. catch (Exception ex)
  5623. {
  5624. self.completedCount = CompleteCount;
  5625. self.completionSource.TrySetException(ex);
  5626. return;
  5627. }
  5628. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  5629. }
  5630. return;
  5631. COMPLETE:
  5632. self.completionSource.TrySetResult(false);
  5633. return;
  5634. }
  5635. static void Completed7(object state)
  5636. {
  5637. var self = (_CombineLatest)state;
  5638. self.running7 = false;
  5639. try
  5640. {
  5641. if (self.awaiter7.GetResult())
  5642. {
  5643. self.hasCurrent7 = true;
  5644. self.current7 = self.enumerator7.Current;
  5645. goto SUCCESS;
  5646. }
  5647. else
  5648. {
  5649. self.running7 = true; // as complete, no more call MoveNextAsync.
  5650. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5651. {
  5652. goto COMPLETE;
  5653. }
  5654. return;
  5655. }
  5656. }
  5657. catch (Exception ex)
  5658. {
  5659. self.running7 = true; // as complete, no more call MoveNextAsync.
  5660. self.completedCount = CompleteCount;
  5661. self.completionSource.TrySetException(ex);
  5662. return;
  5663. }
  5664. SUCCESS:
  5665. if (!self.TrySetResult())
  5666. {
  5667. if (self.syncRunning) return;
  5668. self.running7 = true; // as complete, no more call MoveNextAsync.
  5669. try
  5670. {
  5671. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  5672. }
  5673. catch (Exception ex)
  5674. {
  5675. self.completedCount = CompleteCount;
  5676. self.completionSource.TrySetException(ex);
  5677. return;
  5678. }
  5679. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  5680. }
  5681. return;
  5682. COMPLETE:
  5683. self.completionSource.TrySetResult(false);
  5684. return;
  5685. }
  5686. static void Completed8(object state)
  5687. {
  5688. var self = (_CombineLatest)state;
  5689. self.running8 = false;
  5690. try
  5691. {
  5692. if (self.awaiter8.GetResult())
  5693. {
  5694. self.hasCurrent8 = true;
  5695. self.current8 = self.enumerator8.Current;
  5696. goto SUCCESS;
  5697. }
  5698. else
  5699. {
  5700. self.running8 = true; // as complete, no more call MoveNextAsync.
  5701. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5702. {
  5703. goto COMPLETE;
  5704. }
  5705. return;
  5706. }
  5707. }
  5708. catch (Exception ex)
  5709. {
  5710. self.running8 = true; // as complete, no more call MoveNextAsync.
  5711. self.completedCount = CompleteCount;
  5712. self.completionSource.TrySetException(ex);
  5713. return;
  5714. }
  5715. SUCCESS:
  5716. if (!self.TrySetResult())
  5717. {
  5718. if (self.syncRunning) return;
  5719. self.running8 = true; // as complete, no more call MoveNextAsync.
  5720. try
  5721. {
  5722. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  5723. }
  5724. catch (Exception ex)
  5725. {
  5726. self.completedCount = CompleteCount;
  5727. self.completionSource.TrySetException(ex);
  5728. return;
  5729. }
  5730. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  5731. }
  5732. return;
  5733. COMPLETE:
  5734. self.completionSource.TrySetResult(false);
  5735. return;
  5736. }
  5737. static void Completed9(object state)
  5738. {
  5739. var self = (_CombineLatest)state;
  5740. self.running9 = false;
  5741. try
  5742. {
  5743. if (self.awaiter9.GetResult())
  5744. {
  5745. self.hasCurrent9 = true;
  5746. self.current9 = self.enumerator9.Current;
  5747. goto SUCCESS;
  5748. }
  5749. else
  5750. {
  5751. self.running9 = true; // as complete, no more call MoveNextAsync.
  5752. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5753. {
  5754. goto COMPLETE;
  5755. }
  5756. return;
  5757. }
  5758. }
  5759. catch (Exception ex)
  5760. {
  5761. self.running9 = true; // as complete, no more call MoveNextAsync.
  5762. self.completedCount = CompleteCount;
  5763. self.completionSource.TrySetException(ex);
  5764. return;
  5765. }
  5766. SUCCESS:
  5767. if (!self.TrySetResult())
  5768. {
  5769. if (self.syncRunning) return;
  5770. self.running9 = true; // as complete, no more call MoveNextAsync.
  5771. try
  5772. {
  5773. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  5774. }
  5775. catch (Exception ex)
  5776. {
  5777. self.completedCount = CompleteCount;
  5778. self.completionSource.TrySetException(ex);
  5779. return;
  5780. }
  5781. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  5782. }
  5783. return;
  5784. COMPLETE:
  5785. self.completionSource.TrySetResult(false);
  5786. return;
  5787. }
  5788. static void Completed10(object state)
  5789. {
  5790. var self = (_CombineLatest)state;
  5791. self.running10 = false;
  5792. try
  5793. {
  5794. if (self.awaiter10.GetResult())
  5795. {
  5796. self.hasCurrent10 = true;
  5797. self.current10 = self.enumerator10.Current;
  5798. goto SUCCESS;
  5799. }
  5800. else
  5801. {
  5802. self.running10 = true; // as complete, no more call MoveNextAsync.
  5803. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5804. {
  5805. goto COMPLETE;
  5806. }
  5807. return;
  5808. }
  5809. }
  5810. catch (Exception ex)
  5811. {
  5812. self.running10 = true; // as complete, no more call MoveNextAsync.
  5813. self.completedCount = CompleteCount;
  5814. self.completionSource.TrySetException(ex);
  5815. return;
  5816. }
  5817. SUCCESS:
  5818. if (!self.TrySetResult())
  5819. {
  5820. if (self.syncRunning) return;
  5821. self.running10 = true; // as complete, no more call MoveNextAsync.
  5822. try
  5823. {
  5824. self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter();
  5825. }
  5826. catch (Exception ex)
  5827. {
  5828. self.completedCount = CompleteCount;
  5829. self.completionSource.TrySetException(ex);
  5830. return;
  5831. }
  5832. self.awaiter10.SourceOnCompleted(Completed10Delegate, self);
  5833. }
  5834. return;
  5835. COMPLETE:
  5836. self.completionSource.TrySetResult(false);
  5837. return;
  5838. }
  5839. static void Completed11(object state)
  5840. {
  5841. var self = (_CombineLatest)state;
  5842. self.running11 = false;
  5843. try
  5844. {
  5845. if (self.awaiter11.GetResult())
  5846. {
  5847. self.hasCurrent11 = true;
  5848. self.current11 = self.enumerator11.Current;
  5849. goto SUCCESS;
  5850. }
  5851. else
  5852. {
  5853. self.running11 = true; // as complete, no more call MoveNextAsync.
  5854. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  5855. {
  5856. goto COMPLETE;
  5857. }
  5858. return;
  5859. }
  5860. }
  5861. catch (Exception ex)
  5862. {
  5863. self.running11 = true; // as complete, no more call MoveNextAsync.
  5864. self.completedCount = CompleteCount;
  5865. self.completionSource.TrySetException(ex);
  5866. return;
  5867. }
  5868. SUCCESS:
  5869. if (!self.TrySetResult())
  5870. {
  5871. if (self.syncRunning) return;
  5872. self.running11 = true; // as complete, no more call MoveNextAsync.
  5873. try
  5874. {
  5875. self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter();
  5876. }
  5877. catch (Exception ex)
  5878. {
  5879. self.completedCount = CompleteCount;
  5880. self.completionSource.TrySetException(ex);
  5881. return;
  5882. }
  5883. self.awaiter11.SourceOnCompleted(Completed11Delegate, self);
  5884. }
  5885. return;
  5886. COMPLETE:
  5887. self.completionSource.TrySetResult(false);
  5888. return;
  5889. }
  5890. bool TrySetResult()
  5891. {
  5892. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11)
  5893. {
  5894. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11);
  5895. completionSource.TrySetResult(true);
  5896. return true;
  5897. }
  5898. else
  5899. {
  5900. return false;
  5901. }
  5902. }
  5903. public async UniTask DisposeAsync()
  5904. {
  5905. TaskTracker.RemoveTracking(this);
  5906. if (enumerator1 != null)
  5907. {
  5908. await enumerator1.DisposeAsync();
  5909. }
  5910. if (enumerator2 != null)
  5911. {
  5912. await enumerator2.DisposeAsync();
  5913. }
  5914. if (enumerator3 != null)
  5915. {
  5916. await enumerator3.DisposeAsync();
  5917. }
  5918. if (enumerator4 != null)
  5919. {
  5920. await enumerator4.DisposeAsync();
  5921. }
  5922. if (enumerator5 != null)
  5923. {
  5924. await enumerator5.DisposeAsync();
  5925. }
  5926. if (enumerator6 != null)
  5927. {
  5928. await enumerator6.DisposeAsync();
  5929. }
  5930. if (enumerator7 != null)
  5931. {
  5932. await enumerator7.DisposeAsync();
  5933. }
  5934. if (enumerator8 != null)
  5935. {
  5936. await enumerator8.DisposeAsync();
  5937. }
  5938. if (enumerator9 != null)
  5939. {
  5940. await enumerator9.DisposeAsync();
  5941. }
  5942. if (enumerator10 != null)
  5943. {
  5944. await enumerator10.DisposeAsync();
  5945. }
  5946. if (enumerator11 != null)
  5947. {
  5948. await enumerator11.DisposeAsync();
  5949. }
  5950. }
  5951. }
  5952. }
  5953. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> : IUniTaskAsyncEnumerable<TResult>
  5954. {
  5955. readonly IUniTaskAsyncEnumerable<T1> source1;
  5956. readonly IUniTaskAsyncEnumerable<T2> source2;
  5957. readonly IUniTaskAsyncEnumerable<T3> source3;
  5958. readonly IUniTaskAsyncEnumerable<T4> source4;
  5959. readonly IUniTaskAsyncEnumerable<T5> source5;
  5960. readonly IUniTaskAsyncEnumerable<T6> source6;
  5961. readonly IUniTaskAsyncEnumerable<T7> source7;
  5962. readonly IUniTaskAsyncEnumerable<T8> source8;
  5963. readonly IUniTaskAsyncEnumerable<T9> source9;
  5964. readonly IUniTaskAsyncEnumerable<T10> source10;
  5965. readonly IUniTaskAsyncEnumerable<T11> source11;
  5966. readonly IUniTaskAsyncEnumerable<T12> source12;
  5967. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> resultSelector;
  5968. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> resultSelector)
  5969. {
  5970. this.source1 = source1;
  5971. this.source2 = source2;
  5972. this.source3 = source3;
  5973. this.source4 = source4;
  5974. this.source5 = source5;
  5975. this.source6 = source6;
  5976. this.source7 = source7;
  5977. this.source8 = source8;
  5978. this.source9 = source9;
  5979. this.source10 = source10;
  5980. this.source11 = source11;
  5981. this.source12 = source12;
  5982. this.resultSelector = resultSelector;
  5983. }
  5984. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  5985. {
  5986. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, resultSelector, cancellationToken);
  5987. }
  5988. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  5989. {
  5990. static readonly Action<object> Completed1Delegate = Completed1;
  5991. static readonly Action<object> Completed2Delegate = Completed2;
  5992. static readonly Action<object> Completed3Delegate = Completed3;
  5993. static readonly Action<object> Completed4Delegate = Completed4;
  5994. static readonly Action<object> Completed5Delegate = Completed5;
  5995. static readonly Action<object> Completed6Delegate = Completed6;
  5996. static readonly Action<object> Completed7Delegate = Completed7;
  5997. static readonly Action<object> Completed8Delegate = Completed8;
  5998. static readonly Action<object> Completed9Delegate = Completed9;
  5999. static readonly Action<object> Completed10Delegate = Completed10;
  6000. static readonly Action<object> Completed11Delegate = Completed11;
  6001. static readonly Action<object> Completed12Delegate = Completed12;
  6002. const int CompleteCount = 12;
  6003. readonly IUniTaskAsyncEnumerable<T1> source1;
  6004. readonly IUniTaskAsyncEnumerable<T2> source2;
  6005. readonly IUniTaskAsyncEnumerable<T3> source3;
  6006. readonly IUniTaskAsyncEnumerable<T4> source4;
  6007. readonly IUniTaskAsyncEnumerable<T5> source5;
  6008. readonly IUniTaskAsyncEnumerable<T6> source6;
  6009. readonly IUniTaskAsyncEnumerable<T7> source7;
  6010. readonly IUniTaskAsyncEnumerable<T8> source8;
  6011. readonly IUniTaskAsyncEnumerable<T9> source9;
  6012. readonly IUniTaskAsyncEnumerable<T10> source10;
  6013. readonly IUniTaskAsyncEnumerable<T11> source11;
  6014. readonly IUniTaskAsyncEnumerable<T12> source12;
  6015. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> resultSelector;
  6016. CancellationToken cancellationToken;
  6017. IUniTaskAsyncEnumerator<T1> enumerator1;
  6018. UniTask<bool>.Awaiter awaiter1;
  6019. bool hasCurrent1;
  6020. bool running1;
  6021. T1 current1;
  6022. IUniTaskAsyncEnumerator<T2> enumerator2;
  6023. UniTask<bool>.Awaiter awaiter2;
  6024. bool hasCurrent2;
  6025. bool running2;
  6026. T2 current2;
  6027. IUniTaskAsyncEnumerator<T3> enumerator3;
  6028. UniTask<bool>.Awaiter awaiter3;
  6029. bool hasCurrent3;
  6030. bool running3;
  6031. T3 current3;
  6032. IUniTaskAsyncEnumerator<T4> enumerator4;
  6033. UniTask<bool>.Awaiter awaiter4;
  6034. bool hasCurrent4;
  6035. bool running4;
  6036. T4 current4;
  6037. IUniTaskAsyncEnumerator<T5> enumerator5;
  6038. UniTask<bool>.Awaiter awaiter5;
  6039. bool hasCurrent5;
  6040. bool running5;
  6041. T5 current5;
  6042. IUniTaskAsyncEnumerator<T6> enumerator6;
  6043. UniTask<bool>.Awaiter awaiter6;
  6044. bool hasCurrent6;
  6045. bool running6;
  6046. T6 current6;
  6047. IUniTaskAsyncEnumerator<T7> enumerator7;
  6048. UniTask<bool>.Awaiter awaiter7;
  6049. bool hasCurrent7;
  6050. bool running7;
  6051. T7 current7;
  6052. IUniTaskAsyncEnumerator<T8> enumerator8;
  6053. UniTask<bool>.Awaiter awaiter8;
  6054. bool hasCurrent8;
  6055. bool running8;
  6056. T8 current8;
  6057. IUniTaskAsyncEnumerator<T9> enumerator9;
  6058. UniTask<bool>.Awaiter awaiter9;
  6059. bool hasCurrent9;
  6060. bool running9;
  6061. T9 current9;
  6062. IUniTaskAsyncEnumerator<T10> enumerator10;
  6063. UniTask<bool>.Awaiter awaiter10;
  6064. bool hasCurrent10;
  6065. bool running10;
  6066. T10 current10;
  6067. IUniTaskAsyncEnumerator<T11> enumerator11;
  6068. UniTask<bool>.Awaiter awaiter11;
  6069. bool hasCurrent11;
  6070. bool running11;
  6071. T11 current11;
  6072. IUniTaskAsyncEnumerator<T12> enumerator12;
  6073. UniTask<bool>.Awaiter awaiter12;
  6074. bool hasCurrent12;
  6075. bool running12;
  6076. T12 current12;
  6077. int completedCount;
  6078. bool syncRunning;
  6079. TResult result;
  6080. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> resultSelector, CancellationToken cancellationToken)
  6081. {
  6082. this.source1 = source1;
  6083. this.source2 = source2;
  6084. this.source3 = source3;
  6085. this.source4 = source4;
  6086. this.source5 = source5;
  6087. this.source6 = source6;
  6088. this.source7 = source7;
  6089. this.source8 = source8;
  6090. this.source9 = source9;
  6091. this.source10 = source10;
  6092. this.source11 = source11;
  6093. this.source12 = source12;
  6094. this.resultSelector = resultSelector;
  6095. this.cancellationToken = cancellationToken;
  6096. TaskTracker.TrackActiveTask(this, 3);
  6097. }
  6098. public TResult Current => result;
  6099. public UniTask<bool> MoveNextAsync()
  6100. {
  6101. cancellationToken.ThrowIfCancellationRequested();
  6102. if (completedCount == CompleteCount) return CompletedTasks.False;
  6103. if (enumerator1 == null)
  6104. {
  6105. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  6106. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  6107. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  6108. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  6109. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  6110. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  6111. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  6112. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  6113. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  6114. enumerator10 = source10.GetAsyncEnumerator(cancellationToken);
  6115. enumerator11 = source11.GetAsyncEnumerator(cancellationToken);
  6116. enumerator12 = source12.GetAsyncEnumerator(cancellationToken);
  6117. }
  6118. completionSource.Reset();
  6119. AGAIN:
  6120. syncRunning = true;
  6121. if (!running1)
  6122. {
  6123. running1 = true;
  6124. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  6125. if (awaiter1.IsCompleted)
  6126. {
  6127. Completed1(this);
  6128. }
  6129. else
  6130. {
  6131. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  6132. }
  6133. }
  6134. if (!running2)
  6135. {
  6136. running2 = true;
  6137. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  6138. if (awaiter2.IsCompleted)
  6139. {
  6140. Completed2(this);
  6141. }
  6142. else
  6143. {
  6144. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  6145. }
  6146. }
  6147. if (!running3)
  6148. {
  6149. running3 = true;
  6150. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  6151. if (awaiter3.IsCompleted)
  6152. {
  6153. Completed3(this);
  6154. }
  6155. else
  6156. {
  6157. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  6158. }
  6159. }
  6160. if (!running4)
  6161. {
  6162. running4 = true;
  6163. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  6164. if (awaiter4.IsCompleted)
  6165. {
  6166. Completed4(this);
  6167. }
  6168. else
  6169. {
  6170. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  6171. }
  6172. }
  6173. if (!running5)
  6174. {
  6175. running5 = true;
  6176. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  6177. if (awaiter5.IsCompleted)
  6178. {
  6179. Completed5(this);
  6180. }
  6181. else
  6182. {
  6183. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  6184. }
  6185. }
  6186. if (!running6)
  6187. {
  6188. running6 = true;
  6189. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  6190. if (awaiter6.IsCompleted)
  6191. {
  6192. Completed6(this);
  6193. }
  6194. else
  6195. {
  6196. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  6197. }
  6198. }
  6199. if (!running7)
  6200. {
  6201. running7 = true;
  6202. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  6203. if (awaiter7.IsCompleted)
  6204. {
  6205. Completed7(this);
  6206. }
  6207. else
  6208. {
  6209. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  6210. }
  6211. }
  6212. if (!running8)
  6213. {
  6214. running8 = true;
  6215. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  6216. if (awaiter8.IsCompleted)
  6217. {
  6218. Completed8(this);
  6219. }
  6220. else
  6221. {
  6222. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  6223. }
  6224. }
  6225. if (!running9)
  6226. {
  6227. running9 = true;
  6228. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  6229. if (awaiter9.IsCompleted)
  6230. {
  6231. Completed9(this);
  6232. }
  6233. else
  6234. {
  6235. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  6236. }
  6237. }
  6238. if (!running10)
  6239. {
  6240. running10 = true;
  6241. awaiter10 = enumerator10.MoveNextAsync().GetAwaiter();
  6242. if (awaiter10.IsCompleted)
  6243. {
  6244. Completed10(this);
  6245. }
  6246. else
  6247. {
  6248. awaiter10.SourceOnCompleted(Completed10Delegate, this);
  6249. }
  6250. }
  6251. if (!running11)
  6252. {
  6253. running11 = true;
  6254. awaiter11 = enumerator11.MoveNextAsync().GetAwaiter();
  6255. if (awaiter11.IsCompleted)
  6256. {
  6257. Completed11(this);
  6258. }
  6259. else
  6260. {
  6261. awaiter11.SourceOnCompleted(Completed11Delegate, this);
  6262. }
  6263. }
  6264. if (!running12)
  6265. {
  6266. running12 = true;
  6267. awaiter12 = enumerator12.MoveNextAsync().GetAwaiter();
  6268. if (awaiter12.IsCompleted)
  6269. {
  6270. Completed12(this);
  6271. }
  6272. else
  6273. {
  6274. awaiter12.SourceOnCompleted(Completed12Delegate, this);
  6275. }
  6276. }
  6277. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12)
  6278. {
  6279. goto AGAIN;
  6280. }
  6281. syncRunning = false;
  6282. return new UniTask<bool>(this, completionSource.Version);
  6283. }
  6284. static void Completed1(object state)
  6285. {
  6286. var self = (_CombineLatest)state;
  6287. self.running1 = false;
  6288. try
  6289. {
  6290. if (self.awaiter1.GetResult())
  6291. {
  6292. self.hasCurrent1 = true;
  6293. self.current1 = self.enumerator1.Current;
  6294. goto SUCCESS;
  6295. }
  6296. else
  6297. {
  6298. self.running1 = true; // as complete, no more call MoveNextAsync.
  6299. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6300. {
  6301. goto COMPLETE;
  6302. }
  6303. return;
  6304. }
  6305. }
  6306. catch (Exception ex)
  6307. {
  6308. self.running1 = true; // as complete, no more call MoveNextAsync.
  6309. self.completedCount = CompleteCount;
  6310. self.completionSource.TrySetException(ex);
  6311. return;
  6312. }
  6313. SUCCESS:
  6314. if (!self.TrySetResult())
  6315. {
  6316. if (self.syncRunning) return;
  6317. self.running1 = true; // as complete, no more call MoveNextAsync.
  6318. try
  6319. {
  6320. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  6321. }
  6322. catch (Exception ex)
  6323. {
  6324. self.completedCount = CompleteCount;
  6325. self.completionSource.TrySetException(ex);
  6326. return;
  6327. }
  6328. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  6329. }
  6330. return;
  6331. COMPLETE:
  6332. self.completionSource.TrySetResult(false);
  6333. return;
  6334. }
  6335. static void Completed2(object state)
  6336. {
  6337. var self = (_CombineLatest)state;
  6338. self.running2 = false;
  6339. try
  6340. {
  6341. if (self.awaiter2.GetResult())
  6342. {
  6343. self.hasCurrent2 = true;
  6344. self.current2 = self.enumerator2.Current;
  6345. goto SUCCESS;
  6346. }
  6347. else
  6348. {
  6349. self.running2 = true; // as complete, no more call MoveNextAsync.
  6350. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6351. {
  6352. goto COMPLETE;
  6353. }
  6354. return;
  6355. }
  6356. }
  6357. catch (Exception ex)
  6358. {
  6359. self.running2 = true; // as complete, no more call MoveNextAsync.
  6360. self.completedCount = CompleteCount;
  6361. self.completionSource.TrySetException(ex);
  6362. return;
  6363. }
  6364. SUCCESS:
  6365. if (!self.TrySetResult())
  6366. {
  6367. if (self.syncRunning) return;
  6368. self.running2 = true; // as complete, no more call MoveNextAsync.
  6369. try
  6370. {
  6371. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  6372. }
  6373. catch (Exception ex)
  6374. {
  6375. self.completedCount = CompleteCount;
  6376. self.completionSource.TrySetException(ex);
  6377. return;
  6378. }
  6379. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  6380. }
  6381. return;
  6382. COMPLETE:
  6383. self.completionSource.TrySetResult(false);
  6384. return;
  6385. }
  6386. static void Completed3(object state)
  6387. {
  6388. var self = (_CombineLatest)state;
  6389. self.running3 = false;
  6390. try
  6391. {
  6392. if (self.awaiter3.GetResult())
  6393. {
  6394. self.hasCurrent3 = true;
  6395. self.current3 = self.enumerator3.Current;
  6396. goto SUCCESS;
  6397. }
  6398. else
  6399. {
  6400. self.running3 = true; // as complete, no more call MoveNextAsync.
  6401. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6402. {
  6403. goto COMPLETE;
  6404. }
  6405. return;
  6406. }
  6407. }
  6408. catch (Exception ex)
  6409. {
  6410. self.running3 = true; // as complete, no more call MoveNextAsync.
  6411. self.completedCount = CompleteCount;
  6412. self.completionSource.TrySetException(ex);
  6413. return;
  6414. }
  6415. SUCCESS:
  6416. if (!self.TrySetResult())
  6417. {
  6418. if (self.syncRunning) return;
  6419. self.running3 = true; // as complete, no more call MoveNextAsync.
  6420. try
  6421. {
  6422. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  6423. }
  6424. catch (Exception ex)
  6425. {
  6426. self.completedCount = CompleteCount;
  6427. self.completionSource.TrySetException(ex);
  6428. return;
  6429. }
  6430. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  6431. }
  6432. return;
  6433. COMPLETE:
  6434. self.completionSource.TrySetResult(false);
  6435. return;
  6436. }
  6437. static void Completed4(object state)
  6438. {
  6439. var self = (_CombineLatest)state;
  6440. self.running4 = false;
  6441. try
  6442. {
  6443. if (self.awaiter4.GetResult())
  6444. {
  6445. self.hasCurrent4 = true;
  6446. self.current4 = self.enumerator4.Current;
  6447. goto SUCCESS;
  6448. }
  6449. else
  6450. {
  6451. self.running4 = true; // as complete, no more call MoveNextAsync.
  6452. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6453. {
  6454. goto COMPLETE;
  6455. }
  6456. return;
  6457. }
  6458. }
  6459. catch (Exception ex)
  6460. {
  6461. self.running4 = true; // as complete, no more call MoveNextAsync.
  6462. self.completedCount = CompleteCount;
  6463. self.completionSource.TrySetException(ex);
  6464. return;
  6465. }
  6466. SUCCESS:
  6467. if (!self.TrySetResult())
  6468. {
  6469. if (self.syncRunning) return;
  6470. self.running4 = true; // as complete, no more call MoveNextAsync.
  6471. try
  6472. {
  6473. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  6474. }
  6475. catch (Exception ex)
  6476. {
  6477. self.completedCount = CompleteCount;
  6478. self.completionSource.TrySetException(ex);
  6479. return;
  6480. }
  6481. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  6482. }
  6483. return;
  6484. COMPLETE:
  6485. self.completionSource.TrySetResult(false);
  6486. return;
  6487. }
  6488. static void Completed5(object state)
  6489. {
  6490. var self = (_CombineLatest)state;
  6491. self.running5 = false;
  6492. try
  6493. {
  6494. if (self.awaiter5.GetResult())
  6495. {
  6496. self.hasCurrent5 = true;
  6497. self.current5 = self.enumerator5.Current;
  6498. goto SUCCESS;
  6499. }
  6500. else
  6501. {
  6502. self.running5 = true; // as complete, no more call MoveNextAsync.
  6503. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6504. {
  6505. goto COMPLETE;
  6506. }
  6507. return;
  6508. }
  6509. }
  6510. catch (Exception ex)
  6511. {
  6512. self.running5 = true; // as complete, no more call MoveNextAsync.
  6513. self.completedCount = CompleteCount;
  6514. self.completionSource.TrySetException(ex);
  6515. return;
  6516. }
  6517. SUCCESS:
  6518. if (!self.TrySetResult())
  6519. {
  6520. if (self.syncRunning) return;
  6521. self.running5 = true; // as complete, no more call MoveNextAsync.
  6522. try
  6523. {
  6524. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  6525. }
  6526. catch (Exception ex)
  6527. {
  6528. self.completedCount = CompleteCount;
  6529. self.completionSource.TrySetException(ex);
  6530. return;
  6531. }
  6532. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  6533. }
  6534. return;
  6535. COMPLETE:
  6536. self.completionSource.TrySetResult(false);
  6537. return;
  6538. }
  6539. static void Completed6(object state)
  6540. {
  6541. var self = (_CombineLatest)state;
  6542. self.running6 = false;
  6543. try
  6544. {
  6545. if (self.awaiter6.GetResult())
  6546. {
  6547. self.hasCurrent6 = true;
  6548. self.current6 = self.enumerator6.Current;
  6549. goto SUCCESS;
  6550. }
  6551. else
  6552. {
  6553. self.running6 = true; // as complete, no more call MoveNextAsync.
  6554. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6555. {
  6556. goto COMPLETE;
  6557. }
  6558. return;
  6559. }
  6560. }
  6561. catch (Exception ex)
  6562. {
  6563. self.running6 = true; // as complete, no more call MoveNextAsync.
  6564. self.completedCount = CompleteCount;
  6565. self.completionSource.TrySetException(ex);
  6566. return;
  6567. }
  6568. SUCCESS:
  6569. if (!self.TrySetResult())
  6570. {
  6571. if (self.syncRunning) return;
  6572. self.running6 = true; // as complete, no more call MoveNextAsync.
  6573. try
  6574. {
  6575. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  6576. }
  6577. catch (Exception ex)
  6578. {
  6579. self.completedCount = CompleteCount;
  6580. self.completionSource.TrySetException(ex);
  6581. return;
  6582. }
  6583. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  6584. }
  6585. return;
  6586. COMPLETE:
  6587. self.completionSource.TrySetResult(false);
  6588. return;
  6589. }
  6590. static void Completed7(object state)
  6591. {
  6592. var self = (_CombineLatest)state;
  6593. self.running7 = false;
  6594. try
  6595. {
  6596. if (self.awaiter7.GetResult())
  6597. {
  6598. self.hasCurrent7 = true;
  6599. self.current7 = self.enumerator7.Current;
  6600. goto SUCCESS;
  6601. }
  6602. else
  6603. {
  6604. self.running7 = true; // as complete, no more call MoveNextAsync.
  6605. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6606. {
  6607. goto COMPLETE;
  6608. }
  6609. return;
  6610. }
  6611. }
  6612. catch (Exception ex)
  6613. {
  6614. self.running7 = true; // as complete, no more call MoveNextAsync.
  6615. self.completedCount = CompleteCount;
  6616. self.completionSource.TrySetException(ex);
  6617. return;
  6618. }
  6619. SUCCESS:
  6620. if (!self.TrySetResult())
  6621. {
  6622. if (self.syncRunning) return;
  6623. self.running7 = true; // as complete, no more call MoveNextAsync.
  6624. try
  6625. {
  6626. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  6627. }
  6628. catch (Exception ex)
  6629. {
  6630. self.completedCount = CompleteCount;
  6631. self.completionSource.TrySetException(ex);
  6632. return;
  6633. }
  6634. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  6635. }
  6636. return;
  6637. COMPLETE:
  6638. self.completionSource.TrySetResult(false);
  6639. return;
  6640. }
  6641. static void Completed8(object state)
  6642. {
  6643. var self = (_CombineLatest)state;
  6644. self.running8 = false;
  6645. try
  6646. {
  6647. if (self.awaiter8.GetResult())
  6648. {
  6649. self.hasCurrent8 = true;
  6650. self.current8 = self.enumerator8.Current;
  6651. goto SUCCESS;
  6652. }
  6653. else
  6654. {
  6655. self.running8 = true; // as complete, no more call MoveNextAsync.
  6656. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6657. {
  6658. goto COMPLETE;
  6659. }
  6660. return;
  6661. }
  6662. }
  6663. catch (Exception ex)
  6664. {
  6665. self.running8 = true; // as complete, no more call MoveNextAsync.
  6666. self.completedCount = CompleteCount;
  6667. self.completionSource.TrySetException(ex);
  6668. return;
  6669. }
  6670. SUCCESS:
  6671. if (!self.TrySetResult())
  6672. {
  6673. if (self.syncRunning) return;
  6674. self.running8 = true; // as complete, no more call MoveNextAsync.
  6675. try
  6676. {
  6677. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  6678. }
  6679. catch (Exception ex)
  6680. {
  6681. self.completedCount = CompleteCount;
  6682. self.completionSource.TrySetException(ex);
  6683. return;
  6684. }
  6685. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  6686. }
  6687. return;
  6688. COMPLETE:
  6689. self.completionSource.TrySetResult(false);
  6690. return;
  6691. }
  6692. static void Completed9(object state)
  6693. {
  6694. var self = (_CombineLatest)state;
  6695. self.running9 = false;
  6696. try
  6697. {
  6698. if (self.awaiter9.GetResult())
  6699. {
  6700. self.hasCurrent9 = true;
  6701. self.current9 = self.enumerator9.Current;
  6702. goto SUCCESS;
  6703. }
  6704. else
  6705. {
  6706. self.running9 = true; // as complete, no more call MoveNextAsync.
  6707. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6708. {
  6709. goto COMPLETE;
  6710. }
  6711. return;
  6712. }
  6713. }
  6714. catch (Exception ex)
  6715. {
  6716. self.running9 = true; // as complete, no more call MoveNextAsync.
  6717. self.completedCount = CompleteCount;
  6718. self.completionSource.TrySetException(ex);
  6719. return;
  6720. }
  6721. SUCCESS:
  6722. if (!self.TrySetResult())
  6723. {
  6724. if (self.syncRunning) return;
  6725. self.running9 = true; // as complete, no more call MoveNextAsync.
  6726. try
  6727. {
  6728. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  6729. }
  6730. catch (Exception ex)
  6731. {
  6732. self.completedCount = CompleteCount;
  6733. self.completionSource.TrySetException(ex);
  6734. return;
  6735. }
  6736. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  6737. }
  6738. return;
  6739. COMPLETE:
  6740. self.completionSource.TrySetResult(false);
  6741. return;
  6742. }
  6743. static void Completed10(object state)
  6744. {
  6745. var self = (_CombineLatest)state;
  6746. self.running10 = false;
  6747. try
  6748. {
  6749. if (self.awaiter10.GetResult())
  6750. {
  6751. self.hasCurrent10 = true;
  6752. self.current10 = self.enumerator10.Current;
  6753. goto SUCCESS;
  6754. }
  6755. else
  6756. {
  6757. self.running10 = true; // as complete, no more call MoveNextAsync.
  6758. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6759. {
  6760. goto COMPLETE;
  6761. }
  6762. return;
  6763. }
  6764. }
  6765. catch (Exception ex)
  6766. {
  6767. self.running10 = true; // as complete, no more call MoveNextAsync.
  6768. self.completedCount = CompleteCount;
  6769. self.completionSource.TrySetException(ex);
  6770. return;
  6771. }
  6772. SUCCESS:
  6773. if (!self.TrySetResult())
  6774. {
  6775. if (self.syncRunning) return;
  6776. self.running10 = true; // as complete, no more call MoveNextAsync.
  6777. try
  6778. {
  6779. self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter();
  6780. }
  6781. catch (Exception ex)
  6782. {
  6783. self.completedCount = CompleteCount;
  6784. self.completionSource.TrySetException(ex);
  6785. return;
  6786. }
  6787. self.awaiter10.SourceOnCompleted(Completed10Delegate, self);
  6788. }
  6789. return;
  6790. COMPLETE:
  6791. self.completionSource.TrySetResult(false);
  6792. return;
  6793. }
  6794. static void Completed11(object state)
  6795. {
  6796. var self = (_CombineLatest)state;
  6797. self.running11 = false;
  6798. try
  6799. {
  6800. if (self.awaiter11.GetResult())
  6801. {
  6802. self.hasCurrent11 = true;
  6803. self.current11 = self.enumerator11.Current;
  6804. goto SUCCESS;
  6805. }
  6806. else
  6807. {
  6808. self.running11 = true; // as complete, no more call MoveNextAsync.
  6809. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6810. {
  6811. goto COMPLETE;
  6812. }
  6813. return;
  6814. }
  6815. }
  6816. catch (Exception ex)
  6817. {
  6818. self.running11 = true; // as complete, no more call MoveNextAsync.
  6819. self.completedCount = CompleteCount;
  6820. self.completionSource.TrySetException(ex);
  6821. return;
  6822. }
  6823. SUCCESS:
  6824. if (!self.TrySetResult())
  6825. {
  6826. if (self.syncRunning) return;
  6827. self.running11 = true; // as complete, no more call MoveNextAsync.
  6828. try
  6829. {
  6830. self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter();
  6831. }
  6832. catch (Exception ex)
  6833. {
  6834. self.completedCount = CompleteCount;
  6835. self.completionSource.TrySetException(ex);
  6836. return;
  6837. }
  6838. self.awaiter11.SourceOnCompleted(Completed11Delegate, self);
  6839. }
  6840. return;
  6841. COMPLETE:
  6842. self.completionSource.TrySetResult(false);
  6843. return;
  6844. }
  6845. static void Completed12(object state)
  6846. {
  6847. var self = (_CombineLatest)state;
  6848. self.running12 = false;
  6849. try
  6850. {
  6851. if (self.awaiter12.GetResult())
  6852. {
  6853. self.hasCurrent12 = true;
  6854. self.current12 = self.enumerator12.Current;
  6855. goto SUCCESS;
  6856. }
  6857. else
  6858. {
  6859. self.running12 = true; // as complete, no more call MoveNextAsync.
  6860. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  6861. {
  6862. goto COMPLETE;
  6863. }
  6864. return;
  6865. }
  6866. }
  6867. catch (Exception ex)
  6868. {
  6869. self.running12 = true; // as complete, no more call MoveNextAsync.
  6870. self.completedCount = CompleteCount;
  6871. self.completionSource.TrySetException(ex);
  6872. return;
  6873. }
  6874. SUCCESS:
  6875. if (!self.TrySetResult())
  6876. {
  6877. if (self.syncRunning) return;
  6878. self.running12 = true; // as complete, no more call MoveNextAsync.
  6879. try
  6880. {
  6881. self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter();
  6882. }
  6883. catch (Exception ex)
  6884. {
  6885. self.completedCount = CompleteCount;
  6886. self.completionSource.TrySetException(ex);
  6887. return;
  6888. }
  6889. self.awaiter12.SourceOnCompleted(Completed12Delegate, self);
  6890. }
  6891. return;
  6892. COMPLETE:
  6893. self.completionSource.TrySetResult(false);
  6894. return;
  6895. }
  6896. bool TrySetResult()
  6897. {
  6898. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12)
  6899. {
  6900. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12);
  6901. completionSource.TrySetResult(true);
  6902. return true;
  6903. }
  6904. else
  6905. {
  6906. return false;
  6907. }
  6908. }
  6909. public async UniTask DisposeAsync()
  6910. {
  6911. TaskTracker.RemoveTracking(this);
  6912. if (enumerator1 != null)
  6913. {
  6914. await enumerator1.DisposeAsync();
  6915. }
  6916. if (enumerator2 != null)
  6917. {
  6918. await enumerator2.DisposeAsync();
  6919. }
  6920. if (enumerator3 != null)
  6921. {
  6922. await enumerator3.DisposeAsync();
  6923. }
  6924. if (enumerator4 != null)
  6925. {
  6926. await enumerator4.DisposeAsync();
  6927. }
  6928. if (enumerator5 != null)
  6929. {
  6930. await enumerator5.DisposeAsync();
  6931. }
  6932. if (enumerator6 != null)
  6933. {
  6934. await enumerator6.DisposeAsync();
  6935. }
  6936. if (enumerator7 != null)
  6937. {
  6938. await enumerator7.DisposeAsync();
  6939. }
  6940. if (enumerator8 != null)
  6941. {
  6942. await enumerator8.DisposeAsync();
  6943. }
  6944. if (enumerator9 != null)
  6945. {
  6946. await enumerator9.DisposeAsync();
  6947. }
  6948. if (enumerator10 != null)
  6949. {
  6950. await enumerator10.DisposeAsync();
  6951. }
  6952. if (enumerator11 != null)
  6953. {
  6954. await enumerator11.DisposeAsync();
  6955. }
  6956. if (enumerator12 != null)
  6957. {
  6958. await enumerator12.DisposeAsync();
  6959. }
  6960. }
  6961. }
  6962. }
  6963. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> : IUniTaskAsyncEnumerable<TResult>
  6964. {
  6965. readonly IUniTaskAsyncEnumerable<T1> source1;
  6966. readonly IUniTaskAsyncEnumerable<T2> source2;
  6967. readonly IUniTaskAsyncEnumerable<T3> source3;
  6968. readonly IUniTaskAsyncEnumerable<T4> source4;
  6969. readonly IUniTaskAsyncEnumerable<T5> source5;
  6970. readonly IUniTaskAsyncEnumerable<T6> source6;
  6971. readonly IUniTaskAsyncEnumerable<T7> source7;
  6972. readonly IUniTaskAsyncEnumerable<T8> source8;
  6973. readonly IUniTaskAsyncEnumerable<T9> source9;
  6974. readonly IUniTaskAsyncEnumerable<T10> source10;
  6975. readonly IUniTaskAsyncEnumerable<T11> source11;
  6976. readonly IUniTaskAsyncEnumerable<T12> source12;
  6977. readonly IUniTaskAsyncEnumerable<T13> source13;
  6978. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> resultSelector;
  6979. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> resultSelector)
  6980. {
  6981. this.source1 = source1;
  6982. this.source2 = source2;
  6983. this.source3 = source3;
  6984. this.source4 = source4;
  6985. this.source5 = source5;
  6986. this.source6 = source6;
  6987. this.source7 = source7;
  6988. this.source8 = source8;
  6989. this.source9 = source9;
  6990. this.source10 = source10;
  6991. this.source11 = source11;
  6992. this.source12 = source12;
  6993. this.source13 = source13;
  6994. this.resultSelector = resultSelector;
  6995. }
  6996. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  6997. {
  6998. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, resultSelector, cancellationToken);
  6999. }
  7000. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  7001. {
  7002. static readonly Action<object> Completed1Delegate = Completed1;
  7003. static readonly Action<object> Completed2Delegate = Completed2;
  7004. static readonly Action<object> Completed3Delegate = Completed3;
  7005. static readonly Action<object> Completed4Delegate = Completed4;
  7006. static readonly Action<object> Completed5Delegate = Completed5;
  7007. static readonly Action<object> Completed6Delegate = Completed6;
  7008. static readonly Action<object> Completed7Delegate = Completed7;
  7009. static readonly Action<object> Completed8Delegate = Completed8;
  7010. static readonly Action<object> Completed9Delegate = Completed9;
  7011. static readonly Action<object> Completed10Delegate = Completed10;
  7012. static readonly Action<object> Completed11Delegate = Completed11;
  7013. static readonly Action<object> Completed12Delegate = Completed12;
  7014. static readonly Action<object> Completed13Delegate = Completed13;
  7015. const int CompleteCount = 13;
  7016. readonly IUniTaskAsyncEnumerable<T1> source1;
  7017. readonly IUniTaskAsyncEnumerable<T2> source2;
  7018. readonly IUniTaskAsyncEnumerable<T3> source3;
  7019. readonly IUniTaskAsyncEnumerable<T4> source4;
  7020. readonly IUniTaskAsyncEnumerable<T5> source5;
  7021. readonly IUniTaskAsyncEnumerable<T6> source6;
  7022. readonly IUniTaskAsyncEnumerable<T7> source7;
  7023. readonly IUniTaskAsyncEnumerable<T8> source8;
  7024. readonly IUniTaskAsyncEnumerable<T9> source9;
  7025. readonly IUniTaskAsyncEnumerable<T10> source10;
  7026. readonly IUniTaskAsyncEnumerable<T11> source11;
  7027. readonly IUniTaskAsyncEnumerable<T12> source12;
  7028. readonly IUniTaskAsyncEnumerable<T13> source13;
  7029. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> resultSelector;
  7030. CancellationToken cancellationToken;
  7031. IUniTaskAsyncEnumerator<T1> enumerator1;
  7032. UniTask<bool>.Awaiter awaiter1;
  7033. bool hasCurrent1;
  7034. bool running1;
  7035. T1 current1;
  7036. IUniTaskAsyncEnumerator<T2> enumerator2;
  7037. UniTask<bool>.Awaiter awaiter2;
  7038. bool hasCurrent2;
  7039. bool running2;
  7040. T2 current2;
  7041. IUniTaskAsyncEnumerator<T3> enumerator3;
  7042. UniTask<bool>.Awaiter awaiter3;
  7043. bool hasCurrent3;
  7044. bool running3;
  7045. T3 current3;
  7046. IUniTaskAsyncEnumerator<T4> enumerator4;
  7047. UniTask<bool>.Awaiter awaiter4;
  7048. bool hasCurrent4;
  7049. bool running4;
  7050. T4 current4;
  7051. IUniTaskAsyncEnumerator<T5> enumerator5;
  7052. UniTask<bool>.Awaiter awaiter5;
  7053. bool hasCurrent5;
  7054. bool running5;
  7055. T5 current5;
  7056. IUniTaskAsyncEnumerator<T6> enumerator6;
  7057. UniTask<bool>.Awaiter awaiter6;
  7058. bool hasCurrent6;
  7059. bool running6;
  7060. T6 current6;
  7061. IUniTaskAsyncEnumerator<T7> enumerator7;
  7062. UniTask<bool>.Awaiter awaiter7;
  7063. bool hasCurrent7;
  7064. bool running7;
  7065. T7 current7;
  7066. IUniTaskAsyncEnumerator<T8> enumerator8;
  7067. UniTask<bool>.Awaiter awaiter8;
  7068. bool hasCurrent8;
  7069. bool running8;
  7070. T8 current8;
  7071. IUniTaskAsyncEnumerator<T9> enumerator9;
  7072. UniTask<bool>.Awaiter awaiter9;
  7073. bool hasCurrent9;
  7074. bool running9;
  7075. T9 current9;
  7076. IUniTaskAsyncEnumerator<T10> enumerator10;
  7077. UniTask<bool>.Awaiter awaiter10;
  7078. bool hasCurrent10;
  7079. bool running10;
  7080. T10 current10;
  7081. IUniTaskAsyncEnumerator<T11> enumerator11;
  7082. UniTask<bool>.Awaiter awaiter11;
  7083. bool hasCurrent11;
  7084. bool running11;
  7085. T11 current11;
  7086. IUniTaskAsyncEnumerator<T12> enumerator12;
  7087. UniTask<bool>.Awaiter awaiter12;
  7088. bool hasCurrent12;
  7089. bool running12;
  7090. T12 current12;
  7091. IUniTaskAsyncEnumerator<T13> enumerator13;
  7092. UniTask<bool>.Awaiter awaiter13;
  7093. bool hasCurrent13;
  7094. bool running13;
  7095. T13 current13;
  7096. int completedCount;
  7097. bool syncRunning;
  7098. TResult result;
  7099. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> resultSelector, CancellationToken cancellationToken)
  7100. {
  7101. this.source1 = source1;
  7102. this.source2 = source2;
  7103. this.source3 = source3;
  7104. this.source4 = source4;
  7105. this.source5 = source5;
  7106. this.source6 = source6;
  7107. this.source7 = source7;
  7108. this.source8 = source8;
  7109. this.source9 = source9;
  7110. this.source10 = source10;
  7111. this.source11 = source11;
  7112. this.source12 = source12;
  7113. this.source13 = source13;
  7114. this.resultSelector = resultSelector;
  7115. this.cancellationToken = cancellationToken;
  7116. TaskTracker.TrackActiveTask(this, 3);
  7117. }
  7118. public TResult Current => result;
  7119. public UniTask<bool> MoveNextAsync()
  7120. {
  7121. cancellationToken.ThrowIfCancellationRequested();
  7122. if (completedCount == CompleteCount) return CompletedTasks.False;
  7123. if (enumerator1 == null)
  7124. {
  7125. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  7126. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  7127. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  7128. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  7129. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  7130. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  7131. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  7132. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  7133. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  7134. enumerator10 = source10.GetAsyncEnumerator(cancellationToken);
  7135. enumerator11 = source11.GetAsyncEnumerator(cancellationToken);
  7136. enumerator12 = source12.GetAsyncEnumerator(cancellationToken);
  7137. enumerator13 = source13.GetAsyncEnumerator(cancellationToken);
  7138. }
  7139. completionSource.Reset();
  7140. AGAIN:
  7141. syncRunning = true;
  7142. if (!running1)
  7143. {
  7144. running1 = true;
  7145. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  7146. if (awaiter1.IsCompleted)
  7147. {
  7148. Completed1(this);
  7149. }
  7150. else
  7151. {
  7152. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  7153. }
  7154. }
  7155. if (!running2)
  7156. {
  7157. running2 = true;
  7158. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  7159. if (awaiter2.IsCompleted)
  7160. {
  7161. Completed2(this);
  7162. }
  7163. else
  7164. {
  7165. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  7166. }
  7167. }
  7168. if (!running3)
  7169. {
  7170. running3 = true;
  7171. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  7172. if (awaiter3.IsCompleted)
  7173. {
  7174. Completed3(this);
  7175. }
  7176. else
  7177. {
  7178. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  7179. }
  7180. }
  7181. if (!running4)
  7182. {
  7183. running4 = true;
  7184. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  7185. if (awaiter4.IsCompleted)
  7186. {
  7187. Completed4(this);
  7188. }
  7189. else
  7190. {
  7191. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  7192. }
  7193. }
  7194. if (!running5)
  7195. {
  7196. running5 = true;
  7197. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  7198. if (awaiter5.IsCompleted)
  7199. {
  7200. Completed5(this);
  7201. }
  7202. else
  7203. {
  7204. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  7205. }
  7206. }
  7207. if (!running6)
  7208. {
  7209. running6 = true;
  7210. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  7211. if (awaiter6.IsCompleted)
  7212. {
  7213. Completed6(this);
  7214. }
  7215. else
  7216. {
  7217. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  7218. }
  7219. }
  7220. if (!running7)
  7221. {
  7222. running7 = true;
  7223. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  7224. if (awaiter7.IsCompleted)
  7225. {
  7226. Completed7(this);
  7227. }
  7228. else
  7229. {
  7230. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  7231. }
  7232. }
  7233. if (!running8)
  7234. {
  7235. running8 = true;
  7236. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  7237. if (awaiter8.IsCompleted)
  7238. {
  7239. Completed8(this);
  7240. }
  7241. else
  7242. {
  7243. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  7244. }
  7245. }
  7246. if (!running9)
  7247. {
  7248. running9 = true;
  7249. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  7250. if (awaiter9.IsCompleted)
  7251. {
  7252. Completed9(this);
  7253. }
  7254. else
  7255. {
  7256. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  7257. }
  7258. }
  7259. if (!running10)
  7260. {
  7261. running10 = true;
  7262. awaiter10 = enumerator10.MoveNextAsync().GetAwaiter();
  7263. if (awaiter10.IsCompleted)
  7264. {
  7265. Completed10(this);
  7266. }
  7267. else
  7268. {
  7269. awaiter10.SourceOnCompleted(Completed10Delegate, this);
  7270. }
  7271. }
  7272. if (!running11)
  7273. {
  7274. running11 = true;
  7275. awaiter11 = enumerator11.MoveNextAsync().GetAwaiter();
  7276. if (awaiter11.IsCompleted)
  7277. {
  7278. Completed11(this);
  7279. }
  7280. else
  7281. {
  7282. awaiter11.SourceOnCompleted(Completed11Delegate, this);
  7283. }
  7284. }
  7285. if (!running12)
  7286. {
  7287. running12 = true;
  7288. awaiter12 = enumerator12.MoveNextAsync().GetAwaiter();
  7289. if (awaiter12.IsCompleted)
  7290. {
  7291. Completed12(this);
  7292. }
  7293. else
  7294. {
  7295. awaiter12.SourceOnCompleted(Completed12Delegate, this);
  7296. }
  7297. }
  7298. if (!running13)
  7299. {
  7300. running13 = true;
  7301. awaiter13 = enumerator13.MoveNextAsync().GetAwaiter();
  7302. if (awaiter13.IsCompleted)
  7303. {
  7304. Completed13(this);
  7305. }
  7306. else
  7307. {
  7308. awaiter13.SourceOnCompleted(Completed13Delegate, this);
  7309. }
  7310. }
  7311. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12 || !running13)
  7312. {
  7313. goto AGAIN;
  7314. }
  7315. syncRunning = false;
  7316. return new UniTask<bool>(this, completionSource.Version);
  7317. }
  7318. static void Completed1(object state)
  7319. {
  7320. var self = (_CombineLatest)state;
  7321. self.running1 = false;
  7322. try
  7323. {
  7324. if (self.awaiter1.GetResult())
  7325. {
  7326. self.hasCurrent1 = true;
  7327. self.current1 = self.enumerator1.Current;
  7328. goto SUCCESS;
  7329. }
  7330. else
  7331. {
  7332. self.running1 = true; // as complete, no more call MoveNextAsync.
  7333. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7334. {
  7335. goto COMPLETE;
  7336. }
  7337. return;
  7338. }
  7339. }
  7340. catch (Exception ex)
  7341. {
  7342. self.running1 = true; // as complete, no more call MoveNextAsync.
  7343. self.completedCount = CompleteCount;
  7344. self.completionSource.TrySetException(ex);
  7345. return;
  7346. }
  7347. SUCCESS:
  7348. if (!self.TrySetResult())
  7349. {
  7350. if (self.syncRunning) return;
  7351. self.running1 = true; // as complete, no more call MoveNextAsync.
  7352. try
  7353. {
  7354. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  7355. }
  7356. catch (Exception ex)
  7357. {
  7358. self.completedCount = CompleteCount;
  7359. self.completionSource.TrySetException(ex);
  7360. return;
  7361. }
  7362. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  7363. }
  7364. return;
  7365. COMPLETE:
  7366. self.completionSource.TrySetResult(false);
  7367. return;
  7368. }
  7369. static void Completed2(object state)
  7370. {
  7371. var self = (_CombineLatest)state;
  7372. self.running2 = false;
  7373. try
  7374. {
  7375. if (self.awaiter2.GetResult())
  7376. {
  7377. self.hasCurrent2 = true;
  7378. self.current2 = self.enumerator2.Current;
  7379. goto SUCCESS;
  7380. }
  7381. else
  7382. {
  7383. self.running2 = true; // as complete, no more call MoveNextAsync.
  7384. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7385. {
  7386. goto COMPLETE;
  7387. }
  7388. return;
  7389. }
  7390. }
  7391. catch (Exception ex)
  7392. {
  7393. self.running2 = true; // as complete, no more call MoveNextAsync.
  7394. self.completedCount = CompleteCount;
  7395. self.completionSource.TrySetException(ex);
  7396. return;
  7397. }
  7398. SUCCESS:
  7399. if (!self.TrySetResult())
  7400. {
  7401. if (self.syncRunning) return;
  7402. self.running2 = true; // as complete, no more call MoveNextAsync.
  7403. try
  7404. {
  7405. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  7406. }
  7407. catch (Exception ex)
  7408. {
  7409. self.completedCount = CompleteCount;
  7410. self.completionSource.TrySetException(ex);
  7411. return;
  7412. }
  7413. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  7414. }
  7415. return;
  7416. COMPLETE:
  7417. self.completionSource.TrySetResult(false);
  7418. return;
  7419. }
  7420. static void Completed3(object state)
  7421. {
  7422. var self = (_CombineLatest)state;
  7423. self.running3 = false;
  7424. try
  7425. {
  7426. if (self.awaiter3.GetResult())
  7427. {
  7428. self.hasCurrent3 = true;
  7429. self.current3 = self.enumerator3.Current;
  7430. goto SUCCESS;
  7431. }
  7432. else
  7433. {
  7434. self.running3 = true; // as complete, no more call MoveNextAsync.
  7435. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7436. {
  7437. goto COMPLETE;
  7438. }
  7439. return;
  7440. }
  7441. }
  7442. catch (Exception ex)
  7443. {
  7444. self.running3 = true; // as complete, no more call MoveNextAsync.
  7445. self.completedCount = CompleteCount;
  7446. self.completionSource.TrySetException(ex);
  7447. return;
  7448. }
  7449. SUCCESS:
  7450. if (!self.TrySetResult())
  7451. {
  7452. if (self.syncRunning) return;
  7453. self.running3 = true; // as complete, no more call MoveNextAsync.
  7454. try
  7455. {
  7456. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  7457. }
  7458. catch (Exception ex)
  7459. {
  7460. self.completedCount = CompleteCount;
  7461. self.completionSource.TrySetException(ex);
  7462. return;
  7463. }
  7464. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  7465. }
  7466. return;
  7467. COMPLETE:
  7468. self.completionSource.TrySetResult(false);
  7469. return;
  7470. }
  7471. static void Completed4(object state)
  7472. {
  7473. var self = (_CombineLatest)state;
  7474. self.running4 = false;
  7475. try
  7476. {
  7477. if (self.awaiter4.GetResult())
  7478. {
  7479. self.hasCurrent4 = true;
  7480. self.current4 = self.enumerator4.Current;
  7481. goto SUCCESS;
  7482. }
  7483. else
  7484. {
  7485. self.running4 = true; // as complete, no more call MoveNextAsync.
  7486. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7487. {
  7488. goto COMPLETE;
  7489. }
  7490. return;
  7491. }
  7492. }
  7493. catch (Exception ex)
  7494. {
  7495. self.running4 = true; // as complete, no more call MoveNextAsync.
  7496. self.completedCount = CompleteCount;
  7497. self.completionSource.TrySetException(ex);
  7498. return;
  7499. }
  7500. SUCCESS:
  7501. if (!self.TrySetResult())
  7502. {
  7503. if (self.syncRunning) return;
  7504. self.running4 = true; // as complete, no more call MoveNextAsync.
  7505. try
  7506. {
  7507. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  7508. }
  7509. catch (Exception ex)
  7510. {
  7511. self.completedCount = CompleteCount;
  7512. self.completionSource.TrySetException(ex);
  7513. return;
  7514. }
  7515. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  7516. }
  7517. return;
  7518. COMPLETE:
  7519. self.completionSource.TrySetResult(false);
  7520. return;
  7521. }
  7522. static void Completed5(object state)
  7523. {
  7524. var self = (_CombineLatest)state;
  7525. self.running5 = false;
  7526. try
  7527. {
  7528. if (self.awaiter5.GetResult())
  7529. {
  7530. self.hasCurrent5 = true;
  7531. self.current5 = self.enumerator5.Current;
  7532. goto SUCCESS;
  7533. }
  7534. else
  7535. {
  7536. self.running5 = true; // as complete, no more call MoveNextAsync.
  7537. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7538. {
  7539. goto COMPLETE;
  7540. }
  7541. return;
  7542. }
  7543. }
  7544. catch (Exception ex)
  7545. {
  7546. self.running5 = true; // as complete, no more call MoveNextAsync.
  7547. self.completedCount = CompleteCount;
  7548. self.completionSource.TrySetException(ex);
  7549. return;
  7550. }
  7551. SUCCESS:
  7552. if (!self.TrySetResult())
  7553. {
  7554. if (self.syncRunning) return;
  7555. self.running5 = true; // as complete, no more call MoveNextAsync.
  7556. try
  7557. {
  7558. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  7559. }
  7560. catch (Exception ex)
  7561. {
  7562. self.completedCount = CompleteCount;
  7563. self.completionSource.TrySetException(ex);
  7564. return;
  7565. }
  7566. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  7567. }
  7568. return;
  7569. COMPLETE:
  7570. self.completionSource.TrySetResult(false);
  7571. return;
  7572. }
  7573. static void Completed6(object state)
  7574. {
  7575. var self = (_CombineLatest)state;
  7576. self.running6 = false;
  7577. try
  7578. {
  7579. if (self.awaiter6.GetResult())
  7580. {
  7581. self.hasCurrent6 = true;
  7582. self.current6 = self.enumerator6.Current;
  7583. goto SUCCESS;
  7584. }
  7585. else
  7586. {
  7587. self.running6 = true; // as complete, no more call MoveNextAsync.
  7588. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7589. {
  7590. goto COMPLETE;
  7591. }
  7592. return;
  7593. }
  7594. }
  7595. catch (Exception ex)
  7596. {
  7597. self.running6 = true; // as complete, no more call MoveNextAsync.
  7598. self.completedCount = CompleteCount;
  7599. self.completionSource.TrySetException(ex);
  7600. return;
  7601. }
  7602. SUCCESS:
  7603. if (!self.TrySetResult())
  7604. {
  7605. if (self.syncRunning) return;
  7606. self.running6 = true; // as complete, no more call MoveNextAsync.
  7607. try
  7608. {
  7609. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  7610. }
  7611. catch (Exception ex)
  7612. {
  7613. self.completedCount = CompleteCount;
  7614. self.completionSource.TrySetException(ex);
  7615. return;
  7616. }
  7617. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  7618. }
  7619. return;
  7620. COMPLETE:
  7621. self.completionSource.TrySetResult(false);
  7622. return;
  7623. }
  7624. static void Completed7(object state)
  7625. {
  7626. var self = (_CombineLatest)state;
  7627. self.running7 = false;
  7628. try
  7629. {
  7630. if (self.awaiter7.GetResult())
  7631. {
  7632. self.hasCurrent7 = true;
  7633. self.current7 = self.enumerator7.Current;
  7634. goto SUCCESS;
  7635. }
  7636. else
  7637. {
  7638. self.running7 = true; // as complete, no more call MoveNextAsync.
  7639. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7640. {
  7641. goto COMPLETE;
  7642. }
  7643. return;
  7644. }
  7645. }
  7646. catch (Exception ex)
  7647. {
  7648. self.running7 = true; // as complete, no more call MoveNextAsync.
  7649. self.completedCount = CompleteCount;
  7650. self.completionSource.TrySetException(ex);
  7651. return;
  7652. }
  7653. SUCCESS:
  7654. if (!self.TrySetResult())
  7655. {
  7656. if (self.syncRunning) return;
  7657. self.running7 = true; // as complete, no more call MoveNextAsync.
  7658. try
  7659. {
  7660. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  7661. }
  7662. catch (Exception ex)
  7663. {
  7664. self.completedCount = CompleteCount;
  7665. self.completionSource.TrySetException(ex);
  7666. return;
  7667. }
  7668. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  7669. }
  7670. return;
  7671. COMPLETE:
  7672. self.completionSource.TrySetResult(false);
  7673. return;
  7674. }
  7675. static void Completed8(object state)
  7676. {
  7677. var self = (_CombineLatest)state;
  7678. self.running8 = false;
  7679. try
  7680. {
  7681. if (self.awaiter8.GetResult())
  7682. {
  7683. self.hasCurrent8 = true;
  7684. self.current8 = self.enumerator8.Current;
  7685. goto SUCCESS;
  7686. }
  7687. else
  7688. {
  7689. self.running8 = true; // as complete, no more call MoveNextAsync.
  7690. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7691. {
  7692. goto COMPLETE;
  7693. }
  7694. return;
  7695. }
  7696. }
  7697. catch (Exception ex)
  7698. {
  7699. self.running8 = true; // as complete, no more call MoveNextAsync.
  7700. self.completedCount = CompleteCount;
  7701. self.completionSource.TrySetException(ex);
  7702. return;
  7703. }
  7704. SUCCESS:
  7705. if (!self.TrySetResult())
  7706. {
  7707. if (self.syncRunning) return;
  7708. self.running8 = true; // as complete, no more call MoveNextAsync.
  7709. try
  7710. {
  7711. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  7712. }
  7713. catch (Exception ex)
  7714. {
  7715. self.completedCount = CompleteCount;
  7716. self.completionSource.TrySetException(ex);
  7717. return;
  7718. }
  7719. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  7720. }
  7721. return;
  7722. COMPLETE:
  7723. self.completionSource.TrySetResult(false);
  7724. return;
  7725. }
  7726. static void Completed9(object state)
  7727. {
  7728. var self = (_CombineLatest)state;
  7729. self.running9 = false;
  7730. try
  7731. {
  7732. if (self.awaiter9.GetResult())
  7733. {
  7734. self.hasCurrent9 = true;
  7735. self.current9 = self.enumerator9.Current;
  7736. goto SUCCESS;
  7737. }
  7738. else
  7739. {
  7740. self.running9 = true; // as complete, no more call MoveNextAsync.
  7741. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7742. {
  7743. goto COMPLETE;
  7744. }
  7745. return;
  7746. }
  7747. }
  7748. catch (Exception ex)
  7749. {
  7750. self.running9 = true; // as complete, no more call MoveNextAsync.
  7751. self.completedCount = CompleteCount;
  7752. self.completionSource.TrySetException(ex);
  7753. return;
  7754. }
  7755. SUCCESS:
  7756. if (!self.TrySetResult())
  7757. {
  7758. if (self.syncRunning) return;
  7759. self.running9 = true; // as complete, no more call MoveNextAsync.
  7760. try
  7761. {
  7762. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  7763. }
  7764. catch (Exception ex)
  7765. {
  7766. self.completedCount = CompleteCount;
  7767. self.completionSource.TrySetException(ex);
  7768. return;
  7769. }
  7770. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  7771. }
  7772. return;
  7773. COMPLETE:
  7774. self.completionSource.TrySetResult(false);
  7775. return;
  7776. }
  7777. static void Completed10(object state)
  7778. {
  7779. var self = (_CombineLatest)state;
  7780. self.running10 = false;
  7781. try
  7782. {
  7783. if (self.awaiter10.GetResult())
  7784. {
  7785. self.hasCurrent10 = true;
  7786. self.current10 = self.enumerator10.Current;
  7787. goto SUCCESS;
  7788. }
  7789. else
  7790. {
  7791. self.running10 = true; // as complete, no more call MoveNextAsync.
  7792. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7793. {
  7794. goto COMPLETE;
  7795. }
  7796. return;
  7797. }
  7798. }
  7799. catch (Exception ex)
  7800. {
  7801. self.running10 = true; // as complete, no more call MoveNextAsync.
  7802. self.completedCount = CompleteCount;
  7803. self.completionSource.TrySetException(ex);
  7804. return;
  7805. }
  7806. SUCCESS:
  7807. if (!self.TrySetResult())
  7808. {
  7809. if (self.syncRunning) return;
  7810. self.running10 = true; // as complete, no more call MoveNextAsync.
  7811. try
  7812. {
  7813. self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter();
  7814. }
  7815. catch (Exception ex)
  7816. {
  7817. self.completedCount = CompleteCount;
  7818. self.completionSource.TrySetException(ex);
  7819. return;
  7820. }
  7821. self.awaiter10.SourceOnCompleted(Completed10Delegate, self);
  7822. }
  7823. return;
  7824. COMPLETE:
  7825. self.completionSource.TrySetResult(false);
  7826. return;
  7827. }
  7828. static void Completed11(object state)
  7829. {
  7830. var self = (_CombineLatest)state;
  7831. self.running11 = false;
  7832. try
  7833. {
  7834. if (self.awaiter11.GetResult())
  7835. {
  7836. self.hasCurrent11 = true;
  7837. self.current11 = self.enumerator11.Current;
  7838. goto SUCCESS;
  7839. }
  7840. else
  7841. {
  7842. self.running11 = true; // as complete, no more call MoveNextAsync.
  7843. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7844. {
  7845. goto COMPLETE;
  7846. }
  7847. return;
  7848. }
  7849. }
  7850. catch (Exception ex)
  7851. {
  7852. self.running11 = true; // as complete, no more call MoveNextAsync.
  7853. self.completedCount = CompleteCount;
  7854. self.completionSource.TrySetException(ex);
  7855. return;
  7856. }
  7857. SUCCESS:
  7858. if (!self.TrySetResult())
  7859. {
  7860. if (self.syncRunning) return;
  7861. self.running11 = true; // as complete, no more call MoveNextAsync.
  7862. try
  7863. {
  7864. self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter();
  7865. }
  7866. catch (Exception ex)
  7867. {
  7868. self.completedCount = CompleteCount;
  7869. self.completionSource.TrySetException(ex);
  7870. return;
  7871. }
  7872. self.awaiter11.SourceOnCompleted(Completed11Delegate, self);
  7873. }
  7874. return;
  7875. COMPLETE:
  7876. self.completionSource.TrySetResult(false);
  7877. return;
  7878. }
  7879. static void Completed12(object state)
  7880. {
  7881. var self = (_CombineLatest)state;
  7882. self.running12 = false;
  7883. try
  7884. {
  7885. if (self.awaiter12.GetResult())
  7886. {
  7887. self.hasCurrent12 = true;
  7888. self.current12 = self.enumerator12.Current;
  7889. goto SUCCESS;
  7890. }
  7891. else
  7892. {
  7893. self.running12 = true; // as complete, no more call MoveNextAsync.
  7894. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7895. {
  7896. goto COMPLETE;
  7897. }
  7898. return;
  7899. }
  7900. }
  7901. catch (Exception ex)
  7902. {
  7903. self.running12 = true; // as complete, no more call MoveNextAsync.
  7904. self.completedCount = CompleteCount;
  7905. self.completionSource.TrySetException(ex);
  7906. return;
  7907. }
  7908. SUCCESS:
  7909. if (!self.TrySetResult())
  7910. {
  7911. if (self.syncRunning) return;
  7912. self.running12 = true; // as complete, no more call MoveNextAsync.
  7913. try
  7914. {
  7915. self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter();
  7916. }
  7917. catch (Exception ex)
  7918. {
  7919. self.completedCount = CompleteCount;
  7920. self.completionSource.TrySetException(ex);
  7921. return;
  7922. }
  7923. self.awaiter12.SourceOnCompleted(Completed12Delegate, self);
  7924. }
  7925. return;
  7926. COMPLETE:
  7927. self.completionSource.TrySetResult(false);
  7928. return;
  7929. }
  7930. static void Completed13(object state)
  7931. {
  7932. var self = (_CombineLatest)state;
  7933. self.running13 = false;
  7934. try
  7935. {
  7936. if (self.awaiter13.GetResult())
  7937. {
  7938. self.hasCurrent13 = true;
  7939. self.current13 = self.enumerator13.Current;
  7940. goto SUCCESS;
  7941. }
  7942. else
  7943. {
  7944. self.running13 = true; // as complete, no more call MoveNextAsync.
  7945. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  7946. {
  7947. goto COMPLETE;
  7948. }
  7949. return;
  7950. }
  7951. }
  7952. catch (Exception ex)
  7953. {
  7954. self.running13 = true; // as complete, no more call MoveNextAsync.
  7955. self.completedCount = CompleteCount;
  7956. self.completionSource.TrySetException(ex);
  7957. return;
  7958. }
  7959. SUCCESS:
  7960. if (!self.TrySetResult())
  7961. {
  7962. if (self.syncRunning) return;
  7963. self.running13 = true; // as complete, no more call MoveNextAsync.
  7964. try
  7965. {
  7966. self.awaiter13 = self.enumerator13.MoveNextAsync().GetAwaiter();
  7967. }
  7968. catch (Exception ex)
  7969. {
  7970. self.completedCount = CompleteCount;
  7971. self.completionSource.TrySetException(ex);
  7972. return;
  7973. }
  7974. self.awaiter13.SourceOnCompleted(Completed13Delegate, self);
  7975. }
  7976. return;
  7977. COMPLETE:
  7978. self.completionSource.TrySetResult(false);
  7979. return;
  7980. }
  7981. bool TrySetResult()
  7982. {
  7983. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12 && hasCurrent13)
  7984. {
  7985. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12, current13);
  7986. completionSource.TrySetResult(true);
  7987. return true;
  7988. }
  7989. else
  7990. {
  7991. return false;
  7992. }
  7993. }
  7994. public async UniTask DisposeAsync()
  7995. {
  7996. TaskTracker.RemoveTracking(this);
  7997. if (enumerator1 != null)
  7998. {
  7999. await enumerator1.DisposeAsync();
  8000. }
  8001. if (enumerator2 != null)
  8002. {
  8003. await enumerator2.DisposeAsync();
  8004. }
  8005. if (enumerator3 != null)
  8006. {
  8007. await enumerator3.DisposeAsync();
  8008. }
  8009. if (enumerator4 != null)
  8010. {
  8011. await enumerator4.DisposeAsync();
  8012. }
  8013. if (enumerator5 != null)
  8014. {
  8015. await enumerator5.DisposeAsync();
  8016. }
  8017. if (enumerator6 != null)
  8018. {
  8019. await enumerator6.DisposeAsync();
  8020. }
  8021. if (enumerator7 != null)
  8022. {
  8023. await enumerator7.DisposeAsync();
  8024. }
  8025. if (enumerator8 != null)
  8026. {
  8027. await enumerator8.DisposeAsync();
  8028. }
  8029. if (enumerator9 != null)
  8030. {
  8031. await enumerator9.DisposeAsync();
  8032. }
  8033. if (enumerator10 != null)
  8034. {
  8035. await enumerator10.DisposeAsync();
  8036. }
  8037. if (enumerator11 != null)
  8038. {
  8039. await enumerator11.DisposeAsync();
  8040. }
  8041. if (enumerator12 != null)
  8042. {
  8043. await enumerator12.DisposeAsync();
  8044. }
  8045. if (enumerator13 != null)
  8046. {
  8047. await enumerator13.DisposeAsync();
  8048. }
  8049. }
  8050. }
  8051. }
  8052. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> : IUniTaskAsyncEnumerable<TResult>
  8053. {
  8054. readonly IUniTaskAsyncEnumerable<T1> source1;
  8055. readonly IUniTaskAsyncEnumerable<T2> source2;
  8056. readonly IUniTaskAsyncEnumerable<T3> source3;
  8057. readonly IUniTaskAsyncEnumerable<T4> source4;
  8058. readonly IUniTaskAsyncEnumerable<T5> source5;
  8059. readonly IUniTaskAsyncEnumerable<T6> source6;
  8060. readonly IUniTaskAsyncEnumerable<T7> source7;
  8061. readonly IUniTaskAsyncEnumerable<T8> source8;
  8062. readonly IUniTaskAsyncEnumerable<T9> source9;
  8063. readonly IUniTaskAsyncEnumerable<T10> source10;
  8064. readonly IUniTaskAsyncEnumerable<T11> source11;
  8065. readonly IUniTaskAsyncEnumerable<T12> source12;
  8066. readonly IUniTaskAsyncEnumerable<T13> source13;
  8067. readonly IUniTaskAsyncEnumerable<T14> source14;
  8068. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> resultSelector;
  8069. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, IUniTaskAsyncEnumerable<T14> source14, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> resultSelector)
  8070. {
  8071. this.source1 = source1;
  8072. this.source2 = source2;
  8073. this.source3 = source3;
  8074. this.source4 = source4;
  8075. this.source5 = source5;
  8076. this.source6 = source6;
  8077. this.source7 = source7;
  8078. this.source8 = source8;
  8079. this.source9 = source9;
  8080. this.source10 = source10;
  8081. this.source11 = source11;
  8082. this.source12 = source12;
  8083. this.source13 = source13;
  8084. this.source14 = source14;
  8085. this.resultSelector = resultSelector;
  8086. }
  8087. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  8088. {
  8089. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, resultSelector, cancellationToken);
  8090. }
  8091. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  8092. {
  8093. static readonly Action<object> Completed1Delegate = Completed1;
  8094. static readonly Action<object> Completed2Delegate = Completed2;
  8095. static readonly Action<object> Completed3Delegate = Completed3;
  8096. static readonly Action<object> Completed4Delegate = Completed4;
  8097. static readonly Action<object> Completed5Delegate = Completed5;
  8098. static readonly Action<object> Completed6Delegate = Completed6;
  8099. static readonly Action<object> Completed7Delegate = Completed7;
  8100. static readonly Action<object> Completed8Delegate = Completed8;
  8101. static readonly Action<object> Completed9Delegate = Completed9;
  8102. static readonly Action<object> Completed10Delegate = Completed10;
  8103. static readonly Action<object> Completed11Delegate = Completed11;
  8104. static readonly Action<object> Completed12Delegate = Completed12;
  8105. static readonly Action<object> Completed13Delegate = Completed13;
  8106. static readonly Action<object> Completed14Delegate = Completed14;
  8107. const int CompleteCount = 14;
  8108. readonly IUniTaskAsyncEnumerable<T1> source1;
  8109. readonly IUniTaskAsyncEnumerable<T2> source2;
  8110. readonly IUniTaskAsyncEnumerable<T3> source3;
  8111. readonly IUniTaskAsyncEnumerable<T4> source4;
  8112. readonly IUniTaskAsyncEnumerable<T5> source5;
  8113. readonly IUniTaskAsyncEnumerable<T6> source6;
  8114. readonly IUniTaskAsyncEnumerable<T7> source7;
  8115. readonly IUniTaskAsyncEnumerable<T8> source8;
  8116. readonly IUniTaskAsyncEnumerable<T9> source9;
  8117. readonly IUniTaskAsyncEnumerable<T10> source10;
  8118. readonly IUniTaskAsyncEnumerable<T11> source11;
  8119. readonly IUniTaskAsyncEnumerable<T12> source12;
  8120. readonly IUniTaskAsyncEnumerable<T13> source13;
  8121. readonly IUniTaskAsyncEnumerable<T14> source14;
  8122. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> resultSelector;
  8123. CancellationToken cancellationToken;
  8124. IUniTaskAsyncEnumerator<T1> enumerator1;
  8125. UniTask<bool>.Awaiter awaiter1;
  8126. bool hasCurrent1;
  8127. bool running1;
  8128. T1 current1;
  8129. IUniTaskAsyncEnumerator<T2> enumerator2;
  8130. UniTask<bool>.Awaiter awaiter2;
  8131. bool hasCurrent2;
  8132. bool running2;
  8133. T2 current2;
  8134. IUniTaskAsyncEnumerator<T3> enumerator3;
  8135. UniTask<bool>.Awaiter awaiter3;
  8136. bool hasCurrent3;
  8137. bool running3;
  8138. T3 current3;
  8139. IUniTaskAsyncEnumerator<T4> enumerator4;
  8140. UniTask<bool>.Awaiter awaiter4;
  8141. bool hasCurrent4;
  8142. bool running4;
  8143. T4 current4;
  8144. IUniTaskAsyncEnumerator<T5> enumerator5;
  8145. UniTask<bool>.Awaiter awaiter5;
  8146. bool hasCurrent5;
  8147. bool running5;
  8148. T5 current5;
  8149. IUniTaskAsyncEnumerator<T6> enumerator6;
  8150. UniTask<bool>.Awaiter awaiter6;
  8151. bool hasCurrent6;
  8152. bool running6;
  8153. T6 current6;
  8154. IUniTaskAsyncEnumerator<T7> enumerator7;
  8155. UniTask<bool>.Awaiter awaiter7;
  8156. bool hasCurrent7;
  8157. bool running7;
  8158. T7 current7;
  8159. IUniTaskAsyncEnumerator<T8> enumerator8;
  8160. UniTask<bool>.Awaiter awaiter8;
  8161. bool hasCurrent8;
  8162. bool running8;
  8163. T8 current8;
  8164. IUniTaskAsyncEnumerator<T9> enumerator9;
  8165. UniTask<bool>.Awaiter awaiter9;
  8166. bool hasCurrent9;
  8167. bool running9;
  8168. T9 current9;
  8169. IUniTaskAsyncEnumerator<T10> enumerator10;
  8170. UniTask<bool>.Awaiter awaiter10;
  8171. bool hasCurrent10;
  8172. bool running10;
  8173. T10 current10;
  8174. IUniTaskAsyncEnumerator<T11> enumerator11;
  8175. UniTask<bool>.Awaiter awaiter11;
  8176. bool hasCurrent11;
  8177. bool running11;
  8178. T11 current11;
  8179. IUniTaskAsyncEnumerator<T12> enumerator12;
  8180. UniTask<bool>.Awaiter awaiter12;
  8181. bool hasCurrent12;
  8182. bool running12;
  8183. T12 current12;
  8184. IUniTaskAsyncEnumerator<T13> enumerator13;
  8185. UniTask<bool>.Awaiter awaiter13;
  8186. bool hasCurrent13;
  8187. bool running13;
  8188. T13 current13;
  8189. IUniTaskAsyncEnumerator<T14> enumerator14;
  8190. UniTask<bool>.Awaiter awaiter14;
  8191. bool hasCurrent14;
  8192. bool running14;
  8193. T14 current14;
  8194. int completedCount;
  8195. bool syncRunning;
  8196. TResult result;
  8197. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, IUniTaskAsyncEnumerable<T14> source14, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> resultSelector, CancellationToken cancellationToken)
  8198. {
  8199. this.source1 = source1;
  8200. this.source2 = source2;
  8201. this.source3 = source3;
  8202. this.source4 = source4;
  8203. this.source5 = source5;
  8204. this.source6 = source6;
  8205. this.source7 = source7;
  8206. this.source8 = source8;
  8207. this.source9 = source9;
  8208. this.source10 = source10;
  8209. this.source11 = source11;
  8210. this.source12 = source12;
  8211. this.source13 = source13;
  8212. this.source14 = source14;
  8213. this.resultSelector = resultSelector;
  8214. this.cancellationToken = cancellationToken;
  8215. TaskTracker.TrackActiveTask(this, 3);
  8216. }
  8217. public TResult Current => result;
  8218. public UniTask<bool> MoveNextAsync()
  8219. {
  8220. cancellationToken.ThrowIfCancellationRequested();
  8221. if (completedCount == CompleteCount) return CompletedTasks.False;
  8222. if (enumerator1 == null)
  8223. {
  8224. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  8225. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  8226. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  8227. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  8228. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  8229. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  8230. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  8231. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  8232. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  8233. enumerator10 = source10.GetAsyncEnumerator(cancellationToken);
  8234. enumerator11 = source11.GetAsyncEnumerator(cancellationToken);
  8235. enumerator12 = source12.GetAsyncEnumerator(cancellationToken);
  8236. enumerator13 = source13.GetAsyncEnumerator(cancellationToken);
  8237. enumerator14 = source14.GetAsyncEnumerator(cancellationToken);
  8238. }
  8239. completionSource.Reset();
  8240. AGAIN:
  8241. syncRunning = true;
  8242. if (!running1)
  8243. {
  8244. running1 = true;
  8245. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  8246. if (awaiter1.IsCompleted)
  8247. {
  8248. Completed1(this);
  8249. }
  8250. else
  8251. {
  8252. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  8253. }
  8254. }
  8255. if (!running2)
  8256. {
  8257. running2 = true;
  8258. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  8259. if (awaiter2.IsCompleted)
  8260. {
  8261. Completed2(this);
  8262. }
  8263. else
  8264. {
  8265. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  8266. }
  8267. }
  8268. if (!running3)
  8269. {
  8270. running3 = true;
  8271. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  8272. if (awaiter3.IsCompleted)
  8273. {
  8274. Completed3(this);
  8275. }
  8276. else
  8277. {
  8278. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  8279. }
  8280. }
  8281. if (!running4)
  8282. {
  8283. running4 = true;
  8284. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  8285. if (awaiter4.IsCompleted)
  8286. {
  8287. Completed4(this);
  8288. }
  8289. else
  8290. {
  8291. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  8292. }
  8293. }
  8294. if (!running5)
  8295. {
  8296. running5 = true;
  8297. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  8298. if (awaiter5.IsCompleted)
  8299. {
  8300. Completed5(this);
  8301. }
  8302. else
  8303. {
  8304. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  8305. }
  8306. }
  8307. if (!running6)
  8308. {
  8309. running6 = true;
  8310. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  8311. if (awaiter6.IsCompleted)
  8312. {
  8313. Completed6(this);
  8314. }
  8315. else
  8316. {
  8317. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  8318. }
  8319. }
  8320. if (!running7)
  8321. {
  8322. running7 = true;
  8323. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  8324. if (awaiter7.IsCompleted)
  8325. {
  8326. Completed7(this);
  8327. }
  8328. else
  8329. {
  8330. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  8331. }
  8332. }
  8333. if (!running8)
  8334. {
  8335. running8 = true;
  8336. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  8337. if (awaiter8.IsCompleted)
  8338. {
  8339. Completed8(this);
  8340. }
  8341. else
  8342. {
  8343. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  8344. }
  8345. }
  8346. if (!running9)
  8347. {
  8348. running9 = true;
  8349. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  8350. if (awaiter9.IsCompleted)
  8351. {
  8352. Completed9(this);
  8353. }
  8354. else
  8355. {
  8356. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  8357. }
  8358. }
  8359. if (!running10)
  8360. {
  8361. running10 = true;
  8362. awaiter10 = enumerator10.MoveNextAsync().GetAwaiter();
  8363. if (awaiter10.IsCompleted)
  8364. {
  8365. Completed10(this);
  8366. }
  8367. else
  8368. {
  8369. awaiter10.SourceOnCompleted(Completed10Delegate, this);
  8370. }
  8371. }
  8372. if (!running11)
  8373. {
  8374. running11 = true;
  8375. awaiter11 = enumerator11.MoveNextAsync().GetAwaiter();
  8376. if (awaiter11.IsCompleted)
  8377. {
  8378. Completed11(this);
  8379. }
  8380. else
  8381. {
  8382. awaiter11.SourceOnCompleted(Completed11Delegate, this);
  8383. }
  8384. }
  8385. if (!running12)
  8386. {
  8387. running12 = true;
  8388. awaiter12 = enumerator12.MoveNextAsync().GetAwaiter();
  8389. if (awaiter12.IsCompleted)
  8390. {
  8391. Completed12(this);
  8392. }
  8393. else
  8394. {
  8395. awaiter12.SourceOnCompleted(Completed12Delegate, this);
  8396. }
  8397. }
  8398. if (!running13)
  8399. {
  8400. running13 = true;
  8401. awaiter13 = enumerator13.MoveNextAsync().GetAwaiter();
  8402. if (awaiter13.IsCompleted)
  8403. {
  8404. Completed13(this);
  8405. }
  8406. else
  8407. {
  8408. awaiter13.SourceOnCompleted(Completed13Delegate, this);
  8409. }
  8410. }
  8411. if (!running14)
  8412. {
  8413. running14 = true;
  8414. awaiter14 = enumerator14.MoveNextAsync().GetAwaiter();
  8415. if (awaiter14.IsCompleted)
  8416. {
  8417. Completed14(this);
  8418. }
  8419. else
  8420. {
  8421. awaiter14.SourceOnCompleted(Completed14Delegate, this);
  8422. }
  8423. }
  8424. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12 || !running13 || !running14)
  8425. {
  8426. goto AGAIN;
  8427. }
  8428. syncRunning = false;
  8429. return new UniTask<bool>(this, completionSource.Version);
  8430. }
  8431. static void Completed1(object state)
  8432. {
  8433. var self = (_CombineLatest)state;
  8434. self.running1 = false;
  8435. try
  8436. {
  8437. if (self.awaiter1.GetResult())
  8438. {
  8439. self.hasCurrent1 = true;
  8440. self.current1 = self.enumerator1.Current;
  8441. goto SUCCESS;
  8442. }
  8443. else
  8444. {
  8445. self.running1 = true; // as complete, no more call MoveNextAsync.
  8446. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8447. {
  8448. goto COMPLETE;
  8449. }
  8450. return;
  8451. }
  8452. }
  8453. catch (Exception ex)
  8454. {
  8455. self.running1 = true; // as complete, no more call MoveNextAsync.
  8456. self.completedCount = CompleteCount;
  8457. self.completionSource.TrySetException(ex);
  8458. return;
  8459. }
  8460. SUCCESS:
  8461. if (!self.TrySetResult())
  8462. {
  8463. if (self.syncRunning) return;
  8464. self.running1 = true; // as complete, no more call MoveNextAsync.
  8465. try
  8466. {
  8467. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  8468. }
  8469. catch (Exception ex)
  8470. {
  8471. self.completedCount = CompleteCount;
  8472. self.completionSource.TrySetException(ex);
  8473. return;
  8474. }
  8475. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  8476. }
  8477. return;
  8478. COMPLETE:
  8479. self.completionSource.TrySetResult(false);
  8480. return;
  8481. }
  8482. static void Completed2(object state)
  8483. {
  8484. var self = (_CombineLatest)state;
  8485. self.running2 = false;
  8486. try
  8487. {
  8488. if (self.awaiter2.GetResult())
  8489. {
  8490. self.hasCurrent2 = true;
  8491. self.current2 = self.enumerator2.Current;
  8492. goto SUCCESS;
  8493. }
  8494. else
  8495. {
  8496. self.running2 = true; // as complete, no more call MoveNextAsync.
  8497. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8498. {
  8499. goto COMPLETE;
  8500. }
  8501. return;
  8502. }
  8503. }
  8504. catch (Exception ex)
  8505. {
  8506. self.running2 = true; // as complete, no more call MoveNextAsync.
  8507. self.completedCount = CompleteCount;
  8508. self.completionSource.TrySetException(ex);
  8509. return;
  8510. }
  8511. SUCCESS:
  8512. if (!self.TrySetResult())
  8513. {
  8514. if (self.syncRunning) return;
  8515. self.running2 = true; // as complete, no more call MoveNextAsync.
  8516. try
  8517. {
  8518. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  8519. }
  8520. catch (Exception ex)
  8521. {
  8522. self.completedCount = CompleteCount;
  8523. self.completionSource.TrySetException(ex);
  8524. return;
  8525. }
  8526. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  8527. }
  8528. return;
  8529. COMPLETE:
  8530. self.completionSource.TrySetResult(false);
  8531. return;
  8532. }
  8533. static void Completed3(object state)
  8534. {
  8535. var self = (_CombineLatest)state;
  8536. self.running3 = false;
  8537. try
  8538. {
  8539. if (self.awaiter3.GetResult())
  8540. {
  8541. self.hasCurrent3 = true;
  8542. self.current3 = self.enumerator3.Current;
  8543. goto SUCCESS;
  8544. }
  8545. else
  8546. {
  8547. self.running3 = true; // as complete, no more call MoveNextAsync.
  8548. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8549. {
  8550. goto COMPLETE;
  8551. }
  8552. return;
  8553. }
  8554. }
  8555. catch (Exception ex)
  8556. {
  8557. self.running3 = true; // as complete, no more call MoveNextAsync.
  8558. self.completedCount = CompleteCount;
  8559. self.completionSource.TrySetException(ex);
  8560. return;
  8561. }
  8562. SUCCESS:
  8563. if (!self.TrySetResult())
  8564. {
  8565. if (self.syncRunning) return;
  8566. self.running3 = true; // as complete, no more call MoveNextAsync.
  8567. try
  8568. {
  8569. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  8570. }
  8571. catch (Exception ex)
  8572. {
  8573. self.completedCount = CompleteCount;
  8574. self.completionSource.TrySetException(ex);
  8575. return;
  8576. }
  8577. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  8578. }
  8579. return;
  8580. COMPLETE:
  8581. self.completionSource.TrySetResult(false);
  8582. return;
  8583. }
  8584. static void Completed4(object state)
  8585. {
  8586. var self = (_CombineLatest)state;
  8587. self.running4 = false;
  8588. try
  8589. {
  8590. if (self.awaiter4.GetResult())
  8591. {
  8592. self.hasCurrent4 = true;
  8593. self.current4 = self.enumerator4.Current;
  8594. goto SUCCESS;
  8595. }
  8596. else
  8597. {
  8598. self.running4 = true; // as complete, no more call MoveNextAsync.
  8599. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8600. {
  8601. goto COMPLETE;
  8602. }
  8603. return;
  8604. }
  8605. }
  8606. catch (Exception ex)
  8607. {
  8608. self.running4 = true; // as complete, no more call MoveNextAsync.
  8609. self.completedCount = CompleteCount;
  8610. self.completionSource.TrySetException(ex);
  8611. return;
  8612. }
  8613. SUCCESS:
  8614. if (!self.TrySetResult())
  8615. {
  8616. if (self.syncRunning) return;
  8617. self.running4 = true; // as complete, no more call MoveNextAsync.
  8618. try
  8619. {
  8620. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  8621. }
  8622. catch (Exception ex)
  8623. {
  8624. self.completedCount = CompleteCount;
  8625. self.completionSource.TrySetException(ex);
  8626. return;
  8627. }
  8628. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  8629. }
  8630. return;
  8631. COMPLETE:
  8632. self.completionSource.TrySetResult(false);
  8633. return;
  8634. }
  8635. static void Completed5(object state)
  8636. {
  8637. var self = (_CombineLatest)state;
  8638. self.running5 = false;
  8639. try
  8640. {
  8641. if (self.awaiter5.GetResult())
  8642. {
  8643. self.hasCurrent5 = true;
  8644. self.current5 = self.enumerator5.Current;
  8645. goto SUCCESS;
  8646. }
  8647. else
  8648. {
  8649. self.running5 = true; // as complete, no more call MoveNextAsync.
  8650. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8651. {
  8652. goto COMPLETE;
  8653. }
  8654. return;
  8655. }
  8656. }
  8657. catch (Exception ex)
  8658. {
  8659. self.running5 = true; // as complete, no more call MoveNextAsync.
  8660. self.completedCount = CompleteCount;
  8661. self.completionSource.TrySetException(ex);
  8662. return;
  8663. }
  8664. SUCCESS:
  8665. if (!self.TrySetResult())
  8666. {
  8667. if (self.syncRunning) return;
  8668. self.running5 = true; // as complete, no more call MoveNextAsync.
  8669. try
  8670. {
  8671. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  8672. }
  8673. catch (Exception ex)
  8674. {
  8675. self.completedCount = CompleteCount;
  8676. self.completionSource.TrySetException(ex);
  8677. return;
  8678. }
  8679. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  8680. }
  8681. return;
  8682. COMPLETE:
  8683. self.completionSource.TrySetResult(false);
  8684. return;
  8685. }
  8686. static void Completed6(object state)
  8687. {
  8688. var self = (_CombineLatest)state;
  8689. self.running6 = false;
  8690. try
  8691. {
  8692. if (self.awaiter6.GetResult())
  8693. {
  8694. self.hasCurrent6 = true;
  8695. self.current6 = self.enumerator6.Current;
  8696. goto SUCCESS;
  8697. }
  8698. else
  8699. {
  8700. self.running6 = true; // as complete, no more call MoveNextAsync.
  8701. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8702. {
  8703. goto COMPLETE;
  8704. }
  8705. return;
  8706. }
  8707. }
  8708. catch (Exception ex)
  8709. {
  8710. self.running6 = true; // as complete, no more call MoveNextAsync.
  8711. self.completedCount = CompleteCount;
  8712. self.completionSource.TrySetException(ex);
  8713. return;
  8714. }
  8715. SUCCESS:
  8716. if (!self.TrySetResult())
  8717. {
  8718. if (self.syncRunning) return;
  8719. self.running6 = true; // as complete, no more call MoveNextAsync.
  8720. try
  8721. {
  8722. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  8723. }
  8724. catch (Exception ex)
  8725. {
  8726. self.completedCount = CompleteCount;
  8727. self.completionSource.TrySetException(ex);
  8728. return;
  8729. }
  8730. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  8731. }
  8732. return;
  8733. COMPLETE:
  8734. self.completionSource.TrySetResult(false);
  8735. return;
  8736. }
  8737. static void Completed7(object state)
  8738. {
  8739. var self = (_CombineLatest)state;
  8740. self.running7 = false;
  8741. try
  8742. {
  8743. if (self.awaiter7.GetResult())
  8744. {
  8745. self.hasCurrent7 = true;
  8746. self.current7 = self.enumerator7.Current;
  8747. goto SUCCESS;
  8748. }
  8749. else
  8750. {
  8751. self.running7 = true; // as complete, no more call MoveNextAsync.
  8752. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8753. {
  8754. goto COMPLETE;
  8755. }
  8756. return;
  8757. }
  8758. }
  8759. catch (Exception ex)
  8760. {
  8761. self.running7 = true; // as complete, no more call MoveNextAsync.
  8762. self.completedCount = CompleteCount;
  8763. self.completionSource.TrySetException(ex);
  8764. return;
  8765. }
  8766. SUCCESS:
  8767. if (!self.TrySetResult())
  8768. {
  8769. if (self.syncRunning) return;
  8770. self.running7 = true; // as complete, no more call MoveNextAsync.
  8771. try
  8772. {
  8773. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  8774. }
  8775. catch (Exception ex)
  8776. {
  8777. self.completedCount = CompleteCount;
  8778. self.completionSource.TrySetException(ex);
  8779. return;
  8780. }
  8781. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  8782. }
  8783. return;
  8784. COMPLETE:
  8785. self.completionSource.TrySetResult(false);
  8786. return;
  8787. }
  8788. static void Completed8(object state)
  8789. {
  8790. var self = (_CombineLatest)state;
  8791. self.running8 = false;
  8792. try
  8793. {
  8794. if (self.awaiter8.GetResult())
  8795. {
  8796. self.hasCurrent8 = true;
  8797. self.current8 = self.enumerator8.Current;
  8798. goto SUCCESS;
  8799. }
  8800. else
  8801. {
  8802. self.running8 = true; // as complete, no more call MoveNextAsync.
  8803. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8804. {
  8805. goto COMPLETE;
  8806. }
  8807. return;
  8808. }
  8809. }
  8810. catch (Exception ex)
  8811. {
  8812. self.running8 = true; // as complete, no more call MoveNextAsync.
  8813. self.completedCount = CompleteCount;
  8814. self.completionSource.TrySetException(ex);
  8815. return;
  8816. }
  8817. SUCCESS:
  8818. if (!self.TrySetResult())
  8819. {
  8820. if (self.syncRunning) return;
  8821. self.running8 = true; // as complete, no more call MoveNextAsync.
  8822. try
  8823. {
  8824. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  8825. }
  8826. catch (Exception ex)
  8827. {
  8828. self.completedCount = CompleteCount;
  8829. self.completionSource.TrySetException(ex);
  8830. return;
  8831. }
  8832. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  8833. }
  8834. return;
  8835. COMPLETE:
  8836. self.completionSource.TrySetResult(false);
  8837. return;
  8838. }
  8839. static void Completed9(object state)
  8840. {
  8841. var self = (_CombineLatest)state;
  8842. self.running9 = false;
  8843. try
  8844. {
  8845. if (self.awaiter9.GetResult())
  8846. {
  8847. self.hasCurrent9 = true;
  8848. self.current9 = self.enumerator9.Current;
  8849. goto SUCCESS;
  8850. }
  8851. else
  8852. {
  8853. self.running9 = true; // as complete, no more call MoveNextAsync.
  8854. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8855. {
  8856. goto COMPLETE;
  8857. }
  8858. return;
  8859. }
  8860. }
  8861. catch (Exception ex)
  8862. {
  8863. self.running9 = true; // as complete, no more call MoveNextAsync.
  8864. self.completedCount = CompleteCount;
  8865. self.completionSource.TrySetException(ex);
  8866. return;
  8867. }
  8868. SUCCESS:
  8869. if (!self.TrySetResult())
  8870. {
  8871. if (self.syncRunning) return;
  8872. self.running9 = true; // as complete, no more call MoveNextAsync.
  8873. try
  8874. {
  8875. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  8876. }
  8877. catch (Exception ex)
  8878. {
  8879. self.completedCount = CompleteCount;
  8880. self.completionSource.TrySetException(ex);
  8881. return;
  8882. }
  8883. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  8884. }
  8885. return;
  8886. COMPLETE:
  8887. self.completionSource.TrySetResult(false);
  8888. return;
  8889. }
  8890. static void Completed10(object state)
  8891. {
  8892. var self = (_CombineLatest)state;
  8893. self.running10 = false;
  8894. try
  8895. {
  8896. if (self.awaiter10.GetResult())
  8897. {
  8898. self.hasCurrent10 = true;
  8899. self.current10 = self.enumerator10.Current;
  8900. goto SUCCESS;
  8901. }
  8902. else
  8903. {
  8904. self.running10 = true; // as complete, no more call MoveNextAsync.
  8905. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8906. {
  8907. goto COMPLETE;
  8908. }
  8909. return;
  8910. }
  8911. }
  8912. catch (Exception ex)
  8913. {
  8914. self.running10 = true; // as complete, no more call MoveNextAsync.
  8915. self.completedCount = CompleteCount;
  8916. self.completionSource.TrySetException(ex);
  8917. return;
  8918. }
  8919. SUCCESS:
  8920. if (!self.TrySetResult())
  8921. {
  8922. if (self.syncRunning) return;
  8923. self.running10 = true; // as complete, no more call MoveNextAsync.
  8924. try
  8925. {
  8926. self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter();
  8927. }
  8928. catch (Exception ex)
  8929. {
  8930. self.completedCount = CompleteCount;
  8931. self.completionSource.TrySetException(ex);
  8932. return;
  8933. }
  8934. self.awaiter10.SourceOnCompleted(Completed10Delegate, self);
  8935. }
  8936. return;
  8937. COMPLETE:
  8938. self.completionSource.TrySetResult(false);
  8939. return;
  8940. }
  8941. static void Completed11(object state)
  8942. {
  8943. var self = (_CombineLatest)state;
  8944. self.running11 = false;
  8945. try
  8946. {
  8947. if (self.awaiter11.GetResult())
  8948. {
  8949. self.hasCurrent11 = true;
  8950. self.current11 = self.enumerator11.Current;
  8951. goto SUCCESS;
  8952. }
  8953. else
  8954. {
  8955. self.running11 = true; // as complete, no more call MoveNextAsync.
  8956. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  8957. {
  8958. goto COMPLETE;
  8959. }
  8960. return;
  8961. }
  8962. }
  8963. catch (Exception ex)
  8964. {
  8965. self.running11 = true; // as complete, no more call MoveNextAsync.
  8966. self.completedCount = CompleteCount;
  8967. self.completionSource.TrySetException(ex);
  8968. return;
  8969. }
  8970. SUCCESS:
  8971. if (!self.TrySetResult())
  8972. {
  8973. if (self.syncRunning) return;
  8974. self.running11 = true; // as complete, no more call MoveNextAsync.
  8975. try
  8976. {
  8977. self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter();
  8978. }
  8979. catch (Exception ex)
  8980. {
  8981. self.completedCount = CompleteCount;
  8982. self.completionSource.TrySetException(ex);
  8983. return;
  8984. }
  8985. self.awaiter11.SourceOnCompleted(Completed11Delegate, self);
  8986. }
  8987. return;
  8988. COMPLETE:
  8989. self.completionSource.TrySetResult(false);
  8990. return;
  8991. }
  8992. static void Completed12(object state)
  8993. {
  8994. var self = (_CombineLatest)state;
  8995. self.running12 = false;
  8996. try
  8997. {
  8998. if (self.awaiter12.GetResult())
  8999. {
  9000. self.hasCurrent12 = true;
  9001. self.current12 = self.enumerator12.Current;
  9002. goto SUCCESS;
  9003. }
  9004. else
  9005. {
  9006. self.running12 = true; // as complete, no more call MoveNextAsync.
  9007. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9008. {
  9009. goto COMPLETE;
  9010. }
  9011. return;
  9012. }
  9013. }
  9014. catch (Exception ex)
  9015. {
  9016. self.running12 = true; // as complete, no more call MoveNextAsync.
  9017. self.completedCount = CompleteCount;
  9018. self.completionSource.TrySetException(ex);
  9019. return;
  9020. }
  9021. SUCCESS:
  9022. if (!self.TrySetResult())
  9023. {
  9024. if (self.syncRunning) return;
  9025. self.running12 = true; // as complete, no more call MoveNextAsync.
  9026. try
  9027. {
  9028. self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter();
  9029. }
  9030. catch (Exception ex)
  9031. {
  9032. self.completedCount = CompleteCount;
  9033. self.completionSource.TrySetException(ex);
  9034. return;
  9035. }
  9036. self.awaiter12.SourceOnCompleted(Completed12Delegate, self);
  9037. }
  9038. return;
  9039. COMPLETE:
  9040. self.completionSource.TrySetResult(false);
  9041. return;
  9042. }
  9043. static void Completed13(object state)
  9044. {
  9045. var self = (_CombineLatest)state;
  9046. self.running13 = false;
  9047. try
  9048. {
  9049. if (self.awaiter13.GetResult())
  9050. {
  9051. self.hasCurrent13 = true;
  9052. self.current13 = self.enumerator13.Current;
  9053. goto SUCCESS;
  9054. }
  9055. else
  9056. {
  9057. self.running13 = true; // as complete, no more call MoveNextAsync.
  9058. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9059. {
  9060. goto COMPLETE;
  9061. }
  9062. return;
  9063. }
  9064. }
  9065. catch (Exception ex)
  9066. {
  9067. self.running13 = true; // as complete, no more call MoveNextAsync.
  9068. self.completedCount = CompleteCount;
  9069. self.completionSource.TrySetException(ex);
  9070. return;
  9071. }
  9072. SUCCESS:
  9073. if (!self.TrySetResult())
  9074. {
  9075. if (self.syncRunning) return;
  9076. self.running13 = true; // as complete, no more call MoveNextAsync.
  9077. try
  9078. {
  9079. self.awaiter13 = self.enumerator13.MoveNextAsync().GetAwaiter();
  9080. }
  9081. catch (Exception ex)
  9082. {
  9083. self.completedCount = CompleteCount;
  9084. self.completionSource.TrySetException(ex);
  9085. return;
  9086. }
  9087. self.awaiter13.SourceOnCompleted(Completed13Delegate, self);
  9088. }
  9089. return;
  9090. COMPLETE:
  9091. self.completionSource.TrySetResult(false);
  9092. return;
  9093. }
  9094. static void Completed14(object state)
  9095. {
  9096. var self = (_CombineLatest)state;
  9097. self.running14 = false;
  9098. try
  9099. {
  9100. if (self.awaiter14.GetResult())
  9101. {
  9102. self.hasCurrent14 = true;
  9103. self.current14 = self.enumerator14.Current;
  9104. goto SUCCESS;
  9105. }
  9106. else
  9107. {
  9108. self.running14 = true; // as complete, no more call MoveNextAsync.
  9109. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9110. {
  9111. goto COMPLETE;
  9112. }
  9113. return;
  9114. }
  9115. }
  9116. catch (Exception ex)
  9117. {
  9118. self.running14 = true; // as complete, no more call MoveNextAsync.
  9119. self.completedCount = CompleteCount;
  9120. self.completionSource.TrySetException(ex);
  9121. return;
  9122. }
  9123. SUCCESS:
  9124. if (!self.TrySetResult())
  9125. {
  9126. if (self.syncRunning) return;
  9127. self.running14 = true; // as complete, no more call MoveNextAsync.
  9128. try
  9129. {
  9130. self.awaiter14 = self.enumerator14.MoveNextAsync().GetAwaiter();
  9131. }
  9132. catch (Exception ex)
  9133. {
  9134. self.completedCount = CompleteCount;
  9135. self.completionSource.TrySetException(ex);
  9136. return;
  9137. }
  9138. self.awaiter14.SourceOnCompleted(Completed14Delegate, self);
  9139. }
  9140. return;
  9141. COMPLETE:
  9142. self.completionSource.TrySetResult(false);
  9143. return;
  9144. }
  9145. bool TrySetResult()
  9146. {
  9147. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12 && hasCurrent13 && hasCurrent14)
  9148. {
  9149. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12, current13, current14);
  9150. completionSource.TrySetResult(true);
  9151. return true;
  9152. }
  9153. else
  9154. {
  9155. return false;
  9156. }
  9157. }
  9158. public async UniTask DisposeAsync()
  9159. {
  9160. TaskTracker.RemoveTracking(this);
  9161. if (enumerator1 != null)
  9162. {
  9163. await enumerator1.DisposeAsync();
  9164. }
  9165. if (enumerator2 != null)
  9166. {
  9167. await enumerator2.DisposeAsync();
  9168. }
  9169. if (enumerator3 != null)
  9170. {
  9171. await enumerator3.DisposeAsync();
  9172. }
  9173. if (enumerator4 != null)
  9174. {
  9175. await enumerator4.DisposeAsync();
  9176. }
  9177. if (enumerator5 != null)
  9178. {
  9179. await enumerator5.DisposeAsync();
  9180. }
  9181. if (enumerator6 != null)
  9182. {
  9183. await enumerator6.DisposeAsync();
  9184. }
  9185. if (enumerator7 != null)
  9186. {
  9187. await enumerator7.DisposeAsync();
  9188. }
  9189. if (enumerator8 != null)
  9190. {
  9191. await enumerator8.DisposeAsync();
  9192. }
  9193. if (enumerator9 != null)
  9194. {
  9195. await enumerator9.DisposeAsync();
  9196. }
  9197. if (enumerator10 != null)
  9198. {
  9199. await enumerator10.DisposeAsync();
  9200. }
  9201. if (enumerator11 != null)
  9202. {
  9203. await enumerator11.DisposeAsync();
  9204. }
  9205. if (enumerator12 != null)
  9206. {
  9207. await enumerator12.DisposeAsync();
  9208. }
  9209. if (enumerator13 != null)
  9210. {
  9211. await enumerator13.DisposeAsync();
  9212. }
  9213. if (enumerator14 != null)
  9214. {
  9215. await enumerator14.DisposeAsync();
  9216. }
  9217. }
  9218. }
  9219. }
  9220. internal class CombineLatest<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> : IUniTaskAsyncEnumerable<TResult>
  9221. {
  9222. readonly IUniTaskAsyncEnumerable<T1> source1;
  9223. readonly IUniTaskAsyncEnumerable<T2> source2;
  9224. readonly IUniTaskAsyncEnumerable<T3> source3;
  9225. readonly IUniTaskAsyncEnumerable<T4> source4;
  9226. readonly IUniTaskAsyncEnumerable<T5> source5;
  9227. readonly IUniTaskAsyncEnumerable<T6> source6;
  9228. readonly IUniTaskAsyncEnumerable<T7> source7;
  9229. readonly IUniTaskAsyncEnumerable<T8> source8;
  9230. readonly IUniTaskAsyncEnumerable<T9> source9;
  9231. readonly IUniTaskAsyncEnumerable<T10> source10;
  9232. readonly IUniTaskAsyncEnumerable<T11> source11;
  9233. readonly IUniTaskAsyncEnumerable<T12> source12;
  9234. readonly IUniTaskAsyncEnumerable<T13> source13;
  9235. readonly IUniTaskAsyncEnumerable<T14> source14;
  9236. readonly IUniTaskAsyncEnumerable<T15> source15;
  9237. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> resultSelector;
  9238. public CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, IUniTaskAsyncEnumerable<T14> source14, IUniTaskAsyncEnumerable<T15> source15, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> resultSelector)
  9239. {
  9240. this.source1 = source1;
  9241. this.source2 = source2;
  9242. this.source3 = source3;
  9243. this.source4 = source4;
  9244. this.source5 = source5;
  9245. this.source6 = source6;
  9246. this.source7 = source7;
  9247. this.source8 = source8;
  9248. this.source9 = source9;
  9249. this.source10 = source10;
  9250. this.source11 = source11;
  9251. this.source12 = source12;
  9252. this.source13 = source13;
  9253. this.source14 = source14;
  9254. this.source15 = source15;
  9255. this.resultSelector = resultSelector;
  9256. }
  9257. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  9258. {
  9259. return new _CombineLatest(source1, source2, source3, source4, source5, source6, source7, source8, source9, source10, source11, source12, source13, source14, source15, resultSelector, cancellationToken);
  9260. }
  9261. class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  9262. {
  9263. static readonly Action<object> Completed1Delegate = Completed1;
  9264. static readonly Action<object> Completed2Delegate = Completed2;
  9265. static readonly Action<object> Completed3Delegate = Completed3;
  9266. static readonly Action<object> Completed4Delegate = Completed4;
  9267. static readonly Action<object> Completed5Delegate = Completed5;
  9268. static readonly Action<object> Completed6Delegate = Completed6;
  9269. static readonly Action<object> Completed7Delegate = Completed7;
  9270. static readonly Action<object> Completed8Delegate = Completed8;
  9271. static readonly Action<object> Completed9Delegate = Completed9;
  9272. static readonly Action<object> Completed10Delegate = Completed10;
  9273. static readonly Action<object> Completed11Delegate = Completed11;
  9274. static readonly Action<object> Completed12Delegate = Completed12;
  9275. static readonly Action<object> Completed13Delegate = Completed13;
  9276. static readonly Action<object> Completed14Delegate = Completed14;
  9277. static readonly Action<object> Completed15Delegate = Completed15;
  9278. const int CompleteCount = 15;
  9279. readonly IUniTaskAsyncEnumerable<T1> source1;
  9280. readonly IUniTaskAsyncEnumerable<T2> source2;
  9281. readonly IUniTaskAsyncEnumerable<T3> source3;
  9282. readonly IUniTaskAsyncEnumerable<T4> source4;
  9283. readonly IUniTaskAsyncEnumerable<T5> source5;
  9284. readonly IUniTaskAsyncEnumerable<T6> source6;
  9285. readonly IUniTaskAsyncEnumerable<T7> source7;
  9286. readonly IUniTaskAsyncEnumerable<T8> source8;
  9287. readonly IUniTaskAsyncEnumerable<T9> source9;
  9288. readonly IUniTaskAsyncEnumerable<T10> source10;
  9289. readonly IUniTaskAsyncEnumerable<T11> source11;
  9290. readonly IUniTaskAsyncEnumerable<T12> source12;
  9291. readonly IUniTaskAsyncEnumerable<T13> source13;
  9292. readonly IUniTaskAsyncEnumerable<T14> source14;
  9293. readonly IUniTaskAsyncEnumerable<T15> source15;
  9294. readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> resultSelector;
  9295. CancellationToken cancellationToken;
  9296. IUniTaskAsyncEnumerator<T1> enumerator1;
  9297. UniTask<bool>.Awaiter awaiter1;
  9298. bool hasCurrent1;
  9299. bool running1;
  9300. T1 current1;
  9301. IUniTaskAsyncEnumerator<T2> enumerator2;
  9302. UniTask<bool>.Awaiter awaiter2;
  9303. bool hasCurrent2;
  9304. bool running2;
  9305. T2 current2;
  9306. IUniTaskAsyncEnumerator<T3> enumerator3;
  9307. UniTask<bool>.Awaiter awaiter3;
  9308. bool hasCurrent3;
  9309. bool running3;
  9310. T3 current3;
  9311. IUniTaskAsyncEnumerator<T4> enumerator4;
  9312. UniTask<bool>.Awaiter awaiter4;
  9313. bool hasCurrent4;
  9314. bool running4;
  9315. T4 current4;
  9316. IUniTaskAsyncEnumerator<T5> enumerator5;
  9317. UniTask<bool>.Awaiter awaiter5;
  9318. bool hasCurrent5;
  9319. bool running5;
  9320. T5 current5;
  9321. IUniTaskAsyncEnumerator<T6> enumerator6;
  9322. UniTask<bool>.Awaiter awaiter6;
  9323. bool hasCurrent6;
  9324. bool running6;
  9325. T6 current6;
  9326. IUniTaskAsyncEnumerator<T7> enumerator7;
  9327. UniTask<bool>.Awaiter awaiter7;
  9328. bool hasCurrent7;
  9329. bool running7;
  9330. T7 current7;
  9331. IUniTaskAsyncEnumerator<T8> enumerator8;
  9332. UniTask<bool>.Awaiter awaiter8;
  9333. bool hasCurrent8;
  9334. bool running8;
  9335. T8 current8;
  9336. IUniTaskAsyncEnumerator<T9> enumerator9;
  9337. UniTask<bool>.Awaiter awaiter9;
  9338. bool hasCurrent9;
  9339. bool running9;
  9340. T9 current9;
  9341. IUniTaskAsyncEnumerator<T10> enumerator10;
  9342. UniTask<bool>.Awaiter awaiter10;
  9343. bool hasCurrent10;
  9344. bool running10;
  9345. T10 current10;
  9346. IUniTaskAsyncEnumerator<T11> enumerator11;
  9347. UniTask<bool>.Awaiter awaiter11;
  9348. bool hasCurrent11;
  9349. bool running11;
  9350. T11 current11;
  9351. IUniTaskAsyncEnumerator<T12> enumerator12;
  9352. UniTask<bool>.Awaiter awaiter12;
  9353. bool hasCurrent12;
  9354. bool running12;
  9355. T12 current12;
  9356. IUniTaskAsyncEnumerator<T13> enumerator13;
  9357. UniTask<bool>.Awaiter awaiter13;
  9358. bool hasCurrent13;
  9359. bool running13;
  9360. T13 current13;
  9361. IUniTaskAsyncEnumerator<T14> enumerator14;
  9362. UniTask<bool>.Awaiter awaiter14;
  9363. bool hasCurrent14;
  9364. bool running14;
  9365. T14 current14;
  9366. IUniTaskAsyncEnumerator<T15> enumerator15;
  9367. UniTask<bool>.Awaiter awaiter15;
  9368. bool hasCurrent15;
  9369. bool running15;
  9370. T15 current15;
  9371. int completedCount;
  9372. bool syncRunning;
  9373. TResult result;
  9374. public _CombineLatest(IUniTaskAsyncEnumerable<T1> source1, IUniTaskAsyncEnumerable<T2> source2, IUniTaskAsyncEnumerable<T3> source3, IUniTaskAsyncEnumerable<T4> source4, IUniTaskAsyncEnumerable<T5> source5, IUniTaskAsyncEnumerable<T6> source6, IUniTaskAsyncEnumerable<T7> source7, IUniTaskAsyncEnumerable<T8> source8, IUniTaskAsyncEnumerable<T9> source9, IUniTaskAsyncEnumerable<T10> source10, IUniTaskAsyncEnumerable<T11> source11, IUniTaskAsyncEnumerable<T12> source12, IUniTaskAsyncEnumerable<T13> source13, IUniTaskAsyncEnumerable<T14> source14, IUniTaskAsyncEnumerable<T15> source15, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, TResult> resultSelector, CancellationToken cancellationToken)
  9375. {
  9376. this.source1 = source1;
  9377. this.source2 = source2;
  9378. this.source3 = source3;
  9379. this.source4 = source4;
  9380. this.source5 = source5;
  9381. this.source6 = source6;
  9382. this.source7 = source7;
  9383. this.source8 = source8;
  9384. this.source9 = source9;
  9385. this.source10 = source10;
  9386. this.source11 = source11;
  9387. this.source12 = source12;
  9388. this.source13 = source13;
  9389. this.source14 = source14;
  9390. this.source15 = source15;
  9391. this.resultSelector = resultSelector;
  9392. this.cancellationToken = cancellationToken;
  9393. TaskTracker.TrackActiveTask(this, 3);
  9394. }
  9395. public TResult Current => result;
  9396. public UniTask<bool> MoveNextAsync()
  9397. {
  9398. cancellationToken.ThrowIfCancellationRequested();
  9399. if (completedCount == CompleteCount) return CompletedTasks.False;
  9400. if (enumerator1 == null)
  9401. {
  9402. enumerator1 = source1.GetAsyncEnumerator(cancellationToken);
  9403. enumerator2 = source2.GetAsyncEnumerator(cancellationToken);
  9404. enumerator3 = source3.GetAsyncEnumerator(cancellationToken);
  9405. enumerator4 = source4.GetAsyncEnumerator(cancellationToken);
  9406. enumerator5 = source5.GetAsyncEnumerator(cancellationToken);
  9407. enumerator6 = source6.GetAsyncEnumerator(cancellationToken);
  9408. enumerator7 = source7.GetAsyncEnumerator(cancellationToken);
  9409. enumerator8 = source8.GetAsyncEnumerator(cancellationToken);
  9410. enumerator9 = source9.GetAsyncEnumerator(cancellationToken);
  9411. enumerator10 = source10.GetAsyncEnumerator(cancellationToken);
  9412. enumerator11 = source11.GetAsyncEnumerator(cancellationToken);
  9413. enumerator12 = source12.GetAsyncEnumerator(cancellationToken);
  9414. enumerator13 = source13.GetAsyncEnumerator(cancellationToken);
  9415. enumerator14 = source14.GetAsyncEnumerator(cancellationToken);
  9416. enumerator15 = source15.GetAsyncEnumerator(cancellationToken);
  9417. }
  9418. completionSource.Reset();
  9419. AGAIN:
  9420. syncRunning = true;
  9421. if (!running1)
  9422. {
  9423. running1 = true;
  9424. awaiter1 = enumerator1.MoveNextAsync().GetAwaiter();
  9425. if (awaiter1.IsCompleted)
  9426. {
  9427. Completed1(this);
  9428. }
  9429. else
  9430. {
  9431. awaiter1.SourceOnCompleted(Completed1Delegate, this);
  9432. }
  9433. }
  9434. if (!running2)
  9435. {
  9436. running2 = true;
  9437. awaiter2 = enumerator2.MoveNextAsync().GetAwaiter();
  9438. if (awaiter2.IsCompleted)
  9439. {
  9440. Completed2(this);
  9441. }
  9442. else
  9443. {
  9444. awaiter2.SourceOnCompleted(Completed2Delegate, this);
  9445. }
  9446. }
  9447. if (!running3)
  9448. {
  9449. running3 = true;
  9450. awaiter3 = enumerator3.MoveNextAsync().GetAwaiter();
  9451. if (awaiter3.IsCompleted)
  9452. {
  9453. Completed3(this);
  9454. }
  9455. else
  9456. {
  9457. awaiter3.SourceOnCompleted(Completed3Delegate, this);
  9458. }
  9459. }
  9460. if (!running4)
  9461. {
  9462. running4 = true;
  9463. awaiter4 = enumerator4.MoveNextAsync().GetAwaiter();
  9464. if (awaiter4.IsCompleted)
  9465. {
  9466. Completed4(this);
  9467. }
  9468. else
  9469. {
  9470. awaiter4.SourceOnCompleted(Completed4Delegate, this);
  9471. }
  9472. }
  9473. if (!running5)
  9474. {
  9475. running5 = true;
  9476. awaiter5 = enumerator5.MoveNextAsync().GetAwaiter();
  9477. if (awaiter5.IsCompleted)
  9478. {
  9479. Completed5(this);
  9480. }
  9481. else
  9482. {
  9483. awaiter5.SourceOnCompleted(Completed5Delegate, this);
  9484. }
  9485. }
  9486. if (!running6)
  9487. {
  9488. running6 = true;
  9489. awaiter6 = enumerator6.MoveNextAsync().GetAwaiter();
  9490. if (awaiter6.IsCompleted)
  9491. {
  9492. Completed6(this);
  9493. }
  9494. else
  9495. {
  9496. awaiter6.SourceOnCompleted(Completed6Delegate, this);
  9497. }
  9498. }
  9499. if (!running7)
  9500. {
  9501. running7 = true;
  9502. awaiter7 = enumerator7.MoveNextAsync().GetAwaiter();
  9503. if (awaiter7.IsCompleted)
  9504. {
  9505. Completed7(this);
  9506. }
  9507. else
  9508. {
  9509. awaiter7.SourceOnCompleted(Completed7Delegate, this);
  9510. }
  9511. }
  9512. if (!running8)
  9513. {
  9514. running8 = true;
  9515. awaiter8 = enumerator8.MoveNextAsync().GetAwaiter();
  9516. if (awaiter8.IsCompleted)
  9517. {
  9518. Completed8(this);
  9519. }
  9520. else
  9521. {
  9522. awaiter8.SourceOnCompleted(Completed8Delegate, this);
  9523. }
  9524. }
  9525. if (!running9)
  9526. {
  9527. running9 = true;
  9528. awaiter9 = enumerator9.MoveNextAsync().GetAwaiter();
  9529. if (awaiter9.IsCompleted)
  9530. {
  9531. Completed9(this);
  9532. }
  9533. else
  9534. {
  9535. awaiter9.SourceOnCompleted(Completed9Delegate, this);
  9536. }
  9537. }
  9538. if (!running10)
  9539. {
  9540. running10 = true;
  9541. awaiter10 = enumerator10.MoveNextAsync().GetAwaiter();
  9542. if (awaiter10.IsCompleted)
  9543. {
  9544. Completed10(this);
  9545. }
  9546. else
  9547. {
  9548. awaiter10.SourceOnCompleted(Completed10Delegate, this);
  9549. }
  9550. }
  9551. if (!running11)
  9552. {
  9553. running11 = true;
  9554. awaiter11 = enumerator11.MoveNextAsync().GetAwaiter();
  9555. if (awaiter11.IsCompleted)
  9556. {
  9557. Completed11(this);
  9558. }
  9559. else
  9560. {
  9561. awaiter11.SourceOnCompleted(Completed11Delegate, this);
  9562. }
  9563. }
  9564. if (!running12)
  9565. {
  9566. running12 = true;
  9567. awaiter12 = enumerator12.MoveNextAsync().GetAwaiter();
  9568. if (awaiter12.IsCompleted)
  9569. {
  9570. Completed12(this);
  9571. }
  9572. else
  9573. {
  9574. awaiter12.SourceOnCompleted(Completed12Delegate, this);
  9575. }
  9576. }
  9577. if (!running13)
  9578. {
  9579. running13 = true;
  9580. awaiter13 = enumerator13.MoveNextAsync().GetAwaiter();
  9581. if (awaiter13.IsCompleted)
  9582. {
  9583. Completed13(this);
  9584. }
  9585. else
  9586. {
  9587. awaiter13.SourceOnCompleted(Completed13Delegate, this);
  9588. }
  9589. }
  9590. if (!running14)
  9591. {
  9592. running14 = true;
  9593. awaiter14 = enumerator14.MoveNextAsync().GetAwaiter();
  9594. if (awaiter14.IsCompleted)
  9595. {
  9596. Completed14(this);
  9597. }
  9598. else
  9599. {
  9600. awaiter14.SourceOnCompleted(Completed14Delegate, this);
  9601. }
  9602. }
  9603. if (!running15)
  9604. {
  9605. running15 = true;
  9606. awaiter15 = enumerator15.MoveNextAsync().GetAwaiter();
  9607. if (awaiter15.IsCompleted)
  9608. {
  9609. Completed15(this);
  9610. }
  9611. else
  9612. {
  9613. awaiter15.SourceOnCompleted(Completed15Delegate, this);
  9614. }
  9615. }
  9616. if (!running1 || !running2 || !running3 || !running4 || !running5 || !running6 || !running7 || !running8 || !running9 || !running10 || !running11 || !running12 || !running13 || !running14 || !running15)
  9617. {
  9618. goto AGAIN;
  9619. }
  9620. syncRunning = false;
  9621. return new UniTask<bool>(this, completionSource.Version);
  9622. }
  9623. static void Completed1(object state)
  9624. {
  9625. var self = (_CombineLatest)state;
  9626. self.running1 = false;
  9627. try
  9628. {
  9629. if (self.awaiter1.GetResult())
  9630. {
  9631. self.hasCurrent1 = true;
  9632. self.current1 = self.enumerator1.Current;
  9633. goto SUCCESS;
  9634. }
  9635. else
  9636. {
  9637. self.running1 = true; // as complete, no more call MoveNextAsync.
  9638. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9639. {
  9640. goto COMPLETE;
  9641. }
  9642. return;
  9643. }
  9644. }
  9645. catch (Exception ex)
  9646. {
  9647. self.running1 = true; // as complete, no more call MoveNextAsync.
  9648. self.completedCount = CompleteCount;
  9649. self.completionSource.TrySetException(ex);
  9650. return;
  9651. }
  9652. SUCCESS:
  9653. if (!self.TrySetResult())
  9654. {
  9655. if (self.syncRunning) return;
  9656. self.running1 = true; // as complete, no more call MoveNextAsync.
  9657. try
  9658. {
  9659. self.awaiter1 = self.enumerator1.MoveNextAsync().GetAwaiter();
  9660. }
  9661. catch (Exception ex)
  9662. {
  9663. self.completedCount = CompleteCount;
  9664. self.completionSource.TrySetException(ex);
  9665. return;
  9666. }
  9667. self.awaiter1.SourceOnCompleted(Completed1Delegate, self);
  9668. }
  9669. return;
  9670. COMPLETE:
  9671. self.completionSource.TrySetResult(false);
  9672. return;
  9673. }
  9674. static void Completed2(object state)
  9675. {
  9676. var self = (_CombineLatest)state;
  9677. self.running2 = false;
  9678. try
  9679. {
  9680. if (self.awaiter2.GetResult())
  9681. {
  9682. self.hasCurrent2 = true;
  9683. self.current2 = self.enumerator2.Current;
  9684. goto SUCCESS;
  9685. }
  9686. else
  9687. {
  9688. self.running2 = true; // as complete, no more call MoveNextAsync.
  9689. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9690. {
  9691. goto COMPLETE;
  9692. }
  9693. return;
  9694. }
  9695. }
  9696. catch (Exception ex)
  9697. {
  9698. self.running2 = true; // as complete, no more call MoveNextAsync.
  9699. self.completedCount = CompleteCount;
  9700. self.completionSource.TrySetException(ex);
  9701. return;
  9702. }
  9703. SUCCESS:
  9704. if (!self.TrySetResult())
  9705. {
  9706. if (self.syncRunning) return;
  9707. self.running2 = true; // as complete, no more call MoveNextAsync.
  9708. try
  9709. {
  9710. self.awaiter2 = self.enumerator2.MoveNextAsync().GetAwaiter();
  9711. }
  9712. catch (Exception ex)
  9713. {
  9714. self.completedCount = CompleteCount;
  9715. self.completionSource.TrySetException(ex);
  9716. return;
  9717. }
  9718. self.awaiter2.SourceOnCompleted(Completed2Delegate, self);
  9719. }
  9720. return;
  9721. COMPLETE:
  9722. self.completionSource.TrySetResult(false);
  9723. return;
  9724. }
  9725. static void Completed3(object state)
  9726. {
  9727. var self = (_CombineLatest)state;
  9728. self.running3 = false;
  9729. try
  9730. {
  9731. if (self.awaiter3.GetResult())
  9732. {
  9733. self.hasCurrent3 = true;
  9734. self.current3 = self.enumerator3.Current;
  9735. goto SUCCESS;
  9736. }
  9737. else
  9738. {
  9739. self.running3 = true; // as complete, no more call MoveNextAsync.
  9740. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9741. {
  9742. goto COMPLETE;
  9743. }
  9744. return;
  9745. }
  9746. }
  9747. catch (Exception ex)
  9748. {
  9749. self.running3 = true; // as complete, no more call MoveNextAsync.
  9750. self.completedCount = CompleteCount;
  9751. self.completionSource.TrySetException(ex);
  9752. return;
  9753. }
  9754. SUCCESS:
  9755. if (!self.TrySetResult())
  9756. {
  9757. if (self.syncRunning) return;
  9758. self.running3 = true; // as complete, no more call MoveNextAsync.
  9759. try
  9760. {
  9761. self.awaiter3 = self.enumerator3.MoveNextAsync().GetAwaiter();
  9762. }
  9763. catch (Exception ex)
  9764. {
  9765. self.completedCount = CompleteCount;
  9766. self.completionSource.TrySetException(ex);
  9767. return;
  9768. }
  9769. self.awaiter3.SourceOnCompleted(Completed3Delegate, self);
  9770. }
  9771. return;
  9772. COMPLETE:
  9773. self.completionSource.TrySetResult(false);
  9774. return;
  9775. }
  9776. static void Completed4(object state)
  9777. {
  9778. var self = (_CombineLatest)state;
  9779. self.running4 = false;
  9780. try
  9781. {
  9782. if (self.awaiter4.GetResult())
  9783. {
  9784. self.hasCurrent4 = true;
  9785. self.current4 = self.enumerator4.Current;
  9786. goto SUCCESS;
  9787. }
  9788. else
  9789. {
  9790. self.running4 = true; // as complete, no more call MoveNextAsync.
  9791. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9792. {
  9793. goto COMPLETE;
  9794. }
  9795. return;
  9796. }
  9797. }
  9798. catch (Exception ex)
  9799. {
  9800. self.running4 = true; // as complete, no more call MoveNextAsync.
  9801. self.completedCount = CompleteCount;
  9802. self.completionSource.TrySetException(ex);
  9803. return;
  9804. }
  9805. SUCCESS:
  9806. if (!self.TrySetResult())
  9807. {
  9808. if (self.syncRunning) return;
  9809. self.running4 = true; // as complete, no more call MoveNextAsync.
  9810. try
  9811. {
  9812. self.awaiter4 = self.enumerator4.MoveNextAsync().GetAwaiter();
  9813. }
  9814. catch (Exception ex)
  9815. {
  9816. self.completedCount = CompleteCount;
  9817. self.completionSource.TrySetException(ex);
  9818. return;
  9819. }
  9820. self.awaiter4.SourceOnCompleted(Completed4Delegate, self);
  9821. }
  9822. return;
  9823. COMPLETE:
  9824. self.completionSource.TrySetResult(false);
  9825. return;
  9826. }
  9827. static void Completed5(object state)
  9828. {
  9829. var self = (_CombineLatest)state;
  9830. self.running5 = false;
  9831. try
  9832. {
  9833. if (self.awaiter5.GetResult())
  9834. {
  9835. self.hasCurrent5 = true;
  9836. self.current5 = self.enumerator5.Current;
  9837. goto SUCCESS;
  9838. }
  9839. else
  9840. {
  9841. self.running5 = true; // as complete, no more call MoveNextAsync.
  9842. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9843. {
  9844. goto COMPLETE;
  9845. }
  9846. return;
  9847. }
  9848. }
  9849. catch (Exception ex)
  9850. {
  9851. self.running5 = true; // as complete, no more call MoveNextAsync.
  9852. self.completedCount = CompleteCount;
  9853. self.completionSource.TrySetException(ex);
  9854. return;
  9855. }
  9856. SUCCESS:
  9857. if (!self.TrySetResult())
  9858. {
  9859. if (self.syncRunning) return;
  9860. self.running5 = true; // as complete, no more call MoveNextAsync.
  9861. try
  9862. {
  9863. self.awaiter5 = self.enumerator5.MoveNextAsync().GetAwaiter();
  9864. }
  9865. catch (Exception ex)
  9866. {
  9867. self.completedCount = CompleteCount;
  9868. self.completionSource.TrySetException(ex);
  9869. return;
  9870. }
  9871. self.awaiter5.SourceOnCompleted(Completed5Delegate, self);
  9872. }
  9873. return;
  9874. COMPLETE:
  9875. self.completionSource.TrySetResult(false);
  9876. return;
  9877. }
  9878. static void Completed6(object state)
  9879. {
  9880. var self = (_CombineLatest)state;
  9881. self.running6 = false;
  9882. try
  9883. {
  9884. if (self.awaiter6.GetResult())
  9885. {
  9886. self.hasCurrent6 = true;
  9887. self.current6 = self.enumerator6.Current;
  9888. goto SUCCESS;
  9889. }
  9890. else
  9891. {
  9892. self.running6 = true; // as complete, no more call MoveNextAsync.
  9893. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9894. {
  9895. goto COMPLETE;
  9896. }
  9897. return;
  9898. }
  9899. }
  9900. catch (Exception ex)
  9901. {
  9902. self.running6 = true; // as complete, no more call MoveNextAsync.
  9903. self.completedCount = CompleteCount;
  9904. self.completionSource.TrySetException(ex);
  9905. return;
  9906. }
  9907. SUCCESS:
  9908. if (!self.TrySetResult())
  9909. {
  9910. if (self.syncRunning) return;
  9911. self.running6 = true; // as complete, no more call MoveNextAsync.
  9912. try
  9913. {
  9914. self.awaiter6 = self.enumerator6.MoveNextAsync().GetAwaiter();
  9915. }
  9916. catch (Exception ex)
  9917. {
  9918. self.completedCount = CompleteCount;
  9919. self.completionSource.TrySetException(ex);
  9920. return;
  9921. }
  9922. self.awaiter6.SourceOnCompleted(Completed6Delegate, self);
  9923. }
  9924. return;
  9925. COMPLETE:
  9926. self.completionSource.TrySetResult(false);
  9927. return;
  9928. }
  9929. static void Completed7(object state)
  9930. {
  9931. var self = (_CombineLatest)state;
  9932. self.running7 = false;
  9933. try
  9934. {
  9935. if (self.awaiter7.GetResult())
  9936. {
  9937. self.hasCurrent7 = true;
  9938. self.current7 = self.enumerator7.Current;
  9939. goto SUCCESS;
  9940. }
  9941. else
  9942. {
  9943. self.running7 = true; // as complete, no more call MoveNextAsync.
  9944. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9945. {
  9946. goto COMPLETE;
  9947. }
  9948. return;
  9949. }
  9950. }
  9951. catch (Exception ex)
  9952. {
  9953. self.running7 = true; // as complete, no more call MoveNextAsync.
  9954. self.completedCount = CompleteCount;
  9955. self.completionSource.TrySetException(ex);
  9956. return;
  9957. }
  9958. SUCCESS:
  9959. if (!self.TrySetResult())
  9960. {
  9961. if (self.syncRunning) return;
  9962. self.running7 = true; // as complete, no more call MoveNextAsync.
  9963. try
  9964. {
  9965. self.awaiter7 = self.enumerator7.MoveNextAsync().GetAwaiter();
  9966. }
  9967. catch (Exception ex)
  9968. {
  9969. self.completedCount = CompleteCount;
  9970. self.completionSource.TrySetException(ex);
  9971. return;
  9972. }
  9973. self.awaiter7.SourceOnCompleted(Completed7Delegate, self);
  9974. }
  9975. return;
  9976. COMPLETE:
  9977. self.completionSource.TrySetResult(false);
  9978. return;
  9979. }
  9980. static void Completed8(object state)
  9981. {
  9982. var self = (_CombineLatest)state;
  9983. self.running8 = false;
  9984. try
  9985. {
  9986. if (self.awaiter8.GetResult())
  9987. {
  9988. self.hasCurrent8 = true;
  9989. self.current8 = self.enumerator8.Current;
  9990. goto SUCCESS;
  9991. }
  9992. else
  9993. {
  9994. self.running8 = true; // as complete, no more call MoveNextAsync.
  9995. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  9996. {
  9997. goto COMPLETE;
  9998. }
  9999. return;
  10000. }
  10001. }
  10002. catch (Exception ex)
  10003. {
  10004. self.running8 = true; // as complete, no more call MoveNextAsync.
  10005. self.completedCount = CompleteCount;
  10006. self.completionSource.TrySetException(ex);
  10007. return;
  10008. }
  10009. SUCCESS:
  10010. if (!self.TrySetResult())
  10011. {
  10012. if (self.syncRunning) return;
  10013. self.running8 = true; // as complete, no more call MoveNextAsync.
  10014. try
  10015. {
  10016. self.awaiter8 = self.enumerator8.MoveNextAsync().GetAwaiter();
  10017. }
  10018. catch (Exception ex)
  10019. {
  10020. self.completedCount = CompleteCount;
  10021. self.completionSource.TrySetException(ex);
  10022. return;
  10023. }
  10024. self.awaiter8.SourceOnCompleted(Completed8Delegate, self);
  10025. }
  10026. return;
  10027. COMPLETE:
  10028. self.completionSource.TrySetResult(false);
  10029. return;
  10030. }
  10031. static void Completed9(object state)
  10032. {
  10033. var self = (_CombineLatest)state;
  10034. self.running9 = false;
  10035. try
  10036. {
  10037. if (self.awaiter9.GetResult())
  10038. {
  10039. self.hasCurrent9 = true;
  10040. self.current9 = self.enumerator9.Current;
  10041. goto SUCCESS;
  10042. }
  10043. else
  10044. {
  10045. self.running9 = true; // as complete, no more call MoveNextAsync.
  10046. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10047. {
  10048. goto COMPLETE;
  10049. }
  10050. return;
  10051. }
  10052. }
  10053. catch (Exception ex)
  10054. {
  10055. self.running9 = true; // as complete, no more call MoveNextAsync.
  10056. self.completedCount = CompleteCount;
  10057. self.completionSource.TrySetException(ex);
  10058. return;
  10059. }
  10060. SUCCESS:
  10061. if (!self.TrySetResult())
  10062. {
  10063. if (self.syncRunning) return;
  10064. self.running9 = true; // as complete, no more call MoveNextAsync.
  10065. try
  10066. {
  10067. self.awaiter9 = self.enumerator9.MoveNextAsync().GetAwaiter();
  10068. }
  10069. catch (Exception ex)
  10070. {
  10071. self.completedCount = CompleteCount;
  10072. self.completionSource.TrySetException(ex);
  10073. return;
  10074. }
  10075. self.awaiter9.SourceOnCompleted(Completed9Delegate, self);
  10076. }
  10077. return;
  10078. COMPLETE:
  10079. self.completionSource.TrySetResult(false);
  10080. return;
  10081. }
  10082. static void Completed10(object state)
  10083. {
  10084. var self = (_CombineLatest)state;
  10085. self.running10 = false;
  10086. try
  10087. {
  10088. if (self.awaiter10.GetResult())
  10089. {
  10090. self.hasCurrent10 = true;
  10091. self.current10 = self.enumerator10.Current;
  10092. goto SUCCESS;
  10093. }
  10094. else
  10095. {
  10096. self.running10 = true; // as complete, no more call MoveNextAsync.
  10097. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10098. {
  10099. goto COMPLETE;
  10100. }
  10101. return;
  10102. }
  10103. }
  10104. catch (Exception ex)
  10105. {
  10106. self.running10 = true; // as complete, no more call MoveNextAsync.
  10107. self.completedCount = CompleteCount;
  10108. self.completionSource.TrySetException(ex);
  10109. return;
  10110. }
  10111. SUCCESS:
  10112. if (!self.TrySetResult())
  10113. {
  10114. if (self.syncRunning) return;
  10115. self.running10 = true; // as complete, no more call MoveNextAsync.
  10116. try
  10117. {
  10118. self.awaiter10 = self.enumerator10.MoveNextAsync().GetAwaiter();
  10119. }
  10120. catch (Exception ex)
  10121. {
  10122. self.completedCount = CompleteCount;
  10123. self.completionSource.TrySetException(ex);
  10124. return;
  10125. }
  10126. self.awaiter10.SourceOnCompleted(Completed10Delegate, self);
  10127. }
  10128. return;
  10129. COMPLETE:
  10130. self.completionSource.TrySetResult(false);
  10131. return;
  10132. }
  10133. static void Completed11(object state)
  10134. {
  10135. var self = (_CombineLatest)state;
  10136. self.running11 = false;
  10137. try
  10138. {
  10139. if (self.awaiter11.GetResult())
  10140. {
  10141. self.hasCurrent11 = true;
  10142. self.current11 = self.enumerator11.Current;
  10143. goto SUCCESS;
  10144. }
  10145. else
  10146. {
  10147. self.running11 = true; // as complete, no more call MoveNextAsync.
  10148. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10149. {
  10150. goto COMPLETE;
  10151. }
  10152. return;
  10153. }
  10154. }
  10155. catch (Exception ex)
  10156. {
  10157. self.running11 = true; // as complete, no more call MoveNextAsync.
  10158. self.completedCount = CompleteCount;
  10159. self.completionSource.TrySetException(ex);
  10160. return;
  10161. }
  10162. SUCCESS:
  10163. if (!self.TrySetResult())
  10164. {
  10165. if (self.syncRunning) return;
  10166. self.running11 = true; // as complete, no more call MoveNextAsync.
  10167. try
  10168. {
  10169. self.awaiter11 = self.enumerator11.MoveNextAsync().GetAwaiter();
  10170. }
  10171. catch (Exception ex)
  10172. {
  10173. self.completedCount = CompleteCount;
  10174. self.completionSource.TrySetException(ex);
  10175. return;
  10176. }
  10177. self.awaiter11.SourceOnCompleted(Completed11Delegate, self);
  10178. }
  10179. return;
  10180. COMPLETE:
  10181. self.completionSource.TrySetResult(false);
  10182. return;
  10183. }
  10184. static void Completed12(object state)
  10185. {
  10186. var self = (_CombineLatest)state;
  10187. self.running12 = false;
  10188. try
  10189. {
  10190. if (self.awaiter12.GetResult())
  10191. {
  10192. self.hasCurrent12 = true;
  10193. self.current12 = self.enumerator12.Current;
  10194. goto SUCCESS;
  10195. }
  10196. else
  10197. {
  10198. self.running12 = true; // as complete, no more call MoveNextAsync.
  10199. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10200. {
  10201. goto COMPLETE;
  10202. }
  10203. return;
  10204. }
  10205. }
  10206. catch (Exception ex)
  10207. {
  10208. self.running12 = true; // as complete, no more call MoveNextAsync.
  10209. self.completedCount = CompleteCount;
  10210. self.completionSource.TrySetException(ex);
  10211. return;
  10212. }
  10213. SUCCESS:
  10214. if (!self.TrySetResult())
  10215. {
  10216. if (self.syncRunning) return;
  10217. self.running12 = true; // as complete, no more call MoveNextAsync.
  10218. try
  10219. {
  10220. self.awaiter12 = self.enumerator12.MoveNextAsync().GetAwaiter();
  10221. }
  10222. catch (Exception ex)
  10223. {
  10224. self.completedCount = CompleteCount;
  10225. self.completionSource.TrySetException(ex);
  10226. return;
  10227. }
  10228. self.awaiter12.SourceOnCompleted(Completed12Delegate, self);
  10229. }
  10230. return;
  10231. COMPLETE:
  10232. self.completionSource.TrySetResult(false);
  10233. return;
  10234. }
  10235. static void Completed13(object state)
  10236. {
  10237. var self = (_CombineLatest)state;
  10238. self.running13 = false;
  10239. try
  10240. {
  10241. if (self.awaiter13.GetResult())
  10242. {
  10243. self.hasCurrent13 = true;
  10244. self.current13 = self.enumerator13.Current;
  10245. goto SUCCESS;
  10246. }
  10247. else
  10248. {
  10249. self.running13 = true; // as complete, no more call MoveNextAsync.
  10250. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10251. {
  10252. goto COMPLETE;
  10253. }
  10254. return;
  10255. }
  10256. }
  10257. catch (Exception ex)
  10258. {
  10259. self.running13 = true; // as complete, no more call MoveNextAsync.
  10260. self.completedCount = CompleteCount;
  10261. self.completionSource.TrySetException(ex);
  10262. return;
  10263. }
  10264. SUCCESS:
  10265. if (!self.TrySetResult())
  10266. {
  10267. if (self.syncRunning) return;
  10268. self.running13 = true; // as complete, no more call MoveNextAsync.
  10269. try
  10270. {
  10271. self.awaiter13 = self.enumerator13.MoveNextAsync().GetAwaiter();
  10272. }
  10273. catch (Exception ex)
  10274. {
  10275. self.completedCount = CompleteCount;
  10276. self.completionSource.TrySetException(ex);
  10277. return;
  10278. }
  10279. self.awaiter13.SourceOnCompleted(Completed13Delegate, self);
  10280. }
  10281. return;
  10282. COMPLETE:
  10283. self.completionSource.TrySetResult(false);
  10284. return;
  10285. }
  10286. static void Completed14(object state)
  10287. {
  10288. var self = (_CombineLatest)state;
  10289. self.running14 = false;
  10290. try
  10291. {
  10292. if (self.awaiter14.GetResult())
  10293. {
  10294. self.hasCurrent14 = true;
  10295. self.current14 = self.enumerator14.Current;
  10296. goto SUCCESS;
  10297. }
  10298. else
  10299. {
  10300. self.running14 = true; // as complete, no more call MoveNextAsync.
  10301. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10302. {
  10303. goto COMPLETE;
  10304. }
  10305. return;
  10306. }
  10307. }
  10308. catch (Exception ex)
  10309. {
  10310. self.running14 = true; // as complete, no more call MoveNextAsync.
  10311. self.completedCount = CompleteCount;
  10312. self.completionSource.TrySetException(ex);
  10313. return;
  10314. }
  10315. SUCCESS:
  10316. if (!self.TrySetResult())
  10317. {
  10318. if (self.syncRunning) return;
  10319. self.running14 = true; // as complete, no more call MoveNextAsync.
  10320. try
  10321. {
  10322. self.awaiter14 = self.enumerator14.MoveNextAsync().GetAwaiter();
  10323. }
  10324. catch (Exception ex)
  10325. {
  10326. self.completedCount = CompleteCount;
  10327. self.completionSource.TrySetException(ex);
  10328. return;
  10329. }
  10330. self.awaiter14.SourceOnCompleted(Completed14Delegate, self);
  10331. }
  10332. return;
  10333. COMPLETE:
  10334. self.completionSource.TrySetResult(false);
  10335. return;
  10336. }
  10337. static void Completed15(object state)
  10338. {
  10339. var self = (_CombineLatest)state;
  10340. self.running15 = false;
  10341. try
  10342. {
  10343. if (self.awaiter15.GetResult())
  10344. {
  10345. self.hasCurrent15 = true;
  10346. self.current15 = self.enumerator15.Current;
  10347. goto SUCCESS;
  10348. }
  10349. else
  10350. {
  10351. self.running15 = true; // as complete, no more call MoveNextAsync.
  10352. if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
  10353. {
  10354. goto COMPLETE;
  10355. }
  10356. return;
  10357. }
  10358. }
  10359. catch (Exception ex)
  10360. {
  10361. self.running15 = true; // as complete, no more call MoveNextAsync.
  10362. self.completedCount = CompleteCount;
  10363. self.completionSource.TrySetException(ex);
  10364. return;
  10365. }
  10366. SUCCESS:
  10367. if (!self.TrySetResult())
  10368. {
  10369. if (self.syncRunning) return;
  10370. self.running15 = true; // as complete, no more call MoveNextAsync.
  10371. try
  10372. {
  10373. self.awaiter15 = self.enumerator15.MoveNextAsync().GetAwaiter();
  10374. }
  10375. catch (Exception ex)
  10376. {
  10377. self.completedCount = CompleteCount;
  10378. self.completionSource.TrySetException(ex);
  10379. return;
  10380. }
  10381. self.awaiter15.SourceOnCompleted(Completed15Delegate, self);
  10382. }
  10383. return;
  10384. COMPLETE:
  10385. self.completionSource.TrySetResult(false);
  10386. return;
  10387. }
  10388. bool TrySetResult()
  10389. {
  10390. if (hasCurrent1 && hasCurrent2 && hasCurrent3 && hasCurrent4 && hasCurrent5 && hasCurrent6 && hasCurrent7 && hasCurrent8 && hasCurrent9 && hasCurrent10 && hasCurrent11 && hasCurrent12 && hasCurrent13 && hasCurrent14 && hasCurrent15)
  10391. {
  10392. result = resultSelector(current1, current2, current3, current4, current5, current6, current7, current8, current9, current10, current11, current12, current13, current14, current15);
  10393. completionSource.TrySetResult(true);
  10394. return true;
  10395. }
  10396. else
  10397. {
  10398. return false;
  10399. }
  10400. }
  10401. public async UniTask DisposeAsync()
  10402. {
  10403. TaskTracker.RemoveTracking(this);
  10404. if (enumerator1 != null)
  10405. {
  10406. await enumerator1.DisposeAsync();
  10407. }
  10408. if (enumerator2 != null)
  10409. {
  10410. await enumerator2.DisposeAsync();
  10411. }
  10412. if (enumerator3 != null)
  10413. {
  10414. await enumerator3.DisposeAsync();
  10415. }
  10416. if (enumerator4 != null)
  10417. {
  10418. await enumerator4.DisposeAsync();
  10419. }
  10420. if (enumerator5 != null)
  10421. {
  10422. await enumerator5.DisposeAsync();
  10423. }
  10424. if (enumerator6 != null)
  10425. {
  10426. await enumerator6.DisposeAsync();
  10427. }
  10428. if (enumerator7 != null)
  10429. {
  10430. await enumerator7.DisposeAsync();
  10431. }
  10432. if (enumerator8 != null)
  10433. {
  10434. await enumerator8.DisposeAsync();
  10435. }
  10436. if (enumerator9 != null)
  10437. {
  10438. await enumerator9.DisposeAsync();
  10439. }
  10440. if (enumerator10 != null)
  10441. {
  10442. await enumerator10.DisposeAsync();
  10443. }
  10444. if (enumerator11 != null)
  10445. {
  10446. await enumerator11.DisposeAsync();
  10447. }
  10448. if (enumerator12 != null)
  10449. {
  10450. await enumerator12.DisposeAsync();
  10451. }
  10452. if (enumerator13 != null)
  10453. {
  10454. await enumerator13.DisposeAsync();
  10455. }
  10456. if (enumerator14 != null)
  10457. {
  10458. await enumerator14.DisposeAsync();
  10459. }
  10460. if (enumerator15 != null)
  10461. {
  10462. await enumerator15.DisposeAsync();
  10463. }
  10464. }
  10465. }
  10466. }
  10467. }