{"id":20163816,"url":"https://github.com/systemlight/f-fuse","last_synced_at":"2025-03-03T03:12:58.811Z","repository":{"id":43988433,"uuid":"242340054","full_name":"SystemLight/f-fuse","owner":"SystemLight","description":":zap: Defining functions to be executed during special periods.【定义特殊执行的函数】","archived":false,"fork":false,"pushed_at":"2023-03-15T06:47:49.000Z","size":663,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-28T22:48:48.066Z","etag":null,"topics":["esnext","function","specific"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SystemLight.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-22T12:44:03.000Z","updated_at":"2022-10-29T17:19:04.000Z","dependencies_parsed_at":"2024-10-23T06:29:00.782Z","dependency_job_id":"8d4e4636-20f1-41eb-adc2-e38ed529a41c","html_url":"https://github.com/SystemLight/f-fuse","commit_stats":{"total_commits":13,"total_committers":2,"mean_commits":6.5,"dds":"0.15384615384615385","last_synced_commit":"a2fa87f78dba636f60005097d0a5b99f39a7a5e4"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystemLight%2Ff-fuse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystemLight%2Ff-fuse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystemLight%2Ff-fuse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystemLight%2Ff-fuse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SystemLight","download_url":"https://codeload.github.com/SystemLight/f-fuse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241600492,"owners_count":19988715,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esnext","function","specific"],"created_at":"2024-11-14T00:31:56.489Z","updated_at":"2025-03-03T03:12:58.795Z","avatar_url":"https://github.com/SystemLight.png","language":"TypeScript","readme":"# f-fuse\n\n[![NPM version](https://img.shields.io/npm/v/f-fuse.svg)](https://www.npmjs.com/package/f-fuse)\n\nDefining functions to be executed during special periods  \nNPM page: https://www.npmjs.com/package/f-fuse\n\n# Installation\n\n```\nnpm install f-fuse --save\n```\n\n# User Guide\n\n```\n// typescript: \n\nimport Fuse from \"f-fuse\";\n\n\nlet f = new Fuse();\n\ninterface func {\n    (item: number): number\n}\n\n// 使用Fuse定义的函数默认只会执行一次，之后重复调用将不再执行\n// 这个行为可以定制，详见Fuse API文档\nlet print_info: func = f.def(function (item) {\n    console.log(\"函数被调用: \", item);\n    return item;\n});\n\nf.on(function (item: number) {\n    return item \u003e 3;\n});\n\n[1, 2, 3, 4, 5, 6, 7].forEach(value =\u003e {\n    // print_info函数执行一次后，再次调用将不会执行\n    let result = print_info(value);\n    console.log(\"调用结果\", result);\n});\n\nexport default {}\n```\n\n```\n// javascript: \n\nimport Fuse from \"f-fuse\";\n\n\n// 示例1： 传入参数大于3时，被执行一次之后不再执行且无返回值\nlet f = new Fuse();\n\nlet print_info = f.def(function (item) {\n    console.log(\"函数被调用: \", item);\n    return item;\n});\n\nf.on(function (item) {\n    return item \u003e 3;\n});\n\n[1, 2, 3, 4, 5, 6, 7].forEach(value =\u003e {\n    let result = print_info(value);\n    console.log(\"调用结果\", result);\n});\n```\n\n```\n// javascript: \n\n// 示例2： 传入参数大于3时，除非手动调用cut()方法终止函数执行，否则与正常函数执行一样\n// 注意手动模式下默认函数是不执行的，需要手动调用weld()方法，函数才会正常执行\n\nlet f2 = new Fuse({manual: true, memory: false});\nf2.weld();\n\nlet print_info2 = f2.def(function (item) {\n    console.log(\"函数被调用: \", item);\n    return item;\n});\n\nf2.on(function (item) {\n    return item \u003e 3;\n});\n\n[1, 2, 3, 4, 5, 6, 7].forEach(value =\u003e {\n    let result = print_info2(value);\n    console.log(\"调用结果\", result);\n});\n```\n\n```\n// javascript: \n\n// 示例3： 始终返回函数被cut时的返回值，但是函数不会被调用\n\nlet f3 = new Fuse({manual: false, memory: true});\n\nlet print_info3 = f3.def(function (item) {\n    console.log(\"函数被调用: \", item);\n    return item;\n});\n\nf3.on(function (item) {\n    return item \u003e 3;\n});\n\n[1, 2, 3, 4, 5, 6, 7].forEach(value =\u003e {\n    let result = print_info3(value);\n    console.log(\"调用结果\", result);\n});\n```\n\n```\n传递闭包参数，closure方法传入的值可以通过closure_arg属性获取，之后可以根据该值进行判定\n如下面的函数当且仅当传入索引值为3时才执行\n\nlet f = new Fuse({manual: true});\nf.weld();\nf.on(function (this: Fuse, item: number) {\n    return this.closure_arg[0] === 3;\n});\n\n\nlet print_info: (item: number) =\u003e number = f.def(function (item) {\n    console.log(\"函数被调用: \", item);\n    return item;\n});\n\n\n[1, 2, 3, 4, 5, 2, 7].forEach((value, index) =\u003e {\n    f.closure(index);\n    let result = print_info(value);\n    console.log(\"调用结果\", result);\n});\n```\n\n# Note\n\n默认情况：函数执行一次后自动被cut，导致之后不会被执行，且调用函数无任何返回内容  \n如不定义on方法，fuse定义的函数将会在被执行第一次即为条件满足被熔断\n\n- new Fuse(options)\n  - manual:手动模式[默认是熔断的，需要调用weld方法，手动模式下函数不会触发一次后就自动熔断，除非手动调用cut方法]\n  - memory:启用记忆[开启记忆功能，函数仍然不会重复被执行，但会始终返回函数被cut时的返回值]\n\n- weld() : 调用该方法，让fuse定义的函数可以正常被执行\n- cut() : 调用该方法，让fuse定义的函数无法被执行\n- on(callback) : 当回调函数返回true时，函数被执行，不声明默认情况下全部情况返回true\n- def(func) : 使用def定义特殊的函数，返回值为函数本身\n- closure() : 闭包函数允许你传递一些值，这些值附着在closure_arg属性上，当使用on注册when回调函数时，可以通过this.closure_arg获取\n\n# Resources\n\nYou can read [f-fuse Documentation](https://github.com/SystemLight/f-fuse) online for more information.\n\n# License\n\nf-fuse uses the MIT license, see LICENSE file for the details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsystemlight%2Ff-fuse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsystemlight%2Ff-fuse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsystemlight%2Ff-fuse/lists"}