Skip to content

Commit

Permalink
Deprecating 'unsubscribe' in favor of 'off' in JS API
Browse files Browse the repository at this point in the history
An extension of a community contribution PR: #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 <[email protected]>
Co-authored-by: Zachary Plata <[email protected]>
  • Loading branch information
3 people committed Jul 19, 2023
1 parent 4240298 commit b9cb46a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d1f8710f5bfda934cbbb20727fe7ca2d88a15ff8
ce55339021039879e0ecd1a29ac70a2cef26ff2a
31 changes: 28 additions & 3 deletions js/src/rive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions js/test/rive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
});
Expand Down

0 comments on commit b9cb46a

Please sign in to comment.