Josm
Object-oriented state manager
Josm is an (JS) object-oriented state manager for the web & node, which aims to be both, a lightweight observable (called Data) implementation & a feature rich state manager, providing an awesome developer experience.
Usage
Note that the state manager can tree-shaken off the observable implementation using esImports & a properly configured bundler (e.g. webpack).
Observable
Data
Observables are called Data
in Jsom. These are the simplest building blocks as they observe the mutations of one primitive value over time. Or in other words: One Data instance contains one value. As you change that value all prior registered obervers (callbacks) get notified (called).
This would log 1; 10; 100; 100
.
DataCollection
To observe multiple values under one observer nest Data
s into one DataCollection
.
This would log [10, 2]; [100, 2]; [100, 20]
.
DataSubscription
Both Data and DataCollection return a DataSubscription
when subscribing (via Data#get(cb)
). These can be used to manage the subscription state & can be viewed independently of their source (their source can be changed).
DataBase
DataBase
s function similar to Data
s, only that they are used to store multiple indexed Data
s (objects / arrays).
Note: Observed objects can be circular
Traversal
This instance can be traversed like a plain object. The primitive values are wrapped inside Data
s.
Bulk change
All operations concerning more than a primitive can be accessed via the various function overloads on a DataBase
.
A simple example for this would be to change multiple values of an object.
Bulk add or delete
Adding or deleting properties (undefined
stands for delete)
Getting
Retrieving the whole object
Note: The observer is being invoked every time something below it changes. So when
db.nestedKey[0]
is changed, the event is propagated to all observers above or on it.
Relative traversal
The object can also be traversed via an overload
Even Data
s can be used as key here
Caveat:
name
(and some other properties) cannot be used, as they are already defined on the function objectCaveat: IntelliSense will show all properties that function has
Derivables
With the above interface virtually every manipulation is possible, but often not very handy.
What increasing a number / appending to a string would look like
Thats what derivables solve. Defining repeated manipulation processes once, directly on the type it is made for.
With this declared, it can be used on the fly as the typing adapts.
Note: While this example is really just about convenience, it excels when defining more complex procedures (like injecting something into a string, etc.)
Caveat: No function name can be used twice withing all dataDerivables or within all dataBaseDerivables.
While derivable usage on Data
s is substantial on its own, applying it to certain interfaces DataBases
s does provide seamless interaction on a very high level.
Specifics
Subscription pertinention
When nesting observer declarations in synchronous code (which would without precautions result in a memory leak), josm tries to unsubscribe the old (unused) subscription in favor of the new one.
Bad practice: In real code, use a
DataCollection
instead. While this would work (without a memory leak), it is not clean nor performant and breaks when the data1 callback were asynchronous. This may however be unavoidable in some situations. The following however is just for demonstration.
Contribute
All feedback is appreciated. Create a pull request or write an issue.
Last updated