From b9cb46a6580f6f84457e9f8caf003851b9658dd7 Mon Sep 17 00:00:00 2001 From: zplata Date: Wed, 19 Jul 2023 14:16:34 +0000 Subject: [PATCH] Deprecating 'unsubscribe' in favor of 'off' in JS API An extension of a community contribution PR: https://github.com/rive-app/rive-wasm/pull/328 to use consistent API naming. Before: `on` and `unsubscribe` for things like onPlay, onStateChange, etc. After: `on` and `off` Had a weird issue where the `.patch` did not apply - and when resolved, still had to make other file changes separately when `git am` finished, thus the additional commits here. Diffs= ce5533902 Deprecating 'unsubscribe' in favor of 'off' in JS API (#5538) Co-authored-by: Linden Co-authored-by: Zachary Plata --- .rive_head | 2 +- js/src/rive.ts | 31 ++++++++++++++++++++++++++++--- js/test/rive.test.ts | 6 +++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/.rive_head b/.rive_head index e85ba01a..6fe3f5a2 100644 --- a/.rive_head +++ b/.rive_head @@ -1 +1 @@ -d1f8710f5bfda934cbbb20727fe7ca2d88a15ff8 +ce55339021039879e0ecd1a29ac70a2cef26ff2a diff --git a/js/src/rive.ts b/js/src/rive.ts index 22eb14e9..6974af30 100644 --- a/js/src/rive.ts +++ b/js/src/rive.ts @@ -1795,23 +1795,48 @@ export class Rive { /** * Unsubscribes from a Rive-generated event - * @param callback the callback to unsubscribe from + * @param type the type of event to unsubscribe from + * @param callback the callback to unsubscribe */ - public unsubscribe(type: EventType, callback: EventCallback) { + public off(type: EventType, callback: EventCallback) { this.eventManager.remove({ type: type, callback: callback, }); } + /** + * Unsubscribes from a Rive-generated event + * @deprecated + * @param callback the callback to unsubscribe from + */ + public unsubscribe(type: EventType, callback: EventCallback) { + console.warn("This function is deprecated: please use `off()` instead."); + this.off(type, callback); + } + + /** + * Unsubscribes all Rive listeners from an event type, or everything if no type is + * given + * @param type the type of event to unsubscribe from, or all types if + * undefined + */ + public removeAllRiveEventListeners(type?: EventType) { + this.eventManager.removeAll(type); + } + /** * Unsubscribes all listeners from an event type, or everything if no type is * given + * @deprecated * @param type the type of event to unsubscribe from, or all types if * undefined */ public unsubscribeAll(type?: EventType) { - this.eventManager.removeAll(type); + console.warn( + "This function is deprecated: please use `removeAllRiveEventListeners()` instead." + ); + this.removeAllRiveEventListeners(type); } /** diff --git a/js/test/rive.test.ts b/js/test/rive.test.ts index 1f2039c4..66243975 100644 --- a/js/test/rive.test.ts +++ b/js/test/rive.test.ts @@ -649,7 +649,7 @@ test("Events can be unsubscribed from", (done) => { onStop: stopCallback, onPlay: (event: rive.Event) => { // Deregister stop subscription - r.unsubscribe(rive.EventType.Stop, stopCallback); + r.off(rive.EventType.Stop, stopCallback); }, }); // Time out after 200 ms @@ -672,7 +672,7 @@ test("Events of a single type can be mass unsubscribed", (done) => { r.on(rive.EventType.Loop, loopCallback2); r.on(rive.EventType.Loop, loopCallback3); // Deregisters all loop subscriptions - r.unsubscribeAll(rive.EventType.Loop); + r.removeAllRiveEventListeners(rive.EventType.Loop); }, onStop: (event: rive.Event) => { // This should not hgave been removed @@ -702,7 +702,7 @@ test("All events can be mass unsubscribed", (done) => { r.on(rive.EventType.Loop, loopCallback3); r.on(rive.EventType.Stop, stopCallback2); // Deregisters all loop subscriptions - r.unsubscribeAll(); + r.removeAllRiveEventListeners(); }, onStop: stopCallback1, });