npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

function-caller

v1.1.1

Published

[![npm version](https://img.shields.io/npm/v/function-caller)](https://www.npmjs.com/package/function-caller) [![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=function-caller&query=$.install.pretty&lab

Downloads

51

Readme

function-caller

npm version install size check-code-coverage npm type definitions NPM

function-caller is a cross-component function call library written in Typescript

Installing

Package manager

Using npm:

npm install function-caller

Using yarn:

$ yarn add function-caller

Once the package is installed, you can import the library using import or require approach:

import FunctionCaller, {set, call} from 'function-caller';

If you use require for importing, only default export is available:

const FunctionCaller = require('function-caller');

CDN

Using unpkg CDN:

<script src="https://unpkg.com/function-caller/dist/index.min.js"></script>

Example

Basic example

// create a function
const myFunc = function (a, b) {
    return a + b;
}

// register a function with the function-caller
const myKey = 'myKey';
FunctionCaller.set(myKey, myFunc);

// call a function using the function-caller
const callReturn = FunctionCaller.call(myKey, 2, 3);
console.log(callReturn); // 5

// call a function asynchronously using the function-caller
FunctionCaller.asyncCall(myKey, 4, 6).then((value) => {
    console.log(value); // 10
}); 

Unregister function

// create a function
const myFunc = function (a, b) {
    return a + b;
}

// register a function with the function-caller
const myKey = 'myKey';
FunctionCaller.set(myKey, myFunc);

// unregister in function-caller
FunctionCaller.remove(myKey);

Clear all registration

FunctionCaller.set('key1', ()=>{});
FunctionCaller.set('key2', ()=>{});
FunctionCaller.set('key3', ()=>{});

FunctionCaller.clear();
// all registration are removed

Working with React

Register function in component A

import React, { useState, useEffect } from 'react'
// import function-caller library
import FunctionCaller from 'function-caller'

// export registration key
export const SET_TEXT = "set text";

export default function A() {
  // create a function
  const [text, setText] = useState('Hi');

  useEffect(() => {
    // register function when the component did mount
    FunctionCaller.set(SET_TEXT, setText);
    return () => {
      // unregister function when component will unmount
      FunctionCaller.remove(SET_TEXT);
    }
  }, [])

  return (
    <div>
      A Component
      <span>{text}</span>
    </div>
  )
}

Call function in component B

import React from 'react'
// import function-caller library
import FunctionCaller from 'function-caller'

// import registration key
import { SET_TEXT } from '../A';

export default function B() {
  const onChange = (event) => {
    const { target } = event;
    const newText = target.value;
    // call the function using the function-caller
    FunctionCaller.call(SET_TEXT, newText);
  }
  return (
    <div>
      B Component
      <input onChange={onChange}/>
    </div>
  )
}

function-caller API

| function | return | | -------------------------------------- | ------------------------------------------------- | | set(key: string, func: Function) | boolean | | call(key: string, ...args: any[]) | any | | asyncCall(key: string, ...args: any[]) | Promise<any> | | remove(key: string) | void | | clear() | void | | hasKey(key: string) | boolean | | getKeys() | IterableIterator<string> | | getEntries() | IterableIterator<[string, Function]> | | getFunction(key: string) | Function | undefined | | getSize() | number |

License

MIT