regorxxx / Callbacks-Framework-SMP

A helper script for Spider Monkey Panel and foobar2000 which allows to easily attach and manage event listeners associated to SMP callbacks. A lifesaver.

Home Page:https://regorxxx.github.io/foobar2000-Framework-SMP.github.io/scripts/callbacks-framework-smp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Callbacks-Framework-SMP

version CodeFactor CodacyBadge GitHub
A helper script for Spider Monkey Panel and foobar2000 which allows to easily attach and manage event listeners associated to SMP callbacks without wrapping or repeating code. A lifesaver.

The problem with current SMP callbacks

Complex scripts, like those with UI, usually have interactive items at multiple places which must be linked to a callback to perform actions at certain events. While SMP allows modularization using include(), callbacks must be placed within a main file since redefinition of the function simply overwrites the previous one. That clearly limits the ability to just mix modules and or add event listeners to non-final scripts. carbon(5) carbon(6)

Using this framework it would translate into this:

carbon(7)

Features

  • Adds event listeners to SMP callbacks.
  • Removes event listeners on demand.
  • Allows to find if an event listener is active.
  • Listeners are able to self-remove.
  • Mimics mozilla implementation.
  • Implements SPM planned design.
  • Bypasses common limitation of including files with existing callbacks, wrapping, etc.

Usage

Just add an event listener to an specific event/callback.

const listener = () => {console.log('Hello world!');}
addEventListener('on_mouse_lbtn_up', listener);

That's all. To remove it just do:

removeEventListener('on_mouse_lbtn_up', listener);

Multiple listeners can be attached to the same event:

addEventListener('on_mouse_lbtn_up', () => {console.log('1!');});
addEventListener('on_mouse_lbtn_up', () => {console.log('2!');});
addEventListener('on_mouse_lbtn_up', () => {console.log('3!');});
addEventListener('on_mouse_lbtn_up', () => {console.log('14!');});

Listener removal may be done using their UUID or function:

const listener = () => {console.log('Hello world!');}
const uuid = addEventListener('on_mouse_lbtn_up', listener);
removeEventListener('on_mouse_lbtn_up', null, uuid);

There are more usage examples on the 'examples' folder.

Installation

Since the framework only requires 1 file, i.e. the main one, you can simply include it along any other script where you usually need a callback.

carbon(1)

About

A helper script for Spider Monkey Panel and foobar2000 which allows to easily attach and manage event listeners associated to SMP callbacks. A lifesaver.

https://regorxxx.github.io/foobar2000-Framework-SMP.github.io/scripts/callbacks-framework-smp/

License:GNU Affero General Public License v3.0


Languages

Language:JavaScript 100.0%