Git Product home page Git Product logo

budget-api's Introduction

Web Budget API

Draft specification

Web applications have been able to execute code, make network requests, and interact with the user through a browser tab or standalone browser window. This has allowed users to directly see that a web application is executing code, and generally doing useful things, on their behalf.

Following the introduction of the Push API and Background Synchronization, this assumption no longer holds: web applications are now able to both trigger and schedule execution of code in the background, outside of the user’s control. This brings web application functionality closer to native applications or browser extensions, both of which are able to perform extensive Background Operations.

In order to protect the user, Chrome has historically required developers to display a notification in response to a message. Firefox grants developers a budget based on the level of engagement a user has with a website, which is a model that Chrome, with the release of Chrome 52, has moved to as well.

In both cases, it's an unknown to the developer whether they have to show a notification. This eliminates a lot of potential use-cases for the Push API. The Budget API aims to standardize this concept of budget in a way that provides value for developers, while not locking user agents into any particular implementation.

Today the Budget API focuses on the Push API, but we chose to generalize the concept in order to (1) be able to provide both immediate and expected values, enabling developers to do near-term resource planning, (2) be able to extend existing APIs such as Background Sync both to alleviate the restrictions and to enable developers to request more retries, and (3) enable future resource consuming APIs such as a Job Scheduler to use the same mechanism.

This API solely focuses on resource consuming APIs.

Use-cases

  • Deciding to not show a notification in response to a low priority push message whose primary purpose was to synchronize data.
  • Deciding whether the origin can schedule a precise timer using a hypothetical Job Scheduler API.
  • Deciding on the frequency of server-initiated cache updates of synchronized data.
  • Deciding on the server whether there is sufficient budget available to hide previously shown notifications when the user dismissed them on other devices.
  • Deciding to temporarily limit background operations if the budget could be used during an upcoming sporting event instead.

Examples

1. Avoid showing low-priority notifications to the user.

self.addEventListener('push', event => {
  // Execute the application-specific logic depending on the contents of the
  // received push message, for example by storing the received update.

  event.waitUntil(async () => {
    const reserved = await navigator.budget.reserve('silent-push');
    if (reserved) {
      return; // No need to show a notification.
    }
    // Not enough budget is available, must show a notification.
    return registration.showNotification(...);
  });
});

2. Calculate the number of precise timers that can be used at some point in the future.

async function getPreciseTimersAvailableAt(time) {
  const [ cost, budget ] = await Promise.all([
    navigator.budget.getCost('precise-timer'),
    navigator.budget.getBudget()
  ]);

  for (const state of budget) {
    if (state.time <= time) {
      continue;
    }

    // The |state| occurs after |time|, so return the budget.
    return state.budgetAt / cost;
  }

  // No budget after |time| is known, so we can't guarantee anything.
  return 0;
}

budget-api's People

Contributors

beverloo avatar foolip avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.