-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp: Implemented
CallableObject
& SwitchableCallback
.
- Loading branch information
Showing
13 changed files
with
309 additions
and
176 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import type { Callback } from "./types.js"; | ||
|
||
export const SmartFunction = (Function as unknown) as | ||
new<T extends Callback<any[], any> = () => void>(...args: string[]) => | ||
(...args: Parameters<T>) => ReturnType<T>; | ||
|
||
export default abstract class CallableObject<T extends Callback<any[], any> = () => void> | ||
extends SmartFunction<T> | ||
{ | ||
public constructor() | ||
{ | ||
super(`return this.invoke(...arguments);`); | ||
|
||
const self = this.bind(this); | ||
Object.setPrototypeOf(this, self); | ||
|
||
return self as this; | ||
} | ||
|
||
public abstract invoke(...args: Parameters<T>): ReturnType<T>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import CallableObject, { SmartFunction } from "./callable-object.js"; | ||
import Publisher from "./publisher.js"; | ||
import SwitchableCallback from "./switchable-callback.js"; | ||
|
||
export { CallableObject, Publisher, SmartFunction, SwitchableCallback }; |
11 changes: 5 additions & 6 deletions
11
src/models/publisher.ts → src/models/callbacks/publisher.ts
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
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { KeyException, NotImplementedException, RuntimeException } from "../exceptions/index.js"; | ||
|
||
import CallableObject from "./callable-object.js"; | ||
import type { Callback } from "./types.js"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export default class SwitchableCallback<T extends Callback<any[], any> = Callback> extends CallableObject<T> | ||
{ | ||
protected _callback: T; | ||
protected _callbacks: Map<string, T>; | ||
|
||
protected _isEnabled: boolean; | ||
public get isEnabled(): boolean { return this._isEnabled; } | ||
|
||
protected _key: string; | ||
public get key(): string { return this._key; } | ||
|
||
public readonly invoke: (...args: Parameters<T>) => ReturnType<T>; | ||
|
||
public constructor() | ||
{ | ||
const _default = () => | ||
{ | ||
throw new NotImplementedException( | ||
"The `SwitchableCallback` has no callback defined yet. " + | ||
"Did you forget to call the `register` method?" | ||
); | ||
}; | ||
|
||
super(); | ||
|
||
this._callback = ((_default) as unknown) as T; | ||
this._callbacks = new Map<string, T>(); | ||
|
||
this._isEnabled = false; | ||
this._key = ""; | ||
|
||
this.invoke = (...args: Parameters<T>): ReturnType<T> => this._callback(...args); | ||
} | ||
|
||
public enable(): void | ||
{ | ||
if (!(this._key)) | ||
{ | ||
throw new KeyException( | ||
"The `SwitchableCallback` has no callback defined yet. " + | ||
"Did you forget to call the `register` method?" | ||
); | ||
} | ||
if (this._isEnabled) | ||
{ | ||
throw new RuntimeException("The `SwitchableCallback` is already enabled."); | ||
} | ||
|
||
this._callback = this._callbacks.get(this._key)!; | ||
this._isEnabled = true; | ||
} | ||
public disable(): void | ||
{ | ||
if (!(this._isEnabled)) | ||
{ | ||
throw new RuntimeException("The `SwitchableCallback` is already disabled."); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
this._callback = (() => { }) as T; | ||
this._isEnabled = false; | ||
} | ||
|
||
public register(key: string, callback: T): void | ||
{ | ||
if (this._callbacks.size === 0) | ||
{ | ||
this._key = key; | ||
this._callback = callback; | ||
} | ||
else if (this._callbacks.has(key)) | ||
{ | ||
throw new KeyException(`The key '${key}' has already been used for another callback.`); | ||
} | ||
|
||
this._callbacks.set(key, callback); | ||
} | ||
public unregister(key: string): void | ||
{ | ||
if (!(this._callbacks.has(key))) | ||
{ | ||
throw new KeyException(`The key '${key}' doesn't yet have any associated callback.`); | ||
} | ||
|
||
this._callbacks.delete(key); | ||
} | ||
|
||
public switch(key: string): void | ||
{ | ||
if (!(this._callbacks.has(key))) | ||
{ | ||
throw new KeyException(`The key '${key}' doesn't yet have any associated callback.`); | ||
} | ||
|
||
this._key = key; | ||
this._callback = this._callbacks.get(key)!; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export type Callback<A extends unknown[] = [], R = void> = (...args: A) => R; |
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
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
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
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
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