Skip to content

Instantly share code, notes, and snippets.

@joshuaalpuerto
joshuaalpuerto / Signal.js
Last active April 20, 2024 20:19
This gist contains a simple implementation of the Signal and Effect pattern in JavaScript. The Signal pattern is a behavioral design pattern that allows communication between objects in a decoupled way, while the Effect pattern is a pattern that allows for the separation of side effects from the main logic of an application.
let currentListener = undefined;
function createSignal(initialValue) {
let value = initialValue;
const subscribers = new Set();
const read = () => {
if (currentListener !== undefined) {
subscribers.add(currentListener);
@joshuaalpuerto
joshuaalpuerto / toRawValue.js
Created April 2, 2022 08:40
Getting raw type
function toRawType (value) {
let _toString = Object.prototype.toString;
let str = _toString.call(value)
return str.slice(8, -1)
}
toRawType('test') === 'String'
@joshuaalpuerto
joshuaalpuerto / aggregateBuilder.js
Last active January 10, 2023 16:04
Mongodb simple aggregate builder using Proxy
function generatePipeline(pipelines = []) {
const callable = () => {};
callable.pipelines = pipelines;
return new Proxy(callable, {
get(callable, propKey) {
return (props) => {
if (propKey === 'run') {
return callable.pipelines;
}
callable.pipelines.push({ [`$${propKey}`]: props });
@joshuaalpuerto
joshuaalpuerto / autocannon.graphql
Last active March 21, 2021 13:01
Auto cannon GraphQL
npx autocannon -c10 -d5 'http://localhost:4040/graphql' -m 'POST' --headers 'Authorization: <Token>' --headers 'Content-Type: application/json' --body '<body>'
@joshuaalpuerto
joshuaalpuerto / use-form.js
Created September 22, 2019 08:04
Basic form handling
import { useReducer, useCallback } from 'react';
export const INITIAL_STATE = {};
function formReducer(state, action) {
switch (action.type) {
case '__reset__':
return INITIAL_STATE;
case action.type:
return {
...state,
@joshuaalpuerto
joshuaalpuerto / use-api.js
Last active March 27, 2020 10:44
Hooks for requesting api built-in state
import { useReducer, useCallback, useRef, useEffect } from 'react';
import cond from 'lodash/cond';
import includes from 'lodash/fp/includes';
import stubTrue from 'lodash/stubTrue';
const initialState = {
response: null, // could be whatever type of response they are ,expecting
loading: false,
success: false,
error: false,