This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios, { CancelTokenStatic } from 'axios'; | |
export const http = axios.create({ | |
baseURL: 'https://api.domen.com/', | |
}); | |
const cancelToken: CancelTokenStatic = axios.CancelToken; | |
export let cancelSource = cancelToken.source(); | |
export const refreshTokenForUpdateApp = (() => { | |
cancelSource = cancelToken.source(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from 'rxjs'; | |
const source = new Observable<number>((subscriber) => { | |
subscriber.next(1); | |
subscriber.next(2); | |
subscriber.next(3); | |
subscriber.complete(); | |
}); | |
// Usage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple object with a `next` and `complete` callback on it. | |
*/ | |
interface Observer<T> { | |
next: (value: T) => void; | |
complete: () => void; | |
} | |
/** | |
* A function that takes a simple object with callbacks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const source = function (subscriber: Observer<number>) { | |
subscriber.next(1); | |
subscriber.next(2); | |
subscriber.next(3); | |
subscriber.complete(); | |
subscriber.next(4); // Упс, этого не должно быть | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A class used to wrap a user-provided Observer. Since the | |
* observer is just a plain objects with a couple of callbacks on it, | |
* this type will wrap that to ensure `next` does nothing if called after | |
* `complete` has been called, and that nothing happens if `complete` | |
* is called more than once. | |
*/ | |
class SafeSubscriber<T> { | |
closed = false; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SafeSubscriber<T> { | |
closed = false; | |
constructor(private destination: Partial<Observer<T>>) {} | |
next(value: T) { | |
if (!this.closed) { | |
this.destination.next?.(value); // Note the ?. check here. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observer<T> { | |
next: (value: T) => void; | |
complete: () => void; | |
error: (err: any) => void; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SafeSubscriber<T> { | |
closed = false; | |
constructor(private destination: Partial<Observer<T>>) {} | |
next(value: T) { | |
if (!this.closed) { | |
this.destination.next?.(value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const helloSocket = new Observable<string>((subscriber) => { | |
// Open a socket. | |
const socket = new WebSocket('wss://echo.websocket.org'); | |
socket.onopen = () => { | |
// Once it's open, send some text. | |
socket.send('Hello, World!'); | |
}; | |
socket.onmessage = (e) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const source = (subscriber: Observer<string>) => { | |
const socket = new WebSocket('wss://echo.websocket.org'); | |
socket.onopen = () => { | |
socket.send('Hello, World!'); | |
}; | |
socket.onmessage = (e) => { | |
subscriber.next(e.data); | |
}; |
OlderNewer