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

fhook

v0.3.0

Published

Dependency Injection for functions via hooks

Downloads

18

Readme

fhook

Functional components with a predictable state, inspired by React-hooks and RxJS. Depeloped with ❤️ TypeScript.

NPM version Build Status Dependency Status Coverage percentage experimental

About

RxJS - is very powerful tool for create multithreading applications, but JavaScript not so suitable language for this problem and people with experience in JavaScript can't use RxJS as usually with imperative style, javascript operators, etc.

React Hooks - is new feature of React, which allow to create a difficult logic with multithreading by simple syntax, use only function (without classes). It has some expenses, because for every changing of state, we should call our function again. But javascript is very quickly and not so many applications have so strong requirements to speed (it is really not so critical).

fhook - is variant of work with observables, use hooks and simple control for execution. For start our greeting, look this example:

import { createFunc, never, useState, useEffect, subscribe, createSubscription } from "../../";

const counter = createFunc(() => {
    const [value, setValue] = useState(1);
    useEffect(() => {
        const timeoutId = setTimeout(() => setValue(value + 1), 1000);
        return () => clearTimeout(timeoutId);
    }, [value]);
    if (value === 5) {
        complete();
    }
    return value.toString();
});
const app = createFunc(() => {
    const [value, setValue] = useState("");
    counter().subscribe({ next: setValue });

    return value !== "" ? "Result: " + value : never();
});
createSubscription(app(), { next: (value) => console.log(value) });
/*
Result: 1
Result: 2
Result: 3
Result: 4
Result: 5
*/

Install

npm install fhook --save

or

yarn add fhook

Usage

Example 1: Simple counter

import { createSubscription, useEffect, useState } from "fhook";
function app() {
    const [counter, setCounter] = useState(0);
    useEffect(() => {
        const id = setTimeout(() => setCounter(counter + 1), 1000);
    }, [counter]);
    return counter;
}
createSubscription(app, { next: (value) => console.log("Counter: " + value) });
/*
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
...
*/

Example 2 Receive and send values

import { useState, usePipe, run, never, createContext, useContext, withContext, useEffect } from "fhook";

const MaxContext = createContext<number>();

function source() {
    const [result, setResult] = useState(0);
    const max = useContext(MaxContext);
    useEffect(() => {
        setInterval(() => {
            setResult(Math.round(Math.random() * max));
        }, 1000);
    }, []);
    return !result ? never() : result;
}

function app() {
    const [value, setValue] = useState("");
    withContext(MaxContext, 10);
    usePipe(source, [], (num) => setValue(num.toString()));
    return value ? "Number: " + value : never();
}
run(app).subscribe((value) => console.log(value));
/*
Number: 9
Number: 3
Number: 1
Number: 4
Number: 1
*/

Example 3: Complex app

In this example, we run function app, which provide DelimiterContext, then it use function one for return itself value. Function one create state name, setName and for first time use function two. Function two use effect, wait one second and set isReady to true, it allow return value Hell In function one we check name for contains a symbol H and return never (which stop propagination a value for this function). Same time we change use function two to three. Function three return to us a value World concatened with ThemeContext (Yellow World). And now, function one can return value Hello, Yellow World! and app propaginate this value and our subscriber got it!

import { useState, usePipe, run, never, createContext, useContext, withContext, useEffect } from "fhook";

const DelimiterContext = createContext<string>();
const ThemeContext = createContext();

function one() {
    const [name, setName] = useState<string | null>(null);
    withContext(ThemeContext, "Yellow");
    if (name === null) {
        usePipe(two, [], setName);
    } else {
        usePipe(three, [], setName);
    }
    return name === null || name.indexOf("H") > -1 ? never() : "Hello, " + name;
}
function two() {
    const [isReady, setIsReady] = useState(false);
    useEffect(() => {
        setTimeout(() => {
            setIsReady(true);
        }, 1000);
    }, []);
    return !isReady ? never() : "Hell";
}
function three() {
    return useContext(ThemeContext) + " World";
}

function app() {
    withContext(DelimiterContext, ":");
    return usePipe(one, []);
}

const { subscribe, dispose } = run(app);

subscribe((value) => {
    console.log(value); // "Hello, Yellow World!"
    dispose();
});

API


useContext<T>(context: Context<T>): T;
usePipe<R, A extends any[]>(fn: IFunction<R, A>, args: A, next?: (value: R) => void): symbol;
useState<T>(defaultValue: T): [T, (value: T) => void];
useEffect(callback: () => any, deps?: any[]): void;
withContext<T>(context: Context<T>, value: T): void;

class Context<T = any> {

}

function createContext<T>(params: IContextParams<T> = {}): Context<T>;

Test

npm install
npm test