summaryrefslogtreecommitdiff
path: root/node_modules/@jet-app/app-store/tmp/src/common/ads/ad-stitcher.js
blob: c9fc6b9a4c741503259866c8b70ccfa712f31cb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * Handles stitching ads into pages.
 *
 * At a high level:
 * 1. Setup `AdStitcher` w/ content to stitch.
 * 2. Pass to builder
 * 3. Destructively consume tasks during building.
 */
"use strict";
import { isNull } from "../../foundation/json-parsing/server-data";
/**
 * Creates a string identifier representing the position information for an ad stitch task.
 * @param positionInfo The position information object to create an identifier for.
 * @returns The string identifier.
 */
function stringIdentifierForPositionInfo(positionInfo) {
    return `${positionInfo.shelfIdentifier}.${positionInfo.slot}`;
}
// region exports
/**
 * Create a new ad stitcher
 */
export function newAdStitcher() {
    return {
        tasks: {},
    };
}
/// Add task
export function addTask(stitcher, task) {
    if (isNull(stitcher)) {
        return;
    }
    const positionIdentifier = stringIdentifierForPositionInfo(task.positionInfo);
    stitcher.tasks[positionIdentifier] = task;
}
/**
 * Consume a single task, if available, for a given position information.
 * @param stitcher The relevant ad stitcher.
 * @param positionInfo The position information to check for an available task.
 * @returns A task for the position, or null if unavailable.
 */
export function consumeTask(stitcher, positionInfo) {
    if (isNull(stitcher)) {
        return null;
    }
    const positionIdentifier = stringIdentifierForPositionInfo(positionInfo);
    const task = stitcher.tasks[positionIdentifier];
    if (isNull(task)) {
        return null;
    }
    delete stitcher.tasks[positionIdentifier];
    return task;
}
/**
 * Consume all tasks for a given shelf identifier. Useful where ad data is being stitched in prior to view models being built.
 * @param stitcher The relevant ad stitcher.
 * @param shelfIdentifier The identifier for the shelf to get all available tasks for.
 * @returns An array of relevant tasks for the shelf.
 */
export function consumeTasksForShelfIdentifier(stitcher, shelfIdentifier) {
    if (isNull(stitcher)) {
        return [];
    }
    const tasksForShelf = [];
    Object.entries(stitcher.tasks).forEach(([key, value]) => {
        if (key.startsWith(shelfIdentifier)) {
            tasksForShelf.push(value);
            delete stitcher.tasks[key];
        }
    });
    return tasksForShelf;
}
//# sourceMappingURL=ad-stitcher.js.map