Skip to content

Commit

Permalink
Debounce localStorage update
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Mar 23, 2024
1 parent dc93929 commit 8a57ca5
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/stores/editor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { throttle } from 'radash';
import { debounce } from 'radash';

import ISelection from '@/types/selection';

const throttledSetItem = throttle({ interval: 1000 }, localStorage.setItem.bind(localStorage));
const debouncedSetItem = debounce({ delay: 200 }, localStorage.setItem.bind(localStorage));

export const useEditorStore = defineStore('editor', () => {
const size = ref(parseInt(localStorage.getItem('size') || '') || 20);
Expand All @@ -15,12 +15,12 @@ export const useEditorStore = defineStore('editor', () => {

function setSize(value: number): void {
size.value = value;
throttledSetItem('size', `${size.value}`);
debouncedSetItem('size', `${size.value}`);
}

function setWidth(value: number): void {
width.value = value;
throttledSetItem('width', `${width.value}`);
debouncedSetItem('width', `${width.value}`);
}

function setScroll(value: number): void {
Expand All @@ -33,7 +33,7 @@ export const useEditorStore = defineStore('editor', () => {

function save(value: string): void {
content.value = value;
throttledSetItem('content', value);
debouncedSetItem('content', value);
}

return {
Expand Down

0 comments on commit 8a57ca5

Please sign in to comment.