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
type SubscribedEvent = (...args) => any; | |
class EventObserver { | |
public events: SubscribedEvent[] = []; | |
subscribe(fn: SubscribedEvent) { | |
this.events.push(fn) | |
} | |
unsubscribe(fn: SubscribedEvent) { |
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
/** | |
* Класс Singleton предоставляет метод getInstance, который позволяет клиентам | |
* получить доступ к уникальному экземпляру одиночки. | |
*/ | |
class Singleton { | |
private static instance: Singleton; | |
/** | |
* Конструктор Singleton должен быть приватный, чтобы кто-нибудь случайно не создал | |
* объект через оператор new. |
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 IParticle { | |
element: HTMLElement; | |
size: number; | |
horizontalSpeed: number; | |
speedUp: number; | |
spinVal: number; | |
spinSpeed: number; | |
top: number; | |
left: number; | |
direction: -1 | 1; |
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
/** | |
* Our subscription type. This is to manage teardowns. | |
*/ | |
class Subscription { | |
private teardowns = new Set<() => void>(); | |
add(teardown: () => void) { | |
this.teardowns.add(teardown); | |
} | |
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); | |
}; |
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
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
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); // 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
/** | |
* 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; | |
NewerOlder