@inno-isw/react-native-rtp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +362 -2
- package/lib/module/client.js +218 -0
- package/lib/module/client.js.map +1 -0
- package/lib/module/constants.js +44 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/emitter.js +46 -0
- package/lib/module/emitter.js.map +1 -0
- package/lib/module/index.js +16 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/mock/makeRtpPayload.js +51 -0
- package/lib/module/mock/makeRtpPayload.js.map +1 -0
- package/lib/module/mock/mockTransport.js +82 -0
- package/lib/module/mock/mockTransport.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/push.js +96 -0
- package/lib/module/push.js.map +1 -0
- package/lib/module/react/RtpProvider.js +142 -0
- package/lib/module/react/RtpProvider.js.map +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/validate.js +80 -0
- package/lib/module/validate.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/client.d.ts +11 -0
- package/lib/typescript/src/client.d.ts.map +1 -0
- package/lib/typescript/src/constants.d.ts +45 -0
- package/lib/typescript/src/constants.d.ts.map +1 -0
- package/lib/typescript/src/emitter.d.ts +15 -0
- package/lib/typescript/src/emitter.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +14 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/mock/makeRtpPayload.d.ts +17 -0
- package/lib/typescript/src/mock/makeRtpPayload.d.ts.map +1 -0
- package/lib/typescript/src/mock/mockTransport.d.ts +29 -0
- package/lib/typescript/src/mock/mockTransport.d.ts.map +1 -0
- package/lib/typescript/src/push.d.ts +41 -0
- package/lib/typescript/src/push.d.ts.map +1 -0
- package/lib/typescript/src/react/RtpProvider.d.ts +56 -0
- package/lib/typescript/src/react/RtpProvider.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +142 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/validate.d.ts +22 -0
- package/lib/typescript/src/validate.d.ts.map +1 -0
- package/package.json +141 -4
- package/src/client.ts +239 -0
- package/src/constants.ts +49 -0
- package/src/emitter.ts +53 -0
- package/src/index.tsx +43 -0
- package/src/mock/makeRtpPayload.ts +66 -0
- package/src/mock/mockTransport.ts +103 -0
- package/src/push.ts +109 -0
- package/src/react/RtpProvider.tsx +200 -0
- package/src/types.ts +161 -0
- package/src/validate.ts +93 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tiny typed event emitter. Deliberately dependency-free so the same contract
|
|
5
|
+
* is trivial to re-implement in Kotlin/Swift.
|
|
6
|
+
*/
|
|
7
|
+
export function createEmitter() {
|
|
8
|
+
const listeners = {
|
|
9
|
+
request: new Set(),
|
|
10
|
+
state: new Set(),
|
|
11
|
+
result: new Set()
|
|
12
|
+
};
|
|
13
|
+
function on(event, handler) {
|
|
14
|
+
const set = listeners[event];
|
|
15
|
+
if (!set) {
|
|
16
|
+
throw new Error(`[react-native-rtp] Unknown event: ${String(event)}`);
|
|
17
|
+
}
|
|
18
|
+
set.add(handler);
|
|
19
|
+
return () => {
|
|
20
|
+
set.delete(handler);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function emit(event, payload) {
|
|
24
|
+
// Copy to a snapshot so handlers can unsubscribe during emit.
|
|
25
|
+
for (const handler of Array.from(listeners[event])) {
|
|
26
|
+
handler(payload);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function clear() {
|
|
30
|
+
listeners.request.clear();
|
|
31
|
+
listeners.state.clear();
|
|
32
|
+
listeners.result.clear();
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
on,
|
|
36
|
+
emit,
|
|
37
|
+
clear
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Default logger used when the host does not provide one. */
|
|
42
|
+
export const noopLogger = () => {};
|
|
43
|
+
|
|
44
|
+
/** Default clock. */
|
|
45
|
+
export const defaultNow = () => Date.now();
|
|
46
|
+
//# sourceMappingURL=emitter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createEmitter","listeners","request","Set","state","result","on","event","handler","set","Error","String","add","delete","emit","payload","Array","from","clear","noopLogger","defaultNow","Date","now"],"sourceRoot":"../../src","sources":["emitter.ts"],"mappings":";;AAIA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAAA,EAAG;EAC9B,MAAMC,SAAqD,GAAG;IAC5DC,OAAO,EAAE,IAAIC,GAAG,CAAC,CAAC;IAClBC,KAAK,EAAE,IAAID,GAAG,CAAC,CAAC;IAChBE,MAAM,EAAE,IAAIF,GAAG,CAAC;EAClB,CAAC;EAED,SAASG,EAAEA,CACTC,KAAQ,EACRC,OAAuB,EACP;IAChB,MAAMC,GAAG,GAAGR,SAAS,CAACM,KAAK,CAAC;IAC5B,IAAI,CAACE,GAAG,EAAE;MACR,MAAM,IAAIC,KAAK,CAAC,qCAAqCC,MAAM,CAACJ,KAAK,CAAC,EAAE,CAAC;IACvE;IACAE,GAAG,CAACG,GAAG,CAACJ,OAAqB,CAAC;IAC9B,OAAO,MAAM;MACXC,GAAG,CAACI,MAAM,CAACL,OAAqB,CAAC;IACnC,CAAC;EACH;EAEA,SAASM,IAAIA,CACXP,KAAQ,EACRQ,OAAsC,EAChC;IACN;IACA,KAAK,MAAMP,OAAO,IAAIQ,KAAK,CAACC,IAAI,CAAChB,SAAS,CAACM,KAAK,CAAC,CAAC,EAAE;MAClDC,OAAO,CAACO,OAAO,CAAC;IAClB;EACF;EAEA,SAASG,KAAKA,CAAA,EAAS;IACrBjB,SAAS,CAACC,OAAO,CAACgB,KAAK,CAAC,CAAC;IACzBjB,SAAS,CAACG,KAAK,CAACc,KAAK,CAAC,CAAC;IACvBjB,SAAS,CAACI,MAAM,CAACa,KAAK,CAAC,CAAC;EAC1B;EAEA,OAAO;IAAEZ,EAAE;IAAEQ,IAAI;IAAEI;EAAM,CAAC;AAC5B;;AAEA;AACA,OAAO,MAAMC,UAAmD,GAAGA,CAAA,KAAM,CAAC,CAAC;;AAE3E;AACA,OAAO,MAAMC,UAAU,GAAGA,CAAA,KAAMC,IAAI,CAACC,GAAG,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Core (platform-agnostic)
|
|
4
|
+
export { createRtpClient } from "./client.js";
|
|
5
|
+
export { RTP_STATUS, RTP_EVENT, RTP_ERROR } from "./constants.js";
|
|
6
|
+
export { isRtpPayload, findValidationErrors, toRtpRequest, isExpired } from "./validate.js";
|
|
7
|
+
export { mapPushToRtp } from "./push.js";
|
|
8
|
+
|
|
9
|
+
// Types
|
|
10
|
+
|
|
11
|
+
// React adapter
|
|
12
|
+
export { RtpProvider, useRtp, RtpContext } from "./react/RtpProvider.js";
|
|
13
|
+
// Mocks (for tests / example app)
|
|
14
|
+
export { createMockTransport } from "./mock/mockTransport.js";
|
|
15
|
+
export { makeRtpPayload, makeExpiredPayload } from "./mock/makeRtpPayload.js";
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createRtpClient","RTP_STATUS","RTP_EVENT","RTP_ERROR","isRtpPayload","findValidationErrors","toRtpRequest","isExpired","mapPushToRtp","RtpProvider","useRtp","RtpContext","createMockTransport","makeRtpPayload","makeExpiredPayload"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA;AACA,SAASA,eAAe,QAAQ,aAAU;AAC1C,SAASC,UAAU,EAAEC,SAAS,EAAEC,SAAS,QAAQ,gBAAa;AAE9D,SACEC,YAAY,EACZC,oBAAoB,EACpBC,YAAY,EACZC,SAAS,QACJ,eAAY;AACnB,SAASC,YAAY,QAAQ,WAAQ;;AAGrC;;AAiBA;AACA,SAASC,WAAW,EAAEC,MAAM,EAAEC,UAAU,QAAQ,wBAAqB;AAGrE;AACA,SAASC,mBAAmB,QAAQ,yBAAsB;AAM1D,SAASC,cAAc,EAAEC,kBAAkB,QAAQ,0BAAuB","ignoreList":[]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** Number of ms in a minute, for readable fixture offsets. */
|
|
4
|
+
const MINUTE = 60_000;
|
|
5
|
+
let counter = 0;
|
|
6
|
+
/**
|
|
7
|
+
* Build a valid RTP payload for tests, fixtures and the example app.
|
|
8
|
+
* Every field is overridable so edge cases are trivial to express.
|
|
9
|
+
*/
|
|
10
|
+
export function makeRtpPayload(overrides = {}) {
|
|
11
|
+
const {
|
|
12
|
+
payer,
|
|
13
|
+
payee,
|
|
14
|
+
expiresInMs = 15 * MINUTE,
|
|
15
|
+
from = Date.now(),
|
|
16
|
+
...rest
|
|
17
|
+
} = overrides;
|
|
18
|
+
counter += 1;
|
|
19
|
+
return {
|
|
20
|
+
version: '1.0',
|
|
21
|
+
type: 'RTP',
|
|
22
|
+
rtpId: `RTP-${from}-${counter}`,
|
|
23
|
+
amount: 250000,
|
|
24
|
+
currency: 'NGN',
|
|
25
|
+
narration: 'Payment for invoice 1042',
|
|
26
|
+
transactionRef: `TRX-${from}-${counter}`,
|
|
27
|
+
recipient: 'john.doe@quickteller.com',
|
|
28
|
+
expiresAt: new Date(from + expiresInMs).toISOString(),
|
|
29
|
+
payer: {
|
|
30
|
+
name: 'Ajibola Shadrach',
|
|
31
|
+
accountNumber: '0123456789',
|
|
32
|
+
bank: 'Quickteller MFB',
|
|
33
|
+
...payer
|
|
34
|
+
},
|
|
35
|
+
payee: {
|
|
36
|
+
name: 'Michael Chinweike',
|
|
37
|
+
accountNumber: '9876543210',
|
|
38
|
+
...payee
|
|
39
|
+
},
|
|
40
|
+
...rest
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A payload that has already expired. */
|
|
45
|
+
export function makeExpiredPayload(overrides = {}) {
|
|
46
|
+
return makeRtpPayload({
|
|
47
|
+
...overrides,
|
|
48
|
+
expiresInMs: -MINUTE
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=makeRtpPayload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["MINUTE","counter","makeRtpPayload","overrides","payer","payee","expiresInMs","from","Date","now","rest","version","type","rtpId","amount","currency","narration","transactionRef","recipient","expiresAt","toISOString","name","accountNumber","bank","makeExpiredPayload"],"sourceRoot":"../../../src","sources":["mock/makeRtpPayload.ts"],"mappings":";;AAEA;AACA,MAAMA,MAAM,GAAG,MAAM;AAErB,IAAIC,OAAO,GAAG,CAAC;AAaf;AACA;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAC5BC,SAAkC,GAAG,CAAC,CAAC,EAC3B;EACZ,MAAM;IACJC,KAAK;IACLC,KAAK;IACLC,WAAW,GAAG,EAAE,GAAGN,MAAM;IACzBO,IAAI,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC;IACjB,GAAGC;EACL,CAAC,GAAGP,SAAS;EAEbF,OAAO,IAAI,CAAC;EAEZ,OAAO;IACLU,OAAO,EAAE,KAAK;IACdC,IAAI,EAAE,KAAK;IACXC,KAAK,EAAE,OAAON,IAAI,IAAIN,OAAO,EAAE;IAC/Ba,MAAM,EAAE,MAAM;IACdC,QAAQ,EAAE,KAAK;IACfC,SAAS,EAAE,0BAA0B;IACrCC,cAAc,EAAE,OAAOV,IAAI,IAAIN,OAAO,EAAE;IACxCiB,SAAS,EAAE,0BAA0B;IACrCC,SAAS,EAAE,IAAIX,IAAI,CAACD,IAAI,GAAGD,WAAW,CAAC,CAACc,WAAW,CAAC,CAAC;IACrDhB,KAAK,EAAE;MACLiB,IAAI,EAAE,kBAAkB;MACxBC,aAAa,EAAE,YAAY;MAC3BC,IAAI,EAAE,iBAAiB;MACvB,GAAGnB;IACL,CAAC;IACDC,KAAK,EAAE;MACLgB,IAAI,EAAE,mBAAmB;MACzBC,aAAa,EAAE,YAAY;MAC3B,GAAGjB;IACL,CAAC;IACD,GAAGK;EACL,CAAC;AACH;;AAEA;AACA,OAAO,SAASc,kBAAkBA,CAChCrB,SAAkC,GAAG,CAAC,CAAC,EAC3B;EACZ,OAAOD,cAAc,CAAC;IAAE,GAAGC,SAAS;IAAEG,WAAW,EAAE,CAACN;EAAO,CAAC,CAAC;AAC/D","ignoreList":[]}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const extractId = path => {
|
|
4
|
+
const parts = path.split('/');
|
|
5
|
+
return parts[parts.length - 2] ?? '';
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Deterministic in-memory transport. No real network calls are made.
|
|
10
|
+
*/
|
|
11
|
+
export function createMockTransport(options = {}) {
|
|
12
|
+
const {
|
|
13
|
+
latencyMs = 250,
|
|
14
|
+
failAccept = false,
|
|
15
|
+
failDecline = false,
|
|
16
|
+
acceptStatus = 'ACCEPTED'
|
|
17
|
+
} = options;
|
|
18
|
+
const calls = [];
|
|
19
|
+
const delay = () => new Promise(resolve => setTimeout(resolve, latencyMs));
|
|
20
|
+
const transport = {
|
|
21
|
+
async post(path, body, headers) {
|
|
22
|
+
calls.push({
|
|
23
|
+
method: 'post',
|
|
24
|
+
path,
|
|
25
|
+
body,
|
|
26
|
+
headers
|
|
27
|
+
});
|
|
28
|
+
await delay();
|
|
29
|
+
if (path.endsWith('/accept')) {
|
|
30
|
+
if (failAccept) {
|
|
31
|
+
throw new Error('Mock network failure on accept');
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
status: 200,
|
|
35
|
+
data: {
|
|
36
|
+
id: extractId(path),
|
|
37
|
+
status: acceptStatus
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (path.endsWith('/decline')) {
|
|
42
|
+
if (failDecline) {
|
|
43
|
+
throw new Error('Mock network failure on decline');
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
status: 200,
|
|
47
|
+
data: {
|
|
48
|
+
id: extractId(path),
|
|
49
|
+
status: 'DECLINED'
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
status: 200,
|
|
55
|
+
data: {}
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
async get(path, headers) {
|
|
59
|
+
calls.push({
|
|
60
|
+
method: 'get',
|
|
61
|
+
path,
|
|
62
|
+
headers
|
|
63
|
+
});
|
|
64
|
+
await delay();
|
|
65
|
+
return {
|
|
66
|
+
status: 200,
|
|
67
|
+
data: {
|
|
68
|
+
id: extractId(path),
|
|
69
|
+
status: 'PENDING'
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
transport,
|
|
76
|
+
calls,
|
|
77
|
+
reset: () => {
|
|
78
|
+
calls.length = 0;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=mockTransport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["extractId","path","parts","split","length","createMockTransport","options","latencyMs","failAccept","failDecline","acceptStatus","calls","delay","Promise","resolve","setTimeout","transport","post","body","headers","push","method","endsWith","Error","status","data","id","get","reset"],"sourceRoot":"../../../src","sources":["mock/mockTransport.ts"],"mappings":";;AA4BA,MAAMA,SAAS,GAAIC,IAAY,IAAa;EAC1C,MAAMC,KAAK,GAAGD,IAAI,CAACE,KAAK,CAAC,GAAG,CAAC;EAC7B,OAAOD,KAAK,CAACA,KAAK,CAACE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE;AACtC,CAAC;;AAED;AACA;AACA;AACA,OAAO,SAASC,mB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,EACZkB,OAAgC,EACH;MAC7BR,KAAK,CAACS,IAAI,CAAC;QAAEC,MAAM,EAAE,KAAK;QAAEpB,IAAI;QAAEkB;MAAQ,CAAC,CAAC;MAC5C,MAAMP,KAAK,CAAC,CAAC;MACb,OAAO;QACLY,MAAM,EAAE,GAAG;QACXC,IAAI,EAAE;UAAEC,EAAE,EAAE1B,SAAS,CAACC,IAAI,CAAC;UAAEuB,MAAM,EAAE;QAAU;MACjD,CAAC;IACH;EACF,CAAC;EAED,OAAO;IACLR,SAAS;IACTL,KAAK;IACLiB,KAAK,EAAEA,CAAA,KAAM;MACXjB,KAAK,CAACP,MAAM,GAAG,CAAC;IAClB;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { isRtpPayload } from "./validate.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A generic push notification envelope.
|
|
7
|
+
*
|
|
8
|
+
* The SDK does not know or care what a push looks like — every host platform
|
|
9
|
+
* has a different shape, and this is the single seam where that difference is
|
|
10
|
+
* absorbed. A Kotlin/Swift integration implements the equivalent of
|
|
11
|
+
* `mapPushToRtp` against the same `RtpPayload` contract.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Extract an RTP payload from a push envelope, or return null if the push is
|
|
16
|
+
* not an RTP request.
|
|
17
|
+
*
|
|
18
|
+
* RTCP delivers two distinct shapes for the same notification:
|
|
19
|
+
*
|
|
20
|
+
* 1. `onRemoteNotification` (foreground/background):
|
|
21
|
+
* `{ data: { app_data: { type: 'RTP', ... } } }`
|
|
22
|
+
*
|
|
23
|
+
* 2. `onNotificationTapped`: scalars are stringified and the custom payload
|
|
24
|
+
* arrives as a JSON string:
|
|
25
|
+
* `{ data: { app_data: '{"type":"RTP",...}' } }`
|
|
26
|
+
*
|
|
27
|
+
* Both are normalized here so callers only ever deal with `RtpPayload`.
|
|
28
|
+
*
|
|
29
|
+
* `getData` is injectable so hosts that expose push data differently (e.g. a
|
|
30
|
+
* Map-like object on native) can plug in their accessor without changing the
|
|
31
|
+
* rest of the SDK.
|
|
32
|
+
*/
|
|
33
|
+
export function mapPushToRtp(envelope, getData) {
|
|
34
|
+
if (!envelope) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const data = (getData ? getData(envelope) : envelope.data) ?? {};
|
|
38
|
+
|
|
39
|
+
// Some providers nest the custom payload under `app_data` (RTCP/FCM).
|
|
40
|
+
const candidate = unwrapAppData(data);
|
|
41
|
+
if (!isRtpPayload(candidate)) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return normalizePayload(candidate);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* RTCP/FCM delivers the custom payload as a JSON string under `app_data`.
|
|
49
|
+
* The tapped shape sits one level deeper, under `data` / `body`.
|
|
50
|
+
*/
|
|
51
|
+
function unwrapAppData(data) {
|
|
52
|
+
let source = data;
|
|
53
|
+
|
|
54
|
+
// Tapped shape: `{ time, app_id, message, read, push_id, app_data }`
|
|
55
|
+
// Received shape: `{ app_data, message, app_id, push_id, time, read }`
|
|
56
|
+
// In both cases `app_data` holds the custom payload.
|
|
57
|
+
const appData = source.app_data;
|
|
58
|
+
if (appData === undefined) {
|
|
59
|
+
return source;
|
|
60
|
+
}
|
|
61
|
+
let parsed = {};
|
|
62
|
+
if (typeof appData === 'string') {
|
|
63
|
+
try {
|
|
64
|
+
parsed = JSON.parse(appData);
|
|
65
|
+
} catch {
|
|
66
|
+
return source;
|
|
67
|
+
}
|
|
68
|
+
} else if (appData && typeof appData === 'object') {
|
|
69
|
+
parsed = appData;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
...source,
|
|
73
|
+
...parsed
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Push transports stringify scalars. Coerce the fields the SDK cares about so
|
|
79
|
+
* the decoded payload always has stable types (this is what makes the payload
|
|
80
|
+
* portable across platforms).
|
|
81
|
+
*/
|
|
82
|
+
function normalizePayload(payload) {
|
|
83
|
+
const normalized = {
|
|
84
|
+
...payload
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// `amount` may arrive as "250000" on tapped notifications.
|
|
88
|
+
if (typeof normalized.amount === 'string') {
|
|
89
|
+
const parsed = Number(normalized.amount);
|
|
90
|
+
if (!Number.isNaN(parsed)) {
|
|
91
|
+
normalized.amount = parsed;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return normalized;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=push.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["isRtpPayload","mapPushToRtp","envelope","getData","data","candidate","unwrapAppData","normalizePayload","source","appData","app_data","undefined","parsed","JSON","parse","payload","normalized","amount","Number","isNaN"],"sourceRoot":"../../src","sources":["push.ts"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,eAAY;;AAGzC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAC1BC,QAAyC,EACzCC,OAAyE,EACtD;EACnB,IAAI,CAACD,QAAQ,EAAE;IACb,OAAO,IAAI;EACb;EAEA,MAAME,IAAI,GAAG,CAACD,OAAO,GAAGA,OAAO,CAACD,QAAQ,CAAC,GAAGA,QAAQ,CAACE,IAAI,KAAK,CAAC,CAAC;;EAEhE;EACA,MAAMC,SAAS,GAAGC,aAAa,CAACF,IAAI,CAAC;EAErC,IAAI,CAACJ,YAAY,CAACK,SAAS,CAAC,EAAE;IAC5B,OAAO,IAAI;EACb;EAEA,OAAOE,gBAAgB,CAACF,SAAS,CAAC;AACpC;;AAEA;AACA;AACA;AACA;AACA,SAASC,aAAaA,CAACF,IAA6B,EAA2B;EAC7E,IAAII,MAAM,GAAGJ,IAAI;;EAEjB;EACA;EACA;EACA,MAAMK,OAAO,GAAGD,MAAM,CAACE,QAAQ;EAC/B,IAAID,OAAO,KAAKE,SAAS,EAAE;IACzB,OAAOH,MAAM;EACf;EAEA,IAAII,MAA+B,GAAG,CAAC,CAAC;EACxC,IAAI,OAAOH,OAAO,KAAK,QAAQ,EAAE;IAC/B,IAAI;MACFG,MAAM,GAAGC,IAAI,CAACC,KAAK,CAACL,OAAO,CAA4B;IACzD,CAAC,CAAC,MAAM;MACN,OAAOD,MAAM;IACf;EACF,CAAC,MAAM,IAAIC,OAAO,IAAI,OAAOA,OAAO,KAAK,QAAQ,EAAE;IACjDG,MAAM,GAAGH,OAAkC;EAC7C;EAEA,OAAO;IAAE,GAAGD,MAAM;IAAE,GAAGI;EAAO,CAAC;AACjC;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASL,gBAAgBA,CAACQ,OAAgC,EAAc;EACtE,MAAMC,UAAmC,GAAG;IAAE,GAAGD;EAAQ,CAAC;;EAE1D;EACA,IAAI,OAAOC,UAAU,CAACC,MAAM,KAAK,QAAQ,EAAE;IACzC,MAAML,MAAM,GAAGM,MAAM,CAACF,UAAU,CAACC,MAAM,CAAC;IACxC,IAAI,CAACC,MAAM,CAACC,KAAK,CAACP,MAAM,CAAC,EAAE;MACzBI,UAAU,CAACC,MAAM,GAAGL,MAAM;IAC5B;EACF;EAEA,OAAOI,UAAU;AACnB","ignoreList":[]}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { RTP_STATUS } from "../constants.js";
|
|
5
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
|
+
const STORAGE_KEY = 'rtp.pendingPayload';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A minimal persistence port for the provider. The host supplies its own
|
|
10
|
+
* storage (e.g. react-native-default-preference / AsyncStorage) so the SDK
|
|
11
|
+
* stays dependency-free. When omitted, persistence is in-memory only.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const RtpContext = /*#__PURE__*/createContext(null);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Bridges the framework-agnostic RTP client into React. Owns no UI — hosts
|
|
18
|
+
* render their own bottom sheet / modal and react to `request` + `status`.
|
|
19
|
+
*/
|
|
20
|
+
export function RtpProvider({
|
|
21
|
+
client,
|
|
22
|
+
isAuthenticated,
|
|
23
|
+
storage,
|
|
24
|
+
onIncomingPayload,
|
|
25
|
+
children
|
|
26
|
+
}) {
|
|
27
|
+
const [request, setRequest] = useState(null);
|
|
28
|
+
const [status, setStatus] = useState(null);
|
|
29
|
+
const [lastResult, setLastResult] = useState(null);
|
|
30
|
+
|
|
31
|
+
// Keep the latest request id so async resume/drain can target it.
|
|
32
|
+
const activeIdRef = useRef(null);
|
|
33
|
+
const receive = useCallback(payload => {
|
|
34
|
+
onIncomingPayload?.(payload);
|
|
35
|
+
client.receive(payload);
|
|
36
|
+
}, [client, onIncomingPayload]);
|
|
37
|
+
|
|
38
|
+
// Subscribe to client events exactly once per client.
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const offRequest = client.on('request', req => {
|
|
41
|
+
activeIdRef.current = req.id;
|
|
42
|
+
setRequest(req);
|
|
43
|
+
});
|
|
44
|
+
const offState = client.on('state', ({
|
|
45
|
+
id,
|
|
46
|
+
status: next
|
|
47
|
+
}) => {
|
|
48
|
+
if (activeIdRef.current && id !== activeIdRef.current) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
setStatus(next);
|
|
52
|
+
});
|
|
53
|
+
const offResult = client.on('result', result => {
|
|
54
|
+
setLastResult(result);
|
|
55
|
+
});
|
|
56
|
+
return () => {
|
|
57
|
+
offRequest();
|
|
58
|
+
offState();
|
|
59
|
+
offResult();
|
|
60
|
+
};
|
|
61
|
+
}, [client]);
|
|
62
|
+
|
|
63
|
+
// Drain any payload persisted before React mounted (cold start / killed app).
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
let cancelled = false;
|
|
66
|
+
async function drain() {
|
|
67
|
+
if (!storage) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const raw = await storage.getItem(STORAGE_KEY);
|
|
71
|
+
if (!raw || cancelled) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
await storage.removeItem(STORAGE_KEY);
|
|
75
|
+
client.receive(JSON.parse(raw));
|
|
76
|
+
}
|
|
77
|
+
drain();
|
|
78
|
+
return () => {
|
|
79
|
+
cancelled = true;
|
|
80
|
+
};
|
|
81
|
+
}, [client, storage]);
|
|
82
|
+
|
|
83
|
+
// Auth gate: once authenticated, release any AWAITING_AUTH requests.
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (isAuthenticated) {
|
|
86
|
+
client.resumeAuth();
|
|
87
|
+
}
|
|
88
|
+
}, [client, isAuthenticated]);
|
|
89
|
+
const accept = useCallback(async credential => {
|
|
90
|
+
const id = request?.id ?? activeIdRef.current;
|
|
91
|
+
if (!id) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
return client.accept({
|
|
95
|
+
id,
|
|
96
|
+
credential
|
|
97
|
+
});
|
|
98
|
+
}, [client, request]);
|
|
99
|
+
const decline = useCallback(async reason => {
|
|
100
|
+
const id = request?.id ?? activeIdRef.current;
|
|
101
|
+
if (!id) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
return client.decline({
|
|
105
|
+
id,
|
|
106
|
+
reason
|
|
107
|
+
});
|
|
108
|
+
}, [client, request]);
|
|
109
|
+
const dismiss = useCallback(() => {
|
|
110
|
+
setRequest(null);
|
|
111
|
+
setStatus(null);
|
|
112
|
+
setLastResult(null);
|
|
113
|
+
activeIdRef.current = null;
|
|
114
|
+
}, []);
|
|
115
|
+
const isReady = status === RTP_STATUS.READY;
|
|
116
|
+
const value = useMemo(() => ({
|
|
117
|
+
request,
|
|
118
|
+
status,
|
|
119
|
+
isReady,
|
|
120
|
+
accept,
|
|
121
|
+
decline,
|
|
122
|
+
dismiss,
|
|
123
|
+
receive,
|
|
124
|
+
lastResult
|
|
125
|
+
}), [request, status, isReady, accept, decline, dismiss, receive, lastResult]);
|
|
126
|
+
return /*#__PURE__*/_jsx(RtpContext.Provider, {
|
|
127
|
+
value: value,
|
|
128
|
+
children: children
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Access the RTP context. Throws when used outside of an `<RtpProvider>`.
|
|
134
|
+
*/
|
|
135
|
+
export function useRtp() {
|
|
136
|
+
const context = useContext(RtpContext);
|
|
137
|
+
if (!context) {
|
|
138
|
+
throw new Error('[react-native-rtp] useRtp must be used within RtpProvider');
|
|
139
|
+
}
|
|
140
|
+
return context;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=RtpProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["React","createContext","useCallback","useContext","useEffect","useMemo","useRef","useState","RTP_STATUS","jsx","_jsx","STORAGE_KEY","RtpContext","RtpProvider","client","isAuthenticated","storage","onIncomingPayload","children","request","setRequest","status","setStatus","lastResult","setLastResult","activeIdRef","receive","payload","offRequest","on","req","current","id","offState","next","offResult","result","cancelled","drain","raw","getItem","removeItem","JSON","parse","resumeAuth","accept","credential","undefined","decline","reason","dismiss","isReady","READY","value","Provider","useRtp","context","Error"],"sourceRoot":"../../../src","sources":["react/RtpProvider.tsx"],"mappings":";;AAAA,OAAOA,KAAK,IACVC,aAAa,EACbC,WAAW,EACXC,UAAU,EACVC,SAAS,EACTC,OAAO,EACPC,MAAM,EACNC,QAAQ,QACH,OAAO;AAEd,SAASC,UAAU,QAAQ,iBAAc;AAAC,SAAAC,GAAA,IAAAC,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ACA,OAAOD,OAAO;AAChB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/** Required top-level fields for a valid RTP payload. */
|
|
4
|
+
const REQUIRED_FIELDS = ['rtpId', 'amount', 'currency', 'payer', 'payee', 'expiresAt'];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type guard for RTP payloads.
|
|
8
|
+
*
|
|
9
|
+
* NOTE: this intentionally inspects the *decoded* payload, not a push
|
|
10
|
+
* envelope. Hosts are responsible for extracting the payload from their own
|
|
11
|
+
* notification system first.
|
|
12
|
+
*
|
|
13
|
+
* `amount` is accepted as either a number (minor units) or a numeric string,
|
|
14
|
+
* because push payloads frequently stringify scalars in transit.
|
|
15
|
+
*/
|
|
16
|
+
export function isRtpPayload(payload) {
|
|
17
|
+
if (!payload || typeof payload !== 'object') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const candidate = payload;
|
|
21
|
+
return candidate.type === 'RTP' && typeof candidate.rtpId === 'string';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns the list of missing/invalid required fields. Empty array means the
|
|
26
|
+
* payload is structurally valid.
|
|
27
|
+
*/
|
|
28
|
+
export function findValidationErrors(payload) {
|
|
29
|
+
const errors = [];
|
|
30
|
+
const record = payload;
|
|
31
|
+
for (const field of REQUIRED_FIELDS) {
|
|
32
|
+
if (record[field] === undefined || record[field] === null) {
|
|
33
|
+
errors.push(field);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (payload.expiresAt !== undefined && Number.isNaN(new Date(payload.expiresAt).getTime())) {
|
|
37
|
+
errors.push('expiresAt');
|
|
38
|
+
}
|
|
39
|
+
for (const side of ['payer', 'payee']) {
|
|
40
|
+
const party = payload[side];
|
|
41
|
+
if (!party || typeof party !== 'object' || typeof party.name !== 'string') {
|
|
42
|
+
errors.push(`${side}.name`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return errors;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Coerce an amount that may arrive as a number or numeric string. */
|
|
49
|
+
function toAmount(value) {
|
|
50
|
+
if (typeof value === 'number') {
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
const parsed = Number(value);
|
|
54
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Map a validated payload into the normalized request held by the SDK. */
|
|
58
|
+
export function toRtpRequest(payload) {
|
|
59
|
+
return {
|
|
60
|
+
id: payload.rtpId,
|
|
61
|
+
amount: toAmount(payload.amount),
|
|
62
|
+
currency: payload.currency,
|
|
63
|
+
payer: payload.payer,
|
|
64
|
+
payee: payload.payee,
|
|
65
|
+
narration: payload.narration,
|
|
66
|
+
transactionRef: payload.transactionRef,
|
|
67
|
+
expiresAt: payload.expiresAt,
|
|
68
|
+
recipient: payload.recipient
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Pure expiry check. Re-evaluated on every transition, not just on receive. */
|
|
73
|
+
export function isExpired(request, now) {
|
|
74
|
+
const expiry = new Date(request.expiresAt).getTime();
|
|
75
|
+
if (Number.isNaN(expiry)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return expiry <= now;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["REQUIRED_FIELDS","isRtpPayload","payload","candidate","type","rtpId","findValidationErrors","errors","record","field","undefined","push","expiresAt","Number","isNaN","Date","getTime","side","party","name","toAmount","value","parsed","toRtpRequest","id","amount","currency","payer","payee","narration","transactionRef","recipient","isExpired","request","now","expiry"],"sourceRoot":"../../src","sources":["validate.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,IAAI,OAAO,CAAC;IAC7B;EACF;EAEA,OAAOV,MAAM;AACf;;AAEA;AACA,SAASa,QAAQA,CAACC,KAAc,EAAU;EACxC,IAAI,OAAOA,KAAK,KAAK,QAAQ,EAAE;IAC7B,OAAOA,KAAK;EACd;EACA,MAAMC,MAAM,GAAGT,MAAM,CAACQ,KAAK,CAAC;EAC5B,OAAOR,MAAM,CAACC,KAAK,CAACQ,MAAM,CAAC,GAAG,CAAC,GAAGA,MAAM;AAC1C;;AAEA;AACA,OAAO,SAASC,YAAYA,CAACrB,OAAmB,EAAc;EAC5D,OAAO;IACLsB,EAAE,EAAEtB,OAAO,CAACG,KAAK;IACjBoB,MAAM,EAAEL,QAAQ,CAAClB,OAAO,CAACuB,MAAM,CAAC;IAChCC,QAAQ,EAAExB,OAAO,CAACwB,QAAQ;IAC1BC,KAAK,EAAEzB,OAAO,CAACyB,KAAK;IACpBC,KAAK,EAAE1B,OAAO,CAAC0B,KAAK;IACpBC,SAAS,EAAE3B,OAAO,CAAC2B,SAAS;IAC5BC,cAAc,EAAE5B,OAAO,CAAC4B,cAAc;IACtClB,SAAS,EAAEV,OAAO,CAACU,SAAS;IAC5BmB,SAAS,EAAE7B,OAAO,CAAC6B;EACrB,CAAC;AACH;;AAEA;AACA,OAAO,SAASC,SAASA,CAACC,OAAmB,EAAEC,GAAW,EAAW;EACnE,MAAMC,MAAM,GAAG,IAAIpB,IAAI,CAACkB,OAAO,CAACrB,SAAS,CAAC,CAACI,OAAO,CAAC,CAAC;EACpD,IAAIH,MAAM,CAACC,KAAK,CAACqB,MAAM,CAAC,EAAE;IACxB,OAAO,KAAK;EACd;EACA,OAAOA,MAAM,IAAID,GAAG;AACtB","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RtpClient, RtpClientOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create a portable Request-to-Pay client.
|
|
4
|
+
*
|
|
5
|
+
* The client owns validation, de-duplication, expiry, status computation and
|
|
6
|
+
* the accept/decline/status calls. It owns no UI, no navigation and no crypto.
|
|
7
|
+
* Every external effect is injected so the same contract can be re-implemented
|
|
8
|
+
* on other platforms.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createRtpClient(options: RtpClientOptions): RtpClient;
|
|
11
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/client.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAKjB,MAAM,YAAS,CAAC;AAOjB;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CA+MpE"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalized RTP status.
|
|
3
|
+
*
|
|
4
|
+
* The SDK is the single source of truth for these values so that every host
|
|
5
|
+
* platform (React Native, Kotlin, Swift) renders the same UI states.
|
|
6
|
+
*/
|
|
7
|
+
export declare const RTP_STATUS: {
|
|
8
|
+
/** Payload arrived and passed validation, but is not yet actionable. */
|
|
9
|
+
readonly RECEIVED: "RECEIVED";
|
|
10
|
+
/** Expired before it could be actioned. */
|
|
11
|
+
readonly EXPIRED: "EXPIRED";
|
|
12
|
+
/** Valid, but the user is not authenticated yet (login gate). */
|
|
13
|
+
readonly AWAITING_AUTH: "AWAITING_AUTH";
|
|
14
|
+
/** Valid, authenticated, and ready for the user to accept/decline. */
|
|
15
|
+
readonly READY: "READY";
|
|
16
|
+
/** Accept/decline in flight. */
|
|
17
|
+
readonly PROCESSING: "PROCESSING";
|
|
18
|
+
/** Backend confirmed acceptance. */
|
|
19
|
+
readonly ACCEPTED: "ACCEPTED";
|
|
20
|
+
/** Backend confirmed (or user initiated) a decline. */
|
|
21
|
+
readonly DECLINED: "DECLINED";
|
|
22
|
+
/** Terminal failure (network, validation, recipient mismatch, etc.). */
|
|
23
|
+
readonly FAILED: "FAILED";
|
|
24
|
+
};
|
|
25
|
+
export type RtpStatus = (typeof RTP_STATUS)[keyof typeof RTP_STATUS];
|
|
26
|
+
/** Events emitted by the client. */
|
|
27
|
+
export declare const RTP_EVENT: {
|
|
28
|
+
/** A new, actionable request is available. */
|
|
29
|
+
readonly REQUEST: "request";
|
|
30
|
+
/** Status transition for a request. */
|
|
31
|
+
readonly STATE: "state";
|
|
32
|
+
/** Terminal outcome for a request. */
|
|
33
|
+
readonly RESULT: "result";
|
|
34
|
+
};
|
|
35
|
+
export type RtpEvent = (typeof RTP_EVENT)[keyof typeof RTP_EVENT];
|
|
36
|
+
/** Reasons a request can end in FAILED, exposed for UI/diagnostics. */
|
|
37
|
+
export declare const RTP_ERROR: {
|
|
38
|
+
readonly INVALID_PAYLOAD: "INVALID_PAYLOAD";
|
|
39
|
+
readonly RECIPIENT_MISMATCH: "RECIPIENT_MISMATCH";
|
|
40
|
+
readonly NETWORK_ERROR: "NETWORK_ERROR";
|
|
41
|
+
readonly BACKEND_REJECTED: "BACKEND_REJECTED";
|
|
42
|
+
readonly UNKNOWN: "UNKNOWN";
|
|
43
|
+
};
|
|
44
|
+
export type RtpError = (typeof RTP_ERROR)[keyof typeof RTP_ERROR];
|
|
45
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,UAAU;IACrB,wEAAwE;;IAExE,2CAA2C;;IAE3C,iEAAiE;;IAEjE,sEAAsE;;IAEtE,gCAAgC;;IAEhC,oCAAoC;;IAEpC,uDAAuD;;IAEvD,wEAAwE;;CAEhE,CAAC;AAEX,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,CAAC;AAErE,oCAAoC;AACpC,eAAO,MAAM,SAAS;IACpB,8CAA8C;;IAE9C,uCAAuC;;IAEvC,sCAAsC;;CAE9B,CAAC;AAEX,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,OAAO,SAAS,CAAC,CAAC;AAElE,uEAAuE;AACvE,eAAO,MAAM,SAAS;;;;;;CAMZ,CAAC;AAEX,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,MAAM,OAAO,SAAS,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RtpClientOptions, RtpEventMap, RtpUnsubscribe } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Tiny typed event emitter. Deliberately dependency-free so the same contract
|
|
4
|
+
* is trivial to re-implement in Kotlin/Swift.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createEmitter(): {
|
|
7
|
+
on: <E extends keyof RtpEventMap>(event: E, handler: RtpEventMap[E]) => RtpUnsubscribe;
|
|
8
|
+
emit: <E extends keyof RtpEventMap>(event: E, payload: Parameters<RtpEventMap[E]>[0]) => void;
|
|
9
|
+
clear: () => void;
|
|
10
|
+
};
|
|
11
|
+
/** Default logger used when the host does not provide one. */
|
|
12
|
+
export declare const noopLogger: NonNullable<RtpClientOptions['logger']>;
|
|
13
|
+
/** Default clock. */
|
|
14
|
+
export declare const defaultNow: () => number;
|
|
15
|
+
//# sourceMappingURL=emitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../../../src/emitter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAS,CAAC;AAI7E;;;GAGG;AACH,wBAAgB,aAAa;SAOf,CAAC,SAAS,MAAM,WAAW,SAC9B,CAAC,WACC,WAAW,CAAC,CAAC,CAAC,KACtB,cAAc;WAWH,CAAC,SAAS,MAAM,WAAW,SAChC,CAAC,WACC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KACrC,IAAI;iBAOW,IAAI;EAOvB;AAED,8DAA8D;AAC9D,eAAO,MAAM,UAAU,EAAE,WAAW,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAY,CAAC;AAE5E,qBAAqB;AACrB,eAAO,MAAM,UAAU,cAAmB,CAAC"}
|