Files
30-seconds-of-code/node_modules/relay-runtime/lib/RelayRecordSourceProxy.js
2019-08-20 15:52:05 +02:00

120 lines
4.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
'use strict';
/**
* @internal
*
* A helper for manipulating a `RecordSource` via an imperative/OO-style API.
*/
var RelayRecordSourceProxy =
/*#__PURE__*/
function () {
function RelayRecordSourceProxy(mutator, handlerProvider) {
this.__mutator = mutator;
this._handlerProvider = handlerProvider || null;
this._proxies = {};
}
var _proto = RelayRecordSourceProxy.prototype;
_proto.publishSource = function publishSource(source, fieldPayloads) {
var _this = this;
var dataIDs = source.getRecordIDs();
dataIDs.forEach(function (dataID) {
var status = source.getStatus(dataID);
if (status === require("./RelayRecordState").EXISTENT) {
var sourceRecord = source.get(dataID);
if (sourceRecord) {
if (_this.__mutator.getStatus(dataID) !== require("./RelayRecordState").EXISTENT) {
_this.create(dataID, require("./RelayModernRecord").getType(sourceRecord));
}
_this.__mutator.copyFieldsFromRecord(sourceRecord, dataID);
delete _this._proxies[dataID];
}
} else if (status === require("./RelayRecordState").NONEXISTENT) {
_this["delete"](dataID);
}
});
if (fieldPayloads && fieldPayloads.length) {
fieldPayloads.forEach(function (fieldPayload) {
var handler = _this._handlerProvider && _this._handlerProvider(fieldPayload.handle);
!handler ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayModernEnvironment: Expected a handler to be provided for handle `%s`.', fieldPayload.handle) : require("fbjs/lib/invariant")(false) : void 0;
handler.update(_this, fieldPayload);
});
}
};
_proto.commitPayload = function commitPayload(operation, response) {
if (!response) {
return new (require("./RelayRecordSourceSelectorProxy"))(this, operation.fragment);
}
var _normalizeRelayPayloa = require("./normalizeRelayPayload")(operation.root, response),
source = _normalizeRelayPayloa.source,
fieldPayloads = _normalizeRelayPayloa.fieldPayloads;
this.publishSource(source, fieldPayloads);
return new (require("./RelayRecordSourceSelectorProxy"))(this, operation.fragment);
};
_proto.create = function create(dataID, typeName) {
this.__mutator.create(dataID, typeName);
delete this._proxies[dataID];
var record = this.get(dataID); // For flow
!record ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayRecordSourceProxy#create(): Expected the created record to exist.') : require("fbjs/lib/invariant")(false) : void 0;
return record;
};
_proto["delete"] = function _delete(dataID) {
!(dataID !== require("./RelayStoreUtils").ROOT_ID) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayRecordSourceProxy#delete(): Cannot delete the root record.') : require("fbjs/lib/invariant")(false) : void 0;
delete this._proxies[dataID];
this.__mutator["delete"](dataID);
};
_proto.get = function get(dataID) {
if (!this._proxies.hasOwnProperty(dataID)) {
var status = this.__mutator.getStatus(dataID);
if (status === require("./RelayRecordState").EXISTENT) {
this._proxies[dataID] = new (require("./RelayRecordProxy"))(this, this.__mutator, dataID);
} else {
this._proxies[dataID] = status === require("./RelayRecordState").NONEXISTENT ? null : undefined;
}
}
return this._proxies[dataID];
};
_proto.getRoot = function getRoot() {
var root = this.get(require("./RelayStoreUtils").ROOT_ID);
if (!root) {
root = this.create(require("./RelayStoreUtils").ROOT_ID, require("./RelayStoreUtils").ROOT_TYPE);
}
!(root && root.getType() === require("./RelayStoreUtils").ROOT_TYPE) ? process.env.NODE_ENV !== "production" ? require("fbjs/lib/invariant")(false, 'RelayRecordSourceProxy#getRoot(): Expected the source to contain a ' + 'root record.') : require("fbjs/lib/invariant")(false) : void 0;
return root;
};
return RelayRecordSourceProxy;
}();
module.exports = RelayRecordSourceProxy;