fluidstate
Version:
Library for fine-grained reactivity state management
154 lines (144 loc) • 5.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.reactiveValueEquals = exports.getDefaultReactiveValueOptions = exports.getDefaultReactionOptions = exports.getDefaultComputedOptions = exports.defaultReactiveValueOptions = exports.defaultReactionScheduler = exports.defaultReactionOptions = exports.defaultEquals = exports.defaultComputedOptions = exports.configureDefaultReactiveValueOptions = exports.configureDefaultReactionOptions = exports.configureDefaultComputedOptions = exports.CHANGED = void 0;
/**
* A special symbol used to indicate that a reactive value has changed,
* bypassing the standard equality check. This is useful in scenarios where
* the content of the reactive values is not needed, but a change notification
* is still desired. Assigning `CHANGED` as the content of the reactive value
* will always result in change propagation. This, however, only applies
* to the default equality check. If the equality check is overwritten,
* `CHANGED` may need to be accounted for in the custom equality check
*/
const CHANGED = Symbol();
exports.CHANGED = CHANGED;
const reactiveValueEquals = (oldValue, newValue) => {
if (defaultReactiveValueOptions.equals) {
return defaultReactiveValueOptions.equals(oldValue, newValue);
}
return defaultEquals(oldValue, newValue);
};
exports.reactiveValueEquals = reactiveValueEquals;
const defaultEquals = (oldValue, newValue) => {
return newValue !== CHANGED && Object.is(oldValue, newValue);
};
// Reactive Value Options
exports.defaultEquals = defaultEquals;
const defaultReactiveValueOptions = {
equals: defaultEquals
};
/**
* Retrieves a copy of the default options used for reactive values.
* This ensures that modifications to the returned options object do not affect
* the global default settings.
*
* @returns A new object containing the current default `ReactiveValueOptions`.
*/
exports.defaultReactiveValueOptions = defaultReactiveValueOptions;
const getDefaultReactiveValueOptions = () => {
return {
...defaultReactiveValueOptions
};
};
/**
* Configures the global default options for reactive values.
* These defaults will be used by newly created reactive values unless overridden
* by options provided at creation time.
*
* @param newOptions - An object containing partial `ReactiveValueOptions` to update.
* Only the specified properties will be updated; unspecified properties will retain
* their current default values. If a property is set to `undefined`, it will revert
* to its original default (e.g., `defaultEquals` for `equals`).
*/
exports.getDefaultReactiveValueOptions = getDefaultReactiveValueOptions;
const configureDefaultReactiveValueOptions = newOptions => {
for (const p in newOptions) {
switch (p) {
case "equals":
defaultReactiveValueOptions[p] = newOptions[p] ?? defaultEquals;
break;
}
}
};
// Computed Options
exports.configureDefaultReactiveValueOptions = configureDefaultReactiveValueOptions;
const defaultComputedOptions = {
equals: defaultEquals
};
/**
* Retrieves a copy of the default options used for computed atoms.
* This ensures that modifications to the returned options object do not affect
* the global default settings.
*
* @returns A new object containing the current default `ComputedOptions`.
*/
exports.defaultComputedOptions = defaultComputedOptions;
const getDefaultComputedOptions = () => {
return {
...defaultComputedOptions
};
};
/**
* Configures the global default options for computed atoms.
* These defaults will be used by newly created computed atoms unless overridden
* by options provided at creation time.
*
* @param newOptions - An object containing partial `ComputedOptions` to update.
* Only the specified properties will be updated; unspecified properties will retain
* their current default values. If a property is set to `undefined`, it will revert
* to its original default (e.g., `defaultEquals` for `equals`).
*/
exports.getDefaultComputedOptions = getDefaultComputedOptions;
const configureDefaultComputedOptions = newOptions => {
for (const p in newOptions) {
switch (p) {
case "equals":
defaultComputedOptions[p] = newOptions[p] ?? defaultEquals;
break;
}
}
};
// Reaction Options
exports.configureDefaultComputedOptions = configureDefaultComputedOptions;
const defaultReactionScheduler = fn => fn();
exports.defaultReactionScheduler = defaultReactionScheduler;
const defaultReactionOptions = {
scheduler: defaultReactionScheduler
};
/**
* Retrieves a copy of the default options used for reactions.
* This ensures that modifications to the returned options object do not affect
* the global default settings.
*
* @returns A new object containing the current default `ReactionOptions`.
*/
exports.defaultReactionOptions = defaultReactionOptions;
const getDefaultReactionOptions = () => {
return {
...defaultReactionOptions
};
};
/**
* Configures the global default options for reactions.
* These defaults will be used by newly created reactions unless overridden
* by options provided at creation time.
*
* @param newOptions - An object containing partial `ReactionOptions` to update.
* Only the specified properties will be updated; unspecified properties will retain
* their current default values. If a property is set to `undefined`, it will revert
* to its original default (e.g., `defaultReactionScheduler` for `scheduler`).
*/
exports.getDefaultReactionOptions = getDefaultReactionOptions;
const configureDefaultReactionOptions = newOptions => {
for (const p in newOptions) {
switch (p) {
case "scheduler":
defaultReactionOptions[p] = newOptions[p] ?? defaultReactionScheduler;
break;
}
}
};
exports.configureDefaultReactionOptions = configureDefaultReactionOptions;
//# sourceMappingURL=reactive-options.js.map