Skip to content

Commit

Permalink
Fix registry typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed Apr 10, 2022
1 parent e01a43d commit 44d5e8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
49 changes: 25 additions & 24 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import { onCleanup, getOwner, runWithOwner } from "solid-js";
import { Owner } from "solid-js/types/reactive/signal";

export type ServiceInitializer<T extends Service> = () => T;
export interface Service extends Record<any, any> {}

export interface Service {}
export type ServiceInitializer<T extends Service> = () => T;

export interface Registry {
has<T extends Service>(initializer: ServiceInitializer<T>): boolean;
get<T extends Service>(initializer: ServiceInitializer<T>): T | undefined;
register<T extends Service>(initializer: ServiceInitializer<T>): T;
clear(): void;
}
export class Registry {
#owner: Owner | null;
#cache: Map<ServiceInitializer<any>, any>;

export function createRegistry(): Registry {
const cache = new Map<ServiceInitializer<any>, any>();
const owner = getOwner();
constructor() {
this.#owner = getOwner();
this.#cache = new Map<ServiceInitializer<any>, any>();

if (owner) {
onCleanup(() => cache.clear());
if (this.#owner) {
onCleanup(() => this.#cache.clear());
}
}

function has<T>(initializer: ServiceInitializer<T>) {
return cache.has(initializer);
has<T extends Service>(initializer: ServiceInitializer<T>): boolean {
return this.#cache.has(initializer);
}

function get<T>(initializer: ServiceInitializer<T>) {
return cache.get(initializer);
get<T extends Service>(initializer: ServiceInitializer<T>): T | undefined {
return this.#cache.get(initializer);
}

function clear() {
cache.clear();
clear(): void {
this.#cache.clear();
}

function register<T>(initializer: ServiceInitializer<T>) {
const registration = owner
? runWithOwner(owner, () => initializer())
register<T extends Service>(initializer: ServiceInitializer<T>): T {
const registration = this.#owner
? runWithOwner(this.#owner, () => initializer())
: initializer();

cache.set(initializer, registration);
this.#cache.set(initializer, registration);

return registration;
}
}

return { has, get, register, clear } as const;
export function createRegistry(): Registry {
return new Registry();
}
12 changes: 10 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"useDefineForClassFields": true,
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"outDir": "./dist/src",
Expand All @@ -16,7 +16,15 @@
"isolatedModules": true,
"noImplicitAny": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true
"strictPropertyInitialization": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"strictNullChecks": true,
"inlineSourceMap": true,
"inlineSources": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
},
"include": ["./src"]
}

0 comments on commit 44d5e8c

Please sign in to comment.