notifying other signals of changes, AFTER the change has actually took effect

pull/11504/head
godzylinux 2 years ago committed by FoHoOV
parent 5950d47b36
commit 9715aa8509

@ -58,7 +58,7 @@ export const make_reactive = (Entity, options) => {
if (typeof orig_property === 'function') { if (typeof orig_property === 'function') {
// bind functions directly to the `TEntity` // bind functions directly to the `TEntity`
result = ((/** @type {unknown[]} */ ...params) => { result = ((/** @type {unknown[]} */ ...params) => {
notify_if_required( const notifiers = create_notifiers(
version_signal, version_signal,
read_methods_signals, read_methods_signals,
property, property,
@ -66,12 +66,21 @@ export const make_reactive = (Entity, options) => {
options, options,
...params ...params
); );
return orig_property.bind(target)(...params); const result = orig_property.bind(target)(...params);
notifiers.forEach((notifier) => notifier());
return result;
}).bind(target); }).bind(target);
} else { } else {
// handle getters/props // handle getters/props
notify_if_required(version_signal, read_methods_signals, property, target, options); const notifiers = create_notifiers(
version_signal,
read_methods_signals,
property,
target,
options
);
result = Reflect.get(target, property, target); result = Reflect.get(target, property, target);
notifiers.forEach((notifier) => notifier());
} }
return result; return result;
@ -82,6 +91,7 @@ export const make_reactive = (Entity, options) => {
}; };
/** /**
* creates an array of functions that notify other signals based on the changes, you need to run these functions to invoke reactivity
* @template {new (...args: any) => any} TEntity * @template {new (...args: any) => any} TEntity
* @template {(keyof TEntityInstance)[]} TWriteProperties * @template {(keyof TEntityInstance)[]} TWriteProperties
* @template {(keyof TEntityInstance)[]} TReadProperties * @template {(keyof TEntityInstance)[]} TReadProperties
@ -93,8 +103,9 @@ export const make_reactive = (Entity, options) => {
* @param {TEntityInstance} entity_instance * @param {TEntityInstance} entity_instance
* @param {Options<InstanceType<TEntity>, TWriteProperties, TReadProperties>} options * @param {Options<InstanceType<TEntity>, TWriteProperties, TReadProperties>} options
* @param {unknown[]} params * @param {unknown[]} params
* @returns {Function[]}
*/ */
function notify_if_required( function create_notifiers(
version_signal, version_signal,
read_methods_signals, read_methods_signals,
property, property,
@ -102,24 +113,26 @@ function notify_if_required(
options, options,
...params ...params
) { ) {
/**
* @type {Function[]}
*/
const notifiers = [];
if ( if (
options.interceptors?.[property]?.( options.interceptors?.[property]?.(
(methods, ...params) => { (methods, ...params) => {
return notify_read_methods( notifiers.push(() => {
options, notify_read_methods(options, version_signal, read_methods_signals, methods, ...params);
version_signal, });
read_methods_signals,
methods,
...params
);
}, },
entity_instance, entity_instance,
property, property,
...params ...params
) === false ) === false
) { ) {
return; return notifiers;
} }
notifiers.push(() => {
if (options.write_properties.some((v) => v === property)) { if (options.write_properties.some((v) => v === property)) {
increment_signal(version_signal); increment_signal(version_signal);
} else { } else {
@ -133,6 +146,9 @@ function notify_if_required(
get(version_signal); get(version_signal);
} }
} }
});
return notifiers;
} }
/** /**

Loading…
Cancel
Save