Skip to content

Commit

Permalink
add missing explicit types in methods and properties in index.ts and …
Browse files Browse the repository at this point in the history
…EvEmit.ts
  • Loading branch information
pixiedevpraveen committed Nov 30, 2024
1 parent e13100c commit 2dc13e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/EvEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class EvEmit<T> {
* set change event listener fire throttle time
* @param cTime Change emit fire throttle time
*/
setCEVTime(cTime: number) {
setCEVTime(cTime: number): void {
this.cEv = throttle(() => this.emit('C'), cTime)
}

Expand All @@ -45,17 +45,16 @@ export class EvEmit<T> {
* pd.on('L', (ev)=>{ console.log('database loaded') }) // on load event
* pd.on('Q', (ev)=>{}) // on quit event
*/
on<Type extends DBEvent<T>['type'] | DBEvent<T>['type'][], Ev extends Type extends any[] ? Type[number] : Type>(event: Type, fn: ((...args: Extract<DBEvent<T>, { type: Ev }> extends { payload: infer Payload } ? [event: Ev, data: Payload extends any[] ? Payload : [Payload]] : [event: Ev]) => void)) {
on<Type extends DBEvent<T>['type'] | DBEvent<T>['type'][], Ev extends Type extends any[] ? Type[number] : Type>(event: Type, fn: ((...args: Extract<DBEvent<T>, { type: Ev }> extends { payload: infer Payload } ? [event: Ev, data: Payload extends any[] ? Payload : [Payload]] : [event: Ev]) => void)): typeof fn {
toArray(event).forEach(e => {
if (hasOwn(this.events, e))
this.events[e].push(fn)
})
return fn
}
protected offAll() {

protected offAll(): void {
this.events = freeze({ L: [], I: [], U: [], D: [], C: [], Q: [] })
// for (const c in this.events)
// if (hasOwn(this.events, c)) this.events[c] = []
}

/**
Expand All @@ -65,7 +64,7 @@ export class EvEmit<T> {
* @example
* pd.off('Q', (ev)=>{})
*/
off<Type extends DBEvent<T>['type'] | DBEvent<T>['type'][], Ev extends Type extends any[] ? Type[number] : Type>(event: Type, fn: ((...args: Extract<DBEvent<T>, { type: Ev }> extends { payload: infer Payload } ? [event: Ev, data: Payload extends any[] ? Payload : [Payload]] : [event: Ev]) => void)) {
off<Type extends DBEvent<T>['type'] | DBEvent<T>['type'][], Ev extends Type extends any[] ? Type[number] : Type>(event: Type, fn: ((...args: Extract<DBEvent<T>, { type: Ev }> extends { payload: infer Payload } ? [event: Ev, data: Payload extends any[] ? Payload : [Payload]] : [event: Ev]) => void)): typeof fn {
toArray(event).forEach(e => hasOwn(this.events, e) && remove(this.events[e], fn))
return fn
}
Expand All @@ -80,7 +79,7 @@ export class EvEmit<T> {
* pd.emit('L') // emit load event
* pd.emit('Q') // emit quit event
*/
emit<Type extends DBEvent<T>['type']>(...args: Extract<DBEvent<T>, { type: Type }> extends { payload: infer Payload } ? [event: Type, ...data: Payload extends any[] ? Payload : [Payload]] : [event: Type]) {
emit<Type extends DBEvent<T>['type']>(...args: Extract<DBEvent<T>, { type: Type }> extends { payload: infer Payload } ? [event: Type, ...data: Payload extends any[] ? Payload : [Payload]] : [event: Type]): void {
const [ev, ...data] = args;
hasOwn(this.events, ev) && setTimeout(() => this.events[ev].forEach(fn => fn(ev, data)), 10)
'IUD'.includes(ev) && this.cEv()
Expand Down
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class PixieDb<T extends Record<any, any>, Key extends keyof T> extends Ev
/**
* primary key or unique key for the database
*/
readonly key
readonly key: Key

/**
* keyMap is a map of key and data
Expand Down Expand Up @@ -69,7 +69,7 @@ export class PixieDb<T extends Record<any, any>, Key extends keyof T> extends Ev
* pd.select(['name', 'price']).eq('category', 'Fruit').data() // [{ name: 'Apple', price: 10 }, { name: 'Banana', price: 10 }, ...]
* pd.select().eq('category', 'Fruit').orderBy('name', ['price', 'desc']).data() // [{ id: 1, name: 'Apple', price: 10, category: 'Fruit' }, { id: 2, name: 'Banana', price: 10, category: 'Fruit' }, ...]
*/
select<Fields extends readonly (keyof T)[]>(fields?: Fields) {
select<Fields extends readonly (keyof T)[]>(fields?: Fields): SelectQueryBuilder<T, Fields> {
return this.#resutSet(isArray(fields) ? fields : []) as unknown as SelectQueryBuilder<T, Fields>
}

Expand All @@ -79,15 +79,15 @@ export class PixieDb<T extends Record<any, any>, Key extends keyof T> extends Ev
* pd.where().eq('category', 'Fruit').delete() // delete all fruit products
* pd.where().eq('category', 'Fruit').update({ price: 20 }) // update all fruit products price to 20
*/
where() {
return this.#resutSet([], 'where') as unknown as WhereQueryBuilder<T>
where(): WhereQueryBuilder<T> {
return this.#resutSet([], 'where')
}

/**
* @param f fields to select default: [] (means all fields in select)
* @param a action to for query builder
*/
#resutSet(f: Readonly<Array<keyof T>> = [], a?: 'select' | 'where') {
#resutSet(f: Readonly<Array<keyof T>> = [], a?: 'select' | 'where'): ResultSet<T, readonly (keyof T)[], Key> {
return new ResultSet<T, typeof f, Key>(this, this.key, this.#keyMap, this.#idxs, f, a)
}

Expand All @@ -101,7 +101,7 @@ export class PixieDb<T extends Record<any, any>, Key extends keyof T> extends Ev
* @param silent true to not emit events default false
* @param clone false to not clone data before insert
*/
insert<Doc extends T | T[]>(docs: Doc, { silent, clone, upsert }: { silent?: boolean, clone?: boolean, upsert?: boolean } = {}) {
insert<Doc extends T | T[]>(docs: Doc, { silent, clone, upsert }: { silent?: boolean, clone?: boolean, upsert?: boolean } = {}): Doc extends any[] ? T[] : (T | undefined) {
if (typeof docs !== 'object')
throw new PDBError('Value', 'Value must be an object or object array')
let st: Set<any> | undefined
Expand Down Expand Up @@ -149,7 +149,7 @@ export class PixieDb<T extends Record<any, any>, Key extends keyof T> extends Ev
* @param data data list/array to load (without clone)
* @param clear true to clear existing data
*/
load(data: T[], clear = false) {
load(data: T[], clear = false): void {
if (clear) {
this.#keyMap.clear()
Object.values(this.#idxs).forEach(i => i.clear())
Expand Down Expand Up @@ -217,7 +217,11 @@ export class PixieDb<T extends Record<any, any>, Key extends keyof T> extends Ev
* @example
* const json = pd.toJSON() // { key: 'id', indexes: ['price', 'category', {name: 'id', unique: true}], data: [{ id: 1, name: 'Apple', price: 10, category: 'Fruit' }, ...] }
*/
toJSON() {
toJSON(): {
key: Key;
indexes: (keyof T)[];
data: T[];
} {
return { key: this.key, indexes: this.indexes.map(i => this.isUniqIdx(i) ? ({ name: i, unique: true }) : i), data: this.data() }
}
}

0 comments on commit 2dc13e3

Please sign in to comment.