1
1
import React from "react" ;
2
- import Observer from "react-intersection-observer" ;
2
+ import { InView } from "react-intersection-observer" ;
3
3
import { unionize , ofType , UnionOf } from "unionize" ;
4
4
5
5
/**
@@ -48,7 +48,7 @@ export interface RenderCallbackArgs {
48
48
imageState : ImageState ;
49
49
imageProps : ImageProps ;
50
50
/** When not loading eagerly, a ref to bind to the DOM element. This is needed for the intersection calculation to work. */
51
- ref ?: React . RefObject < any > ;
51
+ ref ?: React . RefObject < any > | ( ( node ?: Element | null ) => void ) ;
52
52
}
53
53
54
54
export interface ImageProps {
@@ -194,7 +194,7 @@ export class LazyImageFull extends React.Component<
194
194
static displayName = "LazyImageFull" ;
195
195
196
196
/** A central place to store promises.
197
- * A bit silly, but passing promsises directly in the state
197
+ * A bit silly, but passing promises directly in the state
198
198
* was giving me weird timing issues. This way we can keep
199
199
* the promises in check, and pick them up from the respective methods.
200
200
* FUTURE: Could pass the relevant key in Buffering and Loading, so
@@ -300,7 +300,7 @@ export class LazyImageFull extends React.Component<
300
300
// Clear the Promise Cache
301
301
if ( this . promiseCache . loading ) {
302
302
// NOTE: This does not cancel the request, only the callback.
303
- // We weould need fetch() and an AbortHandler for that.
303
+ // We we would need fetch() and an AbortHandler for that.
304
304
this . promiseCache . loading . cancel ( ) ;
305
305
}
306
306
if ( this . promiseCache . buffering ) {
@@ -331,14 +331,16 @@ export class LazyImageFull extends React.Component<
331
331
} ) ;
332
332
} else {
333
333
return (
334
- < Observer
334
+ < InView
335
335
rootMargin = "50px 0px"
336
336
// TODO: reconsider threshold
337
337
threshold = { 0.01 }
338
338
{ ...observerProps }
339
- onChange = { inView => this . update ( Action . ViewChanged ( { inView } ) ) }
339
+ onChange = { ( inView : boolean ) =>
340
+ this . update ( Action . ViewChanged ( { inView } ) )
341
+ }
340
342
>
341
- { ( { ref } ) =>
343
+ { ( { ref } : RenderProps ) =>
342
344
children ( {
343
345
// We know that the state tags and the enum match up, apart
344
346
// from Buffering not being exposed
@@ -350,12 +352,18 @@ export class LazyImageFull extends React.Component<
350
352
ref
351
353
} )
352
354
}
353
- </ Observer >
355
+ </ InView >
354
356
) ;
355
357
}
356
358
}
357
359
}
358
360
361
+ interface RenderProps {
362
+ inView : boolean ;
363
+ entry : IntersectionObserverEntry | undefined ;
364
+ ref : React . RefObject < any > | ( ( node ?: Element | null ) => void ) ;
365
+ }
366
+
359
367
///// Utilities /////
360
368
361
369
/** Promise constructor for loading an image */
@@ -384,7 +392,7 @@ const loadImage = (
384
392
// TODO: consider writing the .decode() definition and sending a PR
385
393
//@ts -ignore
386
394
. decode ( )
387
- . then ( ( image : HTMLImageElement ) => resolve ( image ) )
395
+ . then ( ( ) => resolve ( image ) )
388
396
. catch ( ( err : any ) => reject ( err ) )
389
397
) ;
390
398
}
@@ -398,7 +406,7 @@ const delayedPromise = (ms: number) =>
398
406
new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
399
407
400
408
interface CancelablePromise {
401
- promise : Promise < { } > ;
409
+ promise : Promise < any > ;
402
410
cancel : ( ) => void ;
403
411
}
404
412
@@ -412,7 +420,7 @@ interface CancelablePromise {
412
420
const makeCancelable = ( promise : Promise < any > ) : CancelablePromise => {
413
421
let hasCanceled_ = false ;
414
422
415
- const wrappedPromise = new Promise ( ( resolve , reject ) => {
423
+ const wrappedPromise : Promise < any > = new Promise ( ( resolve , reject ) => {
416
424
promise . then (
417
425
( val : any ) => ( hasCanceled_ ? reject ( { isCanceled : true } ) : resolve ( val ) )
418
426
) ;
0 commit comments