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

f-fuse

v0.2.0

Published

Defining functions to be executed during special periods.

Downloads

1

Readme

f-fuse

NPM version

Defining functions to be executed during special periods
NPM page: https://www.npmjs.com/package/f-fuse

Installation

npm install f-fuse --save

User Guide

// typescript: 

import {Fuse} from "../src";


let f = new Fuse();

interface func {
    (item: number): number
}

// 使用Fuse定义的函数默认只会执行一次,之后重复调用将不再执行
// 这个行为可以定制,详见Fuse API文档
let print_info: func = f.def(function (item) {
    console.log("函数被调用: ", item);
    return item;
});

f.on(function (item: number) {
    return item > 3;
});

[1, 2, 3, 4, 5, 6, 7].forEach(value => {
    // print_info函数执行一次后,再次调用将不会执行
    let result = print_info(value);
    console.log("调用结果", result);
});

export default {}
// javascript: 

import {Fuse} from "../src";


// 示例1: 传入参数大于3时,被执行一次之后不再执行且无返回值
let f = new Fuse();

let print_info = f.def(function (item) {
    console.log("函数被调用: ", item);
    return item;
});

f.on(function (item) {
    return item > 3;
});

[1, 2, 3, 4, 5, 6, 7].forEach(value => {
    let result = print_info(value);
    console.log("调用结果", result);
});
// javascript: 

// 示例2: 传入参数大于3时,除非手动调用cut()方法终止函数执行,否则与正常函数执行一样
// 注意手动模式下默认函数是不执行的,需要手动调用weld()方法,函数才会正常执行

let f2 = new Fuse({manual: true, memory: false});
f2.weld();

let print_info2 = f2.def(function (item) {
    console.log("函数被调用: ", item);
    return item;
});

f2.on(function (item) {
    return item > 3;
});

[1, 2, 3, 4, 5, 6, 7].forEach(value => {
    let result = print_info2(value);
    console.log("调用结果", result);
});
// javascript: 

// 示例3: 始终返回函数被cut时的返回值,但是函数不会被调用

let f3 = new Fuse({manual: false, memory: true});

let print_info3 = f3.def(function (item) {
    console.log("函数被调用: ", item);
    return item;
});

f3.on(function (item) {
    return item > 3;
});

[1, 2, 3, 4, 5, 6, 7].forEach(value => {
    let result = print_info3(value);
    console.log("调用结果", result);
});
传递闭包参数,closure方法传入的值可以通过closure_arg属性获取,之后可以根据该值进行判定
如下面的函数当且仅当传入索引值为3时才执行

let f = new Fuse({manual: true});
f.weld();
f.on(function (this: Fuse, item: number) {
    return this.closure_arg[0] === 3;
});


let print_info: (item: number) => number = f.def(function (item) {
    console.log("函数被调用: ", item);
    return item;
});


[1, 2, 3, 4, 5, 2, 7].forEach((value, index) => {
    f.closure(index);
    let result = print_info(value);
    console.log("调用结果", result);
});

Note

默认情况:函数执行一次后自动被cut,导致之后不会被执行,且调用函数无任何返回内容
如不定义on方法,fuse定义的函数将会在被执行第一次即为条件满足被熔断

  • new Fuse(options)

    • manual:手动模式[默认是熔断的,需要调用weld方法,手动模式下函数不会触发一次后就自动熔断,除非手动调用cut方法]
    • memory:启用记忆[开启记忆功能,函数仍然不会重复被执行,但会始终返回函数被cut时的返回值]
  • weld() : 调用该方法,让fuse定义的函数可以正常被执行

  • cut() : 调用该方法,让fuse定义的函数无法被执行

  • on(callback) : 当回调函数返回true时,函数被执行,不声明默认情况下全部情况返回true

  • def(func) : 使用def定义特殊的函数,返回值为函数本身

  • closure() : 闭包函数允许你传递一些值,这些值附着在closure_arg属性上,当使用on注册when回调函数时,可以通过this.closure_arg获取

Resources

You can read f-fuse Documentation online for more information.

License

f-fuse uses the MIT license, see LICENSE file for the details.