Created
April 30, 2021 17:26
-
-
Save autr/6f26d9e8ed168c7e211771fe836e4bae to your computer and use it in GitHub Desktop.
SvelteJS store for system statuses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { writable, get } from 'svelte/store' | |
const ERROR = 'error' | |
const SUCCESS = 'success' | |
const TRY = 'try' | |
function init() { | |
const { subscribe, set, update, get } = writable({ | |
log: [], | |
processes: {} | |
}) | |
const define = ( id, level, type, data ) => { | |
const remove = level == undefined && type == undefined && data == undefined | |
if (level == undefined) level = 0 | |
update( o => { | |
if (!remove) { | |
o.processes[id] = { id, level, type, data } | |
o.log.push( { ...o.processes[id] } ) | |
} | |
if (remove) delete o.processes[id] | |
return o | |
}) | |
} | |
const retrieve = id => { | |
let out | |
update( o => { | |
out = o.processes[id] | |
}) | |
return o | |
} | |
const start = ( id, level, data ) => define( id, level, TRY, data ) // create status | |
const success = ( id, level, data ) => define( id, level, SUCCESS, data ) // set status to success | |
const error = ( id, level, data ) => define( id, level, ERROR, data ) // set status to error | |
const clear = ( id ) => define( id ) // clears status | |
const status = ( id ) => retrieve( id ) // return status of process | |
const is = ( id, type ) => retrieve( id ).type == type // return status of process | |
return { | |
set, | |
subscribe, | |
update, | |
ERROR, | |
SUCCESS, | |
TRY, | |
start, | |
success, | |
error, | |
clear, | |
status, | |
is | |
} | |
} | |
export default init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment