@signaltree/ng-forms 4.2.1 → 5.0.0

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.
@@ -0,0 +1,70 @@
1
+ import { getChanges } from '../get-changes.js';
2
+
3
+ function createAuditTracker(tree, auditLog, config = {}) {
4
+ const {
5
+ getMetadata,
6
+ includePreviousValues = false,
7
+ filter,
8
+ maxEntries = 0
9
+ } = config;
10
+ let previousState = structuredClone(tree());
11
+ let isTracking = true;
12
+ const handleChange = () => {
13
+ if (!isTracking) return;
14
+ const currentState = tree();
15
+ const changes = getChanges(previousState, currentState);
16
+ if (Object.keys(changes).length > 0) {
17
+ if (filter && !filter(changes)) {
18
+ previousState = structuredClone(currentState);
19
+ return;
20
+ }
21
+ const entry = {
22
+ timestamp: Date.now(),
23
+ changes,
24
+ metadata: getMetadata?.()
25
+ };
26
+ if (includePreviousValues) {
27
+ const prevValues = {};
28
+ for (const key of Object.keys(changes)) {
29
+ prevValues[key] = previousState[key];
30
+ }
31
+ entry.previousValues = prevValues;
32
+ }
33
+ auditLog.push(entry);
34
+ if (maxEntries > 0 && auditLog.length > maxEntries) {
35
+ auditLog.splice(0, auditLog.length - maxEntries);
36
+ }
37
+ }
38
+ previousState = structuredClone(currentState);
39
+ };
40
+ let unsubscribe;
41
+ let pollingId;
42
+ try {
43
+ unsubscribe = tree.subscribe(handleChange);
44
+ } catch {
45
+ pollingId = setInterval(handleChange, 100);
46
+ }
47
+ return () => {
48
+ isTracking = false;
49
+ if (unsubscribe) {
50
+ unsubscribe();
51
+ }
52
+ if (pollingId) {
53
+ clearInterval(pollingId);
54
+ }
55
+ };
56
+ }
57
+ function createAuditCallback(auditLog, getMetadata) {
58
+ return (previousState, currentState) => {
59
+ const changes = getChanges(previousState, currentState);
60
+ if (Object.keys(changes).length > 0) {
61
+ auditLog.push({
62
+ timestamp: Date.now(),
63
+ changes,
64
+ metadata: getMetadata?.()
65
+ });
66
+ }
67
+ };
68
+ }
69
+
70
+ export { createAuditCallback, createAuditTracker };
@@ -0,0 +1 @@
1
+ export { createAuditCallback, createAuditTracker } from './audit.js';
@@ -0,0 +1,11 @@
1
+ function getChanges(oldState, newState) {
2
+ const changes = {};
3
+ for (const key in newState) {
4
+ if (oldState[key] !== newState[key]) {
5
+ changes[key] = newState[key];
6
+ }
7
+ }
8
+ return changes;
9
+ }
10
+
11
+ export { getChanges };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signaltree/ng-forms",
3
- "version": "4.2.1",
3
+ "version": "5.0.0",
4
4
  "description": "Complete Angular forms integration for SignalTree. FormTree creation, custom directives, validators, form state tracking, and RxJS bridge.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -12,12 +12,17 @@
12
12
  "types": "./src/index.d.ts",
13
13
  "import": "./dist/index.js",
14
14
  "default": "./dist/index.js"
15
+ },
16
+ "./audit": {
17
+ "types": "./src/audit/index.d.ts",
18
+ "import": "./dist/audit/index.js",
19
+ "default": "./dist/audit/index.js"
15
20
  }
16
21
  },
17
22
  "peerDependencies": {
18
23
  "@angular/core": "^20.3.0",
19
24
  "@angular/forms": "^20.3.0",
20
- "@signaltree/core": "^4.2.1",
25
+ "@signaltree/core": "^5.0.0",
21
26
  "rxjs": "^7.0.0"
22
27
  },
23
28
  "devDependencies": {
@@ -1,11 +1,21 @@
1
- import type { Middleware } from '@signaltree/core';
1
+ import type { SignalTree } from '@signaltree/core';
2
2
  export interface AuditEntry<T = unknown> {
3
3
  timestamp: number;
4
4
  changes: Partial<T>;
5
- metadata?: {
6
- userId?: string;
7
- source?: string;
8
- description?: string;
9
- };
5
+ previousValues?: Partial<T>;
6
+ metadata?: AuditMetadata;
10
7
  }
11
- export declare function createAuditMiddleware<T>(auditLog: AuditEntry<T>[], getMetadata?: () => AuditEntry<T>['metadata']): Middleware<T>;
8
+ export interface AuditMetadata {
9
+ userId?: string;
10
+ source?: string;
11
+ description?: string;
12
+ [key: string]: unknown;
13
+ }
14
+ export interface AuditTrackerConfig<T> {
15
+ getMetadata?: () => AuditMetadata;
16
+ includePreviousValues?: boolean;
17
+ filter?: (changes: Partial<T>) => boolean;
18
+ maxEntries?: number;
19
+ }
20
+ export declare function createAuditTracker<T extends Record<string, unknown>>(tree: SignalTree<T>, auditLog: AuditEntry<T>[], config?: AuditTrackerConfig<T>): () => void;
21
+ export declare function createAuditCallback<T extends Record<string, unknown>>(auditLog: AuditEntry<T>[], getMetadata?: () => AuditMetadata): (previousState: T, currentState: T) => void;
@@ -1 +1 @@
1
- export { createAuditMiddleware, type AuditEntry } from './audit';
1
+ export { createAuditTracker, createAuditCallback, type AuditEntry, type AuditMetadata, type AuditTrackerConfig, } from './audit.js';
package/dist/constants.js DELETED
@@ -1,6 +0,0 @@
1
- const SHARED_DEFAULTS = Object.freeze({
2
- PATH_CACHE_SIZE: 1000
3
- });
4
- const DEFAULT_PATH_CACHE_SIZE = SHARED_DEFAULTS.PATH_CACHE_SIZE;
5
-
6
- export { DEFAULT_PATH_CACHE_SIZE, SHARED_DEFAULTS };
@@ -1,24 +0,0 @@
1
- import { isObservable, firstValueFrom } from 'rxjs';
2
-
3
- function unique(checkFn, message = 'Already exists') {
4
- return async value => {
5
- if (!value) return null;
6
- const exists = await checkFn(value);
7
- return exists ? message : null;
8
- };
9
- }
10
- function debounce(validator, delayMs) {
11
- let timeoutId;
12
- return async value => {
13
- return new Promise(resolve => {
14
- clearTimeout(timeoutId);
15
- timeoutId = setTimeout(async () => {
16
- const maybeAsync = validator(value);
17
- const result = isObservable(maybeAsync) ? await firstValueFrom(maybeAsync) : await maybeAsync;
18
- resolve(result);
19
- }, delayMs);
20
- });
21
- };
22
- }
23
-
24
- export { debounce, unique };