@joryio/react-native-sdk 1.4.0-beta.2 → 1.4.0-beta.5
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/CHANGELOG.md +16 -0
- package/README.md +1 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioCardFeedViewManager.kt +106 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioModule.kt +331 -0
- package/android/src/main/java/io/joryio/reactnative/JoryioPackage.kt +2 -1
- package/android/src/main/java/io/joryio/reactnative/JoryioPlacementViewManager.kt +105 -0
- package/ios/JoryioCardFeedViewManager.m +14 -0
- package/ios/JoryioCardFeedViewManager.swift +85 -0
- package/ios/JoryioModule.m +76 -0
- package/ios/JoryioModule.swift +298 -5
- package/ios/JoryioPlacementViewManager.m +14 -0
- package/ios/JoryioPlacementViewManager.swift +75 -0
- package/lib/index.d.ts +337 -0
- package/lib/index.js +578 -1
- package/lib/index.js.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +749 -0
package/lib/index.js
CHANGED
|
@@ -1,8 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ecommerce = exports.IN_APP_DISPLAY_DWELL_MS = exports.MAX_PRE_INIT_CALLS = exports.CLICK_IDENTITY_PARAM = void 0;
|
|
36
|
+
exports.ecommerce = exports.CARD_FEED_TTL_SECONDS = exports.CARD_MIN_REFRESH_SECONDS = exports.PLACEMENT_PRODUCT_CARD_WIDTH = exports.PLACEMENT_PRODUCT_ELEMENT_PREFIX = exports.PLACEMENT_MIN_REFRESH_SECONDS = exports.PLACEMENT_VIEWABLE_FRACTION = exports.IN_APP_DISPLAY_DWELL_MS = exports.MAX_PRE_INIT_CALLS = exports.CLICK_IDENTITY_PARAM = exports.SDK_VERSION = void 0;
|
|
37
|
+
exports.placementProductElementId = placementProductElementId;
|
|
38
|
+
exports.isAppDeepLink = isAppDeepLink;
|
|
39
|
+
exports.JoryioPlacement = JoryioPlacement;
|
|
40
|
+
exports.JoryioCardFeed = JoryioCardFeed;
|
|
4
41
|
const react_native_1 = require("react-native");
|
|
42
|
+
const ReactNative = __importStar(require("react-native"));
|
|
43
|
+
const React = __importStar(require("react"));
|
|
5
44
|
const { JoryioModule } = react_native_1.NativeModules;
|
|
45
|
+
/**
|
|
46
|
+
* This package's version - the same number every Joryio SDK carries
|
|
47
|
+
* (sdk-version-parity.spec.ts pins it; the release tooling rewrites it).
|
|
48
|
+
* Handed to the native SDK at initialize() as `sdkWrapperVersion`, which
|
|
49
|
+
* sends it on every request as `X-Joryio-SDK-Wrapper-Version` next to
|
|
50
|
+
* `X-Joryio-SDK-Wrapper: react-native` (D427), so the dashboard shows the
|
|
51
|
+
* version of THIS package, the one the app upgrades.
|
|
52
|
+
*/
|
|
53
|
+
exports.SDK_VERSION = '1.4.0';
|
|
6
54
|
const MODULE_MISSING_MESSAGE = '@joryio/react-native-sdk: NativeModule not found. ' +
|
|
7
55
|
'Make sure you ran `pod install` (iOS) / rebuilt the app (Android). ' +
|
|
8
56
|
'All Joryio calls will be no-ops until the native module is linked.';
|
|
@@ -203,6 +251,13 @@ function finiteProperties(caller, properties) {
|
|
|
203
251
|
* expect - was a fatal JS error in a release build and skipped every other
|
|
204
252
|
* listener of the same event. A returned promise that rejects is caught too.
|
|
205
253
|
*/
|
|
254
|
+
/** A flag key must be a non-empty string; otherwise the getter answers its default. */
|
|
255
|
+
function flagKeyOk(method, key) {
|
|
256
|
+
if (typeof key === 'string' && key.length > 0)
|
|
257
|
+
return true;
|
|
258
|
+
warn(`${method}() needs a flag key; the default is returned.`);
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
206
261
|
function guardedCallback(name, callback) {
|
|
207
262
|
const report = (err) => {
|
|
208
263
|
// eslint-disable-next-line no-console
|
|
@@ -256,6 +311,242 @@ exports.MAX_PRE_INIT_CALLS = 1000;
|
|
|
256
311
|
* apply the same rule - see {@link Joryio.trackInAppImpression}.
|
|
257
312
|
*/
|
|
258
313
|
exports.IN_APP_DISPLAY_DWELL_MS = 1000;
|
|
314
|
+
// ── Placements (docs/PLACEMENTS_PLAN.md, D444) ──────────────────────────────
|
|
315
|
+
/** Share of an item that must be on screen for it to count as seen - the same on every Joryio SDK. */
|
|
316
|
+
exports.PLACEMENT_VIEWABLE_FRACTION = 0.5;
|
|
317
|
+
/** A placement key is fetched from the network at most once per this many seconds - the same on every Joryio SDK. */
|
|
318
|
+
exports.PLACEMENT_MIN_REFRESH_SECONDS = 30;
|
|
319
|
+
/**
|
|
320
|
+
* A drawn product card's tap is reported as `clicked` with the elementId
|
|
321
|
+
* `product:<product id>` (at most 128 characters) - the same on every Joryio SDK.
|
|
322
|
+
*/
|
|
323
|
+
exports.PLACEMENT_PRODUCT_ELEMENT_PREFIX = 'product:';
|
|
324
|
+
/** Width of a drawn product card when the app does not set one (dp / points) - the same on every Joryio SDK. */
|
|
325
|
+
exports.PLACEMENT_PRODUCT_CARD_WIDTH = 160;
|
|
326
|
+
/** The elementId a product card's click is reported with - use it with `markClicked` when you draw the cards yourself. */
|
|
327
|
+
function placementProductElementId(productId) {
|
|
328
|
+
return `${exports.PLACEMENT_PRODUCT_ELEMENT_PREFIX}${productId}`.slice(0, 128);
|
|
329
|
+
}
|
|
330
|
+
/** The bridge's product cards, defensively typed: an entry without an id is dropped. */
|
|
331
|
+
function toProducts(raw) {
|
|
332
|
+
if (!Array.isArray(raw))
|
|
333
|
+
return [];
|
|
334
|
+
const str = (v) => (typeof v === 'string' ? v : null);
|
|
335
|
+
return raw
|
|
336
|
+
.filter((p) => p && typeof p === 'object' && (typeof p.id === 'string' || typeof p.id === 'number'))
|
|
337
|
+
.map((p) => ({
|
|
338
|
+
id: String(p.id),
|
|
339
|
+
title: str(p.title),
|
|
340
|
+
price: str(p.price),
|
|
341
|
+
compareAtPrice: str(p.compareAtPrice),
|
|
342
|
+
discountPercent: typeof p.discountPercent === 'number' ? p.discountPercent : null,
|
|
343
|
+
imageUrl: str(p.imageUrl),
|
|
344
|
+
url: str(p.url),
|
|
345
|
+
}));
|
|
346
|
+
}
|
|
347
|
+
/** Native JSON (Placement.toJson on iOS / Android) -> objects whose mark* calls go back to native. */
|
|
348
|
+
function toPlacements(json) {
|
|
349
|
+
let raw = {};
|
|
350
|
+
try {
|
|
351
|
+
raw = typeof json === 'string' ? JSON.parse(json) : json || {};
|
|
352
|
+
}
|
|
353
|
+
catch {
|
|
354
|
+
raw = {};
|
|
355
|
+
}
|
|
356
|
+
const mark = (key, itemId, action, elementId) => {
|
|
357
|
+
JoryioModule?.markPlacement?.(key, itemId, action, elementId ?? null);
|
|
358
|
+
};
|
|
359
|
+
const out = {};
|
|
360
|
+
for (const [key, p] of Object.entries(raw || {})) {
|
|
361
|
+
if (!p || typeof p !== 'object')
|
|
362
|
+
continue;
|
|
363
|
+
out[key] = {
|
|
364
|
+
placementKey: p.placementKey || key,
|
|
365
|
+
isControl: p.isControl === true,
|
|
366
|
+
emptyBehavior: p.emptyBehavior === 'keep_space' ? 'keep_space' : 'collapse',
|
|
367
|
+
maxItems: typeof p.maxItems === 'number' ? p.maxItems : 1,
|
|
368
|
+
ttlSeconds: typeof p.ttlSeconds === 'number' ? p.ttlSeconds : 300,
|
|
369
|
+
items: (Array.isArray(p.items) ? p.items : []).map((i) => ({
|
|
370
|
+
id: String(i.id),
|
|
371
|
+
variantId: i.variantId ?? null,
|
|
372
|
+
kind: i.kind,
|
|
373
|
+
data: i.data || {},
|
|
374
|
+
content: i.content ?? null,
|
|
375
|
+
products: toProducts(i.products),
|
|
376
|
+
markDisplayed: () => mark(key, String(i.id), 'displayed'),
|
|
377
|
+
markClicked: (elementId) => mark(key, String(i.id), 'clicked', elementId),
|
|
378
|
+
markDismissed: () => mark(key, String(i.id), 'dismissed'),
|
|
379
|
+
})),
|
|
380
|
+
markDisplayed: () => mark(key, null, 'control'),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
/** Schemes never opened nor handed to the app, whatever a link says (the backend's placement / card link rule). */
|
|
386
|
+
const REFUSED_LINK_SCHEMES = ['javascript', 'data', 'file', 'vbscript', 'blob', 'about', 'content', 'intent'];
|
|
387
|
+
/**
|
|
388
|
+
* An app deep link (`myapp://cart`) that may go to the app's own handler: a
|
|
389
|
+
* `scheme://` link whose scheme is not http(s) and not a refused one. The
|
|
390
|
+
* native PlacementView applies the same rule before a link reaches JS; this
|
|
391
|
+
* layer checks again. The same rule on every Joryio SDK.
|
|
392
|
+
*/
|
|
393
|
+
function isAppDeepLink(url) {
|
|
394
|
+
if (typeof url !== 'string')
|
|
395
|
+
return false;
|
|
396
|
+
const m = /^([a-z][a-z0-9+.-]*):\/\/./i.exec(url.trim());
|
|
397
|
+
if (!m)
|
|
398
|
+
return false;
|
|
399
|
+
const scheme = m[1].toLowerCase();
|
|
400
|
+
return scheme !== 'http' && scheme !== 'https' && !REFUSED_LINK_SCHEMES.includes(scheme);
|
|
401
|
+
}
|
|
402
|
+
const LINK_SOURCES = ['in_app', 'placement', 'card', 'push'];
|
|
403
|
+
/** The `JoryioLink` event body as a JoryioLink, or null when it is not one. */
|
|
404
|
+
function toLink(e) {
|
|
405
|
+
if (!e || typeof e.url !== 'string' || !LINK_SOURCES.includes(e.source))
|
|
406
|
+
return null;
|
|
407
|
+
const link = { url: e.url, source: e.source, isAppDeepLink: e.isAppDeepLink === true };
|
|
408
|
+
for (const key of ['campaignId', 'placementKey', 'itemId', 'cardId']) {
|
|
409
|
+
if (typeof e[key] === 'string' && e[key])
|
|
410
|
+
link[key] = e[key];
|
|
411
|
+
}
|
|
412
|
+
return link;
|
|
413
|
+
}
|
|
414
|
+
let nativePlacementView = null;
|
|
415
|
+
/**
|
|
416
|
+
* `<JoryioPlacement placementKey="home_hero" />` - the native SDK's own
|
|
417
|
+
* PlacementView (iOS and Android): fetching, caching, viewability, the
|
|
418
|
+
* holdout and the reports are the native ones. The component sizes itself to
|
|
419
|
+
* the drawn content (0 when the placement collapses); a `style` height wins.
|
|
420
|
+
*/
|
|
421
|
+
function JoryioPlacement(props) {
|
|
422
|
+
const [height, setHeight] = React.useState(0);
|
|
423
|
+
if (!nativePlacementView) {
|
|
424
|
+
const requireNative = ReactNative.requireNativeComponent;
|
|
425
|
+
if (typeof requireNative !== 'function')
|
|
426
|
+
return null;
|
|
427
|
+
nativePlacementView = requireNative('JoryioPlacementView');
|
|
428
|
+
}
|
|
429
|
+
return React.createElement(nativePlacementView, {
|
|
430
|
+
placementKey: props.placementKey,
|
|
431
|
+
// Passed to the native PlacementView's properties of the same names.
|
|
432
|
+
showProducts: props.showProducts !== false,
|
|
433
|
+
productButtonText: typeof props.productButtonText === 'string' ? props.productButtonText : null,
|
|
434
|
+
productCardWidth: typeof props.productCardWidth === 'number' && Number.isFinite(props.productCardWidth) && props.productCardWidth > 0
|
|
435
|
+
? props.productCardWidth
|
|
436
|
+
: exports.PLACEMENT_PRODUCT_CARD_WIDTH,
|
|
437
|
+
style: [{ height }, props.style],
|
|
438
|
+
onPlacementRender: (e) => {
|
|
439
|
+
const info = e?.nativeEvent || {};
|
|
440
|
+
setHeight(typeof info.height === 'number' ? info.height : 0);
|
|
441
|
+
props.onRender?.(info);
|
|
442
|
+
},
|
|
443
|
+
onPlacementDeepLink: (e) => {
|
|
444
|
+
const url = e?.nativeEvent?.url;
|
|
445
|
+
if (isAppDeepLink(url))
|
|
446
|
+
props.onDeepLink?.(url);
|
|
447
|
+
},
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
// ── Content cards (docs/PLACEMENTS_PLAN.md section 11, D444 Phase 3) ────────
|
|
451
|
+
/** The feed goes to the network at most once per this many seconds; extra calls coalesce - the same on every Joryio SDK. */
|
|
452
|
+
exports.CARD_MIN_REFRESH_SECONDS = 30;
|
|
453
|
+
/** How long the feed is served from the device cache before it is asked for again - the same on every Joryio SDK. */
|
|
454
|
+
exports.CARD_FEED_TTL_SECONDS = 300;
|
|
455
|
+
const EMPTY_CARD_FEED_JSON = '{"cards":[],"unreadCount":0,"nextCursor":null,"stateScope":"server"}';
|
|
456
|
+
const str = (v) => (typeof v === 'string' ? v : null);
|
|
457
|
+
/** Native JSON (Card.toJson on iOS / Android) -> a Card, or null when it is not one. */
|
|
458
|
+
function toCard(c) {
|
|
459
|
+
if (!c || typeof c !== 'object' || typeof c.id !== 'string')
|
|
460
|
+
return null;
|
|
461
|
+
return {
|
|
462
|
+
id: c.id,
|
|
463
|
+
campaignId: str(c.campaignId) ?? '',
|
|
464
|
+
variantId: str(c.variantId),
|
|
465
|
+
title: str(c.title),
|
|
466
|
+
body: str(c.body),
|
|
467
|
+
imageUrl: str(c.imageUrl),
|
|
468
|
+
buttonText: str(c.buttonText),
|
|
469
|
+
url: str(c.url),
|
|
470
|
+
pinned: c.pinned === true,
|
|
471
|
+
createdAt: str(c.createdAt) ?? '',
|
|
472
|
+
expiresAt: str(c.expiresAt),
|
|
473
|
+
read: c.read === true,
|
|
474
|
+
clicked: c.clicked === true,
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
const parseJson = (json) => {
|
|
478
|
+
try {
|
|
479
|
+
return typeof json === 'string' ? JSON.parse(json) : json;
|
|
480
|
+
}
|
|
481
|
+
catch {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
/** Native JSON (CardFeed.toJson on iOS / Android) -> a CardFeed. A bad answer is the empty feed. */
|
|
486
|
+
function toCardFeed(json) {
|
|
487
|
+
let raw = parseJson(json);
|
|
488
|
+
if (!raw || typeof raw !== 'object')
|
|
489
|
+
raw = {};
|
|
490
|
+
const cards = (Array.isArray(raw.cards) ? raw.cards : []).map(toCard).filter((c) => c !== null);
|
|
491
|
+
return {
|
|
492
|
+
cards,
|
|
493
|
+
unreadCount: typeof raw.unreadCount === 'number' && raw.unreadCount >= 0 ? raw.unreadCount : 0,
|
|
494
|
+
nextCursor: str(raw.nextCursor),
|
|
495
|
+
stateScope: raw.stateScope === 'device' ? 'device' : 'server',
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function joryioSubscription(stop) {
|
|
499
|
+
let done = false;
|
|
500
|
+
const cancel = () => {
|
|
501
|
+
if (done)
|
|
502
|
+
return;
|
|
503
|
+
done = true;
|
|
504
|
+
stop();
|
|
505
|
+
};
|
|
506
|
+
const sub = (() => cancel());
|
|
507
|
+
sub.cancel = cancel;
|
|
508
|
+
return sub;
|
|
509
|
+
}
|
|
510
|
+
/** A card or its id. */
|
|
511
|
+
const cardIdOf = (card) => {
|
|
512
|
+
const id = typeof card === 'string' ? card : card && typeof card === 'object' ? card.id : null;
|
|
513
|
+
return typeof id === 'string' && id.length > 0 ? id : null;
|
|
514
|
+
};
|
|
515
|
+
let nativeCardFeedView = null;
|
|
516
|
+
/**
|
|
517
|
+
* `<JoryioCardFeed />` - the person's content cards as a plain scrolling list:
|
|
518
|
+
* the native SDK's own feed view (iOS CardFeedViewController, Android
|
|
519
|
+
* CardFeedView), so paging, the unread dot, the dismiss control, the
|
|
520
|
+
* viewed / read / clicked / dismissed reports (a card at least
|
|
521
|
+
* PLACEMENT_VIEWABLE_FRACTION on screen for IN_APP_DISPLAY_DWELL_MS is viewed
|
|
522
|
+
* then read) and the link rule are the native ones. The look follows the
|
|
523
|
+
* system theme, dark mode included.
|
|
524
|
+
*/
|
|
525
|
+
function JoryioCardFeed(props) {
|
|
526
|
+
if (!nativeCardFeedView) {
|
|
527
|
+
const requireNative = ReactNative.requireNativeComponent;
|
|
528
|
+
if (typeof requireNative !== 'function')
|
|
529
|
+
return null;
|
|
530
|
+
nativeCardFeedView = requireNative('JoryioCardFeedView');
|
|
531
|
+
}
|
|
532
|
+
return React.createElement(nativeCardFeedView, {
|
|
533
|
+
style: [{ flex: 1 }, props.style],
|
|
534
|
+
emptyText: props.emptyText,
|
|
535
|
+
pinnedLabel: props.pinnedLabel,
|
|
536
|
+
dismissLabel: props.dismissLabel,
|
|
537
|
+
unreadLabel: props.unreadLabel,
|
|
538
|
+
// The native views send the feed and the card as their toJson() (the same JSON as getCards).
|
|
539
|
+
onCardFeedRender: (e) => {
|
|
540
|
+
props.onRender?.(toCardFeed(e?.nativeEvent?.feed));
|
|
541
|
+
},
|
|
542
|
+
onCardDeepLink: (e) => {
|
|
543
|
+
const url = e?.nativeEvent?.url;
|
|
544
|
+
const card = toCard(parseJson(e?.nativeEvent?.card));
|
|
545
|
+
if (typeof url === 'string' && card)
|
|
546
|
+
props.onDeepLink?.(url, card);
|
|
547
|
+
},
|
|
548
|
+
});
|
|
549
|
+
}
|
|
259
550
|
/** Tell each discarded held call (never throws). */
|
|
260
551
|
function settleDropped(calls) {
|
|
261
552
|
for (const call of calls) {
|
|
@@ -346,6 +637,10 @@ class JoryioSDK {
|
|
|
346
637
|
* decide whether the start-up hold ends with native drawing.
|
|
347
638
|
*/
|
|
348
639
|
this._inAppHandedToJs = false;
|
|
640
|
+
this._placementListeners = 0;
|
|
641
|
+
this._cardListeners = 0;
|
|
642
|
+
this._linkHandler = null;
|
|
643
|
+
this._linkSubscription = null;
|
|
349
644
|
this._ready = new Promise((resolve) => {
|
|
350
645
|
this._markReady = resolve;
|
|
351
646
|
});
|
|
@@ -850,6 +1145,8 @@ class JoryioSDK {
|
|
|
850
1145
|
if (forNative.optOut === true)
|
|
851
1146
|
this._initConfigOptedOut = true;
|
|
852
1147
|
}
|
|
1148
|
+
// The bridge names itself on every request (D427); not an app option.
|
|
1149
|
+
forNative.sdkWrapperVersion = exports.SDK_VERSION;
|
|
853
1150
|
try {
|
|
854
1151
|
await JoryioModule.initialize(sdkKey, apiHost, forNative);
|
|
855
1152
|
}
|
|
@@ -1763,6 +2060,286 @@ class JoryioSDK {
|
|
|
1763
2060
|
syncInAppCampaigns() {
|
|
1764
2061
|
this._dispatch(() => JoryioModule.syncInAppCampaigns?.());
|
|
1765
2062
|
}
|
|
2063
|
+
// ─── Feature flags (BACKLOG D446) ───────────────────────────────────────
|
|
2064
|
+
//
|
|
2065
|
+
// The native SDK holds the answer and logs exposures; these route to it.
|
|
2066
|
+
// The getters resolve at once with what the device holds - they never wait
|
|
2067
|
+
// for the network - and with the in-code default before initialize(), for a
|
|
2068
|
+
// flag Joryio did not send, for a value of another type, or without
|
|
2069
|
+
// tracking consent. A Promise because every native call crosses the bridge
|
|
2070
|
+
// asynchronously; the names and the wire are the same on all five SDKs.
|
|
2071
|
+
/** A flag's value of any type, or `defaultValue`. The first read in a session logs an exposure. */
|
|
2072
|
+
getFlag(key, defaultValue) {
|
|
2073
|
+
if (!flagKeyOk('getFlag', key))
|
|
2074
|
+
return Promise.resolve(defaultValue);
|
|
2075
|
+
return this._query('getFlag', null, () => {
|
|
2076
|
+
return JoryioModule.getFlag(key, defaultValue ?? null);
|
|
2077
|
+
}).then((r) => (r && typeof r === 'object' && 'value' in r ? r.value : defaultValue));
|
|
2078
|
+
}
|
|
2079
|
+
/** A boolean flag, or `defaultValue` (also for a flag of another type). */
|
|
2080
|
+
getBoolFlag(key, defaultValue) {
|
|
2081
|
+
if (!flagKeyOk('getBoolFlag', key))
|
|
2082
|
+
return Promise.resolve(defaultValue);
|
|
2083
|
+
return this._query('getBoolFlag', defaultValue, () => {
|
|
2084
|
+
return JoryioModule.getBoolFlag(key, !!defaultValue);
|
|
2085
|
+
});
|
|
2086
|
+
}
|
|
2087
|
+
/** A string flag, or `defaultValue`. */
|
|
2088
|
+
getStringFlag(key, defaultValue) {
|
|
2089
|
+
if (!flagKeyOk('getStringFlag', key))
|
|
2090
|
+
return Promise.resolve(defaultValue);
|
|
2091
|
+
return this._query('getStringFlag', defaultValue, () => {
|
|
2092
|
+
return JoryioModule.getStringFlag(key, String(defaultValue ?? ''));
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2095
|
+
/** A number flag, or `defaultValue`. */
|
|
2096
|
+
getNumberFlag(key, defaultValue) {
|
|
2097
|
+
if (!flagKeyOk('getNumberFlag', key))
|
|
2098
|
+
return Promise.resolve(defaultValue);
|
|
2099
|
+
return this._query('getNumberFlag', defaultValue, () => {
|
|
2100
|
+
return JoryioModule.getNumberFlag(key, Number(defaultValue));
|
|
2101
|
+
});
|
|
2102
|
+
}
|
|
2103
|
+
/** A JSON flag (object, array or scalar), or `defaultValue`. */
|
|
2104
|
+
getJsonFlag(key, defaultValue) {
|
|
2105
|
+
if (!flagKeyOk('getJsonFlag', key))
|
|
2106
|
+
return Promise.resolve(defaultValue);
|
|
2107
|
+
return this._query('getJsonFlag', null, () => {
|
|
2108
|
+
return JoryioModule.getJsonFlag(key, defaultValue ?? null);
|
|
2109
|
+
}).then((r) => (r && typeof r === 'object' && 'value' in r ? r.value : defaultValue));
|
|
2110
|
+
}
|
|
2111
|
+
/** Every flag held for this person, key -> value. Logs no exposure. */
|
|
2112
|
+
getAllFlags() {
|
|
2113
|
+
return this._query('getAllFlags', {}, () => {
|
|
2114
|
+
return JoryioModule.getAllFlags();
|
|
2115
|
+
});
|
|
2116
|
+
}
|
|
2117
|
+
/** Ask the server now; resolves when the answer is in (or the request failed: the held flags stay). */
|
|
2118
|
+
refreshFlags() {
|
|
2119
|
+
return this._query('refreshFlags', undefined, () => {
|
|
2120
|
+
return JoryioModule.refreshFlags();
|
|
2121
|
+
}).then(() => undefined);
|
|
2122
|
+
}
|
|
2123
|
+
/**
|
|
2124
|
+
* `callback(keys)` is called with the keys whose value changed. Returns a
|
|
2125
|
+
* JoryioSubscription: `cancel()` it to stop (calling it is the same). Safe
|
|
2126
|
+
* before initialize(): updates start once the SDK runs.
|
|
2127
|
+
*/
|
|
2128
|
+
onFlagsUpdated(callback) {
|
|
2129
|
+
if (typeof callback !== 'function') {
|
|
2130
|
+
warn('onFlagsUpdated() needs a callback function; ignored.');
|
|
2131
|
+
return joryioSubscription(() => { });
|
|
2132
|
+
}
|
|
2133
|
+
if (!isModuleAvailable || !emitter)
|
|
2134
|
+
return joryioSubscription(() => { });
|
|
2135
|
+
const guarded = guardedCallback('onFlagsUpdated', callback);
|
|
2136
|
+
const subscription = emitter.addListener('JoryioFlagsUpdated', (event) => {
|
|
2137
|
+
const keys = event?.keys;
|
|
2138
|
+
guarded(Array.isArray(keys) ? keys.filter((k) => typeof k === 'string') : []);
|
|
2139
|
+
});
|
|
2140
|
+
// One native subscription for the module, held until initialize() like
|
|
2141
|
+
// any control call; native ignores a second enable.
|
|
2142
|
+
this._dispatch(() => JoryioModule.enableFlagsUpdates?.());
|
|
2143
|
+
return joryioSubscription(() => subscription.remove());
|
|
2144
|
+
}
|
|
2145
|
+
/**
|
|
2146
|
+
* The placements for `placementKeys`, to draw with your own components
|
|
2147
|
+
* (or use `<JoryioPlacement placementKey=... />`). Report with
|
|
2148
|
+
* `item.markDisplayed()` once at least half of it was on screen for
|
|
2149
|
+
* IN_APP_DISPLAY_DWELL_MS, `item.markClicked()`, `item.markDismissed()`,
|
|
2150
|
+
* and `placement.markDisplayed()` for a held-out person's empty spot.
|
|
2151
|
+
* Waits for initialize(); resolves `{}` without the native module.
|
|
2152
|
+
*/
|
|
2153
|
+
getPlacements(placementKeys) {
|
|
2154
|
+
const keys = Array.isArray(placementKeys) ? placementKeys.filter((k) => typeof k === 'string') : [];
|
|
2155
|
+
// A read: waits for initialize() like the other queries.
|
|
2156
|
+
return this._query('getPlacements', '{}', () => {
|
|
2157
|
+
return JoryioModule.getPlacements(keys);
|
|
2158
|
+
}).then(toPlacements);
|
|
2159
|
+
}
|
|
2160
|
+
/** Ask the server again (all placements on screen when no keys). At most once per key per PLACEMENT_MIN_REFRESH_SECONDS. */
|
|
2161
|
+
refreshPlacements(placementKeys) {
|
|
2162
|
+
this._dispatch(() => JoryioModule.refreshPlacements?.(Array.isArray(placementKeys) ? placementKeys : null));
|
|
2163
|
+
}
|
|
2164
|
+
/**
|
|
2165
|
+
* Called with the placements whose content changed. Returns a
|
|
2166
|
+
* JoryioSubscription: `cancel()` it to stop (calling it is the same).
|
|
2167
|
+
* An app with its own push messaging handler should call
|
|
2168
|
+
* refreshPlacements() on a `jry_sync: "placements"` data message; the
|
|
2169
|
+
* native SDKs handle it themselves otherwise.
|
|
2170
|
+
*/
|
|
2171
|
+
onPlacementsUpdated(callback) {
|
|
2172
|
+
if (typeof callback !== 'function' || !isModuleAvailable || !emitter)
|
|
2173
|
+
return joryioSubscription(() => { });
|
|
2174
|
+
const subscription = emitter.addListener('JoryioPlacementsUpdated', (e) => {
|
|
2175
|
+
try {
|
|
2176
|
+
callback(toPlacements(e?.json));
|
|
2177
|
+
}
|
|
2178
|
+
catch (error) {
|
|
2179
|
+
warn(`onPlacementsUpdated callback threw: ${String(error)}`);
|
|
2180
|
+
}
|
|
2181
|
+
});
|
|
2182
|
+
this._placementListeners += 1;
|
|
2183
|
+
this._dispatch(() => JoryioModule.enablePlacementsUpdates?.());
|
|
2184
|
+
return joryioSubscription(() => {
|
|
2185
|
+
subscription.remove();
|
|
2186
|
+
this._placementListeners = Math.max(0, this._placementListeners - 1);
|
|
2187
|
+
if (this._placementListeners === 0)
|
|
2188
|
+
this._dispatch(() => JoryioModule.disablePlacementsUpdates?.());
|
|
2189
|
+
});
|
|
2190
|
+
}
|
|
2191
|
+
/**
|
|
2192
|
+
* The person's content cards - pinned first, newest first - with the unread
|
|
2193
|
+
* count for a badge, from the native SDK's cache for CARD_FEED_TTL_SECONDS.
|
|
2194
|
+
* Pass a feed's `nextCursor` as `{ cursor }` for the next page (the native
|
|
2195
|
+
* SDK adds it to the cached feed). Draw it with `<JoryioCardFeed />`, or
|
|
2196
|
+
* yourself and report with markCardViewed / markCardRead /
|
|
2197
|
+
* markCardClicked / dismissCard. Waits for initialize(); resolves the empty
|
|
2198
|
+
* feed without the native module.
|
|
2199
|
+
*/
|
|
2200
|
+
getCards(options) {
|
|
2201
|
+
const cursor = typeof options?.cursor === 'string' && options.cursor.length > 0 ? options.cursor : null;
|
|
2202
|
+
// A read: waits for initialize() like the other queries.
|
|
2203
|
+
return this._query('getCards', EMPTY_CARD_FEED_JSON, () => {
|
|
2204
|
+
return JoryioModule.getCards(cursor);
|
|
2205
|
+
}).then(toCardFeed);
|
|
2206
|
+
}
|
|
2207
|
+
/**
|
|
2208
|
+
* Unread cards, from the native SDK's cache (never a network call): the
|
|
2209
|
+
* badge. A Promise here because every React Native read crosses the bridge;
|
|
2210
|
+
* the native SDKs answer synchronously.
|
|
2211
|
+
*/
|
|
2212
|
+
getUnreadCardCount() {
|
|
2213
|
+
return this._query('getUnreadCardCount', 0, () => {
|
|
2214
|
+
return JoryioModule.getUnreadCardCount();
|
|
2215
|
+
}).then((n) => (typeof n === 'number' && n >= 0 ? n : 0));
|
|
2216
|
+
}
|
|
2217
|
+
/** The card was on screen (`viewed`). Once per card; `<JoryioCardFeed />` does it itself. */
|
|
2218
|
+
markCardViewed(card) {
|
|
2219
|
+
const id = cardIdOf(card);
|
|
2220
|
+
if (id)
|
|
2221
|
+
this._dispatch(() => JoryioModule.markCardViewed?.(id), 'data');
|
|
2222
|
+
}
|
|
2223
|
+
/** The person read the card (`read`): it stops counting as unread at once. */
|
|
2224
|
+
markCardRead(card) {
|
|
2225
|
+
const id = cardIdOf(card);
|
|
2226
|
+
if (id)
|
|
2227
|
+
this._dispatch(() => JoryioModule.markCardRead?.(id), 'data');
|
|
2228
|
+
}
|
|
2229
|
+
/** The person tapped the card or its button (`clicked`, which also marks it read). Open its url yourself. */
|
|
2230
|
+
markCardClicked(card) {
|
|
2231
|
+
const id = cardIdOf(card);
|
|
2232
|
+
if (id)
|
|
2233
|
+
this._dispatch(() => JoryioModule.markCardClicked?.(id), 'data');
|
|
2234
|
+
}
|
|
2235
|
+
/** The person removed the card (`dismissed`): it leaves the feed at once and never comes back. */
|
|
2236
|
+
dismissCard(card) {
|
|
2237
|
+
const id = cardIdOf(card);
|
|
2238
|
+
if (id)
|
|
2239
|
+
this._dispatch(() => JoryioModule.dismissCard?.(id), 'data');
|
|
2240
|
+
}
|
|
2241
|
+
/**
|
|
2242
|
+
* Ask the server for the feed again. At most once per
|
|
2243
|
+
* CARD_MIN_REFRESH_SECONDS; extra calls coalesce. An app with its own push
|
|
2244
|
+
* messaging handler calls this on a `jry_sync: "cards"` data message; the
|
|
2245
|
+
* native SDKs handle it themselves otherwise.
|
|
2246
|
+
*/
|
|
2247
|
+
refreshCards() {
|
|
2248
|
+
this._dispatch(() => JoryioModule.refreshCards?.());
|
|
2249
|
+
}
|
|
2250
|
+
/**
|
|
2251
|
+
* Called with the whole feed whenever it changes (a refresh, a report, an
|
|
2252
|
+
* identity change). Returns a JoryioSubscription: `cancel()` it to stop
|
|
2253
|
+
* (calling it, `sub()`, is the same).
|
|
2254
|
+
*/
|
|
2255
|
+
onCardsUpdated(callback) {
|
|
2256
|
+
if (typeof callback !== 'function' || !isModuleAvailable || !emitter)
|
|
2257
|
+
return joryioSubscription(() => { });
|
|
2258
|
+
const subscription = emitter.addListener('JoryioCardsUpdated', (e) => {
|
|
2259
|
+
try {
|
|
2260
|
+
callback(toCardFeed(e?.json));
|
|
2261
|
+
}
|
|
2262
|
+
catch (error) {
|
|
2263
|
+
warn(`onCardsUpdated callback threw: ${String(error)}`);
|
|
2264
|
+
}
|
|
2265
|
+
});
|
|
2266
|
+
this._cardListeners += 1;
|
|
2267
|
+
this._dispatch(() => JoryioModule.enableCardsUpdates?.());
|
|
2268
|
+
return joryioSubscription(() => {
|
|
2269
|
+
subscription.remove();
|
|
2270
|
+
this._cardListeners = Math.max(0, this._cardListeners - 1);
|
|
2271
|
+
if (this._cardListeners === 0)
|
|
2272
|
+
this._dispatch(() => JoryioModule.disableCardsUpdates?.());
|
|
2273
|
+
});
|
|
2274
|
+
}
|
|
2275
|
+
/**
|
|
2276
|
+
* Receive EVERY link the native SDK is about to open - an in-app message
|
|
2277
|
+
* button or link, a `<JoryioPlacement />` button, a `<JoryioCardFeed />`
|
|
2278
|
+
* tap, an iOS push tap - before it opens: http(s) links and app deep links
|
|
2279
|
+
* (`myapp://cart`) alike, with where it came from (`link.source`) and the
|
|
2280
|
+
* ids that go with it. Return true to take the link over (navigate in your
|
|
2281
|
+
* app), false to let the native SDK open it as it does without a handler -
|
|
2282
|
+
* http(s) to the system (or your universal-link handling with
|
|
2283
|
+
* `forwardUniversalLinks` on iOS), an app deep link to the component's
|
|
2284
|
+
* `onDeepLink`. A Promise decides later. A refused scheme (javascript:,
|
|
2285
|
+
* data:, intent:, ...) never reaches it. One handler: a second call
|
|
2286
|
+
* replaces the first. Works before initialize(). Same name on iOS,
|
|
2287
|
+
* Android, web and Unity (`SetLinkHandler`).
|
|
2288
|
+
*/
|
|
2289
|
+
setLinkHandler(handler) {
|
|
2290
|
+
if (typeof handler !== 'function') {
|
|
2291
|
+
warn('setLinkHandler() needs a function; use clearLinkHandler() to remove one');
|
|
2292
|
+
return;
|
|
2293
|
+
}
|
|
2294
|
+
this._linkHandler = handler;
|
|
2295
|
+
if (!isModuleAvailable || !emitter)
|
|
2296
|
+
return;
|
|
2297
|
+
if (!this._linkSubscription) {
|
|
2298
|
+
this._linkSubscription = emitter.addListener('JoryioLink', (e) => this._onLink(e));
|
|
2299
|
+
}
|
|
2300
|
+
// Not held for initialize(): the native handler is app-wide and works before it.
|
|
2301
|
+
JoryioModule.enableLinkHandler?.();
|
|
2302
|
+
}
|
|
2303
|
+
/** Remove the handler set with setLinkHandler: links open as they do without one. */
|
|
2304
|
+
clearLinkHandler() {
|
|
2305
|
+
this._linkHandler = null;
|
|
2306
|
+
this._linkSubscription?.remove();
|
|
2307
|
+
this._linkSubscription = null;
|
|
2308
|
+
if (isModuleAvailable)
|
|
2309
|
+
JoryioModule.disableLinkHandler?.();
|
|
2310
|
+
}
|
|
2311
|
+
/** A link from native: ask the handler, then tell native whether it was taken. */
|
|
2312
|
+
_onLink(e) {
|
|
2313
|
+
const linkId = e?.linkId;
|
|
2314
|
+
if (typeof linkId !== 'string')
|
|
2315
|
+
return;
|
|
2316
|
+
const complete = (handled) => JoryioModule.completeLink?.(linkId, handled === true);
|
|
2317
|
+
const handler = this._linkHandler;
|
|
2318
|
+
const link = toLink(e);
|
|
2319
|
+
if (!handler || !link) {
|
|
2320
|
+
complete(false);
|
|
2321
|
+
return;
|
|
2322
|
+
}
|
|
2323
|
+
let result;
|
|
2324
|
+
try {
|
|
2325
|
+
result = handler(link);
|
|
2326
|
+
}
|
|
2327
|
+
catch (error) {
|
|
2328
|
+
// A throwing handler declines: the link still opens.
|
|
2329
|
+
warn(`setLinkHandler handler threw; the link opens as without one: ${String(error)}`);
|
|
2330
|
+
complete(false);
|
|
2331
|
+
return;
|
|
2332
|
+
}
|
|
2333
|
+
if (result && typeof result.then === 'function') {
|
|
2334
|
+
result.then(complete, (error) => {
|
|
2335
|
+
warn(`setLinkHandler handler rejected; the link opens as without one: ${String(error)}`);
|
|
2336
|
+
complete(false);
|
|
2337
|
+
});
|
|
2338
|
+
}
|
|
2339
|
+
else {
|
|
2340
|
+
complete(result);
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
1766
2343
|
/**
|
|
1767
2344
|
* Forget which in-app campaigns have already been shown on this device.
|
|
1768
2345
|
*
|