Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "litecanvas",
"version": "0.103.7",
"version": "0.200.0",
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
"license": "MIT",
"author": "Luiz Bills <luizbills@pm.me>",
Expand Down Expand Up @@ -32,7 +32,7 @@
"creative coding"
],
"devDependencies": {
"@happy-dom/global-registrator": "^20.8.3",
"@happy-dom/global-registrator": "^20.8.4",
"@size-limit/preset-small-lib": "^12.0.1",
"@swc/core": "^1.15.18",
"ava": "^7.0.0",
Expand Down
11 changes: 6 additions & 5 deletions samples/plugin-basics/plugin-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ function pluginTest(engine, config) {
console.log(`Tap detected in X=${x} Y=${y}`)
})

// the `listen()` returns a function that when called removes that listener
// example: call this listencer once and then remove it
let deleteListener = engine.listen('update', () => {
engine.listen('update', updateOnce)
function updateOnce() {
console.log('JUST ONE TIME!')
deleteListener()
})

// the `unlisten()` removes a event listener
engine.unlisten('update', updateOnce)
}

// use `def()` to create or update that instance properties
// example: create a variable named FOO
Expand Down
27 changes: 23 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,9 +1079,8 @@ export default function litecanvas(settings = {}) {
*
* @param {string} eventName the event type name
* @param {Function} callback the function that is called when the event occurs
* @returns {Function} a function to remove the listener
*/
listen(eventName, callback) {
listen: (eventName, callback) => {
DEV: assert(
'string' === typeof eventName,
loggerPrefix + 'listen() 1st param must be a string'
Expand All @@ -1095,9 +1094,29 @@ export default function litecanvas(settings = {}) {

_eventListeners[eventName] = _eventListeners[eventName] || new Set()
_eventListeners[eventName].add(callback)
},

/**
* Remove a game event listener
*
* @param {string} eventName the event type name
* @param {Function} callback the function that is called when the event occurs
*/
unlisten: (eventName, callback) => {
DEV: assert(
'string' === typeof eventName,
loggerPrefix + 'unlisten() 1st param must be a string'
)
DEV: assert(
'function' === typeof callback,
loggerPrefix + 'unlisten() 2nd param must be a function'
)

eventName = lowerCase(eventName)

// return a function to remove this event listener
return () => _eventListeners[eventName]?.delete(callback)
if (_eventListeners[eventName]) {
_eventListeners[eventName].delete(callback)
}
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/_preload/happy-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { GlobalRegistrator } from '@happy-dom/global-registrator'
*/
global.onLitecanvas = function (instance, event, callback) {
return new Promise((resolve, reject) => {
const removeListener = instance.listen(event, (...args) => {
instance.listen(event, (...args) => {
try {
const res = callback(...args)
if (false !== res) {
removeListener()
instance.unlisten(event, callback)
resolve()
}
} catch (err) {
Expand Down
10 changes: 8 additions & 2 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,15 @@ declare global {
*
* @param event The game event type
* @param callback the function that is called when the event occurs
* @returns a function to remove the listener
*/
function listen(event: string, callback: Function): Function
function listen(event: string, callback: Function): void
/**
* Remove a game loop event listener
*
* @param event The game event type
* @param callback the function that is called when the event occurs
*/
function unlisten(event: string, callback: Function): void
/**
* Call all listeners attached to a game event
*
Expand Down
10 changes: 8 additions & 2 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,15 @@ type LitecanvasInstance = {
*
* @param event The game event type
* @param callback the function that is called when the event occurs
* @returns a function to remove the listener
*/
listen(event: string, callback: Function): Function
listen(event: string, callback: Function): void
/**
* Remove a game loop event listener
*
* @param event The game event type
* @param callback the function that is called when the event occurs
*/
unlisten(event: string, callback: Function): void
/**
* Call all listeners attached to a game event
*
Expand Down
Loading