Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

register -> unregister lifecycle #368

Closed
@fredgalvao

Description

I'm updating my project from 1.2.3 to 1.4.4, and I realized that we can do better than the .off() functions.

I just confirmed that the following end up duplicating all listeners on the _handlers object:

class PushNotificationService {
    api;
    constructor() {
        this.api = PushNotification.init({...});
        this.register();
    }

    register() {
        this.api.on('registration', () => {...});
        this.api.on('notification', () => {...});
        this.api.on('error', () => {...});

        something.register.at.my.back.end();
    }

    unregister() {
        this.api.unregister(...);

        something.unregister.at.my.back.end();
    }
}
class PushNotificationController {
    isItOn: boolean = true;
    toggle() {
        this.isItOn = !this.isItOn;

        if (this.isItOn) {
            pushNotificationService.register();
        } else {
            pushNotificationService.unregister();
        }
    }
}
<button ng-click='toggle()'>Do it</button>

The way I'm doing it at the moment, I don't .off() the listeners (simply because I didn't have these functions up to now, 1.2.3 didn't have them), so when I toggle twice, it'll end up being registered but with 2 listeners for each event type. And then I wondered, shouldn't .unregister() automatically .off() all listeners? Either way, we should/could improve the docs on this so that a suggested way to do it exists. I'd say "toggle push notification reception" is a pretty common feature to have in apps.

Options

Automatically:

PushNotification.prototype.unregister = function(successCallback, errorCallback, options) {
    if (errorCallback == null) { errorCallback = function() {}}

    if (typeof errorCallback != "function")  {
        console.log("PushNotification.unregister failure: failure parameter not a function");
        return
    }

    if (typeof successCallback != "function") {
        console.log("PushNotification.unregister failure: success callback parameter must be a function");
        return
    }

    var cleanHandlersAndPassThrough = function() {
        this._handlers = {
            'registration': [],
            'notification': [],
            'error': []
        };

        successCallback();
    }

    exec(cleanHandlersAndPassThrough, errorCallback, "PushNotification", "unregister", [options]);
};

Suggest user to deal with it:

README.md <

If you want to unregister, make sure you clean up your handlers using `.off()` on all events.

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions