MCPcopy
hub / github.com/callumalpass/tasknotes / startTimeTracking

Method startTimeTracking

src/services/TaskService.ts:1298–1371  ·  view source on GitHub ↗

* Start time tracking for a task

(task: TaskInfo)

Source from the content-addressed store, hash-verified

1296 * Start time tracking for a task
1297 */
1298 async startTimeTracking(task: TaskInfo): Promise<TaskInfo> {
1299 const file = this.plugin.app.vault.getAbstractFileByPath(task.path);
1300 if (!(file instanceof TFile)) {
1301 throw new Error(`Cannot find task file: ${task.path}`);
1302 }
1303
1304 // Check if already tracking
1305 const activeSession = this.plugin.getActiveTimeSession(task);
1306 if (activeSession) {
1307 throw new Error("Time tracking is already active for this task");
1308 }
1309
1310 // Step 1: Construct new state in memory
1311 const timeTrackingPlan = buildStartTimeTrackingPlan(
1312 task,
1313 getCurrentTimestamp(),
1314 new Date().toISOString()
1315 );
1316 const { updatedTask, newEntry } = timeTrackingPlan;
1317
1318 // Step 2: Persist to file
1319 await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => {
1320 const timeEntriesField = this.plugin.fieldMapper.toUserField("timeEntries");
1321 const dateModifiedField = this.plugin.fieldMapper.toUserField("dateModified");
1322 applyStartTimeTrackingFrontmatterChange({
1323 frontmatter,
1324 timeEntriesField,
1325 dateModifiedField,
1326 newEntry,
1327 dateModified: timeTrackingPlan.dateModified,
1328 });
1329 });
1330
1331 // Step 3: Wait for fresh data and update cache
1332 try {
1333 // Wait for the metadata cache to have the updated time entries
1334 if (this.plugin.cacheManager.waitForFreshTaskData) {
1335 await this.plugin.cacheManager.waitForFreshTaskData(file);
1336 }
1337 this.plugin.cacheManager.updateTaskInfoInCache(task.path, updatedTask);
1338 } catch (cacheError) {
1339 tasknotesLogger.error("Error updating cache for time tracking start:", {
1340 category: "stale-data",
1341 operation: "updating-cache-time-tracking-start",
1342 error: cacheError,
1343 });
1344 }
1345
1346 // Step 4: Notify system of change
1347 this.plugin.emitter.trigger(EVENT_TASK_UPDATED, {
1348 path: task.path,
1349 originalTask: task,
1350 updatedTask: updatedTask,
1351 });
1352
1353 // Trigger webhook for time tracking start
1354 if (this.webhookNotifier) {
1355 try {

Callers 7

buildMenuMethod · 0.45
buildActionsListMethod · 0.45
startTimeTrackingForTaskFunction · 0.45
startPomodoroMethod · 0.45
resumePomodoroMethod · 0.45

Calls 12

getCurrentTimestampFunction · 0.90
getAbstractFileByPathMethod · 0.80
getActiveTimeSessionMethod · 0.80
waitForFreshTaskDataMethod · 0.80
errorMethod · 0.80
warnMethod · 0.80
processFrontMatterMethod · 0.65
toUserFieldMethod · 0.65
updateTaskInfoInCacheMethod · 0.65
triggerMethod · 0.65
triggerWebhookMethod · 0.65

Tested by

no test coverage detected