Skip to content

Instantly share code, notes, and snippets.

View helabenkhalfallah's full-sized avatar
🎯
Focusing

Héla Ben Khalfallah helabenkhalfallah

🎯
Focusing
View GitHub Profile
@helabenkhalfallah
helabenkhalfallah / FetchingData.js
Created April 28, 2024 19:07
Solid Fetching Data
import { createResource, For, Match, Switch, Show } from "solid-js";
import { render } from "solid-js/web";
// Define an asynchronous function to fetch user data from an API endpoint
const fetchUser = async () => {
// Fetch user data from the API
const response = await fetch(`https://dummyjson.com/users`);
// Parse the JSON response
const jsonResponse = await response.json();
// Return the array of users from the JSON response
@helabenkhalfallah
helabenkhalfallah / createSignalMock.js
Last active April 28, 2024 13:03
Simplified version of createSignal
function createSignal(initialValue) {
let value = initialValue; // Internal state to hold the current value of the signal
const subscribers = new Set(); // Set to keep track of subscribers (components or functions that depend on the signal)
// Getter function to retrieve the current value of the signal
function get() {
return value;
}
// Setter function to update the value of the signal and notify subscribers
@helabenkhalfallah
helabenkhalfallah / SignalDependencyGraph.js
Created April 28, 2024 09:41
Simplified internal implementation of the interaction between the "signal" and the "dependency graph"
class Signal {
constructor(initialValue, dependencyGraph) {
this.value = initialValue;
this.dependencyGraph = dependencyGraph;
}
// Method to set a new value and notify subscribers
setValue(newValue) {
this.value = newValue;
// Notify subscribers through the dependency graph
@helabenkhalfallah
helabenkhalfallah / createSignalExample.js
Created April 28, 2024 08:49
Simplified version of createSignal
// Array to keep track of currently executing computations
const context = [];
// Function to add a computation (running) to the subscriptions of a signal
function subscribe(running, subscriptions) {
// Add the computation to the subscriptions set
subscriptions.add(running);
// Add the subscriptions set to the dependencies of the computation
running.dependencies.add(subscriptions);
}
@helabenkhalfallah
helabenkhalfallah / ProxyWithComponent.js
Last active April 28, 2024 07:55
Subscribing a functional component to reactive store changes
import React, { useState, useEffect } from "react";
// Function to create a reactive store
function createStore(initialState) {
// Store the initial state in a private variable
let state = initialState;
// Array to hold subscribers (callbacks)
const subscribers = [];
@helabenkhalfallah
helabenkhalfallah / ProxyCallbacksExample.js
Created April 28, 2024 07:21
An example of a JavaScript proxy with callbacks
function createStore(initialState) {
// Store the initial state in a private variable
let state = initialState;
// Array to hold subscribers (callbacks)
const subscribers = [];
// Create a proxy for the state object
const stateProxy = new Proxy(state, {
// Trap for getting a property value
@helabenkhalfallah
helabenkhalfallah / ProxyExample.js
Created April 28, 2024 07:20
An example of a JavaScript proxy
// Function to create a reactive store
function createStore(initialState) {
// Store the initial state in a private variable
let state = initialState;
// Create a proxy for the state object
const stateProxy = new Proxy(state, {
// Trap for getting a property value
get: function(target, property, receiver) {
console.log(`Getting property '${property}'`);
<script>
import { onMount } from 'svelte';
// Define an array of tasks
let tasks = [
{ id: 1, title: 'Complete homework', completed: false },
{ id: 2, title: 'Do laundry', completed: false },
{ id: 3, title: 'Buy groceries', completed: false }
];
@helabenkhalfallah
helabenkhalfallah / Parent.svelte
Created April 10, 2024 20:04
Parent Component
<!-- Parent.svelte -->
<!-- Script Section -->
<script>
// Import necessary modules
import axios from 'axios'; // Import axios for making HTTP requests
import Child from './Child.svelte'; // Import the Child component
// Function to handle form submission
const handleSubmit = async (event) => {
@helabenkhalfallah
helabenkhalfallah / Child.svelte
Created April 10, 2024 20:03
Child Component
<!-- Child.svelte -->
<!-- Script Section -->
<script>
// Import necessary functions from Svelte
import { createEventDispatcher } from 'svelte';
// Create a dispatcher for emitting custom events
const dispatch = createEventDispatcher();