Skip to content

Commit d2998c4

Browse files
committed
DRYer code
1 parent 062742f commit d2998c4

File tree

9 files changed

+60
-62
lines changed

9 files changed

+60
-62
lines changed
Lines changed: 12 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import{A,B,C,D,E,F,G,H,I,J,K,L,M,N,a as o,b as r,c as e,d as f,e as m,f as p,g as t,h as x,i as a,j as b,k as c,l as d,m as g,n as h,o as i,p as j,q as k,r as l,s as n,t as q,u as s,v as u,w as v,x as w,y,z}from"./chunk-FMM6R42G.mjs";import"./chunk-57T3EAOB.mjs";export{d as AdUnitFormatImpressionThreshold,b as AdUnitFormatTypeLookup,u as CLOSE_ICON_SVG,e as DEFAULT_SERVICE_ENDPOINT,g as DefaultImpressionThreshold,i as DefaultLogReporter,l as DefaultLogger,n as DefaultMozAdsImpressionObserver,z as FALLBACK_BILLBOARD_SVG,w as FALLBACK_DINO_SVG_FRAGMENT,y as FALLBACK_DONATE_SVG_FRAGMENT,B as FALLBACK_MRECTANGLE_SVG,A as FALLBACK_SKYSCRAPER_SVG,c as FallbackAdURL,K as FetchAdsError,a as FixedSize,t as IABFixedSize,f as INSTRUMENT_ENDPOINT,o as IS_BROWSER,r as IS_PRODUCTION,p as LOG_EMIT_FLAG_DEFAULT,m as LOG_TO_CONSOLE_FLAG_DEFAULT,k as LoggerLevel,x as NonIABFixedSize,v as REPORT_ICON_SVG,h as SeverityLevel,F as StoreType,M as buildPlacementsRequest,q as defaultImpressionObserver,j as defaultLogReporter,L as fetchAds,G as getItemFromStore,J as getOrGenerateContextId,C as l,N as mapResponseToPlacementsWithContent,D as preloadImage,s as recordClick,I as removeItemFromStore,E as renderPlacement,H as setItemInStore};
1+
import{A,B,C,D,E,F,G,H,I,J,K,L,M,N,a as o,b as r,c as e,d as f,e as m,f as p,g as t,h as x,i as a,j as b,k as c,l as d,m as g,n as h,o as i,p as j,q as k,r as l,s as n,t as q,u as s,v as u,w as v,x as w,y,z}from"./chunk-A5TY5LMO.mjs";import"./chunk-57T3EAOB.mjs";export{d as AdUnitFormatImpressionThreshold,b as AdUnitFormatTypeLookup,u as CLOSE_ICON_SVG,e as DEFAULT_SERVICE_ENDPOINT,g as DefaultImpressionThreshold,i as DefaultLogReporter,l as DefaultLogger,n as DefaultMozAdsImpressionObserver,z as FALLBACK_BILLBOARD_SVG,w as FALLBACK_DINO_SVG_FRAGMENT,y as FALLBACK_DONATE_SVG_FRAGMENT,B as FALLBACK_MRECTANGLE_SVG,A as FALLBACK_SKYSCRAPER_SVG,c as FallbackAdURL,K as FetchAdsError,a as FixedSize,t as IABFixedSize,f as INSTRUMENT_ENDPOINT,o as IS_BROWSER,r as IS_PRODUCTION,p as LOG_EMIT_FLAG_DEFAULT,m as LOG_TO_CONSOLE_FLAG_DEFAULT,k as LoggerLevel,x as NonIABFixedSize,v as REPORT_ICON_SVG,h as SeverityLevel,F as StoreType,M as buildPlacementsRequest,q as defaultImpressionObserver,j as defaultLogReporter,L as fetchAds,G as getItemFromStore,J as getOrGenerateContextId,C as l,N as mapResponseToPlacementsWithContent,D as preloadImage,s as recordClick,I as removeItemFromStore,E as renderPlacement,H as setItemInStore};

dist/iife.global.js

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"use client";import{E as h,L as A,r as m,t as P,u as g}from"./chunk-FMM6R42G.mjs";import"./chunk-57T3EAOB.mjs";import{useLayoutEffect as x,useRef as b}from"react";import{createContext as v,useContext as y,useEffect as w,useState as L}from"react";var M;try{M=new m({name:"react.hooks.useMozAdsPlacement"})}catch(e){console.debug(`DefaultLogger for react.hooks.useMozAdsPlacement could not be instantiated: ${e}`)}var f=class{placements={};async getPlacementWithContent(t){let o=this.placements[t.placementId];if(o)return o;try{let r=await A({placements:{[t.placementId]:t}});this.placements={...this.placements,...r}}catch(r){return M?.error(`Unable to fetch ads; ${r.message}`,{type:"placementComponent.adLoad.failure",eventLabel:"ad_load_error",errorId:r?.name}),t.onError?.({error:r}),t}return this.placements[t.placementId]}},R=v(new f),z=({placementId:e,iabContent:t,fixedSize:o,onError:r})=>{let[l,s]=L({placementId:e,iabContent:t,fixedSize:o}),d=y(R),c=async()=>{s(await d.getPlacementWithContent({placementId:e,iabContent:t,fixedSize:o,onError:r}))};return w(()=>{c()},[e]),l};import{jsx as C}from"react/jsx-runtime";var E;try{E=new m({name:"react.components.MozAdsPlacement"})}catch(e){console.debug(`DefaultLogger for react.components.MozAdsPlacement could not be instantiated: ${e}`)}function q({placementId:e,iabContent:t,fixedSize:o,onClick:r,onReport:l,onError:s,onLoad:d}){let{width:c,height:u}=o||{},p={width:c&&`${c}px`,height:u&&`${u}px`};try{let n=z({placementId:e,iabContent:t,fixedSize:o,onError:s}),i=b(null);return x(()=>{i.current&&h(i.current,{placement:n,onClick:a=>{g(n),r?.(a)},onReport:a=>{l?.(a)},onError:a=>{s?.(a)},onLoad:a=>{P.observe(n),d?.(a)}})},[n]),C("div",{ref:i,style:p})}catch(n){try{E?.error(`An unexpected error has occured when rendering ${e}: ${n?.message}`,{type:"placementComponent.render.error",eventLabel:"render_error",placementId:e,errorId:n?.name}),s?.({error:n})}catch{}return C("div",{style:p})}}export{q as MozAdsPlacement,f as MozAdsPlacementContextState,R as mozAdsPlacementContext,z as useMozAdsPlacement};
1+
"use client";import{E as h,L as A,r as m,t as P,u as g}from"./chunk-A5TY5LMO.mjs";import"./chunk-57T3EAOB.mjs";import{useLayoutEffect as x,useRef as b}from"react";import{createContext as v,useContext as y,useEffect as w,useState as L}from"react";var M;try{M=new m({name:"react.hooks.useMozAdsPlacement"})}catch(e){console.debug(`DefaultLogger for react.hooks.useMozAdsPlacement could not be instantiated: ${e}`)}var f=class{placements={};async getPlacementWithContent(t){let o=this.placements[t.placementId];if(o)return o;try{let r=await A({placements:{[t.placementId]:t}});this.placements={...this.placements,...r}}catch(r){return M?.error(`Unable to fetch ads; ${r.message}`,{type:"placementComponent.adLoad.failure",eventLabel:"ad_load_error",errorId:r?.name}),t.onError?.({error:r}),t}return this.placements[t.placementId]}},R=v(new f),z=({placementId:e,iabContent:t,fixedSize:o,onError:r})=>{let[l,s]=L({placementId:e,iabContent:t,fixedSize:o}),d=y(R),c=async()=>{s(await d.getPlacementWithContent({placementId:e,iabContent:t,fixedSize:o,onError:r}))};return w(()=>{c()},[e]),l};import{jsx as C}from"react/jsx-runtime";var E;try{E=new m({name:"react.components.MozAdsPlacement"})}catch(e){console.debug(`DefaultLogger for react.components.MozAdsPlacement could not be instantiated: ${e}`)}function q({placementId:e,iabContent:t,fixedSize:o,onClick:r,onReport:l,onError:s,onLoad:d}){let{width:c,height:u}=o||{},p={width:c&&`${c}px`,height:u&&`${u}px`};try{let n=z({placementId:e,iabContent:t,fixedSize:o,onError:s}),i=b(null);return x(()=>{i.current&&h(i.current,{placement:n,onClick:a=>{g(n),r?.(a)},onReport:a=>{l?.(a)},onError:a=>{s?.(a)},onLoad:a=>{P.observe(n),d?.(a)}})},[n]),C("div",{ref:i,style:p})}catch(n){try{E?.error(`An unexpected error has occured when rendering ${e}: ${n?.message}`,{type:"placementComponent.render.error",eventLabel:"render_error",placementId:e,errorId:n?.name}),s?.({error:n})}catch{}return C("div",{style:p})}}export{q as MozAdsPlacement,f as MozAdsPlacementContextState,R as mozAdsPlacementContext,z as useMozAdsPlacement};

packages/core/src/fallback.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@ export const fallbackAdContentLookup: Partial<Record<AdUnitFormatType, ImageAd>>
99
MediumRectangle: getFallbackMediumRectangle(),
1010
}
1111

12+
export function getFallbackAd(placement: MozAdsPlacementConfig | MozAdsPlacementWithContent): ImageAd {
13+
const fixedSize = placement?.fixedSize
14+
if (!fixedSize) {
15+
// If we don't have a fixedSize specified, there isn't a reliable way to decide what size backup ad to return, so we return nothing.
16+
// This should have the same behavior as if no ad was placed and the space on the page is given back.
17+
return {}
18+
}
19+
20+
const adUnitFormatType = AdUnitFormatTypeLookup[`${fixedSize.width}x${fixedSize.height}`]
21+
return fallbackAdContentLookup[adUnitFormatType] ?? getFallbackSquareDefault()
22+
}
23+
1224
export function getFallbackAds(placements: MozAdsPlacements): AdResponse {
1325
const reducer = (acc: AdResponse, placement: [string, MozAdsPlacementConfig | MozAdsPlacementWithContent]): AdResponse => {
1426
const placementName = placement[0]
15-
const fixedSize = placement[1]?.fixedSize
16-
if (!fixedSize) {
17-
// If we don't have a fixedSize specified, there isn't a reliable way to decide what size backup ad to return, so we return nothing.
18-
// This should have the same behavior as if no ad was placed and the space on the page is given back.
19-
return {
20-
...acc,
21-
[placementName]: [{}],
22-
}
23-
}
27+
const placementWithContent = placement[1]
28+
29+
const adContent = getFallbackAd(placementWithContent)
2430

25-
const adUnitFormatType = AdUnitFormatTypeLookup[`${fixedSize.width}x${fixedSize.height}`]
2631
return {
2732
...acc,
28-
[placementName]: [fallbackAdContentLookup[adUnitFormatType] ?? getFallbackSquareDefault()],
33+
[placementName]: [adContent],
2934
}
3035
}
3136

0 commit comments

Comments
 (0)