Skip to content

Commit ab9f1a4

Browse files
committed
Refactor with jsdoc
1 parent 77763e0 commit ab9f1a4

File tree

1 file changed

+89
-14
lines changed

1 file changed

+89
-14
lines changed

src/lib/stores.js

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
import { writable } from 'svelte/store'
22

3+
/**
4+
* @typedef {import('svelte').ComponentType} SvelteComponent
5+
*/
6+
7+
/**
8+
* @typedef {import('svelte/types/runtime/transition/index').FlyParams} FlyParams
9+
*/
10+
11+
/**
12+
* @typedef {Object} SvelteToastCustomComponent
13+
* @property {SvelteComponent} src - custom Svelte Component
14+
* @property {Object.<string, *>} [props] - props to pass into custom component
15+
* @property {string} [sendIdTo] - forward toast id to prop name
16+
*/
17+
18+
/**
19+
* @callback SvelteToastOnPopCallback
20+
* @param {number} [id] - optionally get the toast id if needed
21+
*/
22+
23+
/**
24+
* @typedef {Object} SvelteToastOptions
25+
* @property {number} [id] - unique id generated for every toast
26+
* @property {string} [target] - container target name to send toast to
27+
* @property {string} [msg] - toast message
28+
* @property {number} [duration] - duration of progress bar tween from initial to next
29+
* @property {number} [initial] - initial progress bar value
30+
* @property {number} [next] - next progress bar value
31+
* @property {boolean} [pausable] - pause progress bar tween on mouse hover
32+
* @property {boolean} [dismissable] - allow dissmiss with close button
33+
* @property {boolean} [reversed] - display toasts in reverse order
34+
* @property {FlyParams} [intro] - toast intro fly animation settings
35+
* @property {Object.<string, string|number>} [theme] - css var overrides
36+
* @property {string[]} [classes] - user-defined classes
37+
* @property {SvelteToastOnPopCallback} [onpop] - callback that runs on toast dismiss
38+
* @property {SvelteToastCustomComponent} [component] - send custom Svelte Component as a message
39+
* @property {number} [progress] - DEPRECATED
40+
*/
41+
42+
/** @type {SvelteToastOptions} */
343
const defaults = {
444
duration: 4000,
545
initial: 1,
@@ -12,11 +52,31 @@ const defaults = {
1252

1353
const createToast = () => {
1454
const { subscribe, update } = writable([])
15-
let count = 0
55+
/** @type {Object.<string, SvelteToastOptions>} */
1656
const options = {}
17-
const _obj = (obj) => obj instanceof Object
18-
const push = (msg, opts = {}) => {
19-
const param = { target: 'default', ...(_obj(msg) ? msg : { ...opts, msg }) }
57+
let count = 0
58+
59+
/** @param {*} obj */
60+
function _obj(obj) {
61+
return obj instanceof Object
62+
}
63+
64+
function _init(target = 'default', opts = {}) {
65+
options[target] = opts
66+
return options
67+
}
68+
69+
/**
70+
* Send a new toast
71+
* @param {(string|SvelteToastOptions)} msg
72+
* @param {SvelteToastOptions} [opts]
73+
* @returns {number}
74+
*/
75+
function push(msg, opts) {
76+
const param = {
77+
target: 'default',
78+
...(_obj(msg) ? /** @type {SvelteToastOptions} */ (msg) : { ...opts, msg })
79+
}
2080
const conf = options[param.target] || {}
2181
const entry = {
2282
...defaults,
@@ -29,16 +89,34 @@ const createToast = () => {
2989
update((n) => (entry.reversed ? [...n, entry] : [entry, ...n]))
3090
return count
3191
}
32-
const pop = (id) => {
92+
93+
/**
94+
* Remove toast(s)
95+
* - toast.pop() // removes the last toast
96+
* - toast.pop(0) // remove all toasts
97+
* - toast.pop(id) // removes the toast with specified `id`
98+
* - toast.pop({ target: 'foo' }) // remove all toasts from target `foo`
99+
* @param {(number|Object.<'target', string>)} [id]
100+
*/
101+
function pop(id) {
33102
update((n) => {
34103
if (!n.length || id === 0) return []
35-
if (_obj(id)) return n.filter((i) => id(i))
36-
const target = id || Math.max(...n.map((i) => i.id))
37-
return n.filter((i) => i.id !== target)
104+
// Filter function is deprecated; shim added for backward compatibility
105+
if (typeof id === 'function') return n.filter((i) => id(i))
106+
if (_obj(id)) return n.filter((i) => i.target !== id.target)
107+
const found = id || Math.max(...n.map((i) => i.id))
108+
return n.filter((i) => i.id !== found)
38109
})
39110
}
40-
const set = (id, opts = {}) => {
41-
const param = _obj(id) ? { ...id } : { ...opts, id }
111+
112+
/**
113+
* Update an existing toast
114+
* @param {(number|SvelteToastOptions)} id
115+
* @param {SvelteToastOptions} [opts]
116+
*/
117+
function set(id, opts) {
118+
/** @type {object} */
119+
const param = _obj(id) ? id : { ...opts, id }
42120
update((n) => {
43121
const idx = n.findIndex((i) => i.id === param.id)
44122
if (idx > -1) {
@@ -47,10 +125,7 @@ const createToast = () => {
47125
return n
48126
})
49127
}
50-
const _init = (target = 'default', opts = {}) => {
51-
options[target] = opts
52-
return options
53-
}
128+
54129
return { subscribe, push, pop, set, _init }
55130
}
56131

0 commit comments

Comments
 (0)