@ -4,29 +4,29 @@ import { get } from '../internal/client/runtime.js';
/ * *
/ * *
* @ template TEntityInstance
* @ template TEntityInstance
* @ template { ( keyof TEntityInstance ) [ ] } T Mutation Properties
* @ template { ( keyof TEntityInstance ) [ ] } T Write Properties
* @ template { ( keyof TEntityInstance ) [ ] } TReadProperties
* @ template { ( keyof TEntityInstance ) [ ] } TReadProperties
* @ typedef { Partial < Record < T Mutation Properties[ number ] , ( notify _read _methods : ( methods : TReadProperties , ... params : unknown [ ] ) => void , value : TEntityInstance , property : T Mutation Properties[ number ] , ... params : unknown [ ] ) => boolean >> } Interceptors - return false if you want to prevent reactivity for this call , DO NOT USE INTERCEPTORS FOR READ METHODS
* @ typedef { Partial < Record < T Write Properties[ number ] , ( notify _read _methods : ( methods : TReadProperties , ... params : unknown [ ] ) => void , value : TEntityInstance , property : T Write Properties[ number ] , ... params : unknown [ ] ) => boolean >> } Interceptors - return false if you want to prevent reactivity for this call , DO NOT USE INTERCEPTORS FOR READ METHODS
* /
* /
/ * *
/ * *
* @ template TEntityInstance
* @ template TEntityInstance
* @ template { ( keyof TEntityInstance ) [ ] } T Mutation Properties
* @ template { ( keyof TEntityInstance ) [ ] } T Write Properties
* @ template { ( keyof TEntityInstance ) [ ] } TReadProperties
* @ template { ( keyof TEntityInstance ) [ ] } TReadProperties
* @ typedef { object } Options
* @ typedef { object } Options
* @ prop { T MutationProperties} mutation _properties - an array of property names on ` TEntityInstance ` that when calling a property on ` TEntityInstance ` , if the property name exists in this array , then mentioned property causes reactivity .
* @ prop { T WriteProperties} write _properties - an array of property names on ` TEntityInstance ` that when calling a property on ` TEntityInstance ` , if the property name exists in this array , then mentioned property causes reactivity .
* @ prop { TReadProperties } read _properties - an array of property names on ` TEntityInstance ` that ` mutation_properties ` affects
* @ prop { TReadProperties } read _properties - an array of property names on ` TEntityInstance ` that ` mutation_properties ` affects
* @ prop { Interceptors < TEntityInstance , T Mutation Properties, TReadProperties > } [ interceptors = { } ] - if the property names in ` mutation_properties ` shouldn 't cause reactivity, such calling `set.add(2)` twice or accessing a property shouldn' t be reactive based on some conditions , you can prevent the reactivity by returning ` false ` from these interceptors
* @ prop { Interceptors < TEntityInstance , T Write Properties, TReadProperties > } [ interceptors = { } ] - if the property names in ` mutation_properties ` shouldn 't cause reactivity, such calling `set.add(2)` twice or accessing a property shouldn' t be reactive based on some conditions , you can prevent the reactivity by returning ` false ` from these interceptors
* /
* /
/** @typedef {Map<string | symbol | number, Map<unknown, import("#client").Source<boolean>>>} ReadMethodsSignals */
/** @typedef {Map<string | symbol | number, Map<unknown, import("#client").Source<boolean>>>} ReadMethodsSignals */
/ * *
/ * *
* @ template { new ( ... args : any ) => any } TEntity - the entity we want to make reactive
* @ template { new ( ... args : any ) => any } TEntity - the entity we want to make reactive
* @ template { ( keyof InstanceType < TEntity > ) [ ] } T Mutation Properties
* @ template { ( keyof InstanceType < TEntity > ) [ ] } T Write Properties
* @ template { ( keyof InstanceType < TEntity > ) [ ] } TReadProperties
* @ template { ( keyof InstanceType < TEntity > ) [ ] } TReadProperties
* @ param { TEntity } Entity - the class / function we want to make reactive
* @ param { TEntity } Entity - the class / function we want to make reactive
* @ param { Options < InstanceType < TEntity > , T Mutation Properties, TReadProperties > } options - configurations for how reactivity works for this entity
* @ param { Options < InstanceType < TEntity > , T Write Properties, TReadProperties > } options - configurations for how reactivity works for this entity
* @ returns { TEntity }
* @ returns { TEntity }
* /
* /
export const make _reactive = ( Entity , options ) => {
export const make _reactive = ( Entity , options ) => {
@ -48,17 +48,19 @@ export const make_reactive = (Entity, options) => {
) {
) {
/ * *
/ * *
* @ param { TReadProperties } method _names
* @ param { TReadProperties } method _names
* @ param { unknown } param
* @ param { unknown [] } param s
* /
* /
function notify _read _methods ( method _names , param ) {
function notify _read _methods ( method _names , ... param s ) {
method _names . forEach ( ( name ) => {
method _names . forEach ( ( name ) => {
if ( DEV && ! options . read _properties . includes ( name ) ) {
if ( DEV && ! options . read _properties . includes ( name ) ) {
throw new Error (
throw new Error (
` when trying to notify reactions got a read method that wasn't defined in options: ${ name . toString ( ) } `
` when trying to notify reactions got a read method that wasn't defined in options: ${ name . toString ( ) } `
) ;
) ;
}
}
const sig = get _signal _for _function ( read _methods _signals , name , param ) ;
( params . length == 0 ? [ null ] : params ) . forEach ( ( param ) => {
increment _signal ( version _signal , sig ) ;
const sig = get _signal _for _function ( read _methods _signals , name , param ) ;
increment _signal ( version _signal , sig ) ;
} ) ;
} ) ;
} ) ;
}
}
@ -72,11 +74,11 @@ export const make_reactive = (Entity, options) => {
) {
) {
return ;
return ;
}
}
if ( options . mutation _properties . some ( ( v ) => v === property ) ) {
if ( options . write _properties . some ( ( v ) => v === property ) ) {
increment _signal ( version _signal ) ;
increment _signal ( version _signal ) ;
} else {
} else {
if ( options . read _properties . includes ( property ) ) {
if ( options . read _properties . includes ( property ) ) {
params . forEach ( ( param ) => {
( params . length == 0 ? [ null ] : params ) . forEach ( ( param ) => {
const sig = get _signal _for _function ( read _methods _signals , property , param ) ;
const sig = get _signal _for _function ( read _methods _signals , property , param ) ;
get ( sig ) ;
get ( sig ) ;
} ) ;
} ) ;