Skip to content

Commit 1457398

Browse files
committed
improved comments
1 parent e93918c commit 1457398

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

packages/svelte/src/reactivity/utils.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ export const NOTIFY_WITH_ALL_PARAMS = Symbol();
88
* @template TEntityInstance
99
* @template {(keyof TEntityInstance)[]} TWriteProperties
1010
* @template {(keyof TEntityInstance)[]} TReadProperties
11-
* @typedef {Partial<Record<TWriteProperties[number], (notify_read_methods: (methods: TReadProperties, ...params: unknown[])=>void ,value: TEntityInstance, property: TWriteProperties[number], ...params: unknown[])=>boolean>>} Interceptors - return false if you want to prevent reactivity for this call, DO NOT USE INTERCEPTORS FOR READ METHODS
11+
* @typedef {Partial<Record<TWriteProperties[number], (notify_read_methods: (methods: TReadProperties, ...params: unknown[])=>void ,value: TEntityInstance, property: TWriteProperties[number], ...params: unknown[])=>boolean>>} Interceptors - return false if you want to prevent reactivity for this call, DO NOT USE INTERCEPTORS FOR READ PROPERTIES
1212
*/
1313

1414
/**
1515
* @template TEntityInstance
1616
* @template {(keyof TEntityInstance)[]} TWriteProperties
1717
* @template {(keyof TEntityInstance)[]} TReadProperties
1818
* @typedef {object} Options
19-
* @prop {TWriteProperties} 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.
20-
* @prop {TReadProperties} read_properties - an array of property names on `TEntityInstance` that `mutation_properties` affects
21-
* @prop {Interceptors<TEntityInstance, TWriteProperties, 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
19+
* @prop {TWriteProperties} write_properties - an array of property names on `TEntityInstance`, could cause reactivity.
20+
* @prop {TReadProperties} read_properties - an array of property names on `TEntityInstance` that `write_properties` affect. typically used for methods. for instance `size` doesn't need to be here because it takes no parameters and is reactive based on the `version` signal.
21+
* @prop {Interceptors<TEntityInstance, TWriteProperties, TReadProperties>} [interceptors={}] - if the property names in `write_properties` shouldn't cause reactivity, such as 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
2222
*/
2323

2424
/** @typedef {Map<string | symbol | number, Map<unknown, import("#client").Source<boolean>>>} ReadMethodsSignals */
2525

2626
/**
27-
* @template {new (...args: any) => any} TEntity - the entity we want to make reactive
27+
* @template {new (...args: any) => any} TEntity
2828
* @template {(keyof InstanceType<TEntity>)[]} TWriteProperties
2929
* @template {(keyof InstanceType<TEntity>)[]} TReadProperties
30-
* @param {TEntity} Entity - the class/function we want to make reactive
30+
* @param {TEntity} Entity - the entity we want to make reactive
3131
* @param {Options<InstanceType<TEntity>, TWriteProperties, TReadProperties>} options - configurations for how reactivity works for this entity
3232
* @returns {TEntity}
3333
*/
@@ -40,11 +40,9 @@ export const make_reactive = (Entity, options) => {
4040
*/
4141
constructor(...params) {
4242
/**
43-
* each read method can be tracked like has, size, get and etc. these props might depend on a parameter. they have to reactive based on the
44-
* parameter they depend on, if it requires a change. for instance if you call `set.has(2)` then call `set.add(5)` the former shouldn't get notified.
45-
* so what is do we need to store if this needs to be generic? function name + parameter. unfortunately for each parameter we need a new signal, why?
46-
* because arrays don't equal themselves even if they have the same value (referential equality).
47-
* fortunately current builtins that we try to make reactive only take 1 parameter for their read methods so this is fine.
43+
* each read method can be tracked like has, get, has and etc. these props might depend on a parameter. they have to reactive based on the
44+
* parameter they depend on. for instance if you have `set.has(2)` and then call `set.add(5)` the former shouldn't get notified.
45+
* based on that we need to store the function_name + parameter(s).
4846
* @type {Map<string | symbol, Map<unknown[], import("#client").Source<boolean>>>}
4947
**/
5048
const read_methods_signals = new Map();
@@ -58,7 +56,7 @@ export const make_reactive = (Entity, options) => {
5856
let result;
5957

6058
if (typeof orig_property === 'function') {
61-
// Bind functions directly to the `TEntity`
59+
// bind functions directly to the `TEntity`
6260
result = ((/** @type {unknown[]} */ ...params) => {
6361
notify_if_required(
6462
version_signal,
@@ -71,7 +69,7 @@ export const make_reactive = (Entity, options) => {
7169
return orig_property.bind(target)(...params);
7270
}).bind(target);
7371
} else {
74-
// Properly handle getters
72+
// handle getters/props
7573
notify_if_required(version_signal, read_methods_signals, property, target, options);
7674
result = Reflect.get(target, property, target);
7775
}
@@ -127,7 +125,7 @@ function notify_if_required(
127125
} else {
128126
if (options.read_properties.includes(property)) {
129127
(params.length == 0 ? [null] : params).forEach((param) => {
130-
// read like methods should create the signal so what on any change to them they get notified
128+
// read like methods should create the signal (if not already created) so they can be reactive when notified based on their param
131129
const sig = get_signal_for_function(read_methods_signals, property, param, true);
132130
get(sig);
133131
});
@@ -146,7 +144,7 @@ function notify_if_required(
146144
* @param {ReadMethodsSignals} read_methods_signals
147145
* @param {Options<InstanceType<TEntity>, TWriteProperties, TReadProperties>} options
148146
* @param {TReadProperties} method_names
149-
* @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
147+
* @param {unknown[]} params - if you want to notify for all parameters pass the `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
150148
*/
151149
function notify_read_methods(
152150
options,
@@ -175,7 +173,7 @@ function notify_read_methods(
175173
}
176174

177175
/**
178-
* gets the signal for this function based on params. If the signal doesn't exist and create_signal_if_doesnt_exist is not set to true, it creates a new one and returns that
176+
* gets the signal for this function based on params. If the signal doesn't exist and `create_signal_if_doesnt_exist` is not set to true, it creates a new one and returns that
179177
* @template {boolean} [TCreateSignalIfDoesntExist=false]
180178
* @param {ReadMethodsSignals} signals_map
181179
* @param {string | symbol | number} function_name
@@ -187,7 +185,7 @@ const get_signal_for_function = (
187185
signals_map,
188186
function_name,
189187
param,
190-
// @ts-ignore: this should be supported in jsdoc based on https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template but doesn't?
188+
// @ts-ignore: this should be supported in jsdoc based on https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template but isn't?
191189
create_signal_if_doesnt_exist = false
192190
) => {
193191
/**

0 commit comments

Comments
 (0)