Comparing version 2.0.0 to 3.0.0
import { Service } from "./service"; | ||
import { Observable, Subject } from 'rxjs'; | ||
/** | ||
* Stores the current container instance in the current operating context. | ||
* A type that represents a class instance of a Service. | ||
* | ||
* NOTE: This should not be used outside of dioc library code | ||
* For example, if you have a `TodoService` defined, this will refer to | ||
* the JS 'class constructor' and not the instance of the class (represented | ||
* by the type `TodoService`). This type is useful in situations where you want | ||
* to have a reference to service type itself, and is mostly used by the `bind` functions | ||
*/ | ||
export declare let currentContainer: { | ||
value: Container | null; | ||
export type ServiceClassInstance<T> = (new (c: Container) => Service<T>) & { | ||
ID: string; | ||
}; | ||
@@ -46,5 +49,3 @@ /** | ||
*/ | ||
hasBound<T extends typeof Service<any> & { | ||
ID: string; | ||
}>(service: T): boolean; | ||
hasBound<T extends ServiceClassInstance<any>>(service: T): boolean; | ||
/** | ||
@@ -63,7 +64,3 @@ * Returns the service bound to the container with the given ID or if not found, undefined. | ||
*/ | ||
bind<T extends typeof Service<any> & { | ||
ID: string; | ||
}>(service: T, bounder?: ((typeof Service<T>) & { | ||
ID: string; | ||
}) | undefined): InstanceType<T>; | ||
bind<T extends ServiceClassInstance<any>>(service: T, bounder?: ServiceClassInstance<any> | undefined): InstanceType<T>; | ||
/** | ||
@@ -70,0 +67,0 @@ * Returns an iterator of the currently bound service IDs and their instances |
var h = Object.defineProperty; | ||
var l = (t, e, r) => e in t ? h(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r; | ||
var a = (t, e, r) => (l(t, typeof e != "symbol" ? e + "" : e, r), r), s = (t, e, r) => { | ||
if (!e.has(t)) | ||
var u = (e, t, r) => t in e ? h(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r; | ||
var o = (e, t, r) => (u(e, typeof t != "symbol" ? t + "" : t, r), r), a = (e, t, r) => { | ||
if (!t.has(e)) | ||
throw TypeError("Cannot " + r); | ||
}; | ||
var c = (t, e, r) => (s(t, e, "read from private field"), r ? r.call(t) : e.get(t)), u = (t, e, r) => { | ||
if (e.has(t)) | ||
var i = (e, t, r) => (a(e, t, "read from private field"), r ? r.call(e) : t.get(e)), c = (e, t, r) => { | ||
if (t.has(e)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
e instanceof WeakSet ? e.add(t) : e.set(t, r); | ||
}, v = (t, e, r, o) => (s(t, e, "write to private field"), o ? o.call(t, r) : e.set(t, r), r); | ||
import { S as w, c as i } from "./container-BTMLLTaW.js"; | ||
import { C as S } from "./container-BTMLLTaW.js"; | ||
t instanceof WeakSet ? t.add(e) : t.set(e, r); | ||
}, v = (e, t, r, s) => (a(e, t, "write to private field"), s ? s.call(e, r) : t.set(e, r), r); | ||
import { S as b } from "./container-DqHk4GBj.js"; | ||
import { C as $ } from "./container-DqHk4GBj.js"; | ||
var n; | ||
class f { | ||
constructor() { | ||
class p { | ||
constructor(t) { | ||
/** | ||
* The internal event stream of the service | ||
*/ | ||
a(this, "event$", new w()); | ||
o(this, "event$", new b()); | ||
/** The container the service is bound to */ | ||
u(this, n, void 0); | ||
if (!i.value) | ||
throw new Error( | ||
`Tried to initialize service with no container (ID: ${this.constructor.ID})` | ||
); | ||
v(this, n, i.value); | ||
c(this, n, void 0); | ||
v(this, n, t); | ||
} | ||
/** | ||
* This function is called when a service is initialized, which is only | ||
* once per container since services are singletons. By | ||
* default this function does nothing and is expected to be overriden by | ||
* services if they want to do like in place of a constructor. | ||
* | ||
* NOTE: The reason why this function exists is so that constructors for | ||
* services need not be exposed and played around with. By norm, it is best | ||
* to override this function than overriding the constructor and providing the | ||
* container that way. | ||
*/ | ||
onServiceInit() { | ||
} | ||
/** | ||
* Binds a dependency service into this service. | ||
* @param service The class reference of the service to bind | ||
*/ | ||
bind(e) { | ||
if (!i.value) | ||
throw new Error("No currentContainer defined."); | ||
return i.value.bind(e, this.constructor); | ||
bind(t) { | ||
return i(this, n).bind(t, this.constructor); | ||
} | ||
@@ -42,3 +49,3 @@ /** | ||
getContainer() { | ||
return c(this, n); | ||
return i(this, n); | ||
} | ||
@@ -49,4 +56,4 @@ /** | ||
*/ | ||
emit(e) { | ||
this.event$.next(e); | ||
emit(t) { | ||
this.event$.next(t); | ||
} | ||
@@ -62,5 +69,4 @@ /** | ||
export { | ||
S as Container, | ||
f as Service, | ||
i as currentContainer | ||
$ as Container, | ||
p as Service | ||
}; |
import { Observable } from 'rxjs'; | ||
import { Container } from './container'; | ||
import { Container, ServiceClassInstance } from './container'; | ||
/** | ||
* A Dioc service that can bound to a container and can bind dependency services. | ||
* | ||
* NOTE: Services cannot have a constructor that takes arguments. | ||
* NOTE: Services have a constructor that take a single argument that is the container. This should always be maintained | ||
* since that is expected for binding to containers. | ||
* | ||
@@ -16,10 +17,20 @@ * @template EventDef The type of events that can be emitted by the service. These will be accessible by event streams | ||
private event$; | ||
constructor(); | ||
constructor(container: Container); | ||
/** | ||
* This function is called when a service is initialized, which is only | ||
* once per container since services are singletons. By | ||
* default this function does nothing and is expected to be overriden by | ||
* services if they want to do like in place of a constructor. | ||
* | ||
* NOTE: The reason why this function exists is so that constructors for | ||
* services need not be exposed and played around with. By norm, it is best | ||
* to override this function than overriding the constructor and providing the | ||
* container that way. | ||
*/ | ||
onServiceInit(): void; | ||
/** | ||
* Binds a dependency service into this service. | ||
* @param service The class reference of the service to bind | ||
*/ | ||
protected bind<T extends typeof Service<any> & { | ||
ID: string; | ||
}>(service: T): InstanceType<T>; | ||
protected bind<T extends ServiceClassInstance<any>>(service: T): InstanceType<T>; | ||
/** | ||
@@ -26,0 +37,0 @@ * Returns the container the service is bound to |
@@ -1,2 +0,2 @@ | ||
import { Container, Service } from "./main"; | ||
import { Container, ServiceClassInstance } from "./main"; | ||
/** | ||
@@ -13,5 +13,3 @@ * A container that can be used for writing tests, contains additional methods | ||
*/ | ||
bindMock<T extends typeof Service<any> & { | ||
ID: string; | ||
}, U extends Partial<InstanceType<T>>>(service: T, mock: U): U; | ||
bindMock<T extends ServiceClassInstance<any>, U extends Partial<InstanceType<T>>>(service: T, mock: U): U; | ||
} |
@@ -1,3 +0,3 @@ | ||
import { C as t } from "./container-BTMLLTaW.js"; | ||
class r extends t { | ||
import { C as o } from "./container-DqHk4GBj.js"; | ||
class i extends o { | ||
/** | ||
@@ -9,14 +9,15 @@ * Binds a mock service to the container. | ||
*/ | ||
bindMock(n, o) { | ||
if (this.boundMap.has(n.ID)) | ||
throw new Error(`Service '${n.ID}' already bound to container. Did you already call bindMock on this ?`); | ||
return this.boundMap.set(n.ID, o), this.event$.next({ | ||
bindMock(t, n) { | ||
var e; | ||
if (this.boundMap.has(t.ID)) | ||
throw new Error(`Service '${t.ID}' already bound to container. Did you already call bindMock on this ?`); | ||
return this.boundMap.set(t.ID, n), (e = n.onServiceInit) == null || e.call(n), this.event$.next({ | ||
type: "SERVICE_BIND", | ||
boundeeID: n.ID, | ||
boundeeID: t.ID, | ||
bounderID: void 0 | ||
}), o; | ||
}), n; | ||
} | ||
} | ||
export { | ||
r as TestContainer | ||
i as TestContainer | ||
}; |
import { Plugin } from "vue"; | ||
import { Service } from "./service"; | ||
import { ServiceClassInstance } from "./container"; | ||
/** | ||
@@ -14,4 +14,2 @@ * The Vue Dioc Plugin, this allows the composables to work and access the container | ||
*/ | ||
export declare function useService<T extends typeof Service<any> & { | ||
ID: string; | ||
}>(service: T): InstanceType<T>; | ||
export declare function useService<T extends ServiceClassInstance<T>>(service: T): InstanceType<T>; |
{ | ||
"name": "dioc", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -43,6 +43,4 @@ # dioc | ||
// Service constructors cannot have arguments | ||
constructor() { | ||
super() | ||
// Services cannot(*) have constructors, but init logic can be mentioned here | ||
override onServiceInit() { | ||
this.todos = JSON.parse(this.persistence.read("todos") ?? "[]") | ||
@@ -144,2 +142,2 @@ } | ||
# Developing | ||
`dioc` repo uses [pnpm](https://pnpm.io/) for package management. Install it and run `pnpm install` to install dependencies. | ||
`dioc` repo uses [pnpm](https://pnpm.io/) for package management. Install it and run `pnpm install` to install dependencies. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44803
882
142