MCPcopy Index your code
hub / github.com/ampproject/amphtml / setStorage

Method setStorage

extensions/amp-script/0.1/amp-script.js:888–934  ·  view source on GitHub ↗

* @param {!StorageLocation} location * @param {?string} key * @param {?string} value * @return {!Promise}

(location, key, value)

Source from the content-addressed store, hash-verified

886 * @return {!Promise}
887 */
888 setStorage(location, key, value) {
889 if (location === StorageLocation.AMP_STATE) {
890 return Services.bindForDocOrNull(this.element_).then((bind) => {
891 if (bind) {
892 const state = tryParseJson(value, () => {
893 dev().error(TAG, 'Invalid AMP.setState() argument: %s', value);
894 });
895 if (state) {
896 // Only evaluate updates in case of recent user interaction or a small fixed layout.
897 const fullEval = this.allowFullEval_();
898 const constrain = this.shouldConstrainEval_()
899 ? [this.element_]
900 : undefined;
901
902 if (!fullEval && !constrain) {
903 user().warn(
904 TAG,
905 'AMP.setState only updated page state and did not reevaluate bindings due to lack of recent user interaction.'
906 );
907 }
908 bind.setState(state, {
909 skipEval: !fullEval && !constrain,
910 skipAmpState: false,
911 constrain,
912 });
913 }
914 }
915 });
916 }
917 const storage = this.storageFor_(location);
918 if (key === null) {
919 if (value === null) {
920 user().error(TAG, 'Storage.clear() is not supported in amp-script.');
921 }
922 } else {
923 if (key.startsWith('amp-')) {
924 user().error(TAG, 'Invalid "amp-" prefix for storage key: %s', key);
925 } else {
926 if (value === null) {
927 storage.removeItem(key);
928 } else {
929 storage.setItem(key, value);
930 }
931 }
932 }
933 return Promise.resolve();
934 }
935
936 /**
937 * @param {!StorageLocation} location

Callers 1

test-amp-script.jsFile · 0.80

Calls 11

storageFor_Method · 0.95
tryParseJsonFunction · 0.90
devFunction · 0.90
userFunction · 0.90
warnMethod · 0.80
removeItemMethod · 0.80
setItemMethod · 0.80
resolveMethod · 0.80
thenMethod · 0.45
errorMethod · 0.45
setStateMethod · 0.45

Tested by

no test coverage detected