Skip to content

Commit 057f872

Browse files
authored
Fixing a possible null pointer exception and removing unnecessary nullability for settings (#99)
1 parent cd7404b commit 057f872

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

Analytics-CSharp/Segment/Analytics/Analytics.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ public virtual void Reset()
176176
/// it's not recommended to be used in async method.
177177
/// </summary>
178178
/// <returns>Instance of <see cref="Settings"/></returns>
179-
public virtual Settings? Settings()
179+
public virtual Settings Settings()
180180
{
181-
Task<Settings?> task = SettingsAsync();
181+
Task<Settings> task = SettingsAsync();
182182
task.Wait();
183183
return task.Result;
184184
}
@@ -187,20 +187,10 @@ public virtual void Reset()
187187
/// Retrieve the settings.
188188
/// </summary>
189189
/// <returns>Instance of <see cref="Settings"/></returns>
190-
public virtual async Task<Settings?> SettingsAsync()
190+
public virtual async Task<Settings> SettingsAsync()
191191
{
192-
Settings? returnSettings = null;
193-
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) */
198-
if (system is System convertedSystem)
199-
{
200-
returnSettings = convertedSystem._settings;
201-
}
202-
203-
return returnSettings;
192+
System system = await Store.CurrentState<System>();
193+
return system._settings;
204194
}
205195

206196
#endregion

Analytics-CSharp/Segment/Analytics/Timeline.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,22 @@ internal void Add(Plugin plugin)
160160
Analytics analytics = plugin.Analytics;
161161
analytics.AnalyticsScope.Launch(analytics.AnalyticsDispatcher, async () =>
162162
{
163-
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-
163+
Settings settings = await plugin.Analytics.SettingsAsync();
171164
// Fetch system afterwards for a minuscule but cool performance gain
172165
System system = await analytics.Store.CurrentState<System>();
173166

167+
// Don't initialize unless we have updated settings from the web.
168+
// CheckSettings will initialize everything added before then, so wait until other inits have happened.
174169
// Check for nullability because CurrentState returns default(IState) which could make the .Count throw a NullReferenceException
175170
if (system._initializedPlugins != null && system._initializedPlugins.Count > 0)
176171
{
177172
await analytics.Store.Dispatch<System.AddInitializedPluginAction, System>(new System.AddInitializedPluginAction(new HashSet<int>{plugin.GetHashCode()}));
178-
plugin.Update(settings.Value, UpdateType.Initial);
173+
plugin.Update(settings, UpdateType.Initial);
179174
}
180175
});
181176
}
182177

178+
183179
internal void Remove(Plugin plugin) => _plugins.RemoveAll(tempPlugin => tempPlugin == plugin);
184180

185181
internal RawEvent Execute(RawEvent incomingEvent)

0 commit comments

Comments
 (0)