![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
vorarbeiter
Advanced tools
npm install vorarbeiter
interface Car {
getDriverName(): string;
}
class CarImpl implements Car {
constructor(private readonly driver: Driver) {}
getDriverName() {
return this.driver.getName();
}
}
interface Driver {
getName(): string;
}
class DriverImpl implements Driver {
getName() {
return "Michael Schumacher";
}
}
class CarFactory implements ServiceFactory {
create(container: ServiceContainer): CarImpl {
const driver = container.get("driver");
return new CarImpl(driver);
}
}
import { createServiceSpecBuilder } from "vorarbeiter";
const specBuilder = createServiceSpecBuilder();
specBuilder.set("car", new CarFactory());
specBuilder.set("driver", () => new DriverImpl());
const spec = specBuilder.getServiceSpec();
If creating a service is trivial, as for driver
, we can simply pass a function as a factory.
As for class based factory we can pass ServiceContainer
as a function parameter.
import { createServiceContainer } from "vorarbeiter";
const serviceContainer = createServiceContainer(spec);
const car: Car = serviceContainer.get("car");
console.log(car.getDriverName());
By default, services have global scope. It means that the same service will be shared across whole application.
Example:
let serviceInstance1;
let serviceInstance2;
// In some part of our application we get a service
serviceInstance1 = serviceContainer.get("myService");
// In some another part of our application we get a service again
serviceInstance2 = serviceContainer.get("myService");
console.log(serviceInstance1 === serviceInstance2); // true
Sometimes we need to have service uniqueness within a specific scope, for example, within one user request. To do that we should specify the Context Resolver when configure Service Specification. Resolving result of the Context Resolver should be any object. To imitate situation when we have two different contexts we can use AsyncLocalStorage from "node:async_hooks" package.
Example:
const asyncLocalStorage = new AsyncLocalStorage<object>();
specBuilder
.set("myScopedService", () => ({ serviceName: "Awesome service" }))
.scoped(() => asyncLocalStorage.getStore());
const serviceContainer = createServiceContainer(specBuilder.getServiceSpec());
let scopedService1;
{
let scopedService2;
asyncLocalStorage.run({}, () => {
scopedService1 = serviceContainer.get("myScopedService");
scopedService2 = serviceContainer.get("myScopedService");
});
console.log(scopedService1 === scopedService2);
}
{
let scopedService3;
let scopedService4;
asyncLocalStorage.run({}, () => {
scopedService3 = serviceContainer.get("myScopedService");
scopedService4 = serviceContainer.get("myScopedService");
});
console.log(scopedService1 === scopedService3);
console.log(scopedService3 === scopedService4);
}
// Output:
// true
// false
// true
The most common type of injection is constructor injection. This type of injection occurs when creating service. But sometimes we want to inject after the service has been created. For this we can specify Service Injector for the service:
specBuilder.set("injectorService", () => {
return new class {
car!: Car;
driver!: Driver;
setDriver(driver: Driver) {
this.driver = driver;
}
};
}).withInjector((service, container) => {
service.car = container.get("car");
service.setDriver(container.get("driver"));
});
This way we can perform property and setter injection.
We can wrap service resolving in middlewares:
const specBuilder = createServiceSpecBuilder();
function middleware1(next: Next): Next {
return function<T>(id: ServiceId): T {
console.log(`middleware1 start`);
const service: T = next(id);
console.log(`middleware1 end`);
return service;
}
}
function middleware2(next: Next): Next {
return function<T>(id: ServiceId): T {
console.log(`middleware2 start`);
const service: T = next(id);
console.log(`middleware2 end`);
return service;
}
}
specBuilder.addMiddleware(middleware1, middleware2);
const spec = specBuilder.getServiceSpec();
serviceContainer.get("someService");
// Output:
// middleware2 start
// middleware1 start
// middleware1 end
// middleware2 end
FAQs
A simple service container
We found that vorarbeiter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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 iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.