Skip to content

Commit

Permalink
Fixing a possible null pointer exception and removing unnecessary nul…
Browse files Browse the repository at this point in the history
…lability for settings
  • Loading branch information
MichaelGHSeg committed Apr 22, 2024
1 parent 0120960 commit a10d3be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
16 changes: 5 additions & 11 deletions Analytics-CSharp/Segment/Analytics/Analytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ public virtual void Reset()
/// it's not recommended to be used in async method.
/// </summary>
/// <returns>Instance of <see cref="Settings"/></returns>
public virtual Settings? Settings()
public virtual Settings Settings()
{
Task<Settings?> task = SettingsAsync();
Task<Settings> task = SettingsAsync();
task.Wait();
return task.Result;
}
Expand All @@ -187,16 +187,10 @@ public virtual void Reset()
/// Retrieve the settings.
/// </summary>
/// <returns>Instance of <see cref="Settings"/></returns>
public virtual async Task<Settings?> SettingsAsync()
public virtual async Task<Settings> SettingsAsync()
{
Settings? returnSettings = null;
IState system = await Store.CurrentState<System>();
if (system is System convertedSystem)
{
returnSettings = convertedSystem._settings;
}

return returnSettings;
System system = await Store.CurrentState<System>();
return system._settings;
}

#endregion
Expand Down
10 changes: 7 additions & 3 deletions Analytics-CSharp/Segment/Analytics/Timeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,22 @@ internal void Add(Plugin plugin)
Analytics analytics = plugin.Analytics;
analytics.AnalyticsScope.Launch(analytics.AnalyticsDispatcher, async () =>
{
Settings? settings = await plugin.Analytics.SettingsAsync();
Settings settings = await plugin.Analytics.SettingsAsync();
// Fetch system afterwards for a minuscule but cool performance gain
System system = await analytics.Store.CurrentState<System>();
// Don't initialize unless we have updated settings from the web.
// CheckSettings will initialize everything added before then, so wait until other inits have happened.
if (settings.HasValue && system._initializedPlugins.Count > 0)
// Check for nullability because CurrentState returns default(IState) which could make the .Count throw a NullReferenceException
if (system._initializedPlugins != null && system._initializedPlugins.Count > 0)
{
await analytics.Store.Dispatch<System.AddInitializedPluginAction, System>(new System.AddInitializedPluginAction(new HashSet<int>{plugin.GetHashCode()}));
plugin.Update(settings.Value, UpdateType.Initial);
plugin.Update(settings, UpdateType.Initial);
}
});
}


internal void Remove(Plugin plugin) => _plugins.RemoveAll(tempPlugin => tempPlugin == plugin);

internal RawEvent Execute(RawEvent incomingEvent)
Expand Down

0 comments on commit a10d3be

Please sign in to comment.