added the ability to notify with all params

pull/11774/head
godzylinux 2 years ago
parent 1bae9b98f0
commit dede52d0a4

@ -1,32 +1,32 @@
import { make_reactive } from './utils.js';
import { make_reactive, NOTIFY_WITH_ALL_PARAMS } from './utils.js';
export const ReactiveMap = make_reactive(Map, {
write_properties: ['clear', 'delete', 'set'],
read_properties: ['get', 'keys', 'size', 'entries', 'values'],
read_properties: ['get', 'keys', 'size', 'entries', 'values', 'has'],
interceptors: {
set: (notify_read_methods, value, property, ...params) => {
if (value.get(params[0]) === params[1] && params[1] !== undefined) {
if (value.get(params[0]) === params[1]) {
return false;
}
if (!value.has(params[0])) {
notify_read_methods(['keys', 'size']);
}
notify_read_methods(['entries', 'values']);
notify_read_methods(['get'], params[1]);
notify_read_methods(['get', 'has'], params[1]);
return true;
},
clear: (notify_read_methods, value, property, ...params) => {
if (value.size === 0) {
return false;
}
notify_read_methods(['keys', 'size', 'values', 'entries']);
notify_read_methods(['keys', 'size', 'values', 'entries', 'has'], NOTIFY_WITH_ALL_PARAMS);
return true;
},
delete: (notify_read_methods, value, property, ...params) => {
if (!value.has(params[0])) {
return false;
}
notify_read_methods(['get'], value.get(params[0]));
notify_read_methods(['get', 'has'], params[0]);
notify_read_methods(['keys', 'size', 'values', 'entries']);
return true;
}

@ -1,4 +1,4 @@
import { make_reactive } from './utils.js';
import { make_reactive, NOTIFY_WITH_ALL_PARAMS } from './utils.js';
export const ReactiveSet = make_reactive(Set, {
write_properties: ['add', 'clear', 'delete'],
@ -16,7 +16,7 @@ export const ReactiveSet = make_reactive(Set, {
if (value.size == 0) {
return false;
}
notify_read_methods(['has'], params[0]);
notify_read_methods(['has'], NOTIFY_WITH_ALL_PARAMS);
notify_read_methods(['size']);
return true;
},

@ -2,6 +2,8 @@ import { DEV } from 'esm-env';
import { source, set } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
export const NOTIFY_WITH_ALL_PARAMS = `${crypto?.randomUUID?.() ?? Date.now().toString() + Date.now().toString()}`;
/**
* @template TEntityInstance
* @template {(keyof TEntityInstance)[]} TWriteProperties
@ -143,7 +145,7 @@ function notify_if_required(
* @param {ReadMethodsSignals} read_methods_signals
* @param {Options<InstanceType<TEntity>, TWriteProperties, TReadProperties>} options
* @param {TReadProperties} method_names
* @param {unknown[]} params
* @param {unknown[]} params - if you want to notify for all parameters pass NOTIFY_WITH_ALL_PARAMS constant, for instance some methods like `clear` should notify all `something.get(x)` methods; on these cases set this flag to true
*/
function notify_read_methods(
options,
@ -158,10 +160,16 @@ function notify_read_methods(
`when trying to notify reactions got a read method that wasn't defined in options: ${name.toString()}`
);
}
(params.length == 0 ? [null] : params).forEach((param) => {
const sig = get_signal_for_function(read_methods_signals, name, param);
increment_signal(version_signal, sig);
});
if (params.length == 1 && params[0] == NOTIFY_WITH_ALL_PARAMS) {
read_methods_signals.get(name)?.forEach((sig) => {
increment_signal(version_signal, sig);
});
} else {
(params.length == 0 ? [null] : params).forEach((param) => {
const sig = get_signal_for_function(read_methods_signals, name, param);
increment_signal(version_signal, sig);
});
}
});
}

Loading…
Cancel
Save