Skip to content
Foxipa
Code Completion 5 Challenges

Complete the code: Decision Tree & Hybrid Transport

Fill in the missing routing logic of the hybrid sender.

Intermediate ~4 min read

Challenge 1

The decision tree's first branch condition.

js Your answer
export function send(payload) {
  const dying = document.visibilityState === '';
  return dying ? viaBeacon(payload) : viaKeepalive(payload);
}

Press Ctrl⌘Enter to check your answer

Challenge 2

Gate the superset path on real support.

js Your answer
const caps = detectBeaconCapabilities();
const transport = caps.fetchKeepalive ? 'fetch' : '';

Press Ctrl⌘Enter to check your answer

Challenge 3

The hybrid sender budgets both transports against…

js Your answer
// both transports charge the same
const pool = ''; // 64 KiB, per fetch group

Press Ctrl⌘Enter to check your answer

Challenge 4

At page death, which transport gets the send?

js Your answer
function chooseAtTeardown(): Transport {
  return capabilities.sendBeacon ? { kind: '' } : { kind: 'fetch', keepalive: true };
}

Press Ctrl⌘Enter to check your answer

Challenge 5

Batch interactive events, then flush on the transition.

js Your answer
queue.push(event);
if (queue.length >= BATCH || visibility === 'hidden') {
  (queue.splice(0));
}

Press Ctrl⌘Enter to check your answer