Git Product home page Git Product logo

deno_di's People

Contributors

hypercubed avatar luvies 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

Watchers

 avatar  avatar  avatar

Forkers

hypercubed

deno_di's Issues

Consider making a Token class for types

Basically just a class to wrap a symbol and a Type so you can get inference through with your types.

e.g.

const token ExampleToken = new Token<IExample>("example") // Internally just has a Symbol("example")

interface IExample {
  run(): void;
}

@Service()
class A implements Iexample {
  public run(): void {
    console.log("A");
  }
}

// Service collection setup.
const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(ExampleToken, A); // IExample is inferred as T from Token<T>

const services = serviceCollection.get(ExampleToken); // sevices is inferred as IExample

Inject doesn't work with `deno bundle`

Has anyone tried using this inside js generated with deno bundle? It appears to me Inject does not work in a bundle.

Here is the TS code:

import "https://cdn.pika.dev/@abraham/reflection@^0.7.0";

import { Inject, Service, ServiceCollection } from "https://deno.land/x/[email protected]/mod.ts";

@Service()
class A {}

// Constructor injection
@Service()
class B {
  constructor(public a: A) {}
}

// Property injection.
@Service()
class C {
  @Inject(A)
  a!: A;
}

// Service collection setup.
const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(A);
serviceCollection.addTransient(B);
serviceCollection.addTransient(C);

const b = serviceCollection.get(B);
console.log(b instanceof B);  // true
console.log(b.a instanceof A);  // true

const c = serviceCollection.get(C);
console.log(c instanceof C);  // true
console.log(c.a instanceof A);  // true with deno run, false in bundle

Saved to a test.ts file the following works (prints true for all four console logs):

deno run -c tsconfig.json ./test.ts

but try generating a bundle you will get three trues and a false.

deno bundle -c tsconfig.json test.ts | node

Constructor parameters not being injected

Hello,

I've been using your example of abstract classes and I'm experincing what seems like constructor parameters not being injected/passed along. The following is a modified version of the example you have posted. I'm inexperienced with Deno/Typescript enough to be uncertain if this is a bug or something I'm misunderstanding of how the language works.

Thank you for any insight you can provide.

import "https://cdn.skypack.dev/@abraham/[email protected]";
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import { Service, ServiceCollection } from "https://deno.land/x/di/mod.ts";

abstract class IA {
    abstract foo(): string;
}

abstract class IB {
    abstract IndirectFoo(): string;
    abstract bar(): string;
}

@Service()
class A implements IA {
    public foo(): string {
        return "foo";
    }
}

@Service()
class B implements IB {
    constructor(
        private a: IA
    ) {}

    public IndirectFoo(): string {
        return this.a.foo();
    }

    public bar(): string {
        return "bar";
    }
}

const serviceCollection = new ServiceCollection();
serviceCollection.addTransient(IA, A);
serviceCollection.addTransient(IB, B);

const ib = serviceCollection.get(IB);

Deno.test("Is expected type", () => {
    assert(ib instanceof B);
});

Deno.test("Expects 'bar'", () => {
    assert(ib.bar() == "bar");
});

Deno.test("Expects 'foo'", () => {
    assert(ib.IndirectFoo() == "foo");
});

Error message:

Check file:///Users/jason/Code/example/example.ts
running 3 tests from file:///Users/jason/Code/example/example.ts
test Is expected type ... ok (6ms)
test Expects 'bar' ... ok (4ms)
test Expects 'foo' ... FAILED (9ms)

failures:

Expects 'foo'
TypeError: Cannot read property 'foo' of undefined
    at B.IndirectFoo (file:///Users/jason/Code/example/example.ts:28:23)
    at file:///Users/jason/Code/example/example.ts:51:15
    at asyncOpSanitizer (deno:runtime/js/40_testing.js:35:15)
    at resourceSanitizer (deno:runtime/js/40_testing.js:72:13)
    at exitSanitizer (deno:runtime/js/40_testing.js:99:15)
    at runTest (deno:runtime/js/40_testing.js:215:13)
    at Object.runTests (deno:runtime/js/40_testing.js:283:28)
    at async file:///Users/jason/Code/example/832dba8c-7c32-4f9d-a6da-f1050ff07807$deno$test.js:4:7

Add multiple services under the same type and getMany?

For example:

const types = {
  IA: Symbol("IA")
};

// Interfaces
interface IA {
  foo(): void;
}

// Implementations
@Service()
class A implements IA {
  public foo(): void {
    console.log("A");
  }
}

@Service()
class B implements IA {
  public foo(): void {
    console.log("B");
  }
}

// Service collection setup.
const serviceCollection = new ServiceCollection();
serviceCollection.pushTransient<IA>(types.IA, A);
serviceCollection.pushTransient<IA>(types.IA, B);

const services = serviceCollection.getMany(types.IA);

Deno version is 1.6.2

Maybe bumping the version to 1.6.2 would be a good idea to check that everything is working as expected.

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.