nip07-awaiter
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

nip07-awaiter

nip07-awaiter provides a few utilities to detect initialization of NIP-07 interface (window.nostr).

Installation

npm install nip07-awaiter

or

<script type="module" src="https://cdn.jsdelivr.net/npm/nip07-awaiter@latest/dist/index.mjs">

Usage

Events

Four custom events are available on window object:

  • beforenostrload: Fire before window.nostr is initialized. Cancellable.
  • nostrloaded: Fire after window.nostr is initialized.
  • beforenostrupdate: Fire before window.nostr is updated for the second or subsequent. Cancellable.
  • nostrupdated: Fire after window.nostr is updated for the second or subsequent.
window.addEventListener("beforenostrload", (ev) => {
  console.log(ev.detail.nostr /* NIP-07 interface to be installed */);
  console.log(window.nostr === undefined /* => true */);

  // You can cancel the installation.
  ev.preventDefault();
});

window.addEventListener("nostrloaded", (ev) => {
  console.log(ev.detail.nostr /* NIP-07 interface installed */);
  console.log(ev.detail.nostr === window.nostr /* => true */);
});

Promise

readyNostr is a promise that resolves to NIP-07 interface.

import { readyNostr } from "nip07-awaiter";

const nostr = await readyNostr;

If you want to set a timeout, you can use waitNostr instead.

import { waitNostr } from "nip07-awaiter";

const nostrOrUndefined = await waitNostr(1000);

Readme

Keywords

Package Sidebar

Install

npm i nip07-awaiter

Weekly Downloads

105

Version

0.2.1

License

MIT

Unpacked Size

9.87 kB

Total Files

8

Last publish

Collaborators

  • penpenpng