|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { Persistence } from '../../model/public_types'; |
| 19 | + |
| 20 | +import { |
| 21 | + PersistenceInternal, |
| 22 | + PersistenceType, |
| 23 | + PersistenceValue, |
| 24 | + StorageEventListener |
| 25 | +} from '../../core/persistence'; |
| 26 | + |
| 27 | +export class CookiePersistence implements PersistenceInternal { |
| 28 | + static type: 'COOKIE' = 'COOKIE'; |
| 29 | + readonly type = PersistenceType.COOKIE; |
| 30 | + listeners: Map<StorageEventListener, (e: any) => void> = new Map(); |
| 31 | + |
| 32 | + async _isAvailable(): Promise<boolean> { |
| 33 | + return navigator.hasOwnProperty('cookieEnabled') ? |
| 34 | + navigator.cookieEnabled : |
| 35 | + true; |
| 36 | + } |
| 37 | + |
| 38 | + async _set(_key: string, _value: PersistenceValue): Promise<void> { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + async _get<T extends PersistenceValue>(key: string): Promise<T | null> { |
| 43 | + const cookie = await (window as any).cookieStore.get(key); |
| 44 | + return cookie?.value; |
| 45 | + } |
| 46 | + |
| 47 | + async _remove(key: string): Promise<void> { |
| 48 | + const cookie = await (window as any).cookieStore.get(key); |
| 49 | + if (!cookie) { |
| 50 | + return; |
| 51 | + } |
| 52 | + await (window as any).cookieStore.set({ ...cookie, value: "0" }); |
| 53 | + await fetch(`/__cookies__`, { method: 'DELETE' }).catch(() => undefined); |
| 54 | + } |
| 55 | + |
| 56 | + _addListener(_key: string, _listener: StorageEventListener): void { |
| 57 | + // TODO fallback to polling if cookieStore is not available |
| 58 | + const cb = (event: any) => { |
| 59 | + const cookie = event.changed.find((change: any) => change.name === _key); |
| 60 | + if (cookie) { |
| 61 | + _listener(cookie.value); |
| 62 | + } |
| 63 | + }; |
| 64 | + this.listeners.set(_listener, cb); |
| 65 | + (window as any).cookieStore.addEventListener('change', cb); |
| 66 | + } |
| 67 | + |
| 68 | + _removeListener(_key: string, _listener: StorageEventListener): void { |
| 69 | + const cb = this.listeners.get(_listener); |
| 70 | + if (!cb) { |
| 71 | + return; |
| 72 | + } |
| 73 | + (window as any).cookieStore.removeEventListener('change', cb); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * An implementation of {@link Persistence} of type 'NONE'. |
| 79 | + * |
| 80 | + * @public |
| 81 | + */ |
| 82 | +export const cookiePersistence: Persistence = CookiePersistence; |
0 commit comments