Farmbot-Web-App/frontend/resources/tagged_resources.ts

108 lines
3.2 KiB
TypeScript
Raw Normal View History

import { isObject, isString, get, chain } from "lodash";
2017-08-15 14:21:41 -06:00
import { betterCompact } from "../util";
2018-04-25 11:45:22 -06:00
import { assertUuid } from "./util";
2018-08-01 07:03:35 -06:00
import {
TaggedResource,
ResourceName,
TaggedRegimen,
TaggedSequence,
TaggedTool,
TaggedFarmEvent,
TaggedToolSlotPointer,
TaggedPlantPointer,
TaggedGenericPointer,
PointerType,
SpecialStatus,
2018-09-13 16:00:14 -06:00
TaggedPlantTemplate,
2019-11-27 13:11:45 -07:00
TaggedSavedGarden,
2018-08-01 07:03:35 -06:00
} from "farmbot";
2017-10-28 09:54:43 -06:00
2017-06-29 12:54:02 -06:00
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;
2017-08-15 14:21:41 -06:00
/** Indicates if the resource is saved, saving or dirty.
* `undefined` denotes that the resource is saved. */
specialStatus: SpecialStatus;
2017-08-15 11:35:25 -06:00
}
2017-08-15 14:21:41 -06:00
/** Given an array of TaggedResources, returns the most "important" special status.
2017-08-16 09:04:54 -06:00
* the hierarchy is SAVED => DIRTY => SAVING */
export function getArrayStatus(i: TaggedResource[]): SpecialStatus {
const r = betterCompact(chain(i).map(x => x.specialStatus).uniq().value());
2017-08-15 14:21:41 -06:00
if (r.length) {
return (r.includes(SpecialStatus.SAVING)) ?
SpecialStatus.SAVING : SpecialStatus.DIRTY;
} else {
return SpecialStatus.SAVED;
2017-08-15 14:21:41 -06:00
}
}
2017-06-29 12:54:02 -06:00
/** Spot check to be certain a TaggedResource is what it says it is. */
2017-07-17 12:34:46 -06:00
export function sanityCheck(x: object): x is TaggedResource {
2017-06-29 12:54:02 -06:00
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 {
2018-10-29 13:00:55 -06:00
const isOk = (isObject(x)
2017-06-29 12:54:02 -06:00
&& isString(get(x, "kind"))
&& isString(get(x, "uuid"))
2017-07-10 08:49:54 -06:00
&& isObject(get(x, "body")));
2018-10-29 13:00:55 -06:00
if (isOk) {
return true;
} else {
console.error(JSON.stringify(x));
2018-10-29 13:00:55 -06:00
return false;
}
2017-06-29 12:54:02 -06:00
}
2017-08-28 05:49:13 -06:00
const is = (r: ResourceName) => function isOfTag(x: object): x is TaggedResource {
const safe = (sanityCheck(x) && isTaggedResource(x) && x.kind == r);
2017-06-29 12:54:02 -06:00
if (!safe) {
if (x) {
throw new Error("Possible bad index");
}
}
return safe;
};
2017-08-28 06:23:53 -06:00
function isTaggedPoint(x: {}): x is PointerType {
2017-10-27 07:31:25 -06:00
return (is("Point")(x)) && (x.kind === "Point");
2017-06-29 12:54:02 -06:00
}
export const isTaggedRegimen =
2017-10-27 07:31:25 -06:00
(x: object): x is TaggedRegimen => is("Regimen")(x);
export const isTaggedFolder =
(x: object): x is TaggedRegimen => is("Folder")(x);
export const isTaggedSequence =
2017-10-27 07:31:25 -06:00
(x: object): x is TaggedSequence => is("Sequence")(x);
export const isTaggedTool =
2017-10-27 07:31:25 -06:00
(x: object): x is TaggedTool => is("Tool")(x);
export const isTaggedFarmEvent =
2017-10-27 07:31:25 -06:00
(x: object): x is TaggedFarmEvent => is("FarmEvent")(x);
export const isTaggedToolSlotPointer =
2017-06-29 12:54:02 -06:00
(x: object): x is TaggedToolSlotPointer => {
2017-07-10 08:49:54 -06:00
return isTaggedPoint(x) && (x.body.pointer_type === "ToolSlot");
2017-06-29 12:54:02 -06:00
};
export const isTaggedPlantPointer =
2017-06-29 12:54:02 -06:00
(x: object): x is TaggedPlantPointer => {
2017-07-10 08:49:54 -06:00
return isTaggedPoint(x) && (x.body.pointer_type === "Plant");
2017-06-29 12:54:02 -06:00
};
export const isTaggedGenericPointer =
2017-06-29 12:54:02 -06:00
(x: object): x is TaggedGenericPointer => {
2017-07-10 08:49:54 -06:00
return isTaggedPoint(x) && (x.body.pointer_type === "GenericPointer");
};
export const isTaggedSavedGarden =
2019-11-27 13:11:45 -07:00
(x: object): x is TaggedSavedGarden => is("SavedGarden")(x);
export const isTaggedPlantTemplate =
2018-09-13 16:00:14 -06:00
(x: object): x is TaggedPlantTemplate => is("PlantTemplate")(x);