{"id":15896098,"url":"https://github.com/peerlibrary/node-fiber-utils","last_synced_at":"2025-03-20T15:32:18.366Z","repository":{"id":52144367,"uuid":"38416730","full_name":"peerlibrary/node-fiber-utils","owner":"peerlibrary","description":"Various fiber utilities","archived":false,"fork":false,"pushed_at":"2021-05-06T21:01:34.000Z","size":63,"stargazers_count":3,"open_issues_count":3,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-06T23:15:25.325Z","etag":null,"topics":["fibers","meteor","meteor-package","nodejs","npm","npm-package"],"latest_commit_sha":null,"homepage":"","language":"CoffeeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peerlibrary.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}},"created_at":"2015-07-02T06:59:39.000Z","updated_at":"2020-07-16T05:30:58.000Z","dependencies_parsed_at":"2022-09-05T02:42:02.913Z","dependency_job_id":null,"html_url":"https://github.com/peerlibrary/node-fiber-utils","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerlibrary%2Fnode-fiber-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerlibrary%2Fnode-fiber-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerlibrary%2Fnode-fiber-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerlibrary%2Fnode-fiber-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peerlibrary","download_url":"https://codeload.github.com/peerlibrary/node-fiber-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221776117,"owners_count":16878490,"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":["fibers","meteor","meteor-package","nodejs","npm","npm-package"],"created_at":"2024-10-06T09:06:00.850Z","updated_at":"2024-10-28T04:02:09.298Z","avatar_url":"https://github.com/peerlibrary.png","language":"CoffeeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Fiber utilities\n===============\n\nA package which provides utilities for [node fibers](https://github.com/laverdet/node-fibers).\n\nInstallation\n------------\n\nNPM:\n\n```\nnpm install fiber-utils\n```\n\nMeteor:\n\n```\nmeteor add peerlibrary:fiber-utils\n```\n\nInitialization (NPM)\n--------------------\n\nNPM package does not depend directly on fibers package because you have to make sure you\nhave loaded fibers package only once. This is why you should install fibers package yourself,\nand then initialize this package using symbols imported from the fibers package:\n\n```js\nvar Fiber = require('fibers');\nvar Future = require('fibers/future')\nvar FiberUtils = new require('fiber-utils').FiberUtils(Fiber, Future);\n```\n\nInitialization (Meteor)\n-----------------------\n\nMeteor package exports initialized `FiberUtils` symbol automatically.\nIt is initialized with fibers package provided by Meteor and shared through all\nits code and packages.\n\nUsage\n-----\n\n### `FiberUtils.sleep(ms)` ###\n\nSleep for `ms` milliseconds inside a current fiber.\n\n### `FiberUtils.wrap(f, scope)` ###\n\nReturns a fiber-enabled synchronous function, which when called will pass any arguments to the\noriginal function `f`, and then wait for the function to finish inside a current fiber.\nYou can optionally bind `this` to `scope` during execution of `f`.\n\n### `FiberUtils.in(f, scope, handleErrors)` ###\n\nWrap function `f` in a way to assure that it is run inside a fiber. If the wrapped function is\ncalled already inside a fiber, it is simply normally executed. But if it is outside of any fiber,\nthen a new fiber is constructed and function `f` is executed inside it.\nYou can optionally bind `this` to `scope` during execution of `f`.\n\nIf you provide `handleErrors`, any exception thrown from a execution of `f` is passed to `handleErrors`.\nIf `handleErrors` is not provided, exception propagates on unhandled.\n\n### `FiberUtils.ensure(f, scope, handleErrors)` ###\n\nSimilar to `FiberUtils.in(f, scope, handleErrors)`, but it also calls wrapped function immediately.\n\n### `FiberUtils.synchronize(guardObject, uniqueId, f, options)` ###\n\nCalls function `f` in a way that only one fiber can call it at a time, and all other calls are queued.\nAfter one execution of `f` finished, the next queued call of `f` starts.\nThis allows one to implement critical sections of your code in which you want to ensure only\none fiber is running it, even if the fiber yields during the execution of the critical section.\n\n`guardObject` serves as an object to synchronize on. You could use one object for everything,\nor you might want a fine-grained synchronization and for example use a current object in a method to\nlimit synchronization to each instance of a class.\n\n`uniqueId` is an ID of this critical section. Again, allows you to decide how granular you want\nyour synchronization to be. It is namespaced to `guardObject`.\n\nThere are the following `options` available:\n* `allowRecursive` (default `true`): do you want to allow recursive reentry of a critical section within the same fiber\n* `allowNested` (default `true`): do you want to allow nested critical sections, which can lead to deadlocks\n* `breakDeadlocks` (default `true`): if we detect a deadlock, we can break the deadlock by making one critical section throw an exception\n\nExamples\n--------\n\n```js\nclass Bank {\n  transfer(from, to, amount) {\n    FiberUtils.ensure(() =\u003e {\n      FiberUtils.synchronize(this, 'transfer', () =\u003e {\n        from.decrease(amount)\n        to.increase(amount)\n      })\n    })\n  }  \n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeerlibrary%2Fnode-fiber-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeerlibrary%2Fnode-fiber-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeerlibrary%2Fnode-fiber-utils/lists"}