Skip to content

Instantly share code, notes, and snippets.

View pfftdammitchris's full-sized avatar
💭
Dreaming

Christopher Tran pfftdammitchris

💭
Dreaming
View GitHub Profile
{
"name": "my-typescript-library",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "tsup src/index.ts",
"start": "npm run build -- --watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
let state = { backgroundColor: 'white', profiles: [] }
let subscribers = []
let commands = {}
function setState(newState) {
let prevState = state
state =
typeof newState === 'function'
? newState(prevState)
: { ...prevState, ...newState }
function notifySubscribers(...args) {
subscribers.forEach((fn) => fn(...args))
}
function setBackgroundColor(color) {
setState((prevState) => ({
...prevState,
backgroundColor: color,
}))
}
registerCommand(
'SET_BACKGROUND_COLOR',
function onSetState({ backgroundColor }) {
setState((prevState) => ({ ...prevState, backgroundColor }))
},
)
registerCommand('NOTIFY_SUBSCRIBERS', function onNotifySubscribers(...args) {
subscribers.forEach((fn) => fn(...args))
})
let state = { backgroundColor: 'white', profiles: [] }
let subscribers = []
function notifySubscribers(...args) {
subscribers.forEach((fn) => fn(...args))
}
function setBackgroundColor(color) {
setState((prevState) => ({
...prevState,
// Private object hidden within this scope
let commands = {}
export function registerCommand(name, callback) {
if (commands[name]) commands[name].push(callback)
else commands[name] = [callback]
}
export function dispatch(name, action) {
commands[name]?.forEach?.((fn) => fn?.(action))
// Private object hidden within this scope
let commands = {}
export function registerCommand(name, callback) {
if (commands[name]) commands[name].push(callback)
else commands[name] = [callback]
}
export function dispatch(name, action) {
commands[name]?.forEach?.((fn) => fn?.(action))
let state = { backgroundColor: 'white', profiles: [] }
let subscribers = []
let commands = {}
function setState(newState) {
let prevState = state
state =
typeof newState === 'function'
? newState(prevState)
: { ...prevState, ...newState }
registerCommand(
'SET_BACKGROUND_COLOR',
function onSetState({ backgroundColor }) {
setState((prevState) => ({ ...prevState, backgroundColor }))
},
)
registerCommand('NOTIFY_SUBSCRIBERS', function onNotifySubscribers(...args) {
subscribers.forEach((fn) => fn(...args))
})