npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leopold

v0.2.0

Published

Event-sourced state support

Downloads

13

Readme

leopold

 __^___
[(0¿0)]
   ~-

Event-sourced models for nodejs or browser

GitHub stars Build Status

Install

npm install --save leopold

Quick Start

Eventable models


import leopold from 'leopold'
import stampit from 'stampit'

// create a leo that includes a unit of work and factory for event providers
const leo = leopold()

const accountSpec = stampit()
    .methods({
        // by convention, event handlers are named '$' + '${event.name}'
        // async handlers are supported
        $initialized: function(e) {
            this.id(e.id)
            this.name = e.name
            this.balance = e.balance
        }
        , $deposited: function(e) {
            this.balance = e.balance
        }
        , initialize: function(name) {
            //returns a promise
            return this.raise({
                event: 'initialized'
                , id: 'checking'
                , balance: 0
                , name: name
            })
        }
        , deposit: function(amount) {
            return this.raise({
                event: 'deposited'
                , balance: (this.balance + amount)
            })
        }
    })
    .compose(leo.eventable())

const customerSpec = stampit()
    .methods({
        $initialized:  function(e) {
            this.id(e.id)
            this.name = e.name
            this.accounts = {}
        }
        , $accountApproved: function(e) {
            //use special _id attribute to initialize new account
            return account()
                .initialize(e.accountId, e.accountName)
                .bind(this)
                .then(function(newAccount) {
                    this.accounts[e.accountName] = newAccount
                })
        }
        , initialize: function(name) {
            return this.raise({
                event: 'initialized'
                , name: name
                , id: 42
            })
        }
        , approveAccount: function(accountName) {
            let acctId = cuid()
            return this.raise({
                event: 'accountApproved'
                , accountName: accountName
                , accountId: acctId
            })
            .bind(this)
            .then(function(){
                return this.accounts[acctId].initialize()
            })
        }
        , makeDeposit: function(account, amount) {
            return this.accounts[account].deposit(amount)
        }
    })


//use 
let customer = customerSpec.create()
customer.approveAccount('checking') // -> customer.accounts['checking']
customer.makeDeposit('checking',300.42) // -> account.balance === 300.42

Notice we have a object graph that is two deep.

Now let's consume a set of events to restore state to where this was


let instance =  customerSpec.create({_id: 1})
let events = [
    { event: 'initialized', id: 1, name: 'mike' }
    , { event: 'accountApproved', id: 1, name: 'checking' }
    , { event: 'initialized', id: 'checking', name: 'checking', balance: 0 }
    , { event: 'deposited', id: 'checking', balance: 300.42}
]
let envelope = {
    revision: 1
    , events: events
}

// events are stored as envelopes
return leo.mount(envelope)
    .then(function(){
        return leo.restore(instance, 0 , Number.MAX_VALUE)
    })

// instance.accounts[{cuid}].balance === 300.42

Taking advantage of events for testing side effects


import leopold from 'leopold'
import stampit from 'stampit'

// create a leo that includes a unit of work and factory for event providers
const envelopes = []
const storage = {
    store(env) { envelopes.push(env )}
}
const leo = leopold({
    //this commits events right away
    atomic: false
    , storage
})

const myModel = stampit()
    .methods({
        // by convention, event handlers are named '$' + '${event.name}'
        // async handlers are supported
        $initialized: function(e) {
            //do stuff
        }
        , initialize: function() {
            return this.raise({ event: ‘initialized’})
        }
    })
    .compose(leo.eventable())

myModel.initialize()
envelopes.length === 1 // true
envelopes[0].events[0].event === ‘initialized’ // true

Dependencies

leopold uses stampit under the hood and the eventable call presumes you are composing event source behavior into an prototype ('spec').

leopold is also using some ES6 features that require babel.

Running tests

make test (nodejs) make browser (browser) then visit on any browser at http://localhost:2222