@craft-ng/core 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7580,6 +7580,74 @@ function reactiveWritableSignal(initialValue, reactionBuilder) {
7580
7580
  return sig;
7581
7581
  }
7582
7582
 
7583
+ /**
7584
+ * Creates an event emitter with automatic cleanup and signal-based value tracking.
7585
+ *
7586
+ * `source$` provides a lightweight event streaming solution that combines event emission,
7587
+ * automatic subscription cleanup via `DestroyRef`, and signal-based reactive value tracking.
7588
+ *
7589
+ * @template T - The type of values emitted by the source
7590
+ *
7591
+ * @returns {Source$<T>} An object with the following methods and properties:
7592
+ * - `emit(value: T)` - Emits a value to all subscribers and updates the internal signal
7593
+ * - `subscribe(callback: (value: T) => void)` - Subscribes to emissions with automatic cleanup
7594
+ * - `value: Signal<T | undefined>` - A read-only signal containing the last emitted value
7595
+ * - `asReadonly()` - Returns a read-only version (only `subscribe` and `value`)
7596
+ * - `preserveLastValue()` - Returns a variant that immediately emits the last value to new subscribers
7597
+ *
7598
+ * @example
7599
+ * Basic usage with state coordination
7600
+ * ```typescript
7601
+ * import { source$, state, on$ } from '@craft-ng/core';
7602
+ *
7603
+ * @Component({
7604
+ * selector: 'app-counter',
7605
+ * template: `
7606
+ * <p>Count: {{ counter() }}</p>
7607
+ * <button (click)="counter.increment()">+1</button>
7608
+ * <button (click)="reset$.emit()">Reset</button>
7609
+ * `,
7610
+ * })
7611
+ * export class CounterComponent {
7612
+ * reset$ = source$<void>();
7613
+ *
7614
+ * counter = state(0, ({ set, update }) => ({
7615
+ * increment: () => update((v) => v + 1),
7616
+ * reset: on$(this.reset$, () => set(0)),
7617
+ * }));
7618
+ * }
7619
+ * ```
7620
+ *
7621
+ *
7622
+ * @example
7623
+ * Late subscriber with preserved last value
7624
+ * ```typescript
7625
+ * const notifications$ = source$<string>().preserveLastValue();
7626
+ *
7627
+ * notifications$.emit('Server started');
7628
+ * notifications$.emit('Database connected');
7629
+ *
7630
+ * // Late subscriber receives the last value immediately
7631
+ * notifications$.subscribe((msg) => {
7632
+ * console.log(msg); // 'Database connected'
7633
+ * });
7634
+ * ```
7635
+ *
7636
+ * @example
7637
+ * Read-only access pattern
7638
+ * ```typescript
7639
+ * class DataService {
7640
+ * private dataUpdated$ = source$<Data>();
7641
+ * readonly dataUpdated = this.dataUpdated$.asReadonly();
7642
+ *
7643
+ * updateData(data: Data) {
7644
+ * this.dataUpdated$.emit(data);
7645
+ * }
7646
+ * }
7647
+ * ```
7648
+ *
7649
+ * @see {@link https://ng-craft.dev/utils/source$ | source$ documentation}
7650
+ */
7583
7651
  function source$() {
7584
7652
  const sourceRef$ = new EventEmitter();
7585
7653
  const destroyRef = inject(DestroyRef);
@@ -7623,9 +7691,42 @@ function source$() {
7623
7691
  };
7624
7692
  }
7625
7693
 
7694
+ function fromEventToSource$(target, eventName, options) {
7695
+ assertInInjectionContext(fromEventToSource$);
7696
+ const eventSource$ = source$();
7697
+ const listener = (event) => {
7698
+ if (options?.computedValue) {
7699
+ const computed = options.computedValue(event);
7700
+ eventSource$.emit(computed);
7701
+ return;
7702
+ }
7703
+ eventSource$.emit(event);
7704
+ };
7705
+ target.addEventListener(eventName, listener, options?.event);
7706
+ const destroyRef = inject(DestroyRef);
7707
+ const dispose = () => {
7708
+ target.removeEventListener(eventName, listener, options?.event);
7709
+ };
7710
+ destroyRef.onDestroy(() => {
7711
+ dispose();
7712
+ });
7713
+ return Object.assign(eventSource$.asReadonly(), {
7714
+ dispose,
7715
+ });
7716
+ }
7717
+
7718
+ function on$(_source, callback) {
7719
+ const sub = _source.subscribe((value) => {
7720
+ callback(value);
7721
+ });
7722
+ const destroyRef = inject(DestroyRef);
7723
+ destroyRef.onDestroy(() => sub.unsubscribe());
7724
+ return SourceBranded;
7725
+ }
7726
+
7626
7727
  /**
7627
7728
  * Generated bundle index. Do not edit.
7628
7729
  */
7629
7730
 
7630
- export { EXTERNALLY_PROVIDED, EmptyContext, GlobalPersisterHandlerService, STORE_CONFIG_TOKEN, SourceBrand, SourceBranded, addMany, addOne, afterRecomputation, asyncProcess, capitalize, computedIds, computedSource, computedTotal, contract, craft, craftAsyncProcesses, craftComputedStates, craftFactoryEntries, craftInject, craftInputs, craftMutations, craftQuery, craftQueryParam, craftQueryParams, craftSetAllQueriesParamsStandalone, craftSources, craftState, createMethodHandlers, insertLocalStoragePersister, insertPaginationPlaceholderData, insertReactOnMutation, isSource, linkedSource, localStoragePersister, map, mapOne, mutation, partialContext, query, queryParam, reactiveWritableSignal, removeAll, removeMany, removeOne, resourceById, serializeQueryParams, serializedQueryParamsObjectToString, setAll, setMany, setOne, signalSource, source$, sourceFromEvent, stackedSource, state, toSource, toggleMany, toggleOne, updateMany, updateOne, upsertMany, upsertOne };
7731
+ export { EXTERNALLY_PROVIDED, EmptyContext, GlobalPersisterHandlerService, STORE_CONFIG_TOKEN, SourceBrand, SourceBranded, addMany, addOne, afterRecomputation, asyncProcess, capitalize, computedIds, computedSource, computedTotal, contract, craft, craftAsyncProcesses, craftComputedStates, craftFactoryEntries, craftInject, craftInputs, craftMutations, craftQuery, craftQueryParam, craftQueryParams, craftSetAllQueriesParamsStandalone, craftSources, craftState, createMethodHandlers, fromEventToSource$, insertLocalStoragePersister, insertPaginationPlaceholderData, insertReactOnMutation, isSource, linkedSource, localStoragePersister, map, mapOne, mutation, on$, partialContext, query, queryParam, reactiveWritableSignal, removeAll, removeMany, removeOne, resourceById, serializeQueryParams, serializedQueryParamsObjectToString, setAll, setMany, setOne, signalSource, source$, sourceFromEvent, stackedSource, state, toSource, toggleMany, toggleOne, updateMany, updateOne, upsertMany, upsertOne };
7631
7732
  //# sourceMappingURL=craft-ng-core.mjs.map