Skip to content

Commit 35b586f

Browse files
authored
[HEAP-43104] Send a foregrounded notification to the SDK on startRecording (#101)
1 parent 92f1583 commit 35b586f

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

Development/Sources/HeapSwiftCore/Implementations/EventConsumer/EventConsumer.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,27 @@ class EventConsumer<StateStore: StateStoreProtocol, DataStore: DataStoreProtocol
113113
}
114114
}
115115
}
116+
117+
if updateResults.outcomes.currentStarted {
118+
// Switch to the main thread so we can check Event.AppVisibility.current.
119+
onMainThread {
120+
121+
if Event.AppVisibility.current == .foregrounded {
122+
123+
for (sourceName, source) in snapshot.sources {
124+
source.applicationDidEnterForeground(timestamp: timestamp) {
125+
HeapLogger.shared.trace("Source [\(sourceName)] has completed all work related to session initialization.")
126+
}
127+
}
128+
129+
for bridge in snapshot.runtimeBridges {
130+
bridge.applicationDidEnterForeground(timestamp: timestamp) {
131+
HeapLogger.shared.trace("Bridge of type [\(type(of: bridge))] has completed all work related to session initialization.")
132+
}
133+
}
134+
}
135+
}
136+
}
116137
}
117138
}
118139

Development/Tests/HeapSwiftCoreTests/EventConsumer/StartRecordingSpec.swift

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ final class EventConsumer_StartRecordingSpec: HeapSpec {
186186
expect(dataStore.usersToUpload()).to(haveCount(1), description: "Data store should have purged three users and created one more")
187187
}
188188

189-
context("with a source and bridge added when called with startSessionImmediately") {
189+
context("with a source and bridge added") {
190190

191191
var bridge: CountingRuntimeBridge!
192192
var source: CountingSource!
@@ -198,54 +198,109 @@ final class EventConsumer_StartRecordingSpec: HeapSpec {
198198
consumer.addRuntimeBridge(bridge)
199199
consumer.addSource(source, isDefault: false)
200200

201-
consumer.startRecording("11", with: [ .startSessionImmediately: true ])
201+
consumer.startRecording("11")
202202
}
203203

204-
it("calls .didStartRecording and .sessionDidStart on sources and bridges") {
204+
it("calls .didStartRecording on sources and bridges") {
205205

206206
expect(bridge.calls).to(equal([
207207
.didStartRecording,
208-
.sessionDidStart,
209-
]))
208+
] + foregroundEventIfForegrounded()))
210209

211210
expect(source.calls).to(equal([
212211
.didStartRecording,
213-
.sessionDidStart,
214-
]))
212+
] + foregroundEventIfForegrounded()))
215213
}
216214

217215
it("calls .didStopRecording on sources and bridges") {
218216

217+
bridge.calls.removeAll()
218+
source.calls.removeAll()
219219
consumer.stopRecording()
220220

221221
expect(bridge.calls).to(equal([
222-
.didStartRecording,
223-
.sessionDidStart,
224222
.didStopRecording
225223
]))
226224

227225
expect(source.calls).to(equal([
228-
.didStartRecording,
229-
.sessionDidStart,
230226
.didStopRecording
231227
]))
232228
}
233229

234230
it("does not call .didStopRecording on sources and bridges when called twice") {
235231

232+
bridge.calls.removeAll()
233+
source.calls.removeAll()
236234
consumer.startRecording("12")
237235

236+
expect(bridge.calls).to(equal([
237+
.didStartRecording,
238+
] + foregroundEventIfForegrounded()))
239+
240+
expect(source.calls).to(equal([
241+
.didStartRecording,
242+
] + foregroundEventIfForegrounded()))
243+
}
244+
}
245+
246+
context("with a source and bridge added, called with startSessionImmediately") {
247+
248+
var bridge: CountingRuntimeBridge!
249+
var source: CountingSource!
250+
251+
beforeEach {
252+
bridge = CountingRuntimeBridge()
253+
source = CountingSource(name: "A", version: "1")
254+
255+
consumer.addRuntimeBridge(bridge)
256+
consumer.addSource(source, isDefault: false)
257+
258+
consumer.startRecording("11", with: [ .startSessionImmediately: true ])
259+
}
260+
261+
it("calls .didStartRecording and .sessionDidStart on sources and bridges") {
262+
238263
expect(bridge.calls).to(equal([
239264
.didStartRecording,
240265
.sessionDidStart,
266+
] + foregroundEventIfForegrounded()))
267+
268+
expect(source.calls).to(equal([
241269
.didStartRecording,
270+
.sessionDidStart,
271+
] + foregroundEventIfForegrounded()))
272+
}
273+
274+
it("calls .didStopRecording on sources and bridges") {
275+
276+
bridge.calls.removeAll()
277+
source.calls.removeAll()
278+
consumer.stopRecording()
279+
280+
expect(bridge.calls).to(equal([
281+
.didStopRecording
242282
]))
243283

244284
expect(source.calls).to(equal([
285+
.didStopRecording
286+
]))
287+
}
288+
289+
it("does not call .didStopRecording on sources and bridges when called twice") {
290+
291+
bridge.calls.removeAll()
292+
source.calls.removeAll()
293+
consumer.startRecording("12", with: [ .startSessionImmediately: true ])
294+
295+
expect(bridge.calls).to(equal([
245296
.didStartRecording,
246297
.sessionDidStart,
298+
] + foregroundEventIfForegrounded()))
299+
300+
expect(source.calls).to(equal([
247301
.didStartRecording,
248-
]))
302+
.sessionDidStart,
303+
] + foregroundEventIfForegrounded()))
249304
}
250305
}
251306

0 commit comments

Comments
 (0)