Skip to content

Instantly share code, notes, and snippets.

View ponyjackal's full-sized avatar
🏠
Working from home

ponyjackal

🏠
Working from home
View GitHub Profile
@ponyjackal
ponyjackal / Gemfile
Last active September 17, 2020 20:59 — forked from dhh/Gemfile
HEY's Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
@ponyjackal
ponyjackal / React useToggle
Created September 17, 2020 23:45
useToggle
import React, { useState, useEffect, useCallback } from "react";
const useToggle = (initState = false) => {
const [status, setStatus] = useState(initState);
const toggle = useCallback(() => {
setStatus((status) => {
return !status;
});
}, []);
@ponyjackal
ponyjackal / React useDebounce
Created September 17, 2020 23:46
useDebounce
import React, { useState, useEffect } from "react";
const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);
useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
@ponyjackal
ponyjackal / graphql-query-builder
Created September 17, 2020 23:50
GraphQL Query Builder
var Query = require('graphql-query-builder');
const client = new ApolloClient({
uri: 'http://localhost:4000',
});
function Test() {
const greeting = 'Hello Function Component!';
@ponyjackal
ponyjackal / Twilio Voice Conference
Created September 18, 2020 00:01
Twilio Voice conference
// Add grant to join voice conference (fire event to join voice conference)
$voiceToken = new AccessToken(
$_ENV['TWILIO_SID'],
$_ENV['TWILIO_VIDEO_SID'],
$_ENV['TWILIO_VIDEO_SECRET'],
3600,
$userIdentity
);
$voiceGrant = new VoiceGrant();
@ponyjackal
ponyjackal / Javascript Callback
Last active November 16, 2020 10:39
Callback in Javascript
Also known as the oldest way to handle asynchronous data, callbacks allow us to inject a function into an asynchronous execution so that we can control the result(s) when they are available. In a very indirect way, it's as if we're sending a "spy" who will inform us when something happens within the call we are executing.
We'll create a simple example to get started, a function that returns a new Date():
function getNewDate() {
return new Date();
}
const result = getNewDate();
@ponyjackal
ponyjackal / Javascript Promise
Last active November 16, 2020 10:39
Promise in Javascript
Promises are a native JavaScript implementation to more elegantly and simply handle asynchronous data.
Just like promises in the real world, promises in JavaScript can or not happen, technically we can say that we have control of the moment of success and error of the flows we are dealing with, but you can think, "but I also already have this possibility of handler with callbacks", and yes you can handler your errors using callbacks too, but imagine being able to compose, pipe your operarations and also get errors in a more elegant and literally more declarative way, that's just a little bit of what Promises can do.
Creating a Promise
The promises have 2 stages, creation and consume. Let's check the basics to create a new promise:
const myPromise = new Promise(function (resolve, reject) {
// do something
if (true) {
@ponyjackal
ponyjackal / Javascript Async Await
Last active November 16, 2020 10:39
Async/Await in Javascript
The keyword async was implemented in ES2017. It makes it possible to create naturally asynchronous functions using the following notation:
async function myAsyncFunction() {}
Something important and even more interesting about this implementation is that every async function returns a Promise, meaning that we can use all the interfaces we already know in the article about promises. Let's look at an example to better understand:
async function myAsyncFunction() {
return "Hello!";
}
@ponyjackal
ponyjackal / React usePromise
Last active November 16, 2020 10:39
usePromise
import { useState, useEffect } from "react";
export const usePromise = (
fn,
{ resolve = false, resolveCondition = [] } = {}
) => {
const [data, setData] = useState();
const [isLoading, setLoading] = useState(resolve);
const [lastUpdated, setLastUpdated] = useState();
const [error, setError] = useState();
@ponyjackal
ponyjackal / gist:f0208e6d2464de4cb4a9c523f1e5b4ff
Created September 27, 2020 16:09 — forked from rxaviers/gist:7360908
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: 😄 :smile: 😆 :laughing:
😊 :blush: 😃 :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
😆 :satisfied: 😁 :grin: 😉 :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: 😀 :grinning:
😗 :kissing: 😙 :kissing_smiling_eyes: 😛 :stuck_out_tongue: