Protect mutation of stores
This commit is contained in:
parent
ec52e33b0d
commit
92fde448ff
|
@ -1,29 +1,32 @@
|
|||
import { get, writable } from 'svelte/store';
|
||||
import { get, readonly, writable } from 'svelte/store';
|
||||
import type { Note, NotesStore } from './notes';
|
||||
|
||||
export class NoteEditorStore {
|
||||
editing = writable<boolean>(false);
|
||||
draftContent = writable<string>('');
|
||||
private _editing = writable<boolean>(false);
|
||||
private _draftContent = writable<string>('');
|
||||
|
||||
readonly editing = readonly(this._editing);
|
||||
readonly draftContent = readonly(this._draftContent);
|
||||
|
||||
constructor(private noteId: Note['id'], private notes: NotesStore) {}
|
||||
|
||||
startEditing = () => {
|
||||
const note = this.notes.get(this.noteId);
|
||||
this.draftContent.set(note?.content ?? '');
|
||||
this.editing.set(true);
|
||||
this._draftContent.set(note?.content ?? '');
|
||||
this._editing.set(true);
|
||||
};
|
||||
|
||||
draftChange = (event: CustomEvent<{ value: string }>) => {
|
||||
this.draftContent.set(event.detail.value);
|
||||
this._draftContent.set(event.detail.value);
|
||||
};
|
||||
|
||||
save = () => {
|
||||
const content = get(this.draftContent);
|
||||
const content = get(this._draftContent);
|
||||
this.notes.update(this.noteId, { content });
|
||||
this.editing.set(false);
|
||||
this._editing.set(false);
|
||||
};
|
||||
|
||||
discard = () => {
|
||||
this.editing.set(false);
|
||||
this._editing.set(false);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ export class NotesStore implements Readable<Note[]> {
|
|||
private offset = 0;
|
||||
private limit = 1000;
|
||||
|
||||
readonly count = readonly(this.notesCount);
|
||||
|
||||
constructor(private tags: TagsStore) {
|
||||
setTimeout(() => {
|
||||
tags.selectedTags.subscribe(() => {
|
||||
|
@ -25,8 +27,6 @@ export class NotesStore implements Readable<Note[]> {
|
|||
|
||||
subscribe = this.notes.subscribe;
|
||||
|
||||
count = readonly(this.notesCount);
|
||||
|
||||
refreshNotes = async () => {
|
||||
const selectedTags = get(this.tags.selectedTags);
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ export class TagsStore implements Readable<TagFilter[]> {
|
|||
private tags = writable<TagFilter[]>([]);
|
||||
private selected = writable<Set<Tag['id']>>(new Set());
|
||||
|
||||
selectedTags = derived(this.selected, ($selected) => Array.from($selected));
|
||||
readonly selectedTags = derived(this.selected, ($selected) => Array.from($selected));
|
||||
|
||||
constructor() {
|
||||
this.initSelections();
|
||||
|
|
Loading…
Reference in New Issue