Skip to content

Commit 42c2533

Browse files
maciejmakowski2003Maciej Makowski
andauthored
Feat/onended/added state to payload (#453)
* feat: added state to payload of onended event * ci: yarn format * fix: added appropriate type to arg of onended method --------- Co-authored-by: Maciej Makowski <maciej.makowski2608@gmail.com>
1 parent 5dde976 commit 42c2533

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

packages/react-native-audio-api/common/cpp/audioapi/core/sources/AudioScheduledSourceNode.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,15 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
144144
void AudioScheduledSourceNode::disable() {
145145
AudioNode::disable();
146146

147-
std::unordered_map<std::string, EventValue> body = {{"value", getStopTime()}};
147+
std::string state = "stopped";
148+
149+
// if it has not been stopped, it is ended
150+
if (stopTime_ < 0) {
151+
state = "ended";
152+
}
153+
154+
std::unordered_map<std::string, EventValue> body = {
155+
{"value", getStopTime()}, {"state", state}};
148156

149157
context_->audioEventHandlerRegistry_->invokeHandlerWithEventBody(
150158
"ended", onEndedCallbackId_, body);

packages/react-native-audio-api/src/core/AudioScheduledSourceNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IAudioScheduledSourceNode } from '../interfaces';
22
import AudioNode from './AudioNode';
33
import { InvalidStateError, RangeError } from '../errors';
4-
import { EventTypeWithValue } from '../events/types';
4+
import { EventTypeWithValueAndState } from '../events/types';
55
import { AudioEventEmitter } from '../events';
66

77
export default class AudioScheduledSourceNode extends AudioNode {
@@ -42,7 +42,7 @@ export default class AudioScheduledSourceNode extends AudioNode {
4242
}
4343

4444
// eslint-disable-next-line accessor-pairs
45-
public set onended(callback: (event: EventTypeWithValue) => void) {
45+
public set onended(callback: (event: EventTypeWithValueAndState) => void) {
4646
const subscription = this.audioEventEmitter.addAudioEventListener(
4747
'ended',
4848
callback

packages/react-native-audio-api/src/events/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ export interface EventTypeWithValue {
66
value: number;
77
}
88

9+
export interface EventTypeWithValueAndState {
10+
value: number;
11+
state: 'stopped' | 'ended';
12+
}
13+
914
interface OnInterruptionEventType {
1015
type: 'ended' | 'began';
1116
shouldResume: boolean;
@@ -40,7 +45,7 @@ export interface OnAudioReadyEventType {
4045
}
4146

4247
interface AudioAPIEvents {
43-
ended: EventTypeWithValue;
48+
ended: EventTypeWithValueAndState;
4449
audioReady: OnAudioReadyEventType;
4550
audioError: EventEmptyType; // to change
4651
systemStateChanged: EventEmptyType; // to change

0 commit comments

Comments
 (0)