Git Product home page Git Product logo

mortice's Introduction

mortice

codecov CI

Isomorphic read/write lock that works in single processes, node clusters and web workers

About

  • Reads occur concurrently
  • Writes occur one at a time
  • No reads occur while a write operation is in progress
  • Locks can be created with different names
  • Reads/writes can time out

Usage

import mortice from 'mortice'
import delay from 'delay'

// the lock name & options objects are both optional
const mutex = mortice('my-lock', {

  // how long before write locks time out (default: 24 hours)
  timeout: 30000,

   // control how many read operations are executed concurrently (default: Infinity)
  concurrency: 5,

  // by default the the lock will be held on the main thread, set this to true if the
  // a lock should reside on each worker (default: false)
  singleProcess: false
})

Promise.all([
  (async () => {
    const release = await mutex.readLock()

    try {
      console.info('read 1')
    } finally {
      release()
    }
  })(),
  (async () => {
    const release = await mutex.readLock()

    try {
      console.info('read 2')
    } finally {
      release()
    }
  })(),
  (async () => {
    const release = await mutex.writeLock()

    try {
      await delay(1000)

      console.info('write 1')
    } finally {
      release()
    }
  })(),
  (async () => {
    const release = await mutex.readLock()

    try {
      console.info('read 3')
    } finally {
      release()
    }
  })()
])
read 1
read 2
<small pause>
write 1
read 3

Browser

Because there's no global way to evesdrop on messages sent by Web Workers, please pass all created Web Workers to the observable-webworkers module:

// main.js
import mortice from 'mortice'
import observe from 'observable-webworkers'

// create our lock on the main thread, it will be held here
const mutex = mortice()

const worker = new Worker('worker.js')

observe(worker)
// worker.js
import mortice from 'mortice'
import delay from 'delay'

const mutex = mortice()

let release = await mutex.readLock()
// read something
release()

release = await mutex.writeLock()
// write something
release()

Install

$ npm i mortice

Browser <script> tag

Loading this module through a script tag will make it's exports available as Mortice in the global namespace.

<script src="https://unpkg.com/mortice/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

mortice's People

Contributors

achingbrain avatar semantic-release-bot avatar dirkmc avatar dependabot[bot] avatar

Stargazers

Nobody avatar Angx avatar v1rtl avatar Abhishiv Saxena avatar Andrejs Agejevs avatar eight avatar

Watchers

James Cloos avatar  avatar

mortice's Issues

Remove references to global

global: global,

Impl = Impl || global.Worker

These references to global make this library incompatible in the browser because global is part of the node API and is not available in the browser. Given that this library is imported by several other libraries that aim to make use of it in the browser, would you ever consider removing these references?

Later versions of Angular (6+) have tightened their compile-time requirements to alert node-specific references like this which is why these issues are being surfaced now.

bug: does not work in service workers

When testing some code for ipfs-shipyard/helia-service-worker-gateway, we saw the following error:

heliaFetch.ts:106 fs.stat error for cid 'QmbSa3mkSjwTX32feHXC9yMZ6TVH2JzJdR5sHyzssJueLB' and path '/' TypeError: globalThis.postMessage is not a function
    at Object.readLock (browser.ts:67:16)
    at storage.ts:108:41
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (pins.ts:189:4)
    at _next (pins.ts:189:4)
    at pins.ts:189:4
    at new Promise (<anonymous>)
    at pins.ts:189:4
    at BlockStorage.get (storage.ts:125:6)
    at unixFsResolver (index.ts:32:34)

coming from

globalThis.postMessage({

We should move away from postMessage to BroadcastChannel and MessageChannel which should be supported in all browser contexts and node after v15.

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Typescript: No "exports" main defined

Trying to convert my own project to typescript and I'm getting an error from this package, Using version ^3.0.0

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\Users\Project\node_modules\mortice\package.json
at new NodeError (node:internal/errors:371:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
at packageExportsResolve (node:internal/modules/esm/resolve:669:7)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (C:\Users\Frank\Documents\Bitbucket\gitmanagement\dist\services\cloneService.js:10:48)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.