-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
35 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters