Code Monkey home page Code Monkey logo

iti's People

Contributors

molszanski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

iti's Issues

feature: container disposing

A useful feature is to be able to dispose of resources from the container.

E.g. disconnect from the databases and so on.

See how Awilix did it.

Type resolution fails when importing iti with "moduleResolution": "NodeNext"

Problem Description

When using iti in a TypeScript project with "moduleResolution": "NodeNext" in your tsconfig.json, type information for iti cannot be located.

This line:

import { createContainer } from 'iti'

Produces the error:

Could not find a declaration file for module 'iti'. '<project path>/node_modules/iti/dist/iti.modern.js'
implicitly has an 'any' type.
  Try `npm i --save-dev @types/iti` if it exists or add a new declaration (.d.ts) file
  containing `declare module 'iti';`

The project will still compile and run with tsc; you just don't have type information for iti for static checking, linting, etc.

Cause

There are two causes:

  1. The exports map in iti's package.json contains an exports map, but the types key is not defined for each export. See this TypeScript issue for a complete explanation. When exports is defined, top-level types or typings keys are ignored.

  2. The export ... from statements in iti/src/index.ts need to use an explicit .js extension. Without this, import { createContainer } from 'iti' will nominally succeed, but the type of createContainer will just be import, and the static analysis gets very confused.

Proposed fix

  1. The first issue is simple: just add "types": "./dist/src/index.d.ts" to the exports map in package.json.

  2. For the second issue, I'll be honest: I'm fairly new to JS/TS. My understanding is that explicit .js extensions are required for import/export statements when using ESM ("type": "module" in package.json). So I'm not sure how iti is getting away with "bare" imports in the first place. But in any case, adding extensions only to the export statements in src/index.ts is enough to satisfy the consumer's linter and should be backwards-compatible with CJS. With that said, two of the iti tests (dispose....) try to import { createContainer } from '../src', which needs to be ../src/iti after this change. The other tests already import this way.

I have a fork with these proposed changes made to iti and iti-react. I can make a PR if you'd like.

Improve type inference on `getContainerSet`

Is your feature request related to a problem? Please describe.

Considering the following code.

const a = await root.getContainerSet(["userManual", "oven"])
a.oven // There is no error, oven exists, the type in compile-time is the same that the type in run-time

const b = await root.getContainerSet(() => ["userManual"])
b.oven // There is no error, BUT oven DOES NOT exist, the type in compile-time is NOT the same that the type in run-time

It is a problem in this example:

const kitchenContainer = async ({ oven, userManual }) => {
  await oven.preheat()
  return {
    kitchen: new Kitchen(oven, userManual),
  }
}

const node = root.add((ctx, node) => ({
  kitchen: async () =>
    kitchenContainer(await node.getContainerSet(["userManual"])),
}))

We are requiring from kitchenContainer two dependencies: over and userManual. But we are only providing userManual. This will cause an error at run-time.

Describe the solution you'd like

The returned type of getContainerSet should be automatically inferred from what we pass in parameter.

Solution

(tested and it works)

Edit these lines

iti/iti/src/iti.ts

Lines 371 to 373 in 0a3a006

public async getContainerSet<T extends keyof Context>(
tokensOrCb: KeysOrCb<Context>,
) {
to

 public async getContainerSet<T extends keyof Context>( 
   tokensOrCb: Pick<KeysOrCb<Context>, T>, 
 ) { 

(This solution may be applicable to other functions like subscribeToContainerSet and _extractTokens too)

Note: We can improve the type printing (on hovering on VSCode for example), by wrapping the Pick<...> into the Prettify helper.

type Prettify<T> = {
  [K in keyof T]: T[K];
} & {};
With PrettifyWithout Prettify
Capture d’écran 2024-04-06 à 02 54 27 Capture d’écran 2024-04-06 à 02 54 44

Subset of features for faster type inference

Is your feature request related to a problem? Please describe.
Adding a lot of add(..) slows down Intellisense to the point that I have to restart the TS server every now and then.

Describe the solution you'd like
I was wondering whether it would be feasible to provide a version of iti that has less features but provides faster type inference. I haven't dug into the code yet, just wondering whether something like that is out of the question.

What would be an ideal API for your use case?
I just need add(), upsert() and items(). No async injection or any of that disposal API.

Event Hooks for dependencies

Class-based / object dependencies could implement event hooks.

For example

class Logger implements Init {
     init() {
       this.info("logger started");
     }
}

or

{ init : () => this.info("logger started") }

There could be various hooks on dependencies that would be called when an iti even occurs.
Describe the solution you'd like
A clear and concise description of what you want to happen.
Naive solution
For any / all events, loop through dependencies, calling corresponding function.

//init event occurs
for(dependency of Object.values(cache)) {
   dependency?.init?.()
}

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.