@real1ty-obsidian-plugins/utils 2.6.0 → 2.8.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.
@@ -8,4 +8,14 @@ export interface BatchOperationResult {
8
8
  }
9
9
  export declare function runBatchOperation<T>(items: T[], operationLabel: string, handler: (item: T) => Promise<void>, showResult?: boolean): Promise<BatchOperationResult>;
10
10
  export declare function showBatchOperationResult(operation: string, successCount: number, errorCount: number): void;
11
+ /**
12
+ * Executes an async operation with a lock to prevent concurrent execution for the same key.
13
+ * If a lock already exists for the key, waits for it to complete instead of starting a new operation.
14
+ *
15
+ * @param lockMap - Map storing active locks by key
16
+ * @param key - Unique identifier for the lock
17
+ * @param operation - Async function to execute with the lock
18
+ * @returns Promise resolving to the operation's result
19
+ */
20
+ export declare function withLock<T>(lockMap: Map<string, Promise<T>>, key: string, operation: () => Promise<T>): Promise<T>;
11
21
  //# sourceMappingURL=batch-operations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"batch-operations.d.ts","sourceRoot":"","sources":["../../src/async/batch-operations.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,qBAAqB;IACrC,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,oBAAoB;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EACxC,KAAK,EAAE,CAAC,EAAE,EACV,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,EACnC,UAAU,GAAE,OAAc,GACxB,OAAO,CAAC,oBAAoB,CAAC,CAmB/B;AAED,wBAAgB,wBAAwB,CACvC,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,GAChB,IAAI,CAUN"}
1
+ {"version":3,"file":"batch-operations.d.ts","sourceRoot":"","sources":["../../src/async/batch-operations.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,qBAAqB;IACrC,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,oBAAoB;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EACxC,KAAK,EAAE,CAAC,EAAE,EACV,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,EACnC,UAAU,GAAE,OAAc,GACxB,OAAO,CAAC,oBAAoB,CAAC,CAmB/B;AAED,wBAAgB,wBAAwB,CACvC,SAAS,EAAE,MAAM,EACjB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,GAChB,IAAI,CAUN;AAED;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAAC,CAAC,EAC/B,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAChC,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACzB,OAAO,CAAC,CAAC,CAAC,CAkBZ"}
@@ -28,4 +28,33 @@ export function showBatchOperationResult(operation, successCount, errorCount) {
28
28
  new Notice(`${operation}: ${successCount} succeeded, ${errorCount} failed. Check console for details.`);
29
29
  }
30
30
  }
31
+ /**
32
+ * Executes an async operation with a lock to prevent concurrent execution for the same key.
33
+ * If a lock already exists for the key, waits for it to complete instead of starting a new operation.
34
+ *
35
+ * @param lockMap - Map storing active locks by key
36
+ * @param key - Unique identifier for the lock
37
+ * @param operation - Async function to execute with the lock
38
+ * @returns Promise resolving to the operation's result
39
+ */
40
+ export function withLock(lockMap, key, operation) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ // Check if there's already an operation in progress for this key
43
+ const existingLock = lockMap.get(key);
44
+ if (existingLock) {
45
+ // Wait for the existing operation to complete instead of starting a new one
46
+ return yield existingLock;
47
+ }
48
+ // Create a new locked operation
49
+ const lockPromise = operation();
50
+ lockMap.set(key, lockPromise);
51
+ try {
52
+ return yield lockPromise;
53
+ }
54
+ finally {
55
+ // Always remove the lock when done
56
+ lockMap.delete(key);
57
+ }
58
+ });
59
+ }
31
60
  //# sourceMappingURL=batch-operations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"batch-operations.js","sourceRoot":"","sources":["../../src/async/batch-operations.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAYlC,MAAM,UAAgB,iBAAiB;yDACtC,KAAU,EACV,cAAsB,EACtB,OAAmC,EACnC,aAAsB,IAAI;QAE1B,IAAI,YAAY,GAAG,CAAC,CAAC;QACrB,IAAI,UAAU,GAAG,CAAC,CAAC;QAEnB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YAC1B,IAAI,CAAC;gBACJ,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;gBACpB,YAAY,EAAE,CAAC;YAChB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBAChB,OAAO,CAAC,KAAK,CAAC,GAAG,cAAc,0BAA0B,EAAE,KAAK,CAAC,CAAC;gBAClE,UAAU,EAAE,CAAC;YACd,CAAC;QACF,CAAC;QAED,IAAI,UAAU,EAAE,CAAC;YAChB,wBAAwB,CAAC,cAAc,EAAE,YAAY,EAAE,UAAU,CAAC,CAAC;QACpE,CAAC;QAED,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC;IACrC,CAAC;CAAA;AAED,MAAM,UAAU,wBAAwB,CACvC,SAAiB,EACjB,YAAoB,EACpB,UAAkB;IAElB,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;QACtB,IAAI,MAAM,CACT,GAAG,SAAS,KAAK,YAAY,QAAQ,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,yBAAyB,CAC3F,CAAC;IACH,CAAC;SAAM,CAAC;QACP,IAAI,MAAM,CACT,GAAG,SAAS,KAAK,YAAY,eAAe,UAAU,qCAAqC,CAC3F,CAAC;IACH,CAAC;AACF,CAAC","sourcesContent":["import { Notice } from \"obsidian\";\n\nexport interface BatchOperationOptions {\n\tcloseAfter?: boolean;\n\tcallOnComplete?: boolean;\n}\n\nexport interface BatchOperationResult {\n\tsuccessCount: number;\n\terrorCount: number;\n}\n\nexport async function runBatchOperation<T>(\n\titems: T[],\n\toperationLabel: string,\n\thandler: (item: T) => Promise<void>,\n\tshowResult: boolean = true\n): Promise<BatchOperationResult> {\n\tlet successCount = 0;\n\tlet errorCount = 0;\n\n\tfor (const item of items) {\n\t\ttry {\n\t\t\tawait handler(item);\n\t\t\tsuccessCount++;\n\t\t} catch (error) {\n\t\t\tconsole.error(`${operationLabel}: error processing item:`, error);\n\t\t\terrorCount++;\n\t\t}\n\t}\n\n\tif (showResult) {\n\t\tshowBatchOperationResult(operationLabel, successCount, errorCount);\n\t}\n\n\treturn { successCount, errorCount };\n}\n\nexport function showBatchOperationResult(\n\toperation: string,\n\tsuccessCount: number,\n\terrorCount: number\n): void {\n\tif (errorCount === 0) {\n\t\tnew Notice(\n\t\t\t`${operation}: ${successCount} item${successCount === 1 ? \"\" : \"s\"} processed successfully`\n\t\t);\n\t} else {\n\t\tnew Notice(\n\t\t\t`${operation}: ${successCount} succeeded, ${errorCount} failed. Check console for details.`\n\t\t);\n\t}\n}\n"]}
1
+ {"version":3,"file":"batch-operations.js","sourceRoot":"","sources":["../../src/async/batch-operations.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAYlC,MAAM,UAAgB,iBAAiB;yDACtC,KAAU,EACV,cAAsB,EACtB,OAAmC,EACnC,aAAsB,IAAI;QAE1B,IAAI,YAAY,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CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QAE9B,IAAI,CAAC;YACJ,OAAO,MAAM,WAAW,CAAC;QAC1B,CAAC;gBAAS,CAAC;YACV,mCAAmC;YACnC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;CAAA","sourcesContent":["import { Notice } from \"obsidian\";\n\nexport interface BatchOperationOptions {\n\tcloseAfter?: boolean;\n\tcallOnComplete?: boolean;\n}\n\nexport interface BatchOperationResult {\n\tsuccessCount: number;\n\terrorCount: number;\n}\n\nexport async function runBatchOperation<T>(\n\titems: T[],\n\toperationLabel: string,\n\thandler: (item: T) => Promise<void>,\n\tshowResult: boolean = true\n): Promise<BatchOperationResult> {\n\tlet successCount = 0;\n\tlet errorCount = 0;\n\n\tfor (const item of items) {\n\t\ttry {\n\t\t\tawait handler(item);\n\t\t\tsuccessCount++;\n\t\t} catch (error) {\n\t\t\tconsole.error(`${operationLabel}: error processing item:`, error);\n\t\t\terrorCount++;\n\t\t}\n\t}\n\n\tif (showResult) {\n\t\tshowBatchOperationResult(operationLabel, successCount, errorCount);\n\t}\n\n\treturn { successCount, errorCount };\n}\n\nexport function showBatchOperationResult(\n\toperation: string,\n\tsuccessCount: number,\n\terrorCount: number\n): void {\n\tif (errorCount === 0) {\n\t\tnew Notice(\n\t\t\t`${operation}: ${successCount} item${successCount === 1 ? \"\" : \"s\"} processed successfully`\n\t\t);\n\t} else {\n\t\tnew Notice(\n\t\t\t`${operation}: ${successCount} succeeded, ${errorCount} failed. Check console for details.`\n\t\t);\n\t}\n}\n\n/**\n * Executes an async operation with a lock to prevent concurrent execution for the same key.\n * If a lock already exists for the key, waits for it to complete instead of starting a new operation.\n *\n * @param lockMap - Map storing active locks by key\n * @param key - Unique identifier for the lock\n * @param operation - Async function to execute with the lock\n * @returns Promise resolving to the operation's result\n */\nexport async function withLock<T>(\n\tlockMap: Map<string, Promise<T>>,\n\tkey: string,\n\toperation: () => Promise<T>\n): Promise<T> {\n\t// Check if there's already an operation in progress for this key\n\tconst existingLock = lockMap.get(key);\n\tif (existingLock) {\n\t\t// Wait for the existing operation to complete instead of starting a new one\n\t\treturn await existingLock;\n\t}\n\n\t// Create a new locked operation\n\tconst lockPromise = operation();\n\tlockMap.set(key, lockPromise);\n\n\ttry {\n\t\treturn await lockPromise;\n\t} finally {\n\t\t// Always remove the lock when done\n\t\tlockMap.delete(key);\n\t}\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./async";
2
2
  export * from "./core";
3
3
  export * from "./date";
4
4
  export * from "./file";
5
+ export * from "./inputs";
5
6
  export * from "./settings";
6
7
  export * from "./string";
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,SAAS,CAAC;AAExB,cAAc,QAAQ,CAAC;AAGvB,cAAc,QAAQ,CAAC;AAEvB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAE3B,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,SAAS,CAAC;AAExB,cAAc,QAAQ,CAAC;AAGvB,cAAc,QAAQ,CAAC;AAEvB,cAAc,QAAQ,CAAC;AAEvB,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAE3B,cAAc,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -7,6 +7,8 @@ export * from "./core";
7
7
  export * from "./date";
8
8
  // File operations
9
9
  export * from "./file";
10
+ // Input utilities
11
+ export * from "./inputs";
10
12
  export * from "./settings";
11
13
  // String utilities
12
14
  export * from "./string";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,WAAW;AAEX,kBAAkB;AAClB,cAAc,SAAS,CAAC;AACxB,iBAAiB;AACjB,cAAc,QAAQ,CAAC;AAEvB,kBAAkB;AAClB,cAAc,QAAQ,CAAC;AACvB,kBAAkB;AAClB,cAAc,QAAQ,CAAC;AACvB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,UAAU,CAAC","sourcesContent":["// Settings\n\n// Async utilities\nexport * from \"./async\";\n// Core utilities\nexport * from \"./core\";\n\n// Date operations\nexport * from \"./date\";\n// File operations\nexport * from \"./file\";\nexport * from \"./settings\";\n// String utilities\nexport * from \"./string\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,WAAW;AAEX,kBAAkB;AAClB,cAAc,SAAS,CAAC;AACxB,iBAAiB;AACjB,cAAc,QAAQ,CAAC;AAEvB,kBAAkB;AAClB,cAAc,QAAQ,CAAC;AACvB,kBAAkB;AAClB,cAAc,QAAQ,CAAC;AACvB,kBAAkB;AAClB,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,mBAAmB;AACnB,cAAc,UAAU,CAAC","sourcesContent":["// Settings\n\n// Async utilities\nexport * from \"./async\";\n// Core utilities\nexport * from \"./core\";\n\n// Date operations\nexport * from \"./date\";\n// File operations\nexport * from \"./file\";\n// Input utilities\nexport * from \"./inputs\";\nexport * from \"./settings\";\n// String utilities\nexport * from \"./string\";\n"]}
@@ -0,0 +1,2 @@
1
+ export * from "./input-filter-manager";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/inputs/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./input-filter-manager";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/inputs/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC","sourcesContent":["export * from \"./input-filter-manager\";\n"]}
@@ -0,0 +1,72 @@
1
+ export type FilterChangeCallback = (filterValue: string) => void;
2
+ export interface InputFilterManagerOptions {
3
+ placeholder: string;
4
+ cssClass: string;
5
+ cssPrefix: string;
6
+ onFilterChange: FilterChangeCallback;
7
+ initiallyVisible?: boolean;
8
+ onHide?: () => void;
9
+ debounceMs?: number;
10
+ }
11
+ /**
12
+ * Abstract base class for managing input-based filters with debouncing.
13
+ * Provides a reusable pattern for filter inputs with show/hide functionality,
14
+ * keyboard shortcuts, and debounced updates.
15
+ *
16
+ * @template T - The type of data being filtered (optional, defaults to unknown)
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * class MyFilterManager extends InputFilterManager<MyDataType> {
21
+ * shouldInclude(data: MyDataType): boolean {
22
+ * const filter = this.getCurrentValue().toLowerCase();
23
+ * return data.name.toLowerCase().includes(filter);
24
+ * }
25
+ * }
26
+ *
27
+ * const manager = new MyFilterManager(
28
+ * containerEl,
29
+ * {
30
+ * placeholder: "Filter items...",
31
+ * cssClass: "my-filter-input",
32
+ * onFilterChange: (value) => console.log("Filter changed:", value),
33
+ * initiallyVisible: false,
34
+ * }
35
+ * );
36
+ * ```
37
+ */
38
+ export declare abstract class InputFilterManager<T> {
39
+ protected parentEl: HTMLElement;
40
+ protected containerEl: HTMLElement;
41
+ protected inputEl: HTMLInputElement | null;
42
+ protected debounceTimer: number | null;
43
+ protected currentValue: string;
44
+ protected persistentlyVisible: boolean;
45
+ protected onHide?: () => void;
46
+ protected readonly debounceMs: number;
47
+ protected readonly placeholder: string;
48
+ protected readonly cssClass: string;
49
+ protected readonly cssPrefix: string;
50
+ protected readonly onFilterChange: FilterChangeCallback;
51
+ constructor(parentEl: HTMLElement, options: InputFilterManagerOptions);
52
+ private render;
53
+ private handleInputChange;
54
+ private applyFilterImmediately;
55
+ protected updateFilterValue(value: string): void;
56
+ getCurrentValue(): string;
57
+ show(): void;
58
+ hide(): void;
59
+ focus(): void;
60
+ isVisible(): boolean;
61
+ setPersistentlyVisible(value: boolean): void;
62
+ destroy(): void;
63
+ /**
64
+ * Abstract method that subclasses must implement to determine
65
+ * whether a data item should be included based on the current filter.
66
+ *
67
+ * @param data - The data item to check
68
+ * @returns True if the item should be included, false otherwise
69
+ */
70
+ abstract shouldInclude(data: T): boolean;
71
+ }
72
+ //# sourceMappingURL=input-filter-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-filter-manager.d.ts","sourceRoot":"","sources":["../../src/inputs/input-filter-manager.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAAG,CAAC,WAAW,EAAE,MAAM,KAAK,IAAI,CAAC;AAIjE,MAAM,WAAW,yBAAyB;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,oBAAoB,CAAC;IACrC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,8BAAsB,kBAAkB,CAAC,CAAC;IAcxC,SAAS,CAAC,QAAQ,EAAE,WAAW;IAbhC,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC;IACnC,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAQ;IAClD,SAAS,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,CAAQ;IAC9C,SAAS,CAAC,YAAY,SAAM;IAC5B,SAAS,CAAC,mBAAmB,UAAS;IACtC,SAAS,CAAC,MAAM,CAAC,EAAE,MAAM,IAAI,CAAC;IAC9B,SAAS,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IACtC,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IACvC,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IACpC,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IACrC,SAAS,CAAC,QAAQ,CAAC,cAAc,EAAE,oBAAoB,CAAC;gBAG7C,QAAQ,EAAE,WAAW,EAC/B,OAAO,EAAE,yBAAyB;IA2BnC,OAAO,CAAC,MAAM;IA0Bd,OAAO,CAAC,iBAAiB;IAUzB,OAAO,CAAC,sBAAsB;IAQ9B,SAAS,CAAC,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKhD,eAAe,IAAI,MAAM;IAIzB,IAAI,IAAI,IAAI;IAKZ,IAAI,IAAI,IAAI;IAgBZ,KAAK,IAAI,IAAI;IAIb,SAAS,IAAI,OAAO;IAIpB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAU5C,OAAO,IAAI,IAAI;IAUf;;;;;;OAMG;IACH,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,GAAG,OAAO;CACxC"}
@@ -0,0 +1,140 @@
1
+ const DEFAULT_DEBOUNCE_MS = 150;
2
+ /**
3
+ * Abstract base class for managing input-based filters with debouncing.
4
+ * Provides a reusable pattern for filter inputs with show/hide functionality,
5
+ * keyboard shortcuts, and debounced updates.
6
+ *
7
+ * @template T - The type of data being filtered (optional, defaults to unknown)
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * class MyFilterManager extends InputFilterManager<MyDataType> {
12
+ * shouldInclude(data: MyDataType): boolean {
13
+ * const filter = this.getCurrentValue().toLowerCase();
14
+ * return data.name.toLowerCase().includes(filter);
15
+ * }
16
+ * }
17
+ *
18
+ * const manager = new MyFilterManager(
19
+ * containerEl,
20
+ * {
21
+ * placeholder: "Filter items...",
22
+ * cssClass: "my-filter-input",
23
+ * onFilterChange: (value) => console.log("Filter changed:", value),
24
+ * initiallyVisible: false,
25
+ * }
26
+ * );
27
+ * ```
28
+ */
29
+ export class InputFilterManager {
30
+ constructor(parentEl, options) {
31
+ this.parentEl = parentEl;
32
+ this.inputEl = null;
33
+ this.debounceTimer = null;
34
+ this.currentValue = "";
35
+ this.persistentlyVisible = false;
36
+ const { placeholder, cssClass, cssPrefix, onFilterChange, initiallyVisible = false, onHide, debounceMs = DEFAULT_DEBOUNCE_MS, } = options;
37
+ this.debounceMs = debounceMs;
38
+ this.placeholder = placeholder;
39
+ this.cssClass = cssClass;
40
+ this.cssPrefix = cssPrefix;
41
+ this.onFilterChange = onFilterChange;
42
+ this.onHide = onHide;
43
+ const classes = `${cssClass}-container${initiallyVisible ? "" : ` ${cssPrefix}-hidden`}`;
44
+ this.containerEl = this.parentEl.createEl("div", {
45
+ cls: classes,
46
+ });
47
+ this.render();
48
+ }
49
+ render() {
50
+ this.inputEl = this.containerEl.createEl("input", {
51
+ type: "text",
52
+ cls: this.cssClass,
53
+ placeholder: this.placeholder,
54
+ });
55
+ this.inputEl.addEventListener("input", () => {
56
+ this.handleInputChange();
57
+ });
58
+ this.inputEl.addEventListener("keydown", (evt) => {
59
+ var _a;
60
+ if (evt.key === "Escape") {
61
+ // Only allow hiding if not persistently visible
62
+ if (!this.persistentlyVisible) {
63
+ this.hide();
64
+ }
65
+ else {
66
+ // Just blur the input if persistently visible
67
+ (_a = this.inputEl) === null || _a === void 0 ? void 0 : _a.blur();
68
+ }
69
+ }
70
+ else if (evt.key === "Enter") {
71
+ this.applyFilterImmediately();
72
+ }
73
+ });
74
+ }
75
+ handleInputChange() {
76
+ if (this.debounceTimer !== null) {
77
+ window.clearTimeout(this.debounceTimer);
78
+ }
79
+ this.debounceTimer = window.setTimeout(() => {
80
+ this.applyFilterImmediately();
81
+ }, this.debounceMs);
82
+ }
83
+ applyFilterImmediately() {
84
+ var _a, _b;
85
+ const newValue = (_b = (_a = this.inputEl) === null || _a === void 0 ? void 0 : _a.value.trim()) !== null && _b !== void 0 ? _b : "";
86
+ if (newValue !== this.currentValue) {
87
+ this.updateFilterValue(newValue);
88
+ }
89
+ }
90
+ updateFilterValue(value) {
91
+ this.currentValue = value;
92
+ this.onFilterChange(value);
93
+ }
94
+ getCurrentValue() {
95
+ return this.currentValue;
96
+ }
97
+ show() {
98
+ var _a;
99
+ this.containerEl.removeClass(`${this.cssPrefix}-hidden`);
100
+ (_a = this.inputEl) === null || _a === void 0 ? void 0 : _a.focus();
101
+ }
102
+ hide() {
103
+ var _a;
104
+ // Don't allow hiding if persistently visible
105
+ if (this.persistentlyVisible) {
106
+ return;
107
+ }
108
+ this.containerEl.addClass(`${this.cssPrefix}-hidden`);
109
+ if (this.inputEl) {
110
+ this.inputEl.value = "";
111
+ }
112
+ this.updateFilterValue("");
113
+ (_a = this.onHide) === null || _a === void 0 ? void 0 : _a.call(this);
114
+ }
115
+ focus() {
116
+ var _a;
117
+ (_a = this.inputEl) === null || _a === void 0 ? void 0 : _a.focus();
118
+ }
119
+ isVisible() {
120
+ return !this.containerEl.hasClass(`${this.cssPrefix}-hidden`);
121
+ }
122
+ setPersistentlyVisible(value) {
123
+ this.persistentlyVisible = value;
124
+ if (value) {
125
+ this.show();
126
+ }
127
+ else {
128
+ this.hide();
129
+ }
130
+ }
131
+ destroy() {
132
+ if (this.debounceTimer !== null) {
133
+ window.clearTimeout(this.debounceTimer);
134
+ this.debounceTimer = null;
135
+ }
136
+ this.containerEl.remove();
137
+ this.inputEl = null;
138
+ }
139
+ }
140
+ //# sourceMappingURL=input-filter-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-filter-manager.js","sourceRoot":"","sources":["../../src/inputs/input-filter-manager.ts"],"names":[],"mappings":"AAEA,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAYhC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAgB,kBAAkB;IAavC,YACW,QAAqB,EAC/B,OAAkC;QADxB,aAAQ,GAAR,QAAQ,CAAa;QAZtB,YAAO,GAA4B,IAAI,CAAC;QACxC,kBAAa,GAAkB,IAAI,CAAC;QACpC,iBAAY,GAAG,EAAE,CAAC;QACl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AAE,CAAC;IACvB,CAAC;IAED,IAAI;;QACH,6CAA6C;QAC7C,IAAI,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC9B,OAAO;QACR,CAAC;QAED,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,SAAS,SAAS,CAAC,CAAC;QAEtD,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,EAAE,CAAC;QACzB,CAAC;QAED,IAAI,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;QAC3B,MAAA,IAAI,CAAC,MAAM,oDAAI,CAAC;IACjB,CAAC;IAED,KAAK;;QACJ,MAAA,IAAI,CAAC,OAAO,0CAAE,KAAK,EAAE,CAAC;IACvB,CAAC;IAED,SAAS;QACR,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAC,SAAS,SAAS,CAAC,CAAC;IAC/D,CAAC;IAED,sBAAsB,CAAC,KAAc;QACpC,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC;QAEjC,IAAI,KAAK,EAAE,CAAC;YACX,IAAI,CAAC,IAAI,EAAE,CAAC;QACb,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,IAAI,EAAE,CAAC;QACb,CAAC;IACF,CAAC;IAED,OAAO;QACN,IAAI,IAAI,CAAC,aAAa,KAAK,IAAI,EAAE,CAAC;YACjC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;YACxC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC3B,CAAC;QAED,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC;QAC1B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;IACrB,CAAC;CAUD","sourcesContent":["export type FilterChangeCallback = (filterValue: string) => void;\n\nconst DEFAULT_DEBOUNCE_MS = 150;\n\nexport interface InputFilterManagerOptions {\n\tplaceholder: string;\n\tcssClass: string;\n\tcssPrefix: string;\n\tonFilterChange: FilterChangeCallback;\n\tinitiallyVisible?: boolean;\n\tonHide?: () => void;\n\tdebounceMs?: number;\n}\n\n/**\n * Abstract base class for managing input-based filters with debouncing.\n * Provides a reusable pattern for filter inputs with show/hide functionality,\n * keyboard shortcuts, and debounced updates.\n *\n * @template T - The type of data being filtered (optional, defaults to unknown)\n *\n * @example\n * ```ts\n * class MyFilterManager extends InputFilterManager<MyDataType> {\n * shouldInclude(data: MyDataType): boolean {\n * const filter = this.getCurrentValue().toLowerCase();\n * return data.name.toLowerCase().includes(filter);\n * }\n * }\n *\n * const manager = new MyFilterManager(\n * containerEl,\n * {\n * placeholder: \"Filter items...\",\n * cssClass: \"my-filter-input\",\n * onFilterChange: (value) => console.log(\"Filter changed:\", value),\n * initiallyVisible: false,\n * }\n * );\n * ```\n */\nexport abstract class InputFilterManager<T> {\n\tprotected containerEl: HTMLElement;\n\tprotected inputEl: HTMLInputElement | null = null;\n\tprotected debounceTimer: number | null = null;\n\tprotected currentValue = \"\";\n\tprotected persistentlyVisible = false;\n\tprotected onHide?: () => void;\n\tprotected readonly debounceMs: number;\n\tprotected readonly placeholder: string;\n\tprotected readonly cssClass: string;\n\tprotected readonly cssPrefix: string;\n\tprotected readonly onFilterChange: FilterChangeCallback;\n\n\tconstructor(\n\t\tprotected parentEl: HTMLElement,\n\t\toptions: InputFilterManagerOptions\n\t) {\n\t\tconst {\n\t\t\tplaceholder,\n\t\t\tcssClass,\n\t\t\tcssPrefix,\n\t\t\tonFilterChange,\n\t\t\tinitiallyVisible = false,\n\t\t\tonHide,\n\t\t\tdebounceMs = DEFAULT_DEBOUNCE_MS,\n\t\t} = options;\n\n\t\tthis.debounceMs = debounceMs;\n\t\tthis.placeholder = placeholder;\n\t\tthis.cssClass = cssClass;\n\t\tthis.cssPrefix = cssPrefix;\n\t\tthis.onFilterChange = onFilterChange;\n\t\tthis.onHide = onHide;\n\n\t\tconst classes = `${cssClass}-container${initiallyVisible ? \"\" : ` ${cssPrefix}-hidden`}`;\n\t\tthis.containerEl = this.parentEl.createEl(\"div\", {\n\t\t\tcls: classes,\n\t\t});\n\n\t\tthis.render();\n\t}\n\n\tprivate render(): void {\n\t\tthis.inputEl = this.containerEl.createEl(\"input\", {\n\t\t\ttype: \"text\",\n\t\t\tcls: this.cssClass,\n\t\t\tplaceholder: this.placeholder,\n\t\t});\n\n\t\tthis.inputEl.addEventListener(\"input\", () => {\n\t\t\tthis.handleInputChange();\n\t\t});\n\n\t\tthis.inputEl.addEventListener(\"keydown\", (evt) => {\n\t\t\tif (evt.key === \"Escape\") {\n\t\t\t\t// Only allow hiding if not persistently visible\n\t\t\t\tif (!this.persistentlyVisible) {\n\t\t\t\t\tthis.hide();\n\t\t\t\t} else {\n\t\t\t\t\t// Just blur the input if persistently visible\n\t\t\t\t\tthis.inputEl?.blur();\n\t\t\t\t}\n\t\t\t} else if (evt.key === \"Enter\") {\n\t\t\t\tthis.applyFilterImmediately();\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate handleInputChange(): void {\n\t\tif (this.debounceTimer !== null) {\n\t\t\twindow.clearTimeout(this.debounceTimer);\n\t\t}\n\n\t\tthis.debounceTimer = window.setTimeout(() => {\n\t\t\tthis.applyFilterImmediately();\n\t\t}, this.debounceMs);\n\t}\n\n\tprivate applyFilterImmediately(): void {\n\t\tconst newValue = this.inputEl?.value.trim() ?? \"\";\n\n\t\tif (newValue !== this.currentValue) {\n\t\t\tthis.updateFilterValue(newValue);\n\t\t}\n\t}\n\n\tprotected updateFilterValue(value: string): void {\n\t\tthis.currentValue = value;\n\t\tthis.onFilterChange(value);\n\t}\n\n\tgetCurrentValue(): string {\n\t\treturn this.currentValue;\n\t}\n\n\tshow(): void {\n\t\tthis.containerEl.removeClass(`${this.cssPrefix}-hidden`);\n\t\tthis.inputEl?.focus();\n\t}\n\n\thide(): void {\n\t\t// Don't allow hiding if persistently visible\n\t\tif (this.persistentlyVisible) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.containerEl.addClass(`${this.cssPrefix}-hidden`);\n\n\t\tif (this.inputEl) {\n\t\t\tthis.inputEl.value = \"\";\n\t\t}\n\n\t\tthis.updateFilterValue(\"\");\n\t\tthis.onHide?.();\n\t}\n\n\tfocus(): void {\n\t\tthis.inputEl?.focus();\n\t}\n\n\tisVisible(): boolean {\n\t\treturn !this.containerEl.hasClass(`${this.cssPrefix}-hidden`);\n\t}\n\n\tsetPersistentlyVisible(value: boolean): void {\n\t\tthis.persistentlyVisible = value;\n\n\t\tif (value) {\n\t\t\tthis.show();\n\t\t} else {\n\t\t\tthis.hide();\n\t\t}\n\t}\n\n\tdestroy(): void {\n\t\tif (this.debounceTimer !== null) {\n\t\t\twindow.clearTimeout(this.debounceTimer);\n\t\t\tthis.debounceTimer = null;\n\t\t}\n\n\t\tthis.containerEl.remove();\n\t\tthis.inputEl = null;\n\t}\n\n\t/**\n\t * Abstract method that subclasses must implement to determine\n\t * whether a data item should be included based on the current filter.\n\t *\n\t * @param data - The data item to check\n\t * @returns True if the item should be included, false otherwise\n\t */\n\tabstract shouldInclude(data: T): boolean;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real1ty-obsidian-plugins/utils",
3
- "version": "2.6.0",
3
+ "version": "2.8.0",
4
4
  "description": "Shared utilities for Obsidian plugins",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -51,3 +51,36 @@ export function showBatchOperationResult(
51
51
  );
52
52
  }
53
53
  }
54
+
55
+ /**
56
+ * Executes an async operation with a lock to prevent concurrent execution for the same key.
57
+ * If a lock already exists for the key, waits for it to complete instead of starting a new operation.
58
+ *
59
+ * @param lockMap - Map storing active locks by key
60
+ * @param key - Unique identifier for the lock
61
+ * @param operation - Async function to execute with the lock
62
+ * @returns Promise resolving to the operation's result
63
+ */
64
+ export async function withLock<T>(
65
+ lockMap: Map<string, Promise<T>>,
66
+ key: string,
67
+ operation: () => Promise<T>
68
+ ): Promise<T> {
69
+ // Check if there's already an operation in progress for this key
70
+ const existingLock = lockMap.get(key);
71
+ if (existingLock) {
72
+ // Wait for the existing operation to complete instead of starting a new one
73
+ return await existingLock;
74
+ }
75
+
76
+ // Create a new locked operation
77
+ const lockPromise = operation();
78
+ lockMap.set(key, lockPromise);
79
+
80
+ try {
81
+ return await lockPromise;
82
+ } finally {
83
+ // Always remove the lock when done
84
+ lockMap.delete(key);
85
+ }
86
+ }
package/src/index.ts CHANGED
@@ -9,6 +9,8 @@ export * from "./core";
9
9
  export * from "./date";
10
10
  // File operations
11
11
  export * from "./file";
12
+ // Input utilities
13
+ export * from "./inputs";
12
14
  export * from "./settings";
13
15
  // String utilities
14
16
  export * from "./string";
@@ -0,0 +1 @@
1
+ export * from "./input-filter-manager";
@@ -0,0 +1,194 @@
1
+ export type FilterChangeCallback = (filterValue: string) => void;
2
+
3
+ const DEFAULT_DEBOUNCE_MS = 150;
4
+
5
+ export interface InputFilterManagerOptions {
6
+ placeholder: string;
7
+ cssClass: string;
8
+ cssPrefix: string;
9
+ onFilterChange: FilterChangeCallback;
10
+ initiallyVisible?: boolean;
11
+ onHide?: () => void;
12
+ debounceMs?: number;
13
+ }
14
+
15
+ /**
16
+ * Abstract base class for managing input-based filters with debouncing.
17
+ * Provides a reusable pattern for filter inputs with show/hide functionality,
18
+ * keyboard shortcuts, and debounced updates.
19
+ *
20
+ * @template T - The type of data being filtered (optional, defaults to unknown)
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * class MyFilterManager extends InputFilterManager<MyDataType> {
25
+ * shouldInclude(data: MyDataType): boolean {
26
+ * const filter = this.getCurrentValue().toLowerCase();
27
+ * return data.name.toLowerCase().includes(filter);
28
+ * }
29
+ * }
30
+ *
31
+ * const manager = new MyFilterManager(
32
+ * containerEl,
33
+ * {
34
+ * placeholder: "Filter items...",
35
+ * cssClass: "my-filter-input",
36
+ * onFilterChange: (value) => console.log("Filter changed:", value),
37
+ * initiallyVisible: false,
38
+ * }
39
+ * );
40
+ * ```
41
+ */
42
+ export abstract class InputFilterManager<T> {
43
+ protected containerEl: HTMLElement;
44
+ protected inputEl: HTMLInputElement | null = null;
45
+ protected debounceTimer: number | null = null;
46
+ protected currentValue = "";
47
+ protected persistentlyVisible = false;
48
+ protected onHide?: () => void;
49
+ protected readonly debounceMs: number;
50
+ protected readonly placeholder: string;
51
+ protected readonly cssClass: string;
52
+ protected readonly cssPrefix: string;
53
+ protected readonly onFilterChange: FilterChangeCallback;
54
+
55
+ constructor(
56
+ protected parentEl: HTMLElement,
57
+ options: InputFilterManagerOptions
58
+ ) {
59
+ const {
60
+ placeholder,
61
+ cssClass,
62
+ cssPrefix,
63
+ onFilterChange,
64
+ initiallyVisible = false,
65
+ onHide,
66
+ debounceMs = DEFAULT_DEBOUNCE_MS,
67
+ } = options;
68
+
69
+ this.debounceMs = debounceMs;
70
+ this.placeholder = placeholder;
71
+ this.cssClass = cssClass;
72
+ this.cssPrefix = cssPrefix;
73
+ this.onFilterChange = onFilterChange;
74
+ this.onHide = onHide;
75
+
76
+ const classes = `${cssClass}-container${initiallyVisible ? "" : ` ${cssPrefix}-hidden`}`;
77
+ this.containerEl = this.parentEl.createEl("div", {
78
+ cls: classes,
79
+ });
80
+
81
+ this.render();
82
+ }
83
+
84
+ private render(): void {
85
+ this.inputEl = this.containerEl.createEl("input", {
86
+ type: "text",
87
+ cls: this.cssClass,
88
+ placeholder: this.placeholder,
89
+ });
90
+
91
+ this.inputEl.addEventListener("input", () => {
92
+ this.handleInputChange();
93
+ });
94
+
95
+ this.inputEl.addEventListener("keydown", (evt) => {
96
+ if (evt.key === "Escape") {
97
+ // Only allow hiding if not persistently visible
98
+ if (!this.persistentlyVisible) {
99
+ this.hide();
100
+ } else {
101
+ // Just blur the input if persistently visible
102
+ this.inputEl?.blur();
103
+ }
104
+ } else if (evt.key === "Enter") {
105
+ this.applyFilterImmediately();
106
+ }
107
+ });
108
+ }
109
+
110
+ private handleInputChange(): void {
111
+ if (this.debounceTimer !== null) {
112
+ window.clearTimeout(this.debounceTimer);
113
+ }
114
+
115
+ this.debounceTimer = window.setTimeout(() => {
116
+ this.applyFilterImmediately();
117
+ }, this.debounceMs);
118
+ }
119
+
120
+ private applyFilterImmediately(): void {
121
+ const newValue = this.inputEl?.value.trim() ?? "";
122
+
123
+ if (newValue !== this.currentValue) {
124
+ this.updateFilterValue(newValue);
125
+ }
126
+ }
127
+
128
+ protected updateFilterValue(value: string): void {
129
+ this.currentValue = value;
130
+ this.onFilterChange(value);
131
+ }
132
+
133
+ getCurrentValue(): string {
134
+ return this.currentValue;
135
+ }
136
+
137
+ show(): void {
138
+ this.containerEl.removeClass(`${this.cssPrefix}-hidden`);
139
+ this.inputEl?.focus();
140
+ }
141
+
142
+ hide(): void {
143
+ // Don't allow hiding if persistently visible
144
+ if (this.persistentlyVisible) {
145
+ return;
146
+ }
147
+
148
+ this.containerEl.addClass(`${this.cssPrefix}-hidden`);
149
+
150
+ if (this.inputEl) {
151
+ this.inputEl.value = "";
152
+ }
153
+
154
+ this.updateFilterValue("");
155
+ this.onHide?.();
156
+ }
157
+
158
+ focus(): void {
159
+ this.inputEl?.focus();
160
+ }
161
+
162
+ isVisible(): boolean {
163
+ return !this.containerEl.hasClass(`${this.cssPrefix}-hidden`);
164
+ }
165
+
166
+ setPersistentlyVisible(value: boolean): void {
167
+ this.persistentlyVisible = value;
168
+
169
+ if (value) {
170
+ this.show();
171
+ } else {
172
+ this.hide();
173
+ }
174
+ }
175
+
176
+ destroy(): void {
177
+ if (this.debounceTimer !== null) {
178
+ window.clearTimeout(this.debounceTimer);
179
+ this.debounceTimer = null;
180
+ }
181
+
182
+ this.containerEl.remove();
183
+ this.inputEl = null;
184
+ }
185
+
186
+ /**
187
+ * Abstract method that subclasses must implement to determine
188
+ * whether a data item should be included based on the current filter.
189
+ *
190
+ * @param data - The data item to check
191
+ * @returns True if the item should be included, false otherwise
192
+ */
193
+ abstract shouldInclude(data: T): boolean;
194
+ }