« get me outta code hell

album.js « thing « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/thing/album.js
blob: e99cfc36063cd93fcc4a541834fb3a1086468ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Thing from './thing.js';

import {
    validateDirectory,
    validateReference
} from './structures.js';

import {
    showAggregate,
    withAggregate
} from '../util/sugar.js';

export default class Album extends Thing {
    #directory = null;
    #tracks = [];

    static updateError = {
        directory: Thing.extendPropertyError('directory'),
        tracks: Thing.extendPropertyError('tracks')
    };

    update(source) {
        const err = this.constructor.updateError;

        withAggregate(({ nest, filter, throws }) => {

            if (source.directory) {
                nest(throws(err.directory), ({ call }) => {
                    if (call(validateDirectory, source.directory)) {
                        this.#directory = source.directory;
                    }
                });
            }

            if (source.tracks)
                this.#tracks = filter(source.tracks, validateReference('track'), throws(err.tracks));
        });
    }

    get directory() { return this.#directory; }
    get tracks() { return this.#tracks; }
}

const album = new Album();

console.log('tracks (before):', album.tracks);

try {
    album.update({
        directory: 'oh yes',
        tracks: [
            'lol',
            123,
            'track:oh-yeah',
            'group:what-am-i-doing-here'
        ]
    });
} catch (error) {
    showAggregate(error);
}

console.log('tracks (after):', album.tracks);