![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@youri-kane/minimal-statestore
Advanced tools
a minimal state data store, able to validate data before storage and to fire callbacks on data update
Store data and trigger handlers on state update
Optional data validation
12.1kb minified
Uses ES6 Proxy
const stateStore = new StateStore({
namespace: String, //optional store name
state: {
//data value that will be observed
},
model: {
//optional validation
},
handlers: {
//this property holds functions that will be called when the associated state changes
},
modelOptions: {
shallowValidation: Boolean, // default to false
},
onStateChange: Function, // triggered only if the new state is different from the previous one
onValidationFail: Function // triggered when a state fail model validation
});
Full documentation available on Github Pages.
npx install @youri-kane/minimal-statestore
{
"build": "node ./node_modules/webpack/bin/webpack.js --config ./webpack.config.js ./src/index.js",
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose",
"test": "jest",
"test-verbose": "jest --coverage --config ./jest.config.js"
}
setState => Model Validation() => State Update => Trigger Handler
The state property allow you to store initial values while initializing the store States can be updated using the stateStoreInstance.setState method, they can also be retrieved using the stateStoreInstance.state property exemple of initialisation:
state: {
name: 'John Doe',
age: 29,
}
Note: initial values won't be checked against the model
The model property allow you to validate data before setting state
It can be:
model: {
name: new RegExp(/.{1,}/)
}
model: {
name: value => (typeof value == 'string' && value.length < 6)
}
model: {
name: 'string',
age: 'number',
other: ['number', 'boolean'],
}
model: {
status: [0, 1, 2, 3]
}
if stateStoreInstance is initialized with new StateStore({..., modelOptions = { shallowValidation: true, }}) it will be possible to update states that were not declared on stateStoreInstance init
handlers function are named after state so they can be triggered when a state is updated Note: Handlers will be triggered after all state modifications occurred
handlers: {
name: ({ oldValue, value, store }) => {
console.log(oldValue) // previous state value
console.log(value) // current state value
console.log(store) // current state of the store
}
}
setState function is the only way to update the stored values
stateStoreInstance.setState({
property: value
}, ({ differences, oldState, state }) => {
differences //object containing only the updated states
olState //object previous state of the store
state //object actual state of the store
})
clearState function allows you to empty/reset the state without triggering any handler
stateStoreInstance.clearState({props: 'value to reset', prop2: 'value to reset'} /* defaults to an empty object */, () => {
//doesn't get any params
})
toggleShallowValidation allow you to switch the mode of state validation when the value is true, state not declared in the model will be updatable when set to false only the properties set to false only state declared in the model will be updatable
const enabled = stateStore.toggleShallowValidation
console.log(enabled) //true || false
const onStateChange = event => void console.log(event)
stateStoreInstance.on('statechange', onStateChange)
stateStoreInstance.off('statechange', onStateChange)
stateStoreInstance.setModelField('fieldname', fieldModel)
console.log(stateStoreInstance.model.fieldname) // fieldModel
stateStoreInstance.unsetModelField('fieldname')
console.log(stateStoreInstance.model.fieldname) // undefined
add state handler
stateStoreInstance.setHandler('fieldname', Function)
remove state handler
stateStoreInstance.unsetModelField('fieldname')
created event is fired when new StateStore instance is created
stateStoreInstance.on('created', event => {
console.log(event.detail) // { state: initial state, storeName: name of the created store }
})
statechange event is fired when the previous state is different form the current state
stateStoreInstance.on('statechange', event => {
console.log(event.detail) // { state: current state, oldState: previous state, differences: states that changed }
})
validationfail event is fired when a state update has failed model validation
stateStoreInstance.on('validationfail', event => {
console.log(event.detail) // { state: state that failed to update, rejected: value that failed to validate }
})
const stateStore = new StateStore({
state: {
name: '',
},
model: {
name: 'string'
},
handlers: {
name: ({ oldValue, value }) => {
alert('Hello, ' + value)
console.log(stateStore.state.value)
}
},
});
stateStore.setState({
name: 'John Doe',
age: 18,
}, ({ differences, oldState, state }) => {
console.log(difference) // { name: 'John Doe' }
console.log(oldState) // { name: '' }
console.log(state) // { name: 'John Doe' }
})
const enabled = stateStore.toggleShallowValidation()
console.log(enabled) // true
stateStore.setState({
age: 18
}, ({ differences, oldState, state }) => {
console.log(difference) // { age: '18' }
console.log(oldState) // { name: 'John Doe' }
console.log(state) // { name: 'John Doe', age: 18 }
})
FAQs
a minimal state data store, able to validate data before storage and to fire callbacks on data update
The npm package @youri-kane/minimal-statestore receives a total of 14 weekly downloads. As such, @youri-kane/minimal-statestore popularity was classified as not popular.
We found that @youri-kane/minimal-statestore demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.