Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add User change event listener and getters for OneSignal Id and external Id #1158

Merged
merged 8 commits into from
Feb 27, 2024

Conversation

shepherd-l
Copy link
Contributor

@shepherd-l shepherd-l commented Feb 16, 2024

Description

1 Line Summary

Adds change event listener, onesignalId and externalId properties to OneSignal.User.

Details

The User change event listener has the same name as the PushSubscription change event listener and should not be added to the same emitter because it will lead to both events being fired twice.
Another emitter was added in the UserNamespace. All other namespaces still use the same static emitter from the OneSignal class.

The User emitter is static because there are two instances of UserNamespace where one is created on the property and again later when OneSignal initializes. When addEventListener is called by the dev, the event is being added before OneSignal initializes and to the property instance (new UserNamespace(false)). To keep the event on the new instance after initialization, the emitter is static.

OneSignal.ts
_initializeCoreModuleAndOSNamespace() {
  …
  OneSignal.User = new UserNamespace(true, subscription, permission); // initialize: boolean
}

static User = new UserNamespace(false); // initialize: boolean

Example:

Adding the user change event listener: https://github.com/OneSignal/OneSignal-Website-SDK/blob/main/express_webpack/index.html#L128

    OneSignalDeferred.push(function(onesignal) {
        onesignal.User.addEventListener('change', function (event) {
          showEventAlert('change', { event });
        });
    });

Adding the push subscription change event listener: https://github.com/OneSignal/OneSignal-Website-SDK/blob/main/express_webpack/index.html#L122

    OneSignalDeferred.push(function(onesignal) {
        onesignal.User.PushSubscription.addEventListener('change', function (event) {
          showEventAlert('change', { event });
        });
    });

Future considerations:

  • Change externalId to be a property on User
  • Add unit/integration tests
  • Add an emitter for each namespace

Systems Affected

  • WebSDK
  • Backend
  • Dashboard

Validation

Tests

Manually tested OneSignal.User.onesignalId, OneSignal.User.externalId, and User change event firing when calling login and logout on the OneSignal WebSDK Sandbox.

Info

Checklist

  • All the automated tests pass or I explained why that is not possible
  • I have personally tested this on my machine or explained why that is not possible
  • I have included test coverage for these changes or explained why they are not needed

Programming Checklist
Interfaces:

  • Don't use default export
  • New interfaces are in model files

Functions:

  • Don't use default export
  • All function signatures have return types
  • Helpers should not access any data but rather be given the data to operate on.

Typescript:

  • No Typescript warnings
  • Avoid silencing null/undefined warnings with the exclamation point

Other:

  • Iteration: refrain from using elem of array syntax. Prefer forEach or use map
  • Avoid using global OneSignal accessor for context if possible. Instead, we can pass it to function/constructor so that we don't call OneSignal.context

Screenshots

Info

Checklist

  • I have included screenshots/recordings of the intended results or explained why they are not needed

Related Tickets



This change is Reviewable

Fire when onesignalId or externalId changes
Add user change event listener, onesignalId and externalId properties
Add user change event listener, onesignalId and externalId properties
@shepherd-l shepherd-l force-pushed the feat/onesignalIdAndExternalId branch from cd08c92 to e3f6f55 Compare February 16, 2024 23:43
@shepherd-l shepherd-l force-pushed the feat/onesignalIdAndExternalId branch from e3f6f55 to c16e9bd Compare February 16, 2024 23:59
@shepherd-l shepherd-l changed the title [Feat] Add User change event listener and getters for OneSignal Id and external Id WIP: [Feat] Add User change event listener and getters for OneSignal Id and external Id Feb 17, 2024
Fixes "TypeError: Cannot read properties of undefined (reading 'toString')" when running the login test suite. Tests passed when ran individually without the fix.
@shepherd-l shepherd-l force-pushed the feat/onesignalIdAndExternalId branch from 9388e2c to cd3180e Compare February 20, 2024 18:14
@shepherd-l shepherd-l changed the title WIP: [Feat] Add User change event listener and getters for OneSignal Id and external Id [Feat] Add User change event listener and getters for OneSignal Id and external Id Feb 20, 2024
@shepherd-l shepherd-l merged commit e35a9b4 into main Feb 27, 2024
4 checks passed
@shepherd-l shepherd-l deleted the feat/onesignalIdAndExternalId branch February 27, 2024 17:02
@jkasten2 jkasten2 mentioned this pull request Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants