Skip to content

Commit

Permalink
Minor touch up
Browse files Browse the repository at this point in the history
  • Loading branch information
mikker committed Dec 28, 2023
1 parent e89eb5c commit 6e86da5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
3 changes: 0 additions & 3 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

const sourdough = new Sourdough({
richColors: true,
yPosition: "top",
xPosition: "center",
expandedByDefault: true,
});

window.addEventListener("DOMContentLoaded", () => {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "0.0.1",
"description": "Delicious Sourdough Toast",
"main": "src/sourdough-toast.js",
"files": [
"src"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "five-server --open=false"
Expand Down
63 changes: 25 additions & 38 deletions src/sourdough-toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const DEFAULT_OPTIONS = {
maxToasts: 3,
duration: 4000, // * 10000,
duration: 4000,
width: 356,
gap: 14,
theme: "light",
Expand Down Expand Up @@ -204,18 +204,13 @@ function getDocumentDirection() {
}
// }}}

const EVENTS = {
add: "sourdough:add",
remove: "sourdough:remove",
};
// {{{ class Store
class Store extends EventTarget {
#subscribers = [];

// {{{ class State
class State extends EventTarget {
constructor() {
super();

this.subscribers = [];

this.data = {
toasts: [],
expanded: false,
Expand All @@ -224,64 +219,56 @@ class State extends EventTarget {
}

subscribe = (fn) => {
this.subscribers.push(fn);
this.#subscribers.push(fn);

return () => {
const index = this.subscribers.indexOf(fn);
this.subscribers.splice(index, 1);
this.#subscribers = this.#subscribers.filter((f) => f !== fn);
};
};

publish = (data) => {
this.subscribers.forEach((fn) => fn(data));
};

touch = () => {
this.publish(this.data);
#publish = () => {
this.#subscribers.forEach((fn) => fn(this.data));
};

set = (fn) => {
#set = (fn) => {
const change = fn({ ...this.data });
console.log(change);
this.data = { ...this.data, ...change };
this.publish(this.data);
this.#publish(this.data);
};

add = (toast) => {
this.set((data) => ({
touch = () => {
this.#publish();
};

create = (opts) => {
const id = (toastsCounter++).toString();
const toast = { id, ...opts };

this.#set((data) => ({
toasts: [...data.toasts, toast],
}));
};

remove = (id) => {
this.set((data) => ({
this.#set((data) => ({
toasts: data.toasts.filter((t) => t.id !== id),
}));
};

expand = () => {
this.set(() => ({ expanded: true }));
this.#set(() => ({ expanded: true }));
};

collapse = () => {
this.set(() => ({ expanded: false }));
this.#set(() => ({ expanded: false }));
};

focus = () => {
this.set(() => ({ interacting: true }));
this.#set(() => ({ interacting: true }));
};

blur = () => {
this.set(() => ({ interacting: false }));
};

create = (opts) => {
const id = (toastsCounter++).toString();
const toast = { id, ...opts };

this.add(toast);

return id;
this.#set(() => ({ interacting: false }));
};
} // }}}

Expand Down Expand Up @@ -533,7 +520,7 @@ class Sourdough {
};
}

const state = new State();
const state = new Store();

const toast = (title) => {
state.create({ title });
Expand Down

0 comments on commit 6e86da5

Please sign in to comment.