Skip to content

Instantly share code, notes, and snippets.

View SZharkov's full-sized avatar
😎

Serhii SZharkov

😎
View GitHub Profile
@SZharkov
SZharkov / WebSocketProvider.js
Created July 11, 2023 08:30
WebSocket for ethers to avoid stop working in some time
import { ethers } from 'ethers';
const WEBSOCKET_PING_INTERVAL = 10000
const WEBSOCKET_PONG_TIMEOUT = 5000
const WEBSOCKET_RECONNECT_DELAY = 100
const WebSocketProviderClass = () => class {}
export class WebSocketProvider extends WebSocketProviderClass() {
events = []
@SZharkov
SZharkov / index.js
Created April 24, 2023 15:57
Websocket Stream with Ethers.js
class WebsocketStream {
static EXPECTED_PONG_BACK = 10000;
static KEEP_ALIVE_CHECK_INTERVAL = 5000;
pingTimeout = null;
keepAliveInterval = null;
provider;
providerWss;
constructor() {
this.provider = new ethers.providers.JsonRpcProvider(
@SZharkov
SZharkov / mongoose-two-fields-unique.js
Created April 7, 2023 08:06
Mongoose: make a combination of two fields unique
person.index({ firstName: 1, lastName: 1}, { unique: true });
@SZharkov
SZharkov / mongoose-remove-duplicates.js
Created April 7, 2023 08:03
Remove duplicates in MongoDB with mongoose
db.table.aggregate([
{
"$group": {
_id: {slug: "$slug"},
slugs: { $addToSet: "$_id" } ,
count: { $sum : 1 }
}
},
{
"$match": {
async function fetchUserResources() {
const [posts, photos, todos] = await Promise.allSettled([
fetch('https://jsonplaceholder.typicode.com/posts/1'),
fetch('https://jsonplaceholder.typicode.com/photos/1'),
fetch('https://jsonplaceholder.typicode.com/todos/1')
]);
console.log(posts);
// {status: "fulfilled", value: {"holds_the_posts_object"}}
@SZharkov
SZharkov / almostIncreasingSequence.js
Created September 27, 2020 16:06 — forked from ryanSN/almostIncreasingSequence.js
Javascript almost increasing sequence
function almostIncreasingSequence(sequence) {
var found = false;
for (var i=0;i<sequence.length;i++) {
if(sequence[i] <= sequence[i-1]) {
if(found) {
return false;
}
found = true;
if(i === 1 || i + 1 === sequence.length) {
// import functions from the package
import { SitemapStream, streamToPromise } from "sitemap";
// A custom function I use to fetch data from a backend. I will keep the import to make it more clear why "graphlqlFetch" is used in the code
import graphlqlFetch from "lib/apollo"
export default async (req, res) => {
// Fetch data from a source which will be used to render the sitemap.
const { posts } = await graphlqlFetch(`
query getSitemapData {
@SZharkov
SZharkov / index.js
Created August 2, 2020 11:04
Next.js ClientOnlyPortal
import { useRef, useEffect, useState } from 'react'
import { createPortal } from 'react-dom'
export default function ClientOnlyPortal({ children, selector }) {
const ref = useRef()
const [mounted, setMounted] = useState(false)
useEffect(() => {
ref.current = document.querySelector(selector)
setMounted(true)
@SZharkov
SZharkov / actions.js
Last active March 27, 2022 01:53
Context API store
import {
ADD_INGREDIENT,
INGREDIENTS_REDUCER,
PREPARING_REDUCER,
REMOVE_INGREDIENT,
SET_INGREDIENT_INPUT_VALUE, SET_INPUT_VALUE, SET_OPTIONAL,
SET_SELECTED_INGREDIENT,
} from "Store/add-new-dish/types";
const useAddNewDishActions = dispatch => {
@SZharkov
SZharkov / index.js
Created January 21, 2020 17:33
Basic authentication with fetch
// Node
headers.set('Authorization', 'Basic ' + Buffer.from(username + ":" + password).toString('base64'));
// Browser
headers.set('Authorization', 'Basic ' + btoa(username + ":" + password));