lib/core/src/items/item.class.ts
Properties |
|
Methods |
|
Accessors |
constructor(config: C)
|
||||||
Defined in lib/core/src/items/item.class.ts:54
|
||||||
Parameters :
|
Private Optional _config |
Type : C
|
Defined in lib/core/src/items/item.class.ts:54
|
Public Optional id |
Type : string
|
Defined in lib/core/src/items/item.class.ts:50
|
Public Optional update$ |
Type : Observable<string>
|
Defined in lib/core/src/items/item.class.ts:52
|
Protected Optional updateSubject |
Type : Subject<string>
|
Defined in lib/core/src/items/item.class.ts:51
|
Static acceptsAsConfig | ||||
acceptsAsConfig(x)
|
||||
Defined in lib/core/src/items/item.class.ts:27
|
||||
Parameters :
|
Static assertData | ||||
assertData(data)
|
||||
Defined in lib/core/src/items/item.class.ts:40
|
||||
Parameters :
Returns :
never
|
Static findConfigs | ||||
findConfigs(data)
|
||||
Defined in lib/core/src/items/item.class.ts:32
|
||||
Type parameters :
|
||||
Parameters :
Returns :
U[]
|
Public matches | ||||
matches(query)
|
||||
Defined in lib/core/src/items/item.class.ts:80
|
||||
TODO: Is this useful or needed anywhere?
Parameters :
Returns :
boolean
|
Public toJSON |
toJSON()
|
Defined in lib/core/src/items/item.class.ts:75
|
Returns :
string
|
config | ||||||
getconfig()
|
||||||
Defined in lib/core/src/items/item.class.ts:62
|
||||||
setconfig(config: C)
|
||||||
Defined in lib/core/src/items/item.class.ts:66
|
||||||
Parameters :
Returns :
void
|
import {
Observable,
Subject
} from 'rxjs'
import {
throwError,
assert
} from '../utils'
export type ItemConfig = unknown
export type ConfigOf<I> = I extends Item<infer C>
? C extends ItemConfig
? C
: never
: never
export abstract class Item<C extends ItemConfig = unknown> {
// STATIC:
public static acceptsAsConfig(x: unknown): x is unknown { // make this a typeguard when extending
throwError("Item#acceptsAsConfig() please implement this static method on classes extending Item. ", x)
return false
}
static findConfigs<U>(data:unknown): U[] { //sadly I cannot use C[] here: https://github.com/microsoft/TypeScript/issues/34665
if(this.acceptsAsConfig(data)) return [data as U]
if(Array.isArray(data)) return (data as unknown[]).map( (x:unknown) => this.findConfigs<U>(x) ).flat()
return []
}
static assertData(data:unknown): asserts data is never { // make this a proper assertion when extending
const className = this.name
assert(this.acceptsAsConfig(data), `${className}.assertData() failed.`, data)
}
// INSTANCE:
public id? : string
protected updateSubject? : Subject<string>
public update$? : Observable<string>
private _config? : C
constructor(config: C) {
this.config = config
}
public get config(): C {
return this._config
}
public set config(config: C){
const itemClass = this.constructor as unknown as { acceptsAsConfig: ((c:unknown) => boolean), name: string }
itemClass.acceptsAsConfig(config) || throwError(`Invalid ${itemClass.name} config.`)
this._config = config
}
public toJSON(): string { return JSON.stringify(this.config) }
/**
* TODO: Is this useful or needed anywhere?
*/
public matches(query:unknown): boolean {
const regex = new RegExp(String(query), 'gi')
return !!JSON.stringify(this.config || {}).match(regex)
}
}