Skip to content

Instantly share code, notes, and snippets.

View qmzik's full-sized avatar

Nikita Lebedev qmzik

  • Yekaterinburg, Russia
View GitHub Profile
type SubscribedEvent = (...args) => any;
class EventObserver {
public events: SubscribedEvent[] = [];
subscribe(fn: SubscribedEvent) {
this.events.push(fn)
}
unsubscribe(fn: SubscribedEvent) {
/**
* Класс Singleton предоставляет метод getInstance, который позволяет клиентам
* получить доступ к уникальному экземпляру одиночки.
*/
class Singleton {
private static instance: Singleton;
/**
* Конструктор Singleton должен быть приватный, чтобы кто-нибудь случайно не создал
* объект через оператор new.
interface IParticle {
element: HTMLElement;
size: number;
horizontalSpeed: number;
speedUp: number;
spinVal: number;
spinSpeed: number;
top: number;
left: number;
direction: -1 | 1;
/**
* Our subscription type. This is to manage teardowns.
*/
class Subscription {
private teardowns = new Set<() => void>();
add(teardown: () => void) {
this.teardowns.add(teardown);
}
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);
};
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) => {
class SafeSubscriber<T> {
closed = false;
constructor(private destination: Partial<Observer<T>>) {}
next(value: T) {
if (!this.closed) {
this.destination.next?.(value);
}
}
interface Observer<T> {
next: (value: T) => void;
complete: () => void;
error: (err: any) => void;
}
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.
}
}
/**
* 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;