Skip to content

Commit 4949b10

Browse files
committed
v0.200
* add `unlisten()` * now `listen()` do not returns a function to remove the listener
1 parent 966cb6c commit 4949b10

File tree

8 files changed

+53
-21
lines changed

8 files changed

+53
-21
lines changed

bun.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "litecanvas",
3-
"version": "0.103.7",
3+
"version": "0.200.0",
44
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
55
"license": "MIT",
66
"author": "Luiz Bills <luizbills@pm.me>",
@@ -32,7 +32,7 @@
3232
"creative coding"
3333
],
3434
"devDependencies": {
35-
"@happy-dom/global-registrator": "^20.8.3",
35+
"@happy-dom/global-registrator": "^20.8.4",
3636
"@size-limit/preset-small-lib": "^12.0.1",
3737
"@swc/core": "^1.15.18",
3838
"ava": "^7.0.0",

samples/plugin-basics/plugin-basics.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ function pluginTest(engine, config) {
4444
console.log(`Tap detected in X=${x} Y=${y}`)
4545
})
4646

47-
// the `listen()` returns a function that when called removes that listener
48-
// example: call this listencer once and then remove it
49-
let deleteListener = engine.listen('update', () => {
47+
engine.listen('update', updateOnce)
48+
function updateOnce() {
5049
console.log('JUST ONE TIME!')
51-
deleteListener()
52-
})
50+
51+
// the `unlisten()` removes a event listener
52+
engine.unlisten('update', updateOnce)
53+
}
5354

5455
// use `def()` to create or update that instance properties
5556
// example: create a variable named FOO

src/index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,9 +1079,8 @@ export default function litecanvas(settings = {}) {
10791079
*
10801080
* @param {string} eventName the event type name
10811081
* @param {Function} callback the function that is called when the event occurs
1082-
* @returns {Function} a function to remove the listener
10831082
*/
1084-
listen(eventName, callback) {
1083+
listen: (eventName, callback) => {
10851084
DEV: assert(
10861085
'string' === typeof eventName,
10871086
loggerPrefix + 'listen() 1st param must be a string'
@@ -1095,9 +1094,29 @@ export default function litecanvas(settings = {}) {
10951094

10961095
_eventListeners[eventName] = _eventListeners[eventName] || new Set()
10971096
_eventListeners[eventName].add(callback)
1097+
},
1098+
1099+
/**
1100+
* Remove a game event listener
1101+
*
1102+
* @param {string} eventName the event type name
1103+
* @param {Function} callback the function that is called when the event occurs
1104+
*/
1105+
unlisten: (eventName, callback) => {
1106+
DEV: assert(
1107+
'string' === typeof eventName,
1108+
loggerPrefix + 'unlisten() 1st param must be a string'
1109+
)
1110+
DEV: assert(
1111+
'function' === typeof callback,
1112+
loggerPrefix + 'unlisten() 2nd param must be a function'
1113+
)
1114+
1115+
eventName = lowerCase(eventName)
10981116

1099-
// return a function to remove this event listener
1100-
return () => _eventListeners[eventName]?.delete(callback)
1117+
if (_eventListeners[eventName]) {
1118+
_eventListeners[eventName].delete(callback)
1119+
}
11011120
},
11021121

11031122
/**

src/version.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/_preload/happy-dom.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { GlobalRegistrator } from '@happy-dom/global-registrator'
88
*/
99
global.onLitecanvas = function (instance, event, callback) {
1010
return new Promise((resolve, reject) => {
11-
const removeListener = instance.listen(event, (...args) => {
11+
instance.listen(event, (...args) => {
1212
try {
1313
const res = callback(...args)
1414
if (false !== res) {
15-
removeListener()
15+
instance.unlisten(event, callback)
1616
resolve()
1717
}
1818
} catch (err) {

types/global.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,15 @@ declare global {
551551
*
552552
* @param event The game event type
553553
* @param callback the function that is called when the event occurs
554-
* @returns a function to remove the listener
555554
*/
556-
function listen(event: string, callback: Function): Function
555+
function listen(event: string, callback: Function): void
556+
/**
557+
* Remove a game loop event listener
558+
*
559+
* @param event The game event type
560+
* @param callback the function that is called when the event occurs
561+
*/
562+
function unlisten(event: string, callback: Function): void
557563
/**
558564
* Call all listeners attached to a game event
559565
*

types/types.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,15 @@ type LitecanvasInstance = {
535535
*
536536
* @param event The game event type
537537
* @param callback the function that is called when the event occurs
538-
* @returns a function to remove the listener
539538
*/
540-
listen(event: string, callback: Function): Function
539+
listen(event: string, callback: Function): void
540+
/**
541+
* Remove a game loop event listener
542+
*
543+
* @param event The game event type
544+
* @param callback the function that is called when the event occurs
545+
*/
546+
unlisten(event: string, callback: Function): void
541547
/**
542548
* Call all listeners attached to a game event
543549
*

0 commit comments

Comments
 (0)