Skip to content

Instantly share code, notes, and snippets.

@vlas-ilya
vlas-ilya / SafeCoroutinesExt2.kt
Last active September 27, 2023 07:28
DSL for handling errors with launch
import kotlinx.coroutines.*
object Dispatchers {
val Main: CoroutineDispatcher get() = kotlinx.coroutines.Dispatchers.Default
val IO: CoroutineDispatcher get() = kotlinx.coroutines.Dispatchers.IO
val Default: CoroutineDispatcher get() = kotlinx.coroutines.Dispatchers.Default
val Unconfined: CoroutineDispatcher get() = kotlinx.coroutines.Dispatchers.Unconfined
}
class LaunchBlock(
@vlas-ilya
vlas-ilya / HomeScreen.kt
Last active April 20, 2022 17:08
ObserveLifecycle
package ru.test.app.ui.features.profile
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
@vlas-ilya
vlas-ilya / LoadableData.helpers.ts
Created January 3, 2022 12:57
Redux | Only one action for async process [no boilerplate code]
import { Init, LoadableData } from "./LoadableData";
import { LoadableDataEvent } from "./LoadableDataEvent";
export function init<T>(data?: T): Init<T> {
return {
state: "init",
data,
};
}
type Supplier<T> = () => T;
type Value<T> = T;
type SupplierOnValue<T> = Supplier<T> | Value<T>;
type Closable = { close(): Promise<void> };
type ClosableCreator<T> = SupplierOnValue<Promise<T>>;
type Use = <C extends Closable>(closableCreator: ClosableCreator<C>) => Promise<C>;
type Runnable<Result> = (use: Use) => Promise<Result>;
type ClosableSync = { close(): void };
@vlas-ilya
vlas-ilya / useSharedState.ts
Created July 19, 2021 18:27
React hook for sharing state between components
import { useEffect, useMemo, useRef, useState } from "react";
interface State<T> {
state: T;
setState: (state: T) => void;
handlers: ((state: T) => void) [];
}
interface GlobalState {
[key: string]: State<any>;
import { useStore } from 'effector-react'
import { useEffect } from 'react';
import { pageMounted, listStore, stateStore } from './TodoList.store';
const TodoList = () => {
useEffect(pageMounted, []);
const list = useStore(listStore);
const state = useStore(stateStore);
import { createStore, createEffect } from 'effector';
import { listApi } from '../../api/Todo';
const store = createStore({
list: [],
state: 'INIT'
});
const loadTodoList = createEffect(listApi);
import { createStore, createEffect } from 'effector';
import { listApi } from '../../api/Todo';
const listStore = createStore([]);
const stateStore = createStore('INIT');
const loadTodoList = createEffect(() => listApi());
stateStore.on(loadTodoList, () => 'LOADING');
@vlas-ilya
vlas-ilya / getAllIpAddresses.js
Created June 29, 2020 17:26
Get ips by mask
const int2ip = (ipInt) => (
(ipInt >>> 24) + '.' +
(ipInt >> 16 & 255) + '.' +
(ipInt >> 8 & 255) + '.' +
(ipInt & 255)
);
const ip2int = (ip) => ip.split('.').reduce(
(ipInt, octet) => (ipInt << 8) + parseInt(octet, 10), 0
) >>> 0;
import React, { Component } from 'react'
import { connect } from 'react-redux'
@connect(
state => ({
user: state.home.authentication.user
})
)
export default class Authorized extends Component {
constructor(props, context) {