* @param {!StorageLocation} location * @param {?string} key * @param {?string} value * @return {!Promise}
(location, key, value)
| 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 |
no test coverage detected