Skip to content

Instantly share code, notes, and snippets.

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

Poorshad Shaddel pshaddel

🏋️‍♀️
View GitHub Profile
@pshaddel
pshaddel / c_read_file_uv.c
Created October 13, 2024 09:04
Read File Using Libuv
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#include <fcntl.h>
#define BUFFER_SIZE 1024
// Forward declaration of the on_open function
void on_open(uv_fs_t *req);
@pshaddel
pshaddel / c_read_file_read.c
Created October 13, 2024 09:02
Read File in C
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
int fd;
@pshaddel
pshaddel / c_read_file_fread.c
Last active October 13, 2024 08:59
Read File in C using different implementations
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
FILE *file;
char buffer[BUFFER_SIZE];
size_t bytesRead;
@pshaddel
pshaddel / node_read_file_sync.js
Last active October 13, 2024 08:53
node_read_file_sync
const fs = require('fs');
function read_file_1() {
try {
// get the number of times we should repeat the read operation
const repeat = process.argv[2] || 1;
for (let i = 0; i < repeat; i++) {
const data = fs.readFileSync('./hello.txt', 'utf8');
console.log(data);
}
@pshaddel
pshaddel / node_read_file_promise.js
Last active October 13, 2024 08:50
node_read_file_promise
const fs = require('fs').promises;
async function read_file_1() {
try {
// get the number of times we should repeat the read operation
const repeat = process.argv[2] || 1;
for (let i = 0; i < repeat; i++) {
const data = await fs.readFile('./hello.txt', 'utf8');
console.log(data);
}
const fs = require('fs');
function read_file_1(repeat, count = 0) {
if (count >= repeat) return;
fs.readFile('./hello.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
@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,