0.3.19 • Published 7 years ago

hub.js v0.3.19

Weekly downloads
23
License
ISC
Repository
github
Last release
7 years ago

hub.js

Build Status js-standard-style npm version Coverage Status

Observable data structures, over the network

What is it not?

  • Its not a database
  • Its not a query language
  • Its not persistent storage

What is it?

  • Software to help scale a realtime back end to millions of users — without adding any extra worries for developers
  • Make an app for users as simple as creating a prototype
  • Use the server to offload most of your client side cpu

Why

There is firebase right?

  • better subscriptions, “observable deep queries”
  • conflict resolution
  • flexible
  • completely open source — can be hosted on now or anywhere else
  • use as a hub for all your integrations — custom AND standard

What does it do?

  • Create realtime branches of data
  • Gives sandboxes for your application to store data in
  • Executes queries
  • Sends diffs, keeps track of change
  • Integrates apis
  • Supports references (serliazable over the network)
  • Performs!
  • Can be a server
  • Can be a client
  • Or both
  • Runs on the browser
  • Can be used as a state store (like redux but simple)
  • Reconnects
  • Server side sessions

Getting started

npm i hub.js

const Hub = require('hub.js')
// creates a hub as a server and as a client
const hub = Hub()
  .listen(80)
  .connect('ws://someurl.com')

Data structure

hub.js uses a data structure modelled to closely resemble plain js objects

Elements can be values and objects at the same time, all element are observable

const hub = Hub({
  something: 'hello'
})

// .set does a deep merge by default
hub.set({
  something: {
    field: 'some field'
  }
})

hub.get('something').on(() => {
  console.log('fires on change!')
})

// object notation for listeners
hub.set({
  something: {
    on: {
      data: () => {} // data emitter type
    }
  }
})

console.log(hub.serialize()) // serialize casts hub objects to plain objects
// logs { something: { val: 'hello', field: 'some field' }}

References

const hub = Hub({
  something: 'hello'
})

// creates an observable reference
hub.set({
  thing: hub.get('something')
})

hub.thing.on(() => {
  console.log('hello')
})

hub.something.set('bye')
// fires the listener on hub.thing

hub.set({
  bla: [ '@', 'root', 'other']
})
// set something to a reference before it exists

hub.set({
  other: 'thing'
})
// will resolve updates for

Subscriptions

Basic

A simple subscription

client.subscribe(true, (target, type) => {
  // fires updates for any update in the hub
  console.log('update!', target, type)
})

Setting on the server

server.set('hello!')
// will fire an update on client

A shallow subscription

client.subscribe('shallow', (target, type) => {
  // fires updates for any update on the value of the hub, but not nested fields
  console.log('update!', target, type)
})

Any

A simple subscription

client.subscribe({
  $any: { title: true }
}, (target, type) => {
  // fires updates when any field updates a title
  console.log('update!', target, type)
})

A complex subscription

client.subscribe({
  $any: {
    $keys: keys => keys.slice(0, 5),
    title: true
  }
}, (target, type) => {
  // fires updates when any field updates a title but only the first 5
  console.log('update!', target, type)
})

A complex subscription with sort

client.subscribe({
  $any: {
    $keys: (keys, state) => keys.sort((a, b) =>
      // get allows you to get a field that does not exist yet
      state.get([ a, 'count' ], 0).compute() >
      state.get([ b, 'count' ], 0).compute()
    ).slice(0, 5),
    title: true
  }
}, (target, type) => {
  // fires updates when any field updates a title but only the first 5 sorted by count
  console.log('update!', target, type)
})

Switch

Switches are probably the most powerful concept in supported in the subscription model, allowing you to branch subscriptions based on certain conditions

client.subscribe({
  $any: {
    kind: {
      $switch: state => {
        if (state.compute() === 'dog') {
          return {
            diet: true
          }
        } else {
          title: true
        }
      }
    }
  }
}, (target, type) => {
  // fires updates on diet when it finds a dog else fires updates for title
  console.log('update!', target, type)
})
0.3.19

7 years ago

0.3.18

7 years ago

0.3.17

7 years ago

0.3.16

7 years ago

0.3.15

7 years ago

0.3.14

7 years ago

0.3.13

7 years ago

0.3.12

7 years ago

0.3.11

7 years ago

0.3.10

7 years ago

0.3.9

7 years ago

0.3.8

7 years ago

0.3.7

7 years ago

0.3.6

7 years ago

0.3.4

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.1-try1

7 years ago

0.2.27

7 years ago

0.3.0

7 years ago

0.2.26

7 years ago

0.2.25

7 years ago

0.2.24

7 years ago

0.2.23

7 years ago

0.2.22

7 years ago

0.2.21

7 years ago

0.2.20

7 years ago

0.2.19

7 years ago

0.2.18

7 years ago

0.2.17

7 years ago

0.2.16

7 years ago

0.2.15

7 years ago

0.2.14

7 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.0.30

7 years ago

0.0.29

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.26

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago