Skip to content

Instantly share code, notes, and snippets.

View tehSLy's full-sized avatar

Andrei tehSLy

  • Saint-Petersburg
View GitHub Profile
{
"items": {
"1234": {
"id": 1,
"name": "John Doe"
},
"4321": {
"id": 2,
"name": "Foo Bar"
}
const createConnectionModel = (config: any, refreshURL: string) => {
const instance = axios.create(config);
let refreshRequest: Promise<any>;
const unauthorized = createEvent();
/* если юзаете JWT без кук, что впрочем, я не рекомендую
instance.interceptors.request.use((config) => {
config.headers['auth'] = localStorage.getItem(...);
})
import { createEffect, createEvent, createStore, restore } from "effector";
export const createWebSocketConnection = (url: string, retries = 5) => {
const onEvent = createEvent<Event>();
const statusChanged = onEvent.map(({ type }) => type);
const message = createEvent<MessageEvent>();
const send = createEffect();
const $status = restore(statusChanged.filter({ fn: (status) => status === "close" || status === "open" }), "close");
const error = onEvent.filter({fn: ({type}) => type === "error"})
const retry = createEffect();
import { createActions, handleActions, combineActions } from 'redux-actions';
const defaultState = { counter: 10 };
const { increment, decrement } = createActions({
INCREMENT: (amount = 1) => ({ amount }),
DECREMENT: (amount = 1) => ({ amount: -amount })
});
const reducer = handleActions(
const createConnectionModel = (config: AxiosRequestConfig, refreshURL: string) => {
const unauthenticated = createEvent();
let refreshRequest: Promise<any>;
const connection = axios.create(config);
connection.interceptors.response.use(null, async (err) => {
if(err.response.code === 401){
if(err.response.data === "Unauthenticated"){
unauthenticated();
throw new Error("unauthenticated");
import { createEffect, createEvent, createStore, Event as EffectorEvent } from "effector";
export const createWebSocketConnection = (url: string) => {
const instance = new WebSocket(url);
const sendMessage = createEffect<object, any, Error>().use((data) => instance.send(JSON.stringify(data)));
const {message, statusChanged} = createEvents(instance);
const status = createStore<keyof WebSocketEventMap>("close")
.on(statusChanged, (_, e) => e.type)