Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

atomic

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

atomic - npm Package Compare versions

Comparing version 0.0.2 to 1.0.0

dist/cjs/index.d.ts

97

package.json

@@ -1,18 +0,81 @@

{ "name" : "atomic"
, "description" : "Atomic operations"
, "version" : "0.0.2"
, "homepage" : "https://github.com/stagas/atomic"
, "author" : "George Stagas <gstagas@gmail.com> (http://stagas.com/)"
, "repository" :
{ "type" : "git"
, "url" : "https://github.com/stagas/atomic.git"
{
"name": "atomic",
"author": "stagas",
"short": "stagas/atomic",
"description": "makes a function atomic with timeout and abort signal",
"version": "1.0.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://git@github.com:stagas/atomic.git"
},
"keywords": [
"atomic",
"promise",
"concurrency",
"timeout",
"signal",
"abort",
"race-conditions"
],
"module": "dist/esm/index.js",
"main": "dist/cjs/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"files": [
"dist"
],
"scripts": {
"build:watch": "tsc --outDir dist/esm --module esnext --watch",
"cov:watch": "jest --coverage --watchAll",
"test": "npm run test:node && npm run test:web",
"test:node": "jest",
"test:web": "web-test-runner",
"cov": "jest --coverage",
"docs": "documentation readme src --resolve=node --pe ts --re ts --re d.ts --github --section=API --markdown-toc-max-depth=3",
"build": "npm run build:cjs && npm run build:esm && echo done.",
"build:cjs": "tsc --outDir dist/cjs",
"build:esm": "tsc --outDir dist/esm --module esnext && echo '{\"type\":\"module\"}' >dist/esm/package.json",
"build:min": "esbuild src/index.ts --bundle --format=esm | terser --compress --module --mangle -o build.min.js",
"lint": "eslint src && prettier --check src",
"lint:fix": "eslint --fix src && prettier --write src",
"clean": "rimraf dist",
"prepack": "npm run clean && npm run build",
"prepack:dry": "npm pack --dry-run",
"prepare": "husky install"
},
"devDependencies": {
"@n1kk/intspector": "^1.0.2",
"@rollup/plugin-commonjs": "^21.0.1",
"@stagas/documentation-fork": "^13.2.5",
"@stagas/sucrase-jest-plugin": "^2.2.0-fork",
"@swc-node/jest": "^1.4.3",
"@tsconfig/node16": "^1.0.2",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.9",
"@typescript-eslint/eslint-plugin": "^5.10.0",
"@typescript-eslint/parser": "^5.10.0",
"@web/dev-server-esbuild": "^0.2.16",
"@web/dev-server-rollup": "^0.3.14",
"@web/test-runner": "^0.13.25",
"esbuild": "^0.14.11",
"esbuild-register": "^3.3.2",
"eslint": "^8.7.0",
"eslint-config-html-jsx": "^1.0.0",
"eslint-plugin-import": "^2.25.4",
"husky": "^7.0.4",
"jest": "^27.4.7",
"jest-browser-globals": "^25.1.0-beta",
"onchange": "^7.1.0",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"terser": "^5.10.0",
"ts-jest": "^27.1.3",
"ts-node": "^10.4.0",
"typescript": "^4.5.4"
}
, "main": "./index.js"
, "devDependencies" : {
"tap" : "=0.0.x"
}
, "scripts" : {
"test" : "tap test"
}
, "engines" : { "node" : ">=0.4.12" }
}
}

90

README.md

@@ -1,52 +0,74 @@

# atomic
<h1 align="center">atomic</h1>
Atomic operations
<p align="center">
makes a function atomic with timeout and abort signal
</p>
<p align="center">
<a href="#install"> 🔧 <strong>Install</strong></a>
· <a href="#example"> 🧩 <strong>Example</strong></a>
· <a href="#api"> 📜 <strong>API docs</strong></a>
· <a href="https://github.com/stagas/atomic/releases"> 🔥 <strong>Releases</strong></a>
· <a href="#contribute"> 💪🏼 <strong>Contribute</strong></a>
· <a href="https://github.com/stagas/atomic/issues"> 🖐️ <strong>Help</strong></a>
</p>
## Introduction
***
atomic lets you perform atomic operations based on keys. Additional tasks that
try to access the same key get queued up and executed when the previous task
calls `done()` to indicate it has finished its operation. It's useful when you
want to perform a multiple I/O calls on databases and don't want anything else
messing with your values in-between calls.
## Install
```sh
$ npm i atomic
```
## Installation
## API
`npm install atomic`
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
#### Table of Contents
## Example
* [atomic](#atomic)
* [Parameters](#parameters)
```javascript
var atomic = require('atomic')()
### atomic
var o = {} // here's our db
[src/index.ts:33-78](https://github.com/stagas/atomic/blob/2a0a9f5694e915eb4fe86dea4895ed398d1458c3/src/index.ts#L33-L78 "Source code on GitHub")
for (var i = 1; i <= 100; i++) {
// create an atomic operation on key 'foo'
atomic('foo', function (done, key, i) {
setTimeout(function () {
o[key] = i // store i in o[key]
done() // done with this task, "unlock" key
// and move to the next task in queue
}, Math.floor(Math.random() * 50)) // sometime in the future
}, i) // pass i to the arguments (closure)
}
Makes a function atomic.
atomic('foo', function (done, key) {
console.log(o[key])
done()
})
```js
const fn = atomic(signal => async () => {
// some long async operation
// if we've been aborted during the long
// async process above, we wouldn't want
// to continue so lets return here
if (signal.aborted) return
// do things here if we didn't abort
}, 500) // timeout at 500ms (don't pass anything for no timeout)
fn()
fn()
fn()
await fn() // this will run after the above have settled
```
#### Parameters
## Usage
* `signalClosure` **function (signal: AbortSignal): function (...args: [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<any>): [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<any>** A function that receives the signal object
from an AbortController and returns the function to become atomic.
* `maxTimeMs` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Time in milliseconds to timeout the operation. Will also signal abort.
### atomic(key, callback(done, key, arg1, arg2, ...) [, arg1, arg2, ...])
Returns **any** An atomic function
This creates an atomic operation on `key`. The callback is called when the key
is free (other tasks have finished working on it) and you must call `done()`
when you're finished your operations. For convenience, the key name and extra
arguments are passed to the callback for your function to use.
## Contribute
[Fork](https://github.com/stagas/atomic/fork) or
[edit](https://github.dev/stagas/atomic) and submit a PR.
All contributions are welcome!
## License
MIT © 2022
[stagas](https://github.com/stagas)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc