isottope

package module
v0.0.0-...-588a397 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2016 License: MIT Imports: 3 Imported by: 0

README

isottope

Is an Event Loop for otto JavaScript engine, which itself is written in pure Go(ld).

Three JavaScript functions added; subscribe, unsubscribe and emit. Events can get triggered from inside both JavaScript and Go.

Not used heavily yet (just in one side project of mine), so any bug report is most appreciated.

From examples directory; you can define your own events and fire them, like this:

func customEvent(vm *otto.Otto, loop *isottope.EventLoop) {
	loop.RegisterEvent(isottope.SimpleEvent(`TEST_OK`), true)

	vm.Run(`
		function target() {
			var args = ['input args:'];
			if (arguments.length > 0) {
				for (i = 0; i < arguments.length; i++) {
					args.push(arguments[i]);
				}
			}
			var msg = args.join(' ');
			console.log(msg);
		}

		subscribe('TEST_OK', target, 'DEFAULT-ARG-1', 'DEFAULT-ARG-2');

		emit('TEST_OK', "Custome Arg");	
		emit('TEST_OK');	
	`)

	loop.Emit(`TEST_OK`, `Another Custome Arg`)
	loop.Emit(`TEST_OK`)
}

Using timers is also possible; for example:

func timer(vm *otto.Otto) {
	vm.Run(`
		var R = {};
		var c = 0;
		function target() {
			c++;
			console.log('registrar #' + c);
			if (c < 5) {
				return;
			}
			clearTimeout(R);
		}

		try {
			R = setInterval(target, 200);
		}
		catch(e) {
			console.error(e);
		}		
	`)
}

It's possible to implement more complicated event patterns by implementing isottope.Event interface - the timer itself is implemented using isottope.Event interface.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

Error constant error based on http://dave.cheney.net/2016/04/07/constant-errors

func (Error) Error

func (e Error) Error() string

type Event

type Event interface {
	GetID() string
	Pre(bool) func(...interface{}) []interface{}
	Post(bool) func(...interface{}) bool
}

Event is a protocol that an event context must follow

type EventLoop

type EventLoop struct {
	// contains filtered or unexported fields
}

EventLoop creates an event loop for otto

func Init

func Init(vm *otto.Otto) *EventLoop

Init creates & initializes a new EventLoop; it starts a go routine TODO: stop it

func (*EventLoop) Emit

func (loop *EventLoop) Emit(name string, arguments ...interface{})

Emit emits an event

func (*EventLoop) RegisterEvent

func (loop *EventLoop) RegisterEvent(e Event, ensure bool)

RegisterEvent registers an event

type SimpleEvent

type SimpleEvent string

SimpleEvent is a default implementation of Event, with no Pre or Post functionality, serves as a normal event

func (SimpleEvent) GetID

func (e SimpleEvent) GetID() string

GetID implements Event

func (SimpleEvent) Post

func (e SimpleEvent) Post(bool) func(...interface{}) bool

Post implements Event

func (SimpleEvent) Pre

func (e SimpleEvent) Pre(bool) func(...interface{}) []interface{}

Pre implements Event

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL