{"version":3,"file":"ngxtension-computed-previous.mjs","sources":["../../../../libs/ngxtension/computed-previous/src/computed-previous.ts","../../../../libs/ngxtension/computed-previous/src/ngxtension-computed-previous.ts"],"sourcesContent":["import { computed, CreateComputedOptions, type Signal } from '@angular/core';\n\nenum Kind {\n\tINITIAL,\n\tCOMPUTED,\n}\n\ninterface InitialState {\n\tkind: Kind.INITIAL;\n\tpreviousValue: null;\n\tcurrentValue: null;\n}\n\ninterface ComputedState<T> {\n\tkind: Kind.COMPUTED;\n\tpreviousValue: T;\n\tcurrentValue: T;\n}\n\ntype State<T> = InitialState | ComputedState<T>;\n\n/**\n * Creates a computed signal that tracks the previous value produced by the given computation.\n *\n * The first time the computed signal is evaluated, the returned signal emits the same value\n * as the current computation result. On subsequent evaluations, it emits the value from the prior update.\n *\n * @example\n * ```ts\n * const value = signal(0);\n * const previous = computedPrevious(value);\n *\n * effect(() => {\n *   console.log('Current value:', value());\n *   console.log('Previous value:', previous());\n * });\n *\n * // Logs initially:\n * // Current value: 0\n * // Previous value: 0\n *\n * value.set(1);\n *\n * // Logs:\n * // Current value: 1\n * // Previous value: 0\n *\n * value.set(2);\n *\n * // Logs:\n * // Current value: 2\n * // Previous value: 1\n * ```\n *\n * @param computation A function that returns the current value. Typically, this is a signal accessor.\n * @param options Optional computed signal configuration.\n * @returns A signal that emits the previous value returned by the computation.\n */\nexport function computedPrevious<T>(\n\tcomputation: () => T,\n\toptions?: CreateComputedOptions<T>,\n): Signal<T> {\n\tlet state: State<T> = {\n\t\tkind: Kind.INITIAL,\n\t\tpreviousValue: null,\n\t\tcurrentValue: null,\n\t};\n\n\treturn computed(() => {\n\t\tconst currentValue = computation();\n\n\t\tif (state.kind === Kind.INITIAL) {\n\t\t\tstate = {\n\t\t\t\tkind: Kind.COMPUTED,\n\t\t\t\tpreviousValue: currentValue,\n\t\t\t\tcurrentValue,\n\t\t\t};\n\t\t}\n\n\t\tstate.previousValue = state.currentValue;\n\t\tstate.currentValue = currentValue;\n\n\t\treturn state.previousValue;\n\t}, options);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAEA,IAAK,IAGJ,CAAA;AAHD,CAAA,UAAK,IAAI,EAAA;AACR,IAAA,IAAA,CAAA,IAAA,CAAA,SAAA,CAAA,GAAA,CAAA,CAAA,GAAA,SAAO,CAAA;AACP,IAAA,IAAA,CAAA,IAAA,CAAA,UAAA,CAAA,GAAA,CAAA,CAAA,GAAA,UAAQ,CAAA;AACT,CAAC,EAHI,IAAI,KAAJ,IAAI,GAGR,EAAA,CAAA,CAAA,CAAA;AAgBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCG;AACa,SAAA,gBAAgB,CAC/B,WAAoB,EACpB,OAAkC,EAAA;AAElC,IAAA,IAAI,KAAK,GAAa;QACrB,IAAI,EAAE,IAAI,CAAC,OAAO;AAClB,QAAA,aAAa,EAAE,IAAI;AACnB,QAAA,YAAY,EAAE,IAAI;KAClB,CAAC;IAEF,OAAO,QAAQ,CAAC,MAAK;AACpB,QAAA,MAAM,YAAY,GAAG,WAAW,EAAE,CAAC;QAEnC,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE;AAChC,YAAA,KAAK,GAAG;gBACP,IAAI,EAAE,IAAI,CAAC,QAAQ;AACnB,gBAAA,aAAa,EAAE,YAAY;gBAC3B,YAAY;aACZ,CAAC;SACF;AAED,QAAA,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC,YAAY,CAAC;AACzC,QAAA,KAAK,CAAC,YAAY,GAAG,YAAY,CAAC;QAElC,OAAO,KAAK,CAAC,aAAa,CAAC;KAC3B,EAAE,OAAO,CAAC,CAAC;AACb;;ACpFA;;AAEG;;;;"}