Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/storage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const get = (key, defaultState) =>
JSON.parse(localStorage.getItem(key)) || defaultState;
export const set = (key, newState) =>
localStorage.setItem(key, JSON.stringify(newState));
33 changes: 18 additions & 15 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
export default function Store() {
//State
this.state = {
todos: [],
view: "all",
};
//Observer
this.observers = [];
this.addObserver = (observer) => this.observers.push(observer);
this.observing = () =>
this.observers.forEach((observer) => observer.render());
import { get, set } from "../storage/index.js";
const USER = "user";

export default class Store {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

형남님 코드를 보면서 많이 배울 수 있었습니다 .!! 감사합니다 👍

constructor() {
this.state = get(USER, { todos: [], view: "all" });
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store에 state를 저장하는 것은 결합이 강하고 역할이 많으니 좀 더 분리

this.observers = [];
}
addObserver(observer) {
this.observers.push(observer);
}
observing() {
this.observers.forEach((observer) => observer.render());
}
//GET
this.getState = () => {
getState() {
return this.state;
};
}
//SET
this.setState = (newState) => {
setState(newState) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비즈니스 로직은 상태를 가진 계층에서 처리하기

this.state = { ...this.state, ...newState };
set(USER, this.state);
this.observing();
};
}
}