Compare commits

...

2 Commits
0.0.0 ... 0.1.0

Author SHA1 Message Date
e4c7dea9a5 Added typed emitter
All checks were successful
Build / Build NPM Project (push) Successful in 31s
Build / Tag Version (push) Successful in 7s
Build / Publish (push) Successful in 12s
2024-02-23 21:20:44 -05:00
ffaf8558b2 Fixed xhr
All checks were successful
Build / Build NPM Project (push) Successful in 15s
Build / Tag Version (push) Successful in 5s
Build / Publish (push) Successful in 13s
2024-02-07 23:14:37 -05:00
3 changed files with 26 additions and 20 deletions

View File

@ -1,9 +1,10 @@
{
"name": "@ztimson/js-utilities",
"version": "0.0.0",
"version": "0.1.0",
"description": "JavaScript Utility library",
"author": "Zak Timson",
"license": "MIT",
"private": false,
"repository": {
"type": "git",
"url": "https://git.zakscode.com/ztimson/js-utilities"

View File

@ -1,28 +1,33 @@
export type Listener<T> = (event: T) => any;
export type Listener = (...args: any[]) => any;
export type TypedEvents = {[k in string | symbol]: Listener} & {'*': (event: string, ...args: any[]) => any};
export class Emitter<T> {
private listeners: {[key: string]: Listener<T>} = {};
export class TypedEmitter<T extends TypedEvents = TypedEvents> {
private listeners: { [key in keyof T]?: Function[] } = {};
constructor() { }
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>) {
(this.listeners['*'] || []).forEach(l => l(event, ...args));
(this.listeners[event] || []).forEach(l => l(...args));
};
emit(e: T) {
Object.values(this.listeners).forEach(l => l(e));
off<K extends keyof T = string>(event: K, listener: T[K]): this {
console.log('cleared');
this.listeners[event] = (this.listeners[event] || []).filter(l => l === listener);
return this;
}
listen(fn: Listener<T>): () => {};
listen(key: string, fn: Listener<T>): () => {};
listen(keyOrFn: string | Listener<T>, fn?: Listener<T>): () => {} {
const func: any = fn ? fn : keyOrFn;
const key: string = typeof keyOrFn == 'string' ? keyOrFn :
`_${Object.keys(this.listeners).length.toString()}`;
this.listeners[<any>key] = func;
return () => delete this.listeners[<any>key];
on<K extends keyof T = string>(event: K, listener: T[K]) {
if(!this.listeners[event]) this.listeners[event] = [];
this.listeners[event]?.push(listener);
return () => this.off(event, listener);
}
once(fn: Listener<T>) {
const stop = this.listen(e => {
fn(e);
stop();
once<K extends keyof T = string>(event: K, listener?: T[K]): Promise<any> {
return new Promise(res => {
const unsubscribe = this.on(event, <any>((...args: any) => {
res(args.length == 1 ? args[0] : args);
if(listener) listener(...args);
unsubscribe();
}));
});
}
}

View File

@ -45,7 +45,7 @@ export class XHR<T> {
return fetch(`${this.baseUrl}${href || ''}`.replace(/([^:]\/)\/+/g, '$1'), {
headers,
method: opts.method || (body ? 'POST' : 'GET'),
body: (headers['Content-Type'].startsWith('application/json') && body) ? JSON.stringify(body) : body
body: (headers['Content-Type']?.startsWith('application/json') && body) ? JSON.stringify(body) : body
}).then(async resp => {
for(let fn of this.getInterceptors()) {
const wait = new Promise(res =>