Skip to content

An event system for miniSphere influenced by Node.js's EventEmitter class

License

Notifications You must be signed in to change notification settings

Eggbertx/eventemitter-sphere

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventEmitter-sphere

An event system influenced by Node.js's EventEmitter class

Basic usage

// main.js
import { Thread } from 'sphere-runtime';
import { EventEmitter } from 'events';

const maxListeners = 32;
const escOnlyOnce = true;
let ei = new EventEmitter(maxListeners);

export default class Game extends Thread {
	constructor() {
		super();
		ei.addListener("escEvent", () => {
			Sphere.shutDown();
		}, escOnlyOnce);
	}

	on_update() {
		if(kb.isPressed(Key.Escape)) ei.emit("escEvent");
	}
}

This creates an event that is emitted in on_update when the escape key is pressed

The EventEmitter#addListener function also takes an optional parameter that emits the listener when it equals true or is a function that returns true.

Example:

ei.addListener("escEvent", Sphere.shutDown, escOnlyOnce, () => {
	return kb.isPressed(Key.Escape);
});

One-time events

Rather than an EventEmitter#once method like node.js's EventEmitter, this implementation takes a boolean variable (escOnlyOnce in the above examples) and the event is disposed of after execution if it is true.

About

An event system for miniSphere influenced by Node.js's EventEmitter class

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published