• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WolferyScripting / resclient-ts / #36

28 Aug 2025 01:20PM UTC coverage: 51.126% (-0.07%) from 51.192%
#36

push

DonovanDMC
1.1.5

230 of 292 branches covered (78.77%)

Branch coverage included in aggregate %.

1655 of 3395 relevant lines covered (48.75%)

10.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.37
/lib/models/ResCollectionModel.ts
1
import type ResClient from "./ResClient.js";
1✔
2
import ResModel, { type ResModelResourceEvents, type ResModelOptions } from "./ResModel.js";
1✔
3
import { changeDiff } from "../util/util.js";
1✔
4
import Properties from "../util/Properties.js";
1✔
5

1✔
6
export interface ResCollectionModelEvents<T = unknown> {
1✔
7
    add: [data: CollectionModelAddRemove<T>];
1✔
8
    remove: [data: CollectionModelAddRemove<T>];
1✔
9
}
1✔
10
export interface CollectionModelAddRemove<T = unknown> { item: T; key: string; }
1✔
11
export default class ResCollectionModel<T = unknown, ResourceEvents extends { [K in keyof ResourceEvents]: Array<unknown> } = ResModelResourceEvents<Record<string, T>>, ModelEvents extends { [K in keyof ModelEvents]: Array<unknown> } = ResCollectionModelEvents<T>> extends ResModel<Record<string, T>, ResourceEvents, ModelEvents> {
1!
12
    private onChange = this._onChange.bind(this);
×
13
    protected _list!: Array<T>;
×
14
    protected _validateItem!: (item: T) => boolean;
×
15
    constructor(api: ResClient, rid: string, validateItem: (item: T) => boolean, options?: Omit<ResModelOptions, "definition">) {
×
16
        super(api, rid, options);
×
17
        Properties.of(this)
×
18
            .writable("_list", [])
×
19
            .readOnly("_validateItem", validateItem);
×
20
    }
×
21

×
22
    private _onChange(data: Record<string, T | undefined>): void {
×
23
        const { added, removed } = changeDiff(this, data);
×
24

×
25
        for (const add of added) {
×
26
            this._list.push(add.item);
×
27
            this.api.eventBus.emit(this, "add", add);
×
28
        }
×
29

×
30
        for (const remove of removed) {
×
31
            const index = this._list.indexOf(remove.item);
×
32
            if (index !== -1) this._list.splice(index, 1);
×
33
            this.api.eventBus.emit(this, "remove", remove);
×
34
        }
×
35
    }
×
36

×
37
    protected override async _listen(on: boolean): Promise<void> {
×
38
        const m = on ? "resourceOn" : "resourceOff";
×
39
        this[m]("change", this.onChange);
×
40
    }
×
41

×
42
    protected override _shouldPromoteKey(key: string, value: T): boolean {
×
43
        return !this._validateItem(value) && super._shouldPromoteKey(key, value);
×
44
    }
×
45

×
46
    get list(): Array<T> {
×
47
        return Object.values(this.props);
×
48
    }
×
49

×
50
    get(key: string): T | undefined {
×
51
        return this.props[key];
×
52
    }
×
53
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc