Skip to content

Commit cd7404b

Browse files
authored
attempt a fix for issue #97 (#98)
1 parent 0120960 commit cd7404b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Analytics-CSharp/Segment/Analytics/Analytics.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ public virtual void Reset()
191191
{
192192
Settings? returnSettings = null;
193193
IState system = await Store.CurrentState<System>();
194+
195+
// I don't understand this as Store.CurrentState will at worst return a default(TState) which will be a default(System) in this case, hence this cast will always go through?
196+
// System is a struct, so the default is an empty instance, not null
197+
// might as well write /* if (true) */
194198
if (system is System convertedSystem)
195199
{
196200
returnSettings = convertedSystem._settings;

Analytics-CSharp/Segment/Analytics/Timeline.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,18 @@ internal void Add(Plugin plugin)
161161
analytics.AnalyticsScope.Launch(analytics.AnalyticsDispatcher, async () =>
162162
{
163163
Settings? settings = await plugin.Analytics.SettingsAsync();
164+
165+
// This is basically never null, look at the comment in SettingsAsync
166+
if (settings == null)
167+
{
168+
return;
169+
}
170+
171+
// Fetch system afterwards for a minuscule but cool performance gain
164172
System system = await analytics.Store.CurrentState<System>();
165-
// Don't initialize unless we have updated settings from the web.
166-
// CheckSettings will initialize everything added before then, so wait until other inits have happened.
167-
if (settings.HasValue && system._initializedPlugins.Count > 0)
173+
174+
// Check for nullability because CurrentState returns default(IState) which could make the .Count throw a NullReferenceException
175+
if (system._initializedPlugins != null && system._initializedPlugins.Count > 0)
168176
{
169177
await analytics.Store.Dispatch<System.AddInitializedPluginAction, System>(new System.AddInitializedPluginAction(new HashSet<int>{plugin.GetHashCode()}));
170178
plugin.Update(settings.Value, UpdateType.Initial);

0 commit comments

Comments
 (0)