obseriot

WebJar for obseriot

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

obseriot
Last Version

Last Version

0.3.9
Release Date

Release Date

Type

Type

jar
Description

Description

obseriot
WebJar for obseriot
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/obseriot/obseriot

Download obseriot

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/obseriot/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>obseriot</artifactId>
    <version>0.3.9</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/obseriot/
implementation 'org.webjars.npm:obseriot:0.3.9'
// https://jarcasting.com/artifacts/org.webjars.npm/obseriot/
implementation ("org.webjars.npm:obseriot:0.3.9")
'org.webjars.npm:obseriot:jar:0.3.9'
<dependency org="org.webjars.npm" name="obseriot" rev="0.3.9">
  <artifact name="obseriot" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='obseriot', version='0.3.9')
)
libraryDependencies += "org.webjars.npm" % "obseriot" % "0.3.9"
[org.webjars.npm/obseriot "0.3.9"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : riot-observable jar [3.0.0]

Project Modules

There are no modules declared in this project.

Simple observer pattern

Install

npm install obseriot

Usage

Define event

Object with a handler.name and handler.action .

  • handler.name => Event name
  • handler.action => Return the parameters to be provided to the listener
    • Array is provided as a variadic argument
    • Can be provide a variety of types. String, Object, Function, whatever
const urlChange = {
    handler: {
        name: 'url_change',
        action ( collection, id, action ) {
            // Some processing and formatting
            return [ collection, id, action ]
        }
    }
}

Listen and Notify

// obseriot.listen( event object , callback function )
obseriot.listen( urlChange, ( ...arg ) => {
    console.log( arg ) // => 'shop', 1, 'detail'
} )

// obseriot.notify( event object , parameters )
obseriot.notify( urlChange, 'shop', 1, 'detail' )

One time listener

// obseriot.once( event object , callback function )
obseriot.once( urlChange, ( ...arg ) => {
    console.log( arg )
} )

Remove listeners

Remove all registered listeners.

// obseriot.remove( event object )
obseriot.remove( urlChange )

Remove one registered listener.

// obseriot.remove( event object, callback function )
const callback = ( ...arg ) => {
    console.log( arg )
}
obseriot.listen( urlChange, callback ) // Listen to the named function.
obseriot.remove( urlChange, callback ) // Remove!

How to use like Flux

Define Action

export const increment = {
    handler: {
        name: 'action_increment',
        action ( num = 1 ) {
            return [ num ]
        }
    }
}

Define Store

import {increment} from './action/increment'
import obseriot from 'obseriot'

export const count = {
    state: 0,
    handler: {
        name: 'store_count',
        action () {
            return [ count.state ]
        }
    }
}

obseriot.listen( increment, num => {
    count.state = count.state + num
    obseriot.notify( count )
} )

Your Component

import {increment} from './action/increment'
import {count} from './store/count'
import obseriot from 'obseriot'

// Action in somewhere components
obseriot.notify( increment, 1 )

// Listen to Store update
obseriot.listen( count, newCount => {
    console.log( newCount ) // => 1
} )

Versions

Version
0.3.9