Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

background-only

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

background-only

This is a marker package to indicate that a module can only be used in the Background Thread.

  • 0.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
0
Weekly downloads
 
Created
Source

Background Only

This is a marker package to indicate that a module can only be used in the Background thread.

Example usages

Say we have a Logger module that has side effects calling into Native Modules or other APIs that are only available in the "Background" environment:

// Logger.js
import "background-only";

export function log(msg) {
  // Notice how we are calling into NativeModules here.
  NativeModules.hybridMonitor.reportJSError(...);
}

By adding import "background-only" to poison this module, we are now declaring that this module is only safe to be bundled in a "Background" environment, protecting this module from being accidentally bundled into a "Main thread" environment by throwing an error at runtime.

For example, if we use log in a desirable position from a React component, such as in useEffect or an event handler, the log will work as expected:

// App.jsx
import { log } from "./Logger";

function App() {
  useEffect() {
    log();
  }
  return <view />
}

However, if we use log in a undesirable position from a React component, such as in the body of the rendering function, it will throw an error at runtime time:

// App.jsx
import { log } from "./Logger";

function App() {
  // throw!
  log();
  return <view />
}

Credits

This is inspired by the server-only package of React.

Keywords

FAQs

Package last updated on 17 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc