Actualizar sigworkPro.js
This commit is contained in:
106
sigworkPro.js
106
sigworkPro.js
@@ -126,60 +126,71 @@ const trigger = (subs) => {
|
|||||||
scheduleFlush();
|
scheduleFlush();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const $ = (initialValue) => {
|
export const $ = (initialValue, storageKey) => {
|
||||||
|
if (isFunction(initialValue)) {
|
||||||
|
let dirty = true, cached;
|
||||||
|
const s = $();
|
||||||
|
const stop = Watch(() => {
|
||||||
|
const v = initialValue();
|
||||||
|
if (!Object.is(v, cached)) { cached = v; dirty = false; s(v); }
|
||||||
|
});
|
||||||
|
if (currentComponentContext) currentComponentContext.unmount.push(stop);
|
||||||
|
const signal = (newVal) => {
|
||||||
|
if (newVal === undefined) {
|
||||||
|
if (dirty) { cached = initialValue(); dirty = false; s(cached); }
|
||||||
|
return s();
|
||||||
|
}
|
||||||
|
return s(newVal);
|
||||||
|
};
|
||||||
|
return signal;
|
||||||
|
}
|
||||||
|
|
||||||
const subs = new Set();
|
const subs = new Set();
|
||||||
return (newVal) => {
|
let value = initialValue;
|
||||||
|
|
||||||
|
if (storageKey) {
|
||||||
|
try {
|
||||||
|
const item = localStorage.getItem(storageKey);
|
||||||
|
if (item !== null) value = JSON.parse(item);
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const signal = (newVal) => {
|
||||||
if (newVal === undefined) {
|
if (newVal === undefined) {
|
||||||
track(subs);
|
track(subs);
|
||||||
return initialValue;
|
return value;
|
||||||
}
|
}
|
||||||
if (!Object.is(newVal, initialValue)) {
|
const next = isFunction(newVal) ? newVal(value) : newVal;
|
||||||
initialValue = newVal;
|
if (!Object.is(next, value)) {
|
||||||
|
value = next;
|
||||||
|
if (storageKey) {
|
||||||
|
try { localStorage.setItem(storageKey, JSON.stringify(value)); } catch (e) {}
|
||||||
|
}
|
||||||
trigger(subs);
|
trigger(subs);
|
||||||
}
|
}
|
||||||
return initialValue;
|
return value;
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
export const persistent = (initialValue, storageKey) => {
|
if (storageKey) {
|
||||||
let stored = initialValue;
|
const sync = Watch(() => { signal(value); });
|
||||||
try {
|
if (currentComponentContext) currentComponentContext.unmount.push(sync);
|
||||||
const item = localStorage.getItem(storageKey);
|
|
||||||
if (item !== null) stored = JSON.parse(item);
|
|
||||||
} catch (e) {}
|
|
||||||
const sig = $(stored);
|
|
||||||
Watch(() => {
|
|
||||||
localStorage.setItem(storageKey, JSON.stringify(sig()));
|
|
||||||
});
|
|
||||||
return sig;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const computed = (fn) => {
|
|
||||||
const s = $();
|
|
||||||
let dirty = true;
|
|
||||||
let cachedValue;
|
|
||||||
const stop = Watch(() => {
|
|
||||||
const newValue = fn();
|
|
||||||
if (!Object.is(newValue, cachedValue)) {
|
|
||||||
cachedValue = newValue;
|
|
||||||
dirty = false;
|
|
||||||
s(newValue);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (currentComponentContext) {
|
|
||||||
currentComponentContext.unmount.push(stop);
|
|
||||||
}
|
}
|
||||||
const signal = () => {
|
|
||||||
if (dirty) {
|
|
||||||
cachedValue = fn();
|
|
||||||
dirty = false;
|
|
||||||
s(cachedValue);
|
|
||||||
}
|
|
||||||
return s();
|
|
||||||
};
|
|
||||||
return signal;
|
return signal;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const set = (signal, path, value) => {
|
||||||
|
if (value === undefined) {
|
||||||
|
signal(isFunction(path) ? path(signal()) : path);
|
||||||
|
} else {
|
||||||
|
const keys = path.split('.');
|
||||||
|
const last = keys.pop();
|
||||||
|
const obj = keys.reduce((o, k) => ({ ...o, [k]: { ...o[k] } }), { ...signal() });
|
||||||
|
obj[last] = value;
|
||||||
|
signal(obj);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const watch = (source, callback) => {
|
export const watch = (source, callback) => {
|
||||||
let first = true, oldVal;
|
let first = true, oldVal;
|
||||||
return Watch(() => {
|
return Watch(() => {
|
||||||
@@ -311,6 +322,12 @@ export const Tag = (tag, props = {}, ...children) => {
|
|||||||
} else if (isFunction(v)) {
|
} else if (isFunction(v)) {
|
||||||
const stopAttr = Watch(() => setProperty(el, k, v(), isSVG));
|
const stopAttr = Watch(() => setProperty(el, k, v(), isSVG));
|
||||||
registerNodeCleanup(el, stopAttr);
|
registerNodeCleanup(el, stopAttr);
|
||||||
|
if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) && (k === 'value' || k === 'checked')) {
|
||||||
|
const evType = k === 'checked' ? 'change' : 'input';
|
||||||
|
const handler = (e) => v(e.target[k]);
|
||||||
|
el.addEventListener(evType, handler);
|
||||||
|
onUnmount(() => el.removeEventListener(evType, handler));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setProperty(el, k, v, isSVG);
|
setProperty(el, k, v, isSVG);
|
||||||
}
|
}
|
||||||
@@ -416,6 +433,7 @@ export const Router = ({ routes }) => {
|
|||||||
matched.path.split('/').filter(Boolean).forEach((s, i) => {
|
matched.path.split('/').filter(Boolean).forEach((s, i) => {
|
||||||
if (s[0] === ':') params[s.slice(1)] = current.split('/').filter(Boolean)[i];
|
if (s[0] === ':') params[s.slice(1)] = current.split('/').filter(Boolean)[i];
|
||||||
});
|
});
|
||||||
|
Router.params(params);
|
||||||
const view = Tag(matched.component, { params });
|
const view = Tag(matched.component, { params });
|
||||||
outlet.appendChild(view);
|
outlet.appendChild(view);
|
||||||
}
|
}
|
||||||
@@ -423,6 +441,8 @@ export const Router = ({ routes }) => {
|
|||||||
return outlet;
|
return outlet;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Router.params = $({});
|
||||||
|
|
||||||
export const navigate = (path) => { window.location.hash = path; };
|
export const navigate = (path) => { window.location.hash = path; };
|
||||||
export const currentPath = () => window.location.hash.slice(1) || '/';
|
export const currentPath = () => window.location.hash.slice(1) || '/';
|
||||||
|
|
||||||
@@ -441,4 +461,4 @@ export const createApp = (Root, rootProps = {}) => (selector) => {
|
|||||||
globalThis[tag[0].toUpperCase() + tag.slice(1)] = (props, ...children) => Tag(tag, props, ...children);
|
globalThis[tag[0].toUpperCase() + tag.slice(1)] = (props, ...children) => Tag(tag, props, ...children);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default { $, Watch, computed, watch, untrack, Tag, If, For, Transition, Router, createApp, removeNode, navigate, currentPath, onMount, onUnmount, persistent };
|
export default { $, set, Watch, watch, untrack, Tag, If, For, Transition, Router, createApp, removeNode, navigate, currentPath, onMount, onUnmount };
|
||||||
Reference in New Issue
Block a user