Refactoring the Task Store for Scalability in Svelte

Introduction to State Management Scalability

Welcome to the first lesson of our Advanced State Management and Persistence course! In this course, we'll build on the foundation of basic state management to create more robust, scalable applications with Svelte.

As applications grow in complexity, managing state becomes increasingly challenging. What starts as a simple collection of variables can quickly evolve into an intricate web of interdependent data that's difficult to maintain. Our Kanban board application is at this critical juncture — it works well for basic tasks, but we need to prepare it for growth.

Currently, our task store (taskStore.svelte.js) handles the core functionality:

// Current implementation (simplified)
export const tasks = $state([
  { id: 1, title: 'Learn Svelte 5', description: 'Study the new Runes API', status: 'todo' },
  // More tasks...
]);

export function addTask(title, description = '') {
  const newTask = {
    id: Date.now(),
    title,
    description,
    status: 'todo'
  };
  
  tasks.push(newTask);
  return newTask;
}

export function updateTaskStatus(id, newStatus) {
  const taskIndex = tasks.findIndex(task => task.id === id);
  if (taskIndex !== -1) {
    tasks[taskIndex].status = newStatus;
  }
}

While this implementation works, it has several limitations:

  1. No tracking of when tasks are created or modified
  2. No way to delete tasks
  3. Limited organization of state functions

In this lesson, we'll refactor our task store to address these limitations, although the current changes are not visible in the UI they make the app more scalable and ready for the advanced features we'll add in upcoming lessons. Let's start by adding timestamps to our tasks.

Adding Timestamps to Tasks

Timestamps are crucial for tracking the history of tasks. They help us understand when tasks were created and last modified, which can be valuable for sorting, filtering, and providing context to users.

Let's update our task store to include timestamps. We'll add:

  • A createdAt timestamp when tasks are created
  • An updatedAt timestamp when tasks are modified

Here's how we'll modify our initial task data:

// Initialize tasks state with timestamps
export const tasks = $state([
  { 
    id: 1, 
    title: 'Learn Svelte 5', 
    description: 'Study the new Runes API', 
    status: 'todo', 
    createdAt: new Date().toISOString() 
  },
  { 
    id: 2, 
    title: 'Design components', 
    description: '', 
    status: 'todo', 
    createdAt: new Date().toISOString() 
  },
  // More tasks with timestamps...
]);

Notice that we're using new Date().toISOString() to create standardized timestamp strings. The ISO string format (e.g., "2023-11-15T14:30:45.123Z") is ideal because:

  • It's a string, so it can be easily serialized to JSON
  • It maintains timezone information
  • It's sortable (alphabetical sorting works for chronological ordering)
  • It's a standard format recognized by JavaScript and most other languages

Now, let's update our addTask function to include the creation timestamp:

export function addTask(title, description = '') {
  const newTask = {
    id: Date.now(),
    title,
    description,
    status: 'todo',
    createdAt: new Date().toISOString()
  };
  
  tasks.push(newTask);
  return newTask;
}

Next, we'll modify the updateTaskStatus function to add an updatedAt timestamp whenever a task's status changes:

export function updateTaskStatus(id, newStatus) {
  const taskIndex = tasks.findIndex(task => task.id === id);
  if (taskIndex !== -1) {
    tasks[taskIndex].status = newStatus;
    tasks[taskIndex].updatedAt = new Date().toISOString();
  }
}

With these changes, we now have a chronological record of when tasks are created and updated. This information will be valuable for future features like sorting tasks by creation date or showing recently updated tasks.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal