-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache): add
Subscribable
(#1197)
# Overview - add `Subscribable` <!-- A clear and concise description of what this pr is about. --> ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/toss/suspensive/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Jonghyeon Ko <[email protected]>
- Loading branch information
1 parent
0720992
commit 0bb493c
Showing
5 changed files
with
41 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@suspensive/cache': minor | ||
--- | ||
|
||
feat(cache): add `Subscribable` |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
type Listener = (...args: any[]) => unknown | ||
|
||
type Unsubscribe = () => void | ||
type Subscribe<TListener extends Listener> = (listener: TListener) => Unsubscribe | ||
|
||
export class Subscribable<TListener extends Listener> { | ||
protected listeners = new Set<TListener>() | ||
|
||
public subscribe: Subscribe<TListener> = (listener) => { | ||
this.listeners.add(listener) | ||
|
||
const unsubscribe = (): void => { | ||
this.listeners.delete(listener) | ||
} | ||
return unsubscribe | ||
} | ||
|
||
protected notify = (): void => this.listeners.forEach((listener) => listener()) | ||
} |
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