@@ -50,7 +50,7 @@ export interface ViewportConfiguration {
5050/**
5151 * Viewport callback
5252 */
53- export type ViewportCallback = ( breakpoint : string ) => Promise < any > | any
53+ export type ViewportCallback < T > = ( breakpoint : string ) => T
5454
5555/**
5656 * Extend window element with missing types
@@ -75,8 +75,7 @@ declare global {
7575 * @return Selected breakpoints
7676 */
7777export function range (
78- breakpoints : ViewportBreakpoint [ ] ,
79- first : string , last : string = first
78+ breakpoints : ViewportBreakpoint [ ] , first : string , last : string = first
8079) {
8180 const [ from , to ] = [ first , last ] . map ( name => {
8281 const index = breakpoints . findIndex (
@@ -90,6 +89,17 @@ export function range(
9089 return breakpoints . slice ( from , to + 1 )
9190}
9291
92+ /**
93+ * Type guard for Promise
94+ *
95+ * @param value - Value to be checked
96+ *
97+ * @return Whether the value is a Promise
98+ */
99+ function isPromise ( value : any ) : value is Promise < any > {
100+ return Promise . resolve ( value ) === value
101+ }
102+
93103/* ----------------------------------------------------------------------------
94104 * Class
95105 * ------------------------------------------------------------------------- */
@@ -99,7 +109,7 @@ export class Viewport {
99109 /**
100110 * Viewport configuration
101111 */
102- public config : ViewportConfiguration
112+ public config : Readonly < ViewportConfiguration >
103113
104114 /**
105115 * Viewport context
@@ -165,6 +175,7 @@ export class Viewport {
165175 * Set viewport to width (and height) or breakpoint name
166176 *
167177 * @param widthOrBreakpoint - Width in pixels or breakpoint name
178+ * @param height - Height in pixels
168179 */
169180 public set ( width : number , height ?: number ) : void
170181 public set ( breakpoint : string ) : void
@@ -177,13 +188,14 @@ export class Viewport {
177188
178189 /* Set viewport width (and height) */
179190 } else {
180- if ( typeof widthOrBreakpoint !== "number" || widthOrBreakpoint <= 0 )
181- throw new TypeError ( `Invalid breakpoint width: ${ widthOrBreakpoint } ` )
191+ const width = widthOrBreakpoint
192+ if ( typeof width !== "number" || width <= 0 )
193+ throw new TypeError ( `Invalid breakpoint width: ${ width } ` )
182194 if ( height && ( typeof height !== "number" || height <= 0 ) )
183195 throw new TypeError ( `Invalid breakpoint height: ${ height } ` )
184196
185197 /* Set width and height */
186- this . context . style . width = `${ widthOrBreakpoint } px`
198+ this . context . style . width = `${ width } px`
187199 if ( height )
188200 this . context . style . height = `${ height } px`
189201 }
@@ -219,9 +231,12 @@ export class Viewport {
219231 * @param {string } last - Last breakpoint name
220232 * @param {Function } cb - Callback to execute after resizing
221233 */
222- public between (
223- first : string , last : string ,
224- cb : ViewportCallback
234+ public between < T extends Promise < any > > (
235+ first : string , last : string , cb : ViewportCallback < T >
236+ ) : Promise < void >
237+ public between < T > ( first : string , last : string , cb : ViewportCallback < T > ) : void
238+ public between < T > (
239+ first : string , last : string , cb : ViewportCallback < T >
225240 ) : void | Promise < void > {
226241 const [ initial , ...rest ] = range ( this . config . breakpoints , first , last )
227242
@@ -234,11 +249,11 @@ export class Viewport {
234249 /* Execute the first callback and check if it returns a Promise, as we
235250 need to make sure that everything is executed sequentially */
236251 const result = invoke ( initial )
237- if ( Promise . resolve ( result ) === result )
252+ if ( isPromise ( result ) )
238253 return rest
239254
240255 /* Resolve breakpoints and execute callback after resizing */
241- . reduce < Promise < any > > ( ( promise , breakpoint ) => {
256+ . reduce ( ( promise : Promise < any > , breakpoint ) => {
242257 return promise . then ( ( ) => breakpoint ) . then ( invoke )
243258 } , result )
244259
@@ -262,7 +277,9 @@ export class Viewport {
262277 *
263278 * @return Promise resolving with no result
264279 */
265- public each ( cb : ViewportCallback ) {
280+ public each < T extends Promise < any > > ( cb : ViewportCallback < T > ) : Promise < void >
281+ public each < T > ( cb : ViewportCallback < T > ) : void
282+ public each < T > ( cb : ViewportCallback < T > ) : void | Promise < void > {
266283 return this . between ( this . config . breakpoints [ 0 ] . name ,
267284 this . config . breakpoints [ this . config . breakpoints . length - 1 ] . name , cb )
268285 }
@@ -280,7 +297,11 @@ export class Viewport {
280297 *
281298 * @return Promise resolving with no result
282299 */
283- public from ( first : string , cb : ViewportCallback ) {
300+ public from < T extends Promise < any > > (
301+ first : string , cb : ViewportCallback < T >
302+ ) : Promise < void >
303+ public from < T > ( first : string , cb : ViewportCallback < T > ) : void
304+ public from < T > ( first : string , cb : ViewportCallback < T > ) : void | Promise < void > {
284305 return this . between ( first ,
285306 this . config . breakpoints [ this . config . breakpoints . length - 1 ] . name , cb )
286307 }
@@ -298,7 +319,11 @@ export class Viewport {
298319 *
299320 * @return Promise resolving with no result
300321 */
301- public to ( last : string , cb : ViewportCallback ) {
322+ public to < T extends Promise < any > > (
323+ last : string , cb : ViewportCallback < T >
324+ ) : Promise < void >
325+ public to < T > ( last : string , cb : ViewportCallback < T > ) : void
326+ public to < T > ( last : string , cb : ViewportCallback < T > ) : void | Promise < void > {
302327 return this . between ( this . config . breakpoints [ 0 ] . name , last , cb )
303328 }
304329}
0 commit comments