// @ts-check import { WalkThrough } from './WalkThrough.mjs'; import { LazyFactory } from '../function/LazyFactory.mjs'; import { TryAsync } from '../function/TryAsync.mjs'; import { TrySync } from '../function/TrySync.mjs'; import { Console } from './Console.mjs'; import { Effect, mapOfEffects } from './Effect.mjs'; import { DataLog } from './DataLog.mjs'; /** * @typedef { import('../typehints/VivthCleanup.mjs').VivthCleanup } VivthCleanup */ /** * @type {Set>} */ export const setOFSignals = new Set(); /** * @description * - a class for creating effect to signals; * @template VALUE * @implements {VivthCleanup} */ export class Signal { vivthCleanup = async () => { this.remove.ref(); }; /** * @description * - create a `Signal`; * @param {VALUE} value * @param {(data:DataLog)=>void} [performanceChangesReport] * - callback independent from effect; * >- it will always be called when there's value change; * @example * import { Signal, Effect } from 'vivth/neutral'; * * const count = new Signal(0); */ constructor(value, performanceChangesReport = undefined) { this.#value = value; this.#performanceChangesReport = performanceChangesReport; setOFSignals.add(this); } /** * @type {ConstructorParameters>[1]} */ #performanceChangesReport; /** * @description * - subsrcibers reference of this instance; */ subscribers = LazyFactory(() => ({ /** * @instance subscribers * @description * - subscribedEffects * @type {Set} */ setOf: new Set(), /** * @instance subscribers * @description * - manually notify on non primitive value or value that have depths; * @param {(options:{signalInstance:Signal})=>Promise} [callback] * @param {(error:Error|undefined)=>Promise} [afterCompletion] * @returns {Promise} * @example * import { Signal } from 'vivth/neutral'; * * // for deep signal like array or object you can: * const arraySignal = new Signal([1,2]); * arraySignal.value.push(3); * arraySignal.subscribers.notify(); * * // OR for more complex mutation: * const objectSignal = new Signal({a:'test', b:'test'}); * objectSignal.subscribers.notify(async ({ signalInstance }) => { * signalInstance.value['c'] = 'testc'; * signalInstance.value['d'] = 'testd'; * }); */ notify: async (callback = undefined, afterCompletion = undefined) => { if (this.#performanceChangesReport) { this.#performanceChangesReport(new DataLog(this.#value)); } if (callback === undefined) { await Signal.#notify(this, afterCompletion); return; } const [, errorRunningCallback] = await TryAsync(async () => { await callback({ signalInstance: this }); }); if (errorRunningCallback) { Console.error({ errorRunningCallback }); return; } await Signal.#notify(this, afterCompletion); }, })); /** * @param {Signal} signalInstance * @param {(error:Error|undefined)=>void} [afterCompletion] * @returns {Promise} */ static #notify = async ( signalInstance, afterCompletion = async (error) => { if (error === undefined) { return; } Console.error(error); }, ) => { const [, errorRunningWalkThrough] = TrySync(() => { WalkThrough.set(signalInstance.subscribers.setOf, (effectInstance) => { if (!mapOfEffects.has(effectInstance)) { effectInstance.options.removeSignal(signalInstance); return; } /** * effect.run is already wrapped with TryAsync */ effectInstance.run(); }); }); afterCompletion(errorRunningWalkThrough); }; /** * @description * - collection of remove methods */ remove = LazyFactory(() => ({ /** * @instance remove * @description * - remove effect subscriber to react from this instance value changes; * @param {Effect} effectInstance * @returns {void} */ subscriber: (effectInstance) => { /** * this part is not needed, as the effect might need to react to other signals // effectInstance.options.removeEffect(); */ effectInstance.options.removeSignal(this); }, /** * @instance remove * @description * - remove all effect subscribers to react from this instance value changes; * @type {()=>void} */ allSubscribers: () => { WalkThrough.set(this.subscribers.setOf, (effectInstance) => { this.remove.subscriber(effectInstance); }); }, /** * @instance remove * @description * - remove this instance from `vivth` reactivity engine, and nullify it's own value; * @type {()=>void} */ ref: () => { this.remove.allSubscribers(); // @ts-expect-error this.#value = null; setOFSignals.delete(this); }, })); /** * @type {VALUE|undefined} */ #prev; /** * @description * - value before change; * @returns {VALUE|undefined} */ get prev() { return this.#prev; } /** * @type {VALUE} */ #value; /** * @description * - value after change; * @returns {VALUE} * @example * import { Signal, Effect, Derived } from 'vivth/neutral'; * * const count = new Signal(0); * count.value; // not reactive * * new Effect(async ({ subscribe }) =>{ * const countValue = subscribe(count).value; // reactive * }) * const oneMoreThanCount = new Derived(async function({ subscribe }){ * return subscribe(count).value + 1; // reactive * }) */ get value() { return this.#value; } /** * @description * - assign new value then automatically notify all subscribers; * @type {VALUE} * @example * import { Signal } from 'vivth/neutral'; * * const count = new Signal(0); * count.value++; * // OR * count.value = 9; */ set value(newValue) { if (this.#value === newValue) { return; } this.#prev = this.#value; this.#value = newValue; this.subscribers.notify(); } }