Created
November 14, 2022 06:31
-
-
Save pfftdammitchris/7d276a9b6a41f824954a8df752582059 to your computer and use it in GitHub Desktop.
This file contains 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
let state = { backgroundColor: 'white', profiles: [] } | |
let subscribers = [] | |
let commands = {} | |
function setState(newState) { | |
let prevState = state | |
state = | |
typeof newState === 'function' | |
? newState(prevState) | |
: { ...prevState, ...newState } | |
dispatch('NOTIFY_SUBSCRIBERS', { prevState, newState: state }) | |
} | |
function subscribe(callback) { | |
subscribers.push(callback) | |
} | |
function registerCommand(name, callback) { | |
if (commands[name]) commands[name].push(callback) | |
else commands[name] = [callback] | |
} | |
function dispatch(name, action) { | |
commands[name]?.forEach?.((fn) => fn?.(action)) | |
} | |
registerCommand( | |
'SET_BACKGROUND_COLOR', | |
function onSetState({ backgroundColor }) { | |
setState((prevState) => ({ ...prevState, backgroundColor })) | |
}, | |
) | |
registerCommand('NOTIFY_SUBSCRIBERS', function onNotifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
}) | |
registerCommand('ADD_PROFILE', function onAddProfile(profile) { | |
setState((prevState) => ({ | |
...prevState, | |
profiles: prevState.profiles.concat(profile), | |
})) | |
}) | |
subscribe( | |
(function () { | |
function getColor(length) { | |
if (length >= 5 && length <= 8) return 'blue' // Average | |
if (length > 8 && length < 10) return 'orange' // Reaching limit | |
if (length > 10) return 'red' // Limit reached | |
return 'white' // Default | |
} | |
return ({ prevState, newState }) => { | |
const prevProfiles = prevState?.profiles || [] | |
const newProfiles = newState?.profiles || [] | |
if (prevProfiles.length !== newProfiles.length) { | |
dispatch('SET_BACKGROUND_COLOR', { | |
backgroundColor: getColor(newProfiles.length), | |
}) | |
} | |
} | |
})(), | |
) | |
console.log(state.backgroundColor) // 'white' | |
dispatch('ADD_PROFILE', { id: 0, name: 'george', gender: 'male' }) | |
dispatch('ADD_PROFILE', { id: 1, name: 'sally', gender: 'female' }) | |
dispatch('ADD_PROFILE', { id: 2, name: 'kelly', gender: 'female' }) | |
console.log(state.backgroundColor) // 'white' | |
dispatch('ADD_PROFILE', { id: 3, name: 'mike', gender: 'male' }) | |
dispatch('ADD_PROFILE', { id: 4, name: 'bob', gender: 'male' }) | |
console.log(state.backgroundColor) // 'blue' | |
dispatch('ADD_PROFILE', { id: 5, name: 'kevin', gender: 'male' }) | |
dispatch('ADD_PROFILE', { id: 6, name: 'henry', gender: 'male' }) | |
console.log(state.backgroundColor) // 'blue' | |
dispatch('ADD_PROFILE', { id: 7, name: 'ronald', gender: 'male' }) | |
dispatch('ADD_PROFILE', { id: 8, name: 'chris', gender: 'male' }) | |
dispatch('ADD_PROFILE', { id: 9, name: 'andy', gender: 'male' }) | |
dispatch('ADD_PROFILE', { id: 10, name: 'luke', gender: 'male' }) | |
console.log(state.backgroundColor) // 'red' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment