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
npm i -D tsup |
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
{ | |
"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" | |
}, |
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
let state = { backgroundColor: 'white', profiles: [] } | |
let subscribers = [] | |
let commands = {} | |
function setState(newState) { | |
let prevState = state | |
state = | |
typeof newState === 'function' | |
? newState(prevState) | |
: { ...prevState, ...newState } |
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
function notifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
} | |
function setBackgroundColor(color) { | |
setState((prevState) => ({ | |
...prevState, | |
backgroundColor: color, | |
})) | |
} |
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
registerCommand( | |
'SET_BACKGROUND_COLOR', | |
function onSetState({ backgroundColor }) { | |
setState((prevState) => ({ ...prevState, backgroundColor })) | |
}, | |
) | |
registerCommand('NOTIFY_SUBSCRIBERS', function onNotifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
}) |
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
let state = { backgroundColor: 'white', profiles: [] } | |
let subscribers = [] | |
function notifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
} | |
function setBackgroundColor(color) { | |
setState((prevState) => ({ | |
...prevState, |
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
// 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)) |
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
// 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)) |
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
let state = { backgroundColor: 'white', profiles: [] } | |
let subscribers = [] | |
let commands = {} | |
function setState(newState) { | |
let prevState = state | |
state = | |
typeof newState === 'function' | |
? newState(prevState) | |
: { ...prevState, ...newState } |
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
registerCommand( | |
'SET_BACKGROUND_COLOR', | |
function onSetState({ backgroundColor }) { | |
setState((prevState) => ({ ...prevState, backgroundColor })) | |
}, | |
) | |
registerCommand('NOTIFY_SUBSCRIBERS', function onNotifySubscribers(...args) { | |
subscribers.forEach((fn) => fn(...args)) | |
}) |