Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Composition-api in Vanilla js
Vue.js introduced an amazing pattern called Composition API that allows organizing complex logic by splitting it into reusable functions and grouping in logical order. unctx
allows easily implementing composition api pattern in your javascript libraries without hassle.
In your awesome library:
yarn add unctx
# or
npm install unctx
import { createContext } from 'unctx'
const ctx = createContext()
export const useAwesome = ctx.use
// ...
ctx.call({ test: 1 }, () => {
// This is similar to vue setup function
// Any function called here, can use `useAwesome` to get { test: 1 }
})
User code:
import { useAwesome } from 'awesome-lib'
// ...
function setup() {
const ctx = useAwesome()
}
To avoid issues with multiple version of library, unctx
provides a safe global namespace to access context by key (kept in globalThis
). Important: Please use a verbose name for key to avoid conflict with other js libraries. Using npm package name is recommended. Using symbols has no effect since it still causes multiple context issue.
import { useContext, getContext } from 'unctx'
const useAwesome = useContext('awesome-lib')
// or
// const awesomeContext = getContext('awesome-lib')
You can also create your own internal namespace with createNamespace
utility for more advanced use cases.
If you are sure it is safe to use a shared instance (not depending to request), you can also use ctx.set
and ctx.unset
for a singleton pattern.
Note: You cannot combine set
with call
. Always use unset
before replacing instance otherwise you will get Context conflict
error.
import { createContext } from 'unctx'
const ctx = createContext()
ctx.set(new Awesome())
// Replacing instance without unset
// ctx.set(new Awesome(), true)
export const useAwesome = ctx.use
A generic type exists on all utilities to be set for instance/context type:
// Return type of useAwesome is Awesome | null
const { use: useAwesome } = createContext<Awesome>()
Composition of functions is possible using temporary context injection. When calling ctx.call(instance, cb)
, instance
argument will be stored in a temporary variable then cb
is called. Any function inside cb
, can then implicitly access instance by using ctx.use
(or useAwesome
)
context can be only used before first await:
To avoid leaking context, call
method synchronously sets context and unset it as soon as possible. Because of this, useAwesome
should happen before first await
call and reused if necessary.
async function setup() {
const awesome = useAwesome()
await someFunction()
// useAwesome() returns null here
}
Context conflict
error:
In your library, you should only keep one call()
running at a time (unless calling with same reference for first argument)
For instance this makes an error:
ctx.call({ test: 1 }, () => {
ctx.call({ test: 2 }, () => {
// Throws error!
})
})
MIT. Made with 💖
FAQs
Composition-api in Vanilla js
The npm package unctx receives a total of 548,554 weekly downloads. As such, unctx popularity was classified as popular.
We found that unctx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.