This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: abstract logic of stores inside base classes
- Loading branch information
Showing
44 changed files
with
495 additions
and
181 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,22 @@ | ||
import { useGlobalStore } from '../../services/stores'; | ||
import { PublicSubject } from '../../services/stores/common/types'; | ||
|
||
export enum StoreType { | ||
GLOBAL = 'global-store', | ||
COMMENTS = 'comments-store', | ||
WHO_IS_ONLINE = 'who-is-online-store', | ||
} | ||
|
||
type StoreApi<T extends (...args: any[]) => any> = { | ||
[K in keyof ReturnType<T>]: { | ||
subscribe(callback?: (value: keyof T) => void): void; | ||
subject: PublicSubject<keyof T>; | ||
publish<T>(value: T): void; | ||
}; | ||
}; | ||
|
||
// When creating new Stores, expand the ternary with the new Store. For example: | ||
// ...T extends StoreType.GLOBAL ? StoreApi<typeof useGlobalStore> : T extends StoreType.WHO_IS_ONLINE ? StoreApi<typeof useWhoIsOnlineStore> : never; | ||
// Yes, it will be a little bit verbose, but it's not like we'll be creating more and more Stores just for. Rarely will someone need to come here | ||
export type Store<T> = T extends StoreType.GLOBAL ? StoreApi<typeof useGlobalStore> : never; | ||
export type StoresTypes = typeof StoreType; |
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,13 @@ | ||
import { StoreType } from '../types/stores.types'; | ||
|
||
import { useStore } from './use-store'; | ||
|
||
describe('useStore', () => { | ||
test('should return an api to use a store', () => { | ||
const result = useStore.call(this, StoreType.GLOBAL); | ||
|
||
expect(result).toHaveProperty('subscribe'); | ||
expect(result).toHaveProperty('subject'); | ||
expect(result).toHaveProperty('publish'); | ||
}); | ||
}); |
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,56 @@ | ||
import { PublicSubject } from '../../services/stores/common/types'; | ||
import { useGlobalStore } from '../../services/stores/global'; | ||
import { Store, StoreType, StoresTypes } from '../types/stores.types'; | ||
|
||
const stores = { | ||
[StoreType.GLOBAL]: useGlobalStore, | ||
}; | ||
|
||
/** | ||
* @function subscribe | ||
* @description Subscribes to a subject and either update the value of the property each time there is a change, or call the callback that provides a custom behavior to the subscription | ||
* @param name The name of the property to be updated in case there isn't a callback | ||
* @param subject The subject to be subscribed | ||
* @param callback The callback to be called each time there is a change | ||
*/ | ||
function subscribeTo<T>( | ||
name: string, | ||
subject: PublicSubject<T>, | ||
callback?: (value: T) => void, | ||
): void { | ||
subject.subscribe(this, () => { | ||
this[name] = subject.value; | ||
|
||
if (callback) { | ||
callback(subject.value); | ||
} | ||
}); | ||
|
||
this.unsubscribeFrom.push(subject.unsubscribe); | ||
} | ||
|
||
/** | ||
* @function useGlobalStore | ||
* @description Returns a proxy of the global store data and a subscribe function to be used in the components | ||
*/ | ||
export function useStore<T extends StoreType>(name: T): Store<T> { | ||
// @TODO - Improve types to get better sugestions when writing code | ||
const storeData = stores[name as StoreType](); | ||
const bindedSubscribeTo = subscribeTo.bind(this); | ||
|
||
const proxy = new Proxy(storeData, { | ||
get(store: Store<T>, valueName: string) { | ||
return { | ||
subscribe<K extends Store<T>>(callback?: (value: K) => void) { | ||
bindedSubscribeTo(valueName, store[valueName], callback); | ||
}, | ||
subject: store[valueName] as typeof storeData, | ||
publish(newValue: keyof Store<T>) { | ||
this.subject.value = newValue; | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
return proxy; | ||
} |
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
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
Oops, something went wrong.