Skip to content

Instantly share code, notes, and snippets.

View nelsonprsousa's full-sized avatar
:octocat:
Working from home

Nelson Sousa nelsonprsousa

:octocat:
Working from home
View GitHub Profile
@nelsonprsousa
nelsonprsousa / BaseLoader.cs
Last active January 29, 2022 18:19
Useful to avoid multiple requests to the same Task<T> (e.g. an HTTP GET operation). You probably want to registered the loader as a scoped or singleton service. Bare in mind that GetOrAdd is *not* atomic, although you can use your loader safely since it is thread-safe (usage of Lazy<Task<T>> to avoid multiple operations to resolve Task<T>).
public abstract class BaseLoader<T> : IBaseLoader<T>
where T : class
{
private readonly ConcurrentDictionary<string, Lazy<Task<T?>>> cache = new();
public Task<T?> LoadAsync(string key, Func<Task<T?>> factory)
{
return cache.GetOrAdd(key, (_) => new Lazy<Task<T?>>(factory)).Value;
}
}
@brandtnewlabs
brandtnewlabs / ScaleInOut.js
Created February 13, 2021 14:15
Reanimated Scale In & Out Component which pops in with a spring animation if it's visible and moves out with a timing animation if not.
import React, { useEffect } from 'react'
import Animated, {
useAnimatedStyle,
useSharedValue,
withDelay,
withSpring,
withTiming
} from 'react-native-reanimated'
const ScaleInOut = ({