-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce an inter-service communication layer
- Extract the event dispatching mechanism into a new service - Refactor token-related services to handle external changes - Add support for external services to dispatch events - Restructure token store concepts - Restrict tracker access to the token to read-only operation - Add a prefix to tracking-related events to disambiguate with other events
- Loading branch information
1 parent
05c25c1
commit dcb5c91
Showing
51 changed files
with
1,356 additions
and
500 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,21 @@ | ||
import Cache from './index'; | ||
|
||
export default class InMemoryCache implements Cache { | ||
private cache: string | null = null; | ||
private cache?: string; | ||
|
||
public constructor(cache: string | null = null) { | ||
public constructor(cache?: string) { | ||
this.cache = cache; | ||
} | ||
|
||
public get(): string | null { | ||
return this.cache; | ||
return this.cache ?? null; | ||
} | ||
|
||
public put(value: string | null): void { | ||
public put(value: string): void { | ||
this.cache = value; | ||
} | ||
|
||
public clear(): void { | ||
delete this.cache; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
export default interface Cache { | ||
get(): string|null; | ||
put(value: string|null): void; | ||
put(value: string): void; | ||
clear(): void; | ||
} | ||
|
||
export interface CacheListener { | ||
(value: string|null): void; | ||
} | ||
|
||
export interface ObservableCache extends Cache { | ||
addListener(listener: CacheListener): void; | ||
|
||
removeListener(listener: CacheListener): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {CacheListener, ObservableCache} from './index'; | ||
|
||
export default class LocalStorageCache implements ObservableCache { | ||
private readonly storage: Storage; | ||
|
||
private readonly key: string; | ||
|
||
private value: string|null; | ||
|
||
private readonly listeners: CacheListener[] = []; | ||
|
||
public constructor(storage: Storage, key: string) { | ||
this.storage = storage; | ||
this.key = key; | ||
this.value = storage.getItem(key); | ||
} | ||
|
||
public static autoSync(cache: LocalStorageCache): (() => void) { | ||
const listener = cache.sync.bind(cache); | ||
|
||
window.addEventListener('storage', listener); | ||
|
||
return (): void => window.removeEventListener('storage', listener); | ||
} | ||
|
||
public get(): string|null { | ||
return this.value; | ||
} | ||
|
||
public put(value: string): void { | ||
this.storage.setItem(this.key, value); | ||
|
||
if (this.value !== value) { | ||
this.value = value; | ||
this.notifyChange(value); | ||
} | ||
} | ||
|
||
public clear(): void { | ||
this.storage.removeItem(this.key); | ||
|
||
if (this.value !== null) { | ||
this.value = null; | ||
this.notifyChange(null); | ||
} | ||
} | ||
|
||
public addListener(listener: CacheListener): void { | ||
if (!this.listeners.includes(listener)) { | ||
this.listeners.push(listener); | ||
} | ||
} | ||
|
||
public removeListener(listener: CacheListener): void { | ||
const index = this.listeners.indexOf(listener); | ||
|
||
if (index > -1) { | ||
this.listeners.splice(index, 1); | ||
} | ||
} | ||
|
||
private notifyChange(value: string|null): void { | ||
this.listeners.forEach(listener => listener(value)); | ||
} | ||
|
||
private sync(event: StorageEvent): void { | ||
if (event.storageArea !== this.storage || (event.key !== null && event.key !== this.key)) { | ||
// Ignore unrelated changes | ||
return; | ||
} | ||
|
||
/* | ||
* Retrieving the value from the store rather than the event ensures | ||
* the cache will be in sync with the latest value set. | ||
* In case of cascading changes, it prevents notifying listeners | ||
* about intermediate states already outdated at this point. | ||
*/ | ||
const value = this.storage.getItem(this.key); | ||
|
||
if (this.value !== value) { | ||
this.value = value; | ||
this.notifyChange(value); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.