1.7.45 • Published 2 months ago

josm v1.7.45

Weekly downloads
85
License
ISC
Repository
github
Last release
2 months ago

description: Object-oriented state manager

Josm

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. It may be used to build declarative UI (frameworks) or as a server side data store (using a mongodb adapter for persistent storage) suitable for realtime applications (with a websocket bridge).

Usage

Note that the state manager can be tree-shaken off the observable implementation using esImports & a properly configured bundler (e.g. webpack).

import { Data, ... } from "josm"

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).

let data = new Data(1)

data.get(console.log) // Calls (the observer) console.log every time data is set

data.set(10)
data.set(100)

console.log(data.get()) // This gets the current value of data (100)

This would log 1; 10; 100; 100.

DataCollection

To observe multiple values under one observer nest Datas into one DataCollection.

let data1 = new Data(1)
let data2 = new Data(2)

let dataCollection = new DataCollection(data1, data2)

dataCollection.get(console.log, /*initialize: boolean = true*/ false)

data1.set(10)
data1.set(100)
data2.set(20)

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).

let data1 = new Data(1)

let dataSubscription = data.get(console.log)

dataSubscription.deactivate()
data1.set(10)
dataSubscription.activate(/*initialize: boolean = true*/ false)

data1.got(dataSubscription)
data1.get(dataSubscription)


console.log(dataSubscription.data())         // Gets the current data (data1)
console.log(dataSubscription.active())       // Gets the current active status (true)
console.log(dataSubscription.subscription()) // Gets the current subscription (console.log)


let dataCollection = new DataCollection(new Data(2), new Data(3))

dataSubscription.data(dataCollection, /*initialize: boolean = true*/)
dataSubscription.active(false)
dataSubscription.subscription((d2, d3) => {
  console.log("Custom Subscription", d2, d3)
})

DataBase

DataBases function similar to Datas, only that they are used to store multiple indexed Datas (objects / arrays).

let db = new DataBase({
  key1: 1,
  key2: 2
  nestedKey: ["a", "b", "c"]
})

Note: Observed objects can be circular

Traversal

This instance can be traversed like a plain object. The primitive values are wrapped inside Datas.

console.log(db.key1.get())          // 2
console.log(db.nestedKey[2].get())  // "c"

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.

db({key1: 11, key2: 22})
console.log(db.key1.get(), db.key2.get())   // 11, 22

Bulk add or delete

Adding or deleting properties (undefined stands for delete)

db({key3: 33, key1: undefined})
Getting

Retrieving the whole object

// once
console.log(db())         // { key2: 22, key3: 33, nestedKey: ["a", "b", "c"] }

// observed
db((ob) => {
  console.log("db", ob)
})

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

db("nested", 2).get()   // Equivalent to db.key2[2].get()

Even Datas can be used as key here

let lang = new DataBase({
  en: {
    greeting: "Hello",
    appName: "Cool.oi"
  },
  de: {
    greeting: "Hallo",
    appName: "Cool.io"
  }
})

let currentLangKey = new Data("en")

lang(currentLangKey).appName.get(console.log)   // "Cool.oi"  // initially english
currentLangKey.set("de")                        // "Cool.io"  // now german
lang.en.appName.set("Cool.io")
currentLangKey.set("de")                        //            // no change ("Cool.io" > "Cool.io") 

Caveat: name (and some other properties) cannot be used, as they are already defined on the function object

Caveat: 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

let num = new Data(2)
num.set(num.get() + 1)

let str = new Data("Hel")
str.set(str.get() + "lo")

Thats what derivables solve. Defining repeated manipulation processes once, directly on the type it is made for.

const DATA = setDataDerivativeIndex(
  class Num extends Data<number> {
    inc(by: number = 1) {
      this.set(this.get() + by)
    }
    dec(by: number = 1) {
      this.set(this.get() - by)
    }
  },
  class Str extends Data<string> {
    append(txt: string) {
      this.set(this.get() + txt)
    }
  }
)

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.)

let num = new DATA(2)
num.inc()

let str = new DATA("Hel")
str.append("lo")

Caveat: No function name can be used twice withing all dataDerivables or within all dataBaseDerivables.

While derivable usage on Datas is substantial on its own, applying it to certain interfaces DataBasess does provide seamless interaction on a very high level.

interface Person {
  age: number,
  firstName: string,
  lastName: string
}

const DATABASE = setDataBaseDerivativeIndex(
  class Pers extends DataBase<Person> {
    happyBirthday() {
      this.age.inc()
    }
  }
)

let person = new DATABASE({
  age: 18,
  firstName: "Max",
  lastName: "Someone"
})

person.happyBirthday()

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.

let data1 = new Data("value1")
let data2 = new Data("value2")

data1.get((d1) => {
  data2.get((d2) => {
    console.log(d1, d2)
  })
})

Contribute

All feedback is appreciated. Create a pull request or write an issue.

1.7.45

2 months ago

1.7.44

5 months ago

1.7.42

8 months ago

1.7.43

8 months ago

1.7.41

9 months ago

1.7.36

12 months ago

1.7.37

12 months ago

1.7.38

12 months ago

1.7.39

12 months ago

1.7.40

12 months ago

1.7.33

1 year ago

1.7.34

1 year ago

1.7.35

1 year ago

1.7.17

2 years ago

1.7.18

2 years ago

1.7.19

2 years ago

1.7.20

2 years ago

1.7.21

2 years ago

1.7.22

2 years ago

1.7.23

2 years ago

1.7.24

2 years ago

1.7.25

2 years ago

1.7.26

2 years ago

1.7.27

2 years ago

1.7.28

2 years ago

1.7.29

2 years ago

1.7.30

2 years ago

1.7.31

2 years ago

1.7.32

2 years ago

1.7.13

2 years ago

1.7.14

2 years ago

1.7.15

2 years ago

1.7.16

2 years ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.7.10

2 years ago

1.7.11

2 years ago

1.7.12

2 years ago

1.7.9

2 years ago

1.7.8

2 years ago

1.7.7

2 years ago

1.7.6

2 years ago

1.7.5

2 years ago

1.7.4

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.6.20

2 years ago

1.6.22

2 years ago

1.6.21

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.4.5

2 years ago

1.6.11

2 years ago

1.6.10

2 years ago

1.6.13

2 years ago

1.6.12

2 years ago

1.6.15

2 years ago

1.6.14

2 years ago

1.6.17

2 years ago

1.6.16

2 years ago

1.6.19

2 years ago

1.6.18

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.19

3 years ago

1.2.18

3 years ago

1.2.16

3 years ago

1.2.17

3 years ago

1.2.14

3 years ago

1.2.15

3 years ago

1.2.13

3 years ago

1.2.12

3 years ago

1.2.11

3 years ago

1.2.10

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago