Skip to content

Instantly share code, notes, and snippets.

View pshaddel's full-sized avatar
🏋️‍♀️

Poorshad Shaddel pshaddel

🏋️‍♀️
View GitHub Profile
@pshaddel
pshaddel / mountWorkInProgressHook.js
Last active April 13, 2024 20:51
React Fiber Hooks
let currentHook: Hook | null = null;
let workInProgressHook: Hook | null = null;
function mountWorkInProgressHook(): Hook {
const hook: Hook = {
memoizedState: null,
baseState: null,
baseQueue: null,
@pshaddel
pshaddel / mountStateImpl.js
Created April 13, 2024 20:38
mountStateImpl
function mountStateImpl<S>(initialState: (() => S) | S): Hook {
const hook = mountWorkInProgressHook();
if (typeof initialState === 'function') {
const initialStateInitializer = initialState;
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
initialState = initialStateInitializer();
if (shouldDoubleInvokeUserFnsInHooksDEV) {
setIsStrictModeForDevtools(true);
// $FlowFixMe[incompatible-use]: Flow doesn't like mixed types
initialStateInitializer();
@pshaddel
pshaddel / mountState.js
Last active April 13, 2024 20:36
Mount State Function
function mountState<S>(
initialState: (() => S) | S,
): [S, Dispatch<BasicStateAction<S>>] {
const hook = mountStateImpl(initialState);
const queue = hook.queue;
const dispatch: Dispatch<BasicStateAction<S>> = (dispatchSetState.bind(
null,
currentlyRenderingFiber,
queue,
): any);
function dispatchSetState<S, A>(
fiber: Fiber,
queue: UpdateQueue<S, A>,
action: A,
): void {
const lane = requestUpdateLane(fiber);
const update: Update<S, A> = {
lane,
revertLane: NoLane,
@pshaddel
pshaddel / ReactHooks.js
Created April 13, 2024 19:59
useState implementation in ReactHooks.js file
export function useState<S>(
initialState: (() => S) | S,
): [S, Dispatch<BasicStateAction<S>>] {
const dispatcher = resolveDispatcher();
return dispatcher.useState(initialState);
}
@pshaddel
pshaddel / use_state.js
Last active April 13, 2024 17:14
Use State Implementation in Javascript
const React = (function () {
let _state;
return {
useState: function (initialValue) {
if (typeof _state === 'undefined') _state = initialValue;
function setState(newVal) {
_state = newVal;
}
return [_state, setState];
},
@pshaddel
pshaddel / closure_use_state_wrong_implementation.js
Created April 13, 2024 16:59
Use State Wrong Implementation
function useState(initialValue) {
var _state = initialValue // private value
function state() {
return _state
}
function setState(newVal) {
_state = newVal
}
return [_state, setState] // exposing functions for external use
};
@pshaddel
pshaddel / closure.js
Created April 13, 2024 16:40
Closure Simple Example
let count = 0;
function add() {
count++;
}
add();
console.log(count); // 1
@pshaddel
pshaddel / Login.js
Created January 3, 2024 08:51
Login Challenges
const { sleep } = require('@brickwise/express-utility');
const app = require('express').Router();
const UserService = {
getUser: (username) => {
}
}
// V1
app.post('/login', async (req, res) => {
// Step -2 - Bugs
// Step -1 - Dirty Way
// Step 0 - Projection should only accepts fields from interface
// Step 1 - Projection should be typed and only accepts mongodb valid values[0, 1, true, false]
// Step 2 - Projection should be optional
// Step 3 - Projection should support nested projection
// Step 4 - Projection should support nested projection with arrays(i)
// Step 5 - Projection should be nested optional
// Step 6 - MongoDB Projection rule, inclusion and exclusion cannot be mixed
// - Function output should match the projection: