> For the complete documentation index, see [llms.txt](https://www.learnrxjs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.learnrxjs.io/learn-rxjs/recipes/swipe-to-refresh.md).

# Swipe To Refresh

*By* [*adamlubek*](https://github.com/adamlubek)

This recipe demonstrates RxJS implementation of swipe to refresh functionality. Inspired by [@BenLesh](https://twitter.com/BenLesh) RxJs talks.

### Example Code

( [StackBlitz](https://stackblitz.com/edit/rxjs-refresh?file=index.ts\&devtoolsheight=40) )

![Swipe to Refresh](https://drive.google.com/uc?export=view\&id=1BLA2TcAhjwtodkcnsJ8e91ckrvurqkEv)

#### index.ts

```js
// RxJS v6+
import { fromEvent, iif, of, pipe } from 'rxjs';
import {
  finalize,
  mergeMap,
  takeUntil,
  takeWhile,
  repeat,
  map,
  tap,
  exhaustMap,
  delay
} from 'rxjs/operators';

const setRefreshPos = y =>
  (document.getElementById('refresh').style.top = `${y}px`);
const resetRefresh = () => setRefreshPos(10);
const setData = data => (document.getElementById('data').innerText = data);

const fakeRequest = () =>
  of(new Date().toUTCString()).pipe(
    tap(_ => console.log('request')),
    delay(1000)
  );

const takeUntilMouseUpOrRefresh$ = pipe(
  takeUntil(fromEvent(document, 'mouseup')),
  takeWhile(y => y < 110)
);
const moveDot = y => of(y).pipe(tap(setRefreshPos));
const refresh$ = of({}).pipe(
  tap(resetRefresh),
  tap(e => setData('...refreshing...')),
  exhaustMap(_ => fakeRequest()),
  tap(setData)
);

fromEvent(document, 'mousedown')
  .pipe(
    mergeMap(_ => fromEvent(document, 'mousemove')),
    map((e: MouseEvent) => e.clientY),
    takeUntilMouseUpOrRefresh$,
    finalize(resetRefresh),
    exhaustMap(y => iif(() => y < 100, moveDot(y), refresh$)),
    finalize(() => console.log('end')),
    repeat()
  )
  .subscribe();
```

**html**

```
<style>
  #refresh {
    position: absolute;
    width: 20px;
    height: 20px;
    background: grey;
    border-radius: 50%;
    left: 50%;
  }

  #point {
    position: absolute;
    width: 10px;
    height: 10px;
    background: lightgrey;
    border-radius: 50%;
    left: 51%;
    top: 105px;
  }

  #data {
    position: absolute;
    top: 150px;
  }
</style>

<div id='refresh'></div>
<div id='point'></div>
<div id='data'>Swipe gray dot down to get latest date/time</div>
```

### Operators Used

* [delay](/learn-rxjs/operators/utility/delay.md)
* [exhaustMap](/learn-rxjs/operators/transformation/exhaustmap.md)
* [finalize](/learn-rxjs/operators/utility/finalize.md)
* [fromEvent](/learn-rxjs/operators/creation/fromevent.md)
* [iif](/learn-rxjs/operators/conditional/iif.md)
* [map](/learn-rxjs/operators/transformation/map.md)
* [mergeMap](/learn-rxjs/operators/transformation/mergemap.md)
* [of](/learn-rxjs/operators/creation/of.md)
* [repeat](/learn-rxjs/operators/utility/repeat.md)
* [takeUntil](/learn-rxjs/operators/filtering/takeuntil.md)
* [takeWhile](/learn-rxjs/operators/filtering/takewhile.md)
* [tap](/learn-rxjs/operators/utility/do.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.learnrxjs.io/learn-rxjs/recipes/swipe-to-refresh.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
