Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/events #92

Merged
merged 18 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cache/contracts/cache.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module Cache
*/

import type { IListenable } from "@/event-bus/contracts/_module";
import type { IEventListener } from "@/event-bus/contracts/_module";
import type { OneOrMore } from "@/utilities/_module";
import { type AsyncLazyable, type GetOrAddValue } from "@/utilities/_module";
import type { CacheEvents } from "@/cache/contracts/_module";
Expand All @@ -21,7 +21,9 @@ import type { LazyPromise } from "@/async/_module";
* The <i>ICacheListenable</i> contract defines a way for listening <i>{@link ICache}</i> crud operations.
* @group Contracts
*/
export type ICacheListenable<TType = unknown> = IListenable<CacheEvents<TType>>;
export type ICacheListenable<TType = unknown> = IEventListener<
CacheEvents<TType>
>;

/**
* @group Contracts
Expand Down
354 changes: 316 additions & 38 deletions src/cache/contracts/cache.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,322 @@
* @module Cache
*/

import { BaseEvent } from "@/event-bus/contracts/_module";
import type { IFlexibleSerde } from "@/serde/contracts/serde.contract";
import type { TimeSpan } from "@/utilities/_module";

export type CacheEvents<TType = unknown> = {
key_found: {
group: string;
key: string;
value: TType;
};
key_not_found: {
group: string;
key: string;
};
key_added: {
group: string;
key: string;
value: TType;
ttl: TimeSpan | null;
};
key_updated: {
group: string;
key: string;
value: TType;
};
key_removed: {
group: string;
key: string;
};
key_incremented: {
group: string;
key: string;
value: number;
};
key_decremented: {
group: string;
key: string;
value: number;
};
keys_cleared: {
group: string;
};
/**
* @group Events
*/
export type KeyFoundCacheEventFields<TType> = {
group: string;
key: string;
value: TType;
};

/**
* @group Events
*/
export class KeyFoundCacheEvent<TType = unknown> extends BaseEvent<
KeyFoundCacheEventFields<TType>
> {
group!: string;
key!: string;
value!: TType;

static override deserialize<TType>(
serializedEvent: KeyFoundCacheEventFields<TType>,
): KeyFoundCacheEvent<TType> {
return new KeyFoundCacheEvent(serializedEvent);
}

constructor(fields: KeyFoundCacheEventFields<TType>) {
super();
Object.assign(this, fields);
}

override serialize(): KeyFoundCacheEventFields<TType> {
return {
group: this.group,
key: this.key,
value: this.value,
};
}
}

/**
* @group Events
*/
export type KeyNotFoundCacheEventFields = {
group: string;
key: string;
};

/**
* @group Events
*/
export class KeyNotFoundCacheEvent extends BaseEvent<KeyNotFoundCacheEventFields> {
group!: string;
key!: string;

static override deserialize(
serializedEvent: KeyNotFoundCacheEventFields,
): KeyNotFoundCacheEvent {
return new KeyNotFoundCacheEvent(serializedEvent);
}

constructor(fields: KeyNotFoundCacheEventFields) {
super();
Object.assign(this, fields);
}

override serialize(): KeyNotFoundCacheEventFields {
return {
group: this.group,
key: this.key,
};
}
}

/**
* @group Events
*/
export type KeyAddedCacheEventFields<TType = unknown> = {
group: string;
key: string;
value: TType;
ttl: TimeSpan | null;
};

/**
* @group Events
*/
export class KeyAddedCacheEvent<TType = unknown> extends BaseEvent<
KeyAddedCacheEventFields<TType>
> {
group!: string;
key!: string;
value!: TType;
ttl!: TimeSpan | null;

static override deserialize<TType>(
serializedEvent: KeyAddedCacheEventFields<TType>,
): KeyAddedCacheEvent<TType> {
return new KeyAddedCacheEvent(serializedEvent);
}
constructor(fields: KeyAddedCacheEventFields<TType>) {
super();
Object.assign(this, fields);
}

override serialize(): KeyAddedCacheEventFields<TType> {
return {
group: this.group,
key: this.key,
ttl: this.ttl,
value: this.value,
};
}
}

/**
* @group Events
*/
export type KeyUpdatedCacheEventFields<TType = unknown> = {
group: string;
key: string;
value: TType;
};

/**
* @group Events
*/
export class KeyUpdatedCacheEvent<TType = unknown> extends BaseEvent<
KeyUpdatedCacheEventFields<TType>
> {
group!: string;
key!: string;
value!: TType;

static override deserialize<TType>(
serializedEvent: KeyUpdatedCacheEventFields<TType>,
): KeyUpdatedCacheEvent<TType> {
return new KeyUpdatedCacheEvent(serializedEvent);
}
constructor(fields: KeyUpdatedCacheEventFields<TType>) {
super();
Object.assign(this, fields);
}

override serialize(): KeyUpdatedCacheEventFields<TType> {
return {
group: this.group,
key: this.key,
value: this.value,
};
}
}

/**
* @group Events
*/
export type KeyRemovedCacheEventFields = {
group: string;
key: string;
};

/**
* @group Events
*/
export class KeyRemovedCacheEvent extends BaseEvent<KeyRemovedCacheEventFields> {
group!: string;
key!: string;

static override deserialize(
serializedEvent: KeyRemovedCacheEventFields,
): KeyRemovedCacheEvent {
return new KeyRemovedCacheEvent(serializedEvent);
}
constructor(fields: KeyRemovedCacheEventFields) {
super();
Object.assign(this, fields);
}

override serialize(): KeyRemovedCacheEventFields {
return {
group: this.group,
key: this.key,
};
}
}

/**
* @group Events
*/
export type KeyIncrementedCacheEventFields = {
group: string;
key: string;
value: number;
};

/**
* @group Events
*/
export class KeyIncrementedCacheEvent extends BaseEvent<KeyIncrementedCacheEventFields> {
group!: string;
key!: string;
value!: number;

static override deserialize(
serializedEvent: KeyIncrementedCacheEventFields,
): KeyIncrementedCacheEvent {
return new KeyIncrementedCacheEvent(serializedEvent);
}
constructor(fields: KeyIncrementedCacheEventFields) {
super();
Object.assign(this, fields);
}

override serialize(): KeyIncrementedCacheEventFields {
return {
group: this.group,
key: this.key,
value: this.value,
};
}
}

/**
* @group Events
*/
export type KeyDecrementedCacheEventFields = {
group: string;
key: string;
value: number;
};

/**
* @group Events
*/
export class KeyDecrementedCacheEvent extends BaseEvent<KeyDecrementedCacheEventFields> {
group!: string;
key!: string;
value!: number;

static override deserialize(
serializedEvent: KeyDecrementedCacheEventFields,
): KeyDecrementedCacheEvent {
return new KeyDecrementedCacheEvent(serializedEvent);
}
constructor(fields: KeyDecrementedCacheEventFields) {
super();
Object.assign(this, fields);
}

override serialize(): KeyDecrementedCacheEventFields {
return {
group: this.group,
key: this.key,
value: this.value,
};
}
}

/**
* @group Events
*/
export type KeysClearedCacheEventFields = {
group: string;
};

/**
* @group Events
*/
export class KeysClearedCacheEvent extends BaseEvent<KeysClearedCacheEventFields> {
group!: string;

static override deserialize(
serializedEvent: KeysClearedCacheEventFields,
): KeysClearedCacheEvent {
return new KeysClearedCacheEvent(serializedEvent);
}
constructor(fields: KeysClearedCacheEventFields) {
super();
Object.assign(this, fields);
}

override serialize(): KeysClearedCacheEventFields {
return {
group: this.group,
};
}
}

/**
* @group Events
*/
export type CacheEvents<TType = unknown> =
| KeyFoundCacheEvent<TType>
| KeyNotFoundCacheEvent
| KeyAddedCacheEvent<TType>
| KeyUpdatedCacheEvent<TType>
| KeyRemovedCacheEvent
| KeyIncrementedCacheEvent
| KeyDecrementedCacheEvent
| KeysClearedCacheEvent;

/**
* @group Events
*/
export function registerCacheEvents<TSerializedValue>(
serde: IFlexibleSerde<TSerializedValue>,
): void {
serde.registerClass(KeyFoundCacheEvent);
serde.registerClass(KeyNotFoundCacheEvent);
serde.registerClass(KeyAddedCacheEvent);
serde.registerClass(KeyUpdatedCacheEvent);
serde.registerClass(KeyRemovedCacheEvent);
serde.registerClass(KeyIncrementedCacheEvent);
serde.registerClass(KeyDecrementedCacheEvent);
serde.registerClass(KeysClearedCacheEvent);
}
Loading
Loading