semantic-network
Version:
A utility library for manipulating a list of links that form a semantic interface to a network of resources.
60 lines • 2.88 kB
JavaScript
import { __awaiter } from "tslib";
import { TrackedRepresentationFactory } from './trackedRepresentationFactory';
import { RepresentationUtil } from '../utils/representationUtil';
import anylogger from 'anylogger';
import { instanceOfCollection } from '../utils/instanceOf/instanceOfCollection';
import { Status } from './status';
import { TrackedRepresentationUtil } from '../utils/trackedRepresentationUtil';
const log = anylogger('delete');
/**
*
* TODO: accept but don't require TrackedRepresentation interface
* @param resource
* @param options
* @returns removed representation or default
*/
export function del(resource, options) {
return __awaiter(this, void 0, void 0, function* () {
const { where = undefined, removeOnDeleteItem = true, reloadOnDelete = false, } = Object.assign({}, options);
// find specific item in collection to delete
if (where) {
if (instanceOfCollection(resource)) {
// refresh collection first
const collection = yield TrackedRepresentationFactory.load(resource, options);
if (instanceOfCollection(collection)) {
// then check for existence
// TODO: needs to process collection<T & LocalState> rather than collection<T>
const item = RepresentationUtil.findInCollection(collection, options);
if (item) {
const deletedResource = yield TrackedRepresentationFactory.del(item, options);
if (deletedResource && removeOnDeleteItem) {
TrackedRepresentationFactory.removeCollectionItem(collection, deletedResource);
}
return deletedResource;
}
else {
log.debug('Item not found in collection');
return;
}
}
}
else {
log.warn('Where options cannot be used outside of a collection, skipping where');
// fall through to return context resource
}
}
if (instanceOfCollection(resource)) {
log.debug('Attempting to delete collection resource');
}
const deletedResource = yield TrackedRepresentationFactory.del(resource, options);
if (!reloadOnDelete) {
return deletedResource;
}
// some deleted resources are only logically deleted and thus attributes are updates
// reload from the server
// the base logic is that once deleted, it is not retrieved again, so set to stale
TrackedRepresentationUtil.getState(deletedResource).status = Status.stale;
return yield TrackedRepresentationFactory.load(deletedResource, options);
});
}
//# sourceMappingURL=delete.js.map