Skip to content

Commit

Permalink
Fix crashes from propertiesLoadedCallback being called multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
clayton-halim authored Sep 23, 2020
2 parents 75dd48d + a159c6e commit d478d73
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion EXPERIMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Taplytics.newSyncVariable("My Variable", "default").then(value => {
IMPORTANT: The value of these variables will be determined immediately, ie. the SDK will not wait for properties to be loaded from the server. Thus, if you want to ensure that the variables have their correct variables based on your experiment segmentation, you must initialize them after the properties have been loaded from the server. This module provides a callback to achieve this:

```javascript
Taplytics.propertiesLoadedCallback().then(() => {
Taplytics.propertiesLoadedCallback(loaded => {
// load variables here
});
```
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'taplytics-react-native' {
namespace Taplytics {
export interface TaplyticsUserAttributes extends object {
export interface TaplyticsUserAttributes extends Object {
email?: string;
name?: string;
age?: number;
Expand Down Expand Up @@ -53,7 +53,7 @@ declare module 'taplytics-react-native' {
* properties have been loaded from the server. This module provides a callback to
* achieve this:
*/
export function propertiesLoadedCallback(): Promise<void>;
export function propertiesLoadedCallback(callback: (loaded: boolean) => void): void;

/**
* Asynchronous variables take care of insuring that the experiments have been loaded
Expand Down
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import _ from 'lodash';

const { Taplytics } = NativeModules;

Taplytics.nativeEventEmitter = new NativeEventEmitter(Taplytics);

let variables = {}
let variableChangedListener = () => {}

Expand Down Expand Up @@ -94,7 +96,6 @@ Taplytics.setTaplyticsExperimentsUpdatedListener = (listener) => {
})
}


Taplytics.setTaplyticsNewSessionListener = (listener) => {
Taplytics._setTaplyticsNewSessionListener()

Expand Down Expand Up @@ -132,9 +133,16 @@ Taplytics.registerPushReceivedListener = (listener) => {
}
}

Taplytics.propertiesLoadedCallback = (callback) => {
Taplytics.nativeEventEmitter.addListener('propertiesLoadedCallback', (loaded) => {
callback(loaded)
})
Taplytics._propertiesLoadedCallback()
}

if (Platform.OS == 'ios') {
try {
new NativeEventEmitter(Taplytics).addListener("pushOpened", (event) => {
Taplytics.nativeEventEmitter.addListener("pushOpened", (event) => {
_.each(pushOpenedListeners, listener => _.isFunction(listener) && listener(event))
})
}
Expand All @@ -153,7 +161,7 @@ if (Platform.OS == 'ios') {

if (Platform.OS == 'ios') {
try {
new NativeEventEmitter(Taplytics).addListener("pushReceived", (event) => {
Taplytics.nativeEventEmitter.addListener("pushReceived", (event) => {
_.each(pushReceivedListeners, listener => _.isFunction(listener) && listener(event))
})
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions ios/RNTaplyticsReact.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ - (void)sendPushEvent:(id)name withData:(NSDictionary *)userInfo
}];
}

RCT_REMAP_METHOD(propertiesLoadedCallback, propertiesLoadedCallbackResolver:(RCTPromiseResolveBlock)resolve propertiesLoadedCallbackRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_METHOD(_propertiesLoadedCallback)
{
[Taplytics propertiesLoadedCallback:^(BOOL loaded) {
resolve(@(loaded));
[self sendEvent:@"propertiesLoadedCallback" withValue:@(loaded)];
}];
}

Expand Down

0 comments on commit d478d73

Please sign in to comment.