Share core-owned immutable elements and validation facts
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import type {ElementRecord,ComponentDefinition} from './types.ts';
|
||||
import {clone,freeze} from './values.ts';
|
||||
|
||||
// Provenance comes from detaching JSON data, never from an external freeze or
|
||||
// document envelope. This does not assert record validity or registry support.
|
||||
const owned=new WeakSet<object>();
|
||||
export function detachElementData(record:ElementRecord):ElementRecord{
|
||||
if(record&&typeof record==='object'&&owned.has(record))return record;
|
||||
const value=freeze(clone(record));
|
||||
if(value&&typeof value==='object')owned.add(value);
|
||||
return value;
|
||||
}
|
||||
export const hasOwnedElementData=(record:ElementRecord)=>!!record&&typeof record==='object'&&owned.has(record);
|
||||
|
||||
// Framework-owned validators depend only on detached immutable data. Custom
|
||||
// validators remain live, including their method receiver and external context.
|
||||
const builtinValidators=new WeakSet<Function>(),dataProofs=new WeakMap<object,ComponentDefinition['validate']>();
|
||||
export function builtinDataValidator<T extends ComponentDefinition['validate']>(validate:T):T{builtinValidators.add(validate);return validate;}
|
||||
export function componentDataValid(record:ElementRecord,definition:ComponentDefinition):boolean{
|
||||
const validate=definition.validate,reusable=hasOwnedElementData(record)&&builtinValidators.has(validate);
|
||||
if(reusable&&dataProofs.get(record)===validate)return true;
|
||||
const valid=Reflect.apply(validate,definition,[record.data]);
|
||||
if(valid&&reusable)dataProofs.set(record,validate);
|
||||
return valid;
|
||||
}
|
||||
Reference in New Issue
Block a user