Update sigpro.js

This commit is contained in:
Natxo
2026-03-16 01:02:35 +01:00
committed by GitHub
parent b1776bc66b
commit 635c507d8b

View File

@@ -114,7 +114,7 @@ export const $ = (initialValue) => {
* @param {Function} effectFn - The effect function to run
* @returns {Function} Cleanup function to stop the effect
*/
export const $effect = (effectFn) => {
const $e = (effectFn) => {
const effect = {
dependencies: new Set(),
cleanupHandlers: new Set(),
@@ -155,7 +155,7 @@ export const $effect = (effectFn) => {
* @param {Storage} [storage=localStorage] - Storage type (localStorage/sessionStorage)
* @returns {Function} Signal that persists to storage
*/
export const $storage = (key, initialValue, storage = localStorage) => {
const $s = (key, initialValue, storage = localStorage) => {
let initial;
try {
const saved = storage.getItem(key);
@@ -172,7 +172,7 @@ export const $storage = (key, initialValue, storage = localStorage) => {
const signal = $(initial);
$effect(() => {
$e(() => {
try {
const value = signal();
if (value === undefined || value === null) {
@@ -226,7 +226,7 @@ export const html = (strings, ...values) => {
parent.insertBefore(endMarker, node);
let lastResult;
$effect(() => {
$e(() => {
let result = typeof currentValue === "function" ? currentValue() : currentValue;
if (result === lastResult) return;
lastResult = result;
@@ -364,15 +364,15 @@ export const html = (strings, ...values) => {
});
// Cleanup
if ($effect.onCleanup) {
$effect.onCleanup(() => node.removeEventListener(eventName, handlerWrapper));
if ($e.onCleanup) {
$e.onCleanup(() => node.removeEventListener(eventName, handlerWrapper));
}
} else if (firstChar === ":") {
// Two-way binding
const propertyName = attributeName.slice(1);
const eventType = node.type === "checkbox" || node.type === "radio" ? "change" : "input";
$effect(() => {
$e(() => {
const value = typeof currentValue === "function" ? currentValue() : currentValue;
if (node[propertyName] !== value) node[propertyName] = value;
});
@@ -384,14 +384,14 @@ export const html = (strings, ...values) => {
} else if (firstChar === "?") {
// Boolean attribute
const attrName = attributeName.slice(1);
$effect(() => {
$e(() => {
const result = typeof currentValue === "function" ? currentValue() : currentValue;
node.toggleAttribute(attrName, !!result);
});
} else if (firstChar === ".") {
// Property binding
const propertyName = attributeName.slice(1);
$effect(() => {
$e(() => {
let result = typeof currentValue === "function" ? currentValue() : currentValue;
node[propertyName] = result;
if (result != null && typeof result !== "object" && typeof result !== "boolean") {
@@ -401,7 +401,7 @@ export const html = (strings, ...values) => {
} else {
// Regular attribute
if (typeof currentValue === "function") {
$effect(() => node.setAttribute(attributeName, currentValue()));
$e(() => node.setAttribute(attributeName, currentValue()));
} else {
node.setAttribute(attributeName, currentValue);
}
@@ -424,7 +424,7 @@ export const html = (strings, ...values) => {
* @param {Function} setupFunction - Component setup function
* @param {string[]} observedAttributes - Array of observed attributes
*/
export const $component = (tagName, setupFunction, observedAttributes = []) => {
const $c = (tagName, setupFunction, observedAttributes = []) => {
if (customElements.get(tagName)) return;
customElements.define(
@@ -495,7 +495,7 @@ export const $component = (tagName, setupFunction, observedAttributes = []) => {
* @param {Function} [loading] - Optional signal function to track loading state
* @returns {Promise<Object|null>} Parsed JSON response or null on error
*/
export const $fetch = async (url, data, loading) => {
const $f = async (url, data, loading) => {
if (loading) loading(true);
try {
@@ -529,7 +529,7 @@ const sanitizePath = (path) => {
* @param {Array<{path: string|RegExp, component: Function}>} routes - Route configurations
* @returns {HTMLDivElement} Router container element
*/
export const $router = (routes) => {
const $r = (routes) => {
/**
* Gets current path from hash
* @returns {string} Current path
@@ -545,7 +545,7 @@ export const $router = (routes) => {
if (currentPath() !== nextPath) currentPath(nextPath);
});
$effect(() => {
$e(() => {
const path = currentPath();
let matchedRoute = null;
let routeParams = {};
@@ -583,7 +583,7 @@ export const $router = (routes) => {
* Navigates to a specific route
* @param {string} path - Target path
*/
$router.go = (path) => {
$r.go = (path) => {
const targetPath = path.startsWith("/") ? path : `/${path}`;
if (window.location.hash !== `#${targetPath}`) window.location.hash = targetPath;
};
@@ -594,7 +594,7 @@ $router.go = (path) => {
* @param {Object} options - Auto-reconnect options
* @returns {Object} WebSocket with reactive signals
*/
export const $ws = (url, options = {}) => {
const $ws = (url, options = {}) => {
const { reconnect = true, maxReconnect = 5, reconnectInterval = 1000 } = options;
const status = $("disconnected");
@@ -656,14 +656,16 @@ export const $ws = (url, options = {}) => {
connect();
if ($effect?.onCleanup) $effect.onCleanup(close);
if ($e?.onCleanup) $e.onCleanup(close);
return { status, messages, error, send, close };
};
$.effect = $effect;
$.component = $component;
$.fetch = $fetch;
$.router = $router;
/* Can customize the name of your functions */
$.effect = $e;
$.component = $c;
$.fetch = $f;
$.router = $r;
$.ws = $ws;
$.storage = $storage;
$.storage = $s;