/
tagged_resources.ts
103 lines (96 loc) · 3.11 KB
/
tagged_resources.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { isObject, isString, get } from "lodash";
import { betterCompact } from "../util";
import * as _ from "lodash";
import { assertUuid } from "./util";
import {
TaggedCrop,
TaggedResource,
ResourceName,
TaggedRegimen,
TaggedSequence,
TaggedTool,
TaggedFarmEvent,
TaggedLog,
TaggedToolSlotPointer,
TaggedPlantPointer,
TaggedGenericPointer,
PointerType,
SpecialStatus,
TaggedPlantTemplate,
} from "farmbot";
export interface TaggedResourceBase {
kind: ResourceName;
/** Unique identifier and index key.
* We can't use the object's `id` attribute as a local index key because
* unsaved objects don't have one.
*/
uuid: string;
body: object;
/** Indicates if the resource is saved, saving or dirty.
* `undefined` denotes that the resource is saved. */
specialStatus: SpecialStatus;
}
/** Given an array of TaggedResources, returns the most "important" special status.
* the hierarchy is SAVED => DIRTY => SAVING */
export function getArrayStatus(i: TaggedResource[]): SpecialStatus {
const r = betterCompact(_(i).map(x => x.specialStatus).uniq().value());
if (r.length) {
return (r.includes(SpecialStatus.SAVING)) ?
SpecialStatus.SAVING : SpecialStatus.DIRTY;
} else {
return SpecialStatus.SAVED;
}
}
/** Spot check to be certain a TaggedResource is what it says it is. */
export function sanityCheck(x: object): x is TaggedResource {
if (isTaggedResource(x)) {
assertUuid(x.kind, x.uuid);
return true;
} else {
throw new Error("Bad kind, uuid, or body: " + JSON.stringify(x));
}
}
export function isTaggedResource(x: object): x is TaggedResource {
return (isObject(x)
&& isString(get(x, "kind"))
&& isString(get(x, "uuid"))
&& isObject(get(x, "body")));
}
const is = (r: ResourceName) => function isOfTag(x: object): x is TaggedResource {
const safe = (sanityCheck(x) && isTaggedResource(x) && x.kind == r);
if (!safe) {
if (x) {
throw new Error("Possible bad index");
}
}
return safe;
};
function isTaggedPoint(x: {}): x is PointerType {
return (is("Point")(x)) && (x.kind === "Point");
}
export let isTaggedRegimen =
(x: object): x is TaggedRegimen => is("Regimen")(x);
export let isTaggedSequence =
(x: object): x is TaggedSequence => is("Sequence")(x);
export let isTaggedTool =
(x: object): x is TaggedTool => is("Tool")(x);
export let isTaggedCrop =
(x: object): x is TaggedCrop => is("Crop")(x);
export let isTaggedFarmEvent =
(x: object): x is TaggedFarmEvent => is("FarmEvent")(x);
export let isTaggedLog =
(x: object): x is TaggedLog => is("Log")(x);
export let isTaggedToolSlotPointer =
(x: object): x is TaggedToolSlotPointer => {
return isTaggedPoint(x) && (x.body.pointer_type === "ToolSlot");
};
export let isTaggedPlantPointer =
(x: object): x is TaggedPlantPointer => {
return isTaggedPoint(x) && (x.body.pointer_type === "Plant");
};
export let isTaggedGenericPointer =
(x: object): x is TaggedGenericPointer => {
return isTaggedPoint(x) && (x.body.pointer_type === "GenericPointer");
};
export let isTaggedPlantTemplate =
(x: object): x is TaggedPlantTemplate => is("PlantTemplate")(x);