Fixes and responsive character sheet.

This commit is contained in:
2026-06-16 11:14:46 +02:00
parent bc1839c5e3
commit a5317d6156
5 changed files with 293 additions and 138 deletions

View File

@@ -17,7 +17,7 @@ export const _defer = (fn: () => void) => {
_deferSet.add(fn);
}
let activeEffect: (() => void) | null = null, _isTracking = true;
let effectStack: Array<(() => void)> = [], _isTracking = true;
const SYMBOLS = {
PROXY: Symbol('is a proxy'),
ITERATE: Symbol('iterating'),
@@ -26,7 +26,7 @@ const SYMBOLS = {
function reactiveReadArray<T>(array: T[]): T[]
{
const _raw = raw(array)
const _raw = raw(array);
if (_raw === array) return _raw;
track(_raw, SYMBOLS.ITERATE);
return _raw.map(wrapReactive);
@@ -42,7 +42,7 @@ function iterator(self: unknown[], method: keyof Array<unknown>, wrapValue: (val
const iter = (arr[method] as any)() as IterableIterator<unknown> & {
_next: IterableIterator<unknown>['next']
};
if (arr !== self && !isShallow(self))
if (arr !== self)
{
iter._next = iter.next;
iter.next = () => {
@@ -82,7 +82,7 @@ function apply(self: unknown[], method: keyof Array<any>, fn: (item: unknown, in
else if (fn.length > 2)
{
wrappedFn = function (this: unknown, item, index) {
return fn.call(this, item, index, self);
return fn.call(this, wrapReactive(item), index, self);
};
}
}
@@ -95,7 +95,7 @@ function reduce(self: unknown[], method: keyof Array<any>, fn: (acc: unknown, it
let wrappedFn = fn;
if (arr !== self && fn.length > 3)
{
wrappedFn = function (this: unknown, acc, item, index) { return fn.call(this, acc, wrapReactive(item), index, self) };
wrappedFn = function (this: unknown, acc, item, index) { return fn.call(this, acc, wrapReactive(item), index, self) };
}
else
{
@@ -119,14 +119,19 @@ function searchProxy(self: unknown[], method: keyof Array<any>, args: unknown[])
}
function noTracking(self: unknown[], method: keyof Array<any>, args: unknown[] = [])
{
_isTracking = false;
const res = (raw(self) as any)[method].apply(self, args);
_isTracking = true;
return res;
try
{
_isTracking = false;
return (raw(self) as any)[method].apply(self, args);
}
finally
{
_isTracking = true;
}
}
const arraySubstitute = <any>{ // <-- <any> is required to allow __proto__ without getting an error
__proto__: null, // <-- Required to remove the object prototype removing the object default functions from the substitution
__proto__: null, // <-- Required to remove the object prototype, removing the object default functions from the substitution as a result
[Symbol.iterator]() { return iterator(this, Symbol.iterator, item => wrapReactive(item)) },
concat(...args: unknown[]) { return reactiveReadArray(this).concat(...args.map(x => (Array.isArray(x) ? reactiveReadArray(x) : x))) },
entries() { return iterator(this, 'entries', (value: [number, unknown]) => { value[1] = wrapReactive(value[1]); return value; }) },
@@ -192,7 +197,7 @@ function trigger(target: object, key?: string | symbol | null, value?: unknown)
}
function track(target: object, key: string | symbol | null)
{
if(!activeEffect || !_isTracking) return;
if(effectStack.length === 0 || !_isTracking) return;
let dependencies = _tracker.get(target);
if(!dependencies)
@@ -208,9 +213,7 @@ function track(target: object, key: string | symbol | null)
dependencies.set(key, set);
}
set.add(activeEffect);
//if(set) console.log('Tracking %o with key "%s"', target, key, set.size);
set.add(effectStack.slice(-1)[0]!);
}
export type Proxy<T> = T & {
[SYMBOLS.PROXY]?: boolean;
@@ -294,11 +297,11 @@ export function reactivity<T>(reactiveProperty: Reactive<T>, effect: (processed:
// Also useful to retrigger the tracking system if the reactive property provides new properties (via conditions for example)
const secureEffect = () => effect(typeof reactiveProperty === 'function' ? (reactiveProperty as () => T)() : reactiveProperty);
const secureContext = () => {
activeEffect = secureContext;
effectStack.push(secureContext);
try {
return secureEffect();
} finally {
activeEffect = null;
effectStack.pop();
}
};