chore: lint
This commit is contained in:
@@ -3,18 +3,20 @@ import { map } from "rxjs/operators"
|
||||
import assign from "lodash/assign"
|
||||
import clone from "lodash/clone"
|
||||
|
||||
|
||||
type Dispatch<StoreType, DispatchersType extends Dispatchers<StoreType>, K extends keyof DispatchersType> = {
|
||||
dispatcher: K & string,
|
||||
type Dispatch<
|
||||
StoreType,
|
||||
DispatchersType extends Dispatchers<StoreType>,
|
||||
K extends keyof DispatchersType
|
||||
> = {
|
||||
dispatcher: K & string
|
||||
payload: any
|
||||
}
|
||||
|
||||
export type Dispatchers<StoreType> = {
|
||||
[ key: string ]: (currentVal: StoreType, payload: any) => Partial<StoreType>
|
||||
export type Dispatchers<StoreType> = {
|
||||
[key: string]: (currentVal: StoreType, payload: any) => Partial<StoreType>
|
||||
}
|
||||
|
||||
export default class DispatchingStore<StoreType, DispatchersType extends Dispatchers<StoreType>> {
|
||||
|
||||
#state$: BehaviorSubject<StoreType>
|
||||
#dispatchers: Dispatchers<StoreType>
|
||||
#dispatches$: Subject<Dispatch<StoreType, DispatchersType, keyof DispatchersType>> = new Subject()
|
||||
@@ -24,11 +26,8 @@ export default class DispatchingStore<StoreType, DispatchersType extends Dispatc
|
||||
this.#dispatchers = dispatchers
|
||||
|
||||
this.#dispatches$
|
||||
.pipe(
|
||||
map(
|
||||
({ dispatcher, payload }) => this.#dispatchers[dispatcher](this.value, payload)
|
||||
)
|
||||
).subscribe(val => {
|
||||
.pipe(map(({ dispatcher, payload }) => this.#dispatchers[dispatcher](this.value, payload)))
|
||||
.subscribe((val) => {
|
||||
const data = clone(this.value)
|
||||
assign(data, val)
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import isEqual from "lodash/isEqual"
|
||||
import DispatchingStore from "~/newstore/DispatchingStore"
|
||||
|
||||
describe("DispatchingStore", () => {
|
||||
|
||||
test("'subject$' property properly returns an BehaviorSubject", () => {
|
||||
const store = new DispatchingStore({}, {})
|
||||
|
||||
@@ -28,22 +27,25 @@ describe("DispatchingStore", () => {
|
||||
expect(() => {
|
||||
store.dispatch({
|
||||
dispatcher: "non-existent",
|
||||
payload: {}
|
||||
payload: {},
|
||||
})
|
||||
}).toThrow()
|
||||
})
|
||||
|
||||
test("valid dispatcher calls run without throwing", () => {
|
||||
const store = new DispatchingStore({}, {
|
||||
testDispatcher(_currentValue, _payload) {
|
||||
// Nothing here
|
||||
const store = new DispatchingStore(
|
||||
{},
|
||||
{
|
||||
testDispatcher(_currentValue, _payload) {
|
||||
// Nothing here
|
||||
},
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
expect(() => {
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: {}
|
||||
payload: {},
|
||||
})
|
||||
}).not.toThrow()
|
||||
})
|
||||
@@ -52,14 +54,17 @@ describe("DispatchingStore", () => {
|
||||
const dispatchFn = jest.fn().mockReturnValue({})
|
||||
const dontCallDispatchFn = jest.fn().mockReturnValue({})
|
||||
|
||||
const store = new DispatchingStore({}, {
|
||||
testDispatcher: dispatchFn,
|
||||
dontCallDispatcher: dontCallDispatchFn
|
||||
})
|
||||
const store = new DispatchingStore(
|
||||
{},
|
||||
{
|
||||
testDispatcher: dispatchFn,
|
||||
dontCallDispatcher: dontCallDispatchFn,
|
||||
}
|
||||
)
|
||||
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: {}
|
||||
payload: {},
|
||||
})
|
||||
|
||||
expect(dispatchFn).toHaveBeenCalledTimes(1)
|
||||
@@ -73,17 +78,17 @@ describe("DispatchingStore", () => {
|
||||
const testDispatchFn = jest.fn().mockReturnValue({})
|
||||
|
||||
const store = new DispatchingStore(testInitValue, {
|
||||
testDispatcher: testDispatchFn
|
||||
testDispatcher: testDispatchFn,
|
||||
})
|
||||
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: testPayload
|
||||
payload: testPayload,
|
||||
})
|
||||
|
||||
|
||||
expect(testDispatchFn).toHaveBeenCalledWith(testInitValue, testPayload)
|
||||
})
|
||||
|
||||
|
||||
test("dispatcher returns are used to update the store correctly", () => {
|
||||
const testInitValue = { name: "bob" }
|
||||
const testDispatchReturnVal = { name: "alice" }
|
||||
@@ -91,12 +96,12 @@ describe("DispatchingStore", () => {
|
||||
const testDispatchFn = jest.fn().mockReturnValue(testDispatchReturnVal)
|
||||
|
||||
const store = new DispatchingStore(testInitValue, {
|
||||
testDispatcher: testDispatchFn
|
||||
testDispatcher: testDispatchFn,
|
||||
})
|
||||
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: {} // Payload doesn't matter because the function is mocked
|
||||
payload: {}, // Payload doesn't matter because the function is mocked
|
||||
})
|
||||
|
||||
expect(store.value).toEqual(testDispatchReturnVal)
|
||||
@@ -109,47 +114,47 @@ describe("DispatchingStore", () => {
|
||||
const testDispatchFn = jest.fn().mockReturnValue(testDispatchReturnVal)
|
||||
|
||||
const store = new DispatchingStore(testInitValue, {
|
||||
testDispatcher: testDispatchFn
|
||||
testDispatcher: testDispatchFn,
|
||||
})
|
||||
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: {}
|
||||
payload: {},
|
||||
})
|
||||
|
||||
|
||||
expect(store.value).toEqual({
|
||||
name: "bob",
|
||||
age: 25
|
||||
age: 25,
|
||||
})
|
||||
})
|
||||
|
||||
test("emits the current store value to the new subscribers", done => {
|
||||
test("emits the current store value to the new subscribers", (done) => {
|
||||
const testInitValue = { name: "bob" }
|
||||
|
||||
const testDispatchFn = jest.fn().mockReturnValue({})
|
||||
|
||||
const store = new DispatchingStore(testInitValue, {
|
||||
testDispatcher: testDispatchFn
|
||||
testDispatcher: testDispatchFn,
|
||||
})
|
||||
|
||||
store.subject$.subscribe(value => {
|
||||
store.subject$.subscribe((value) => {
|
||||
if (value === testInitValue) {
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test("emits the dispatched store value to the subscribers", done => {
|
||||
test("emits the dispatched store value to the subscribers", (done) => {
|
||||
const testInitValue = { name: "bob" }
|
||||
const testDispatchReturnVal = { age: 25 }
|
||||
|
||||
const testDispatchFn = jest.fn().mockReturnValue(testDispatchReturnVal)
|
||||
|
||||
const store = new DispatchingStore(testInitValue, {
|
||||
testDispatcher: testDispatchFn
|
||||
testDispatcher: testDispatchFn,
|
||||
})
|
||||
|
||||
store.subject$.subscribe(value => {
|
||||
store.subject$.subscribe((value) => {
|
||||
if (isEqual(value, { name: "bob", age: 25 })) {
|
||||
done()
|
||||
}
|
||||
@@ -157,7 +162,7 @@ describe("DispatchingStore", () => {
|
||||
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: {}
|
||||
payload: {},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -168,10 +173,10 @@ describe("DispatchingStore", () => {
|
||||
const testDispatchFn = jest.fn().mockReturnValue({})
|
||||
|
||||
const store = new DispatchingStore(testInitValue, {
|
||||
testDispatcher: testDispatchFn
|
||||
testDispatcher: testDispatchFn,
|
||||
})
|
||||
|
||||
store.dispatches$.subscribe(value => {
|
||||
store.dispatches$.subscribe((value) => {
|
||||
if (isEqual(value, { dispatcher: "testDispatcher", payload: testPayload })) {
|
||||
done()
|
||||
}
|
||||
@@ -179,7 +184,7 @@ describe("DispatchingStore", () => {
|
||||
|
||||
store.dispatch({
|
||||
dispatcher: "testDispatcher",
|
||||
payload: {}
|
||||
payload: {},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -24,12 +24,10 @@ function setupSettingsPersistence() {
|
||||
if (settingsData) {
|
||||
bulkApplySettings(settingsData)
|
||||
}
|
||||
|
||||
|
||||
settingsStore.subject$
|
||||
.subscribe(settings => {
|
||||
window.localStorage.setItem("settings", JSON.stringify(settings))
|
||||
})
|
||||
settingsStore.subject$.subscribe((settings) => {
|
||||
window.localStorage.setItem("settings", JSON.stringify(settings))
|
||||
})
|
||||
}
|
||||
|
||||
export function setupLocalPersistence() {
|
||||
|
||||
@@ -5,7 +5,6 @@ import type { Dispatchers } from "./DispatchingStore"
|
||||
import { Observable } from "rxjs"
|
||||
import type { KeysMatching } from "~/types/ts-utils"
|
||||
|
||||
|
||||
export const defaultSettings = {
|
||||
syncCollections: true,
|
||||
syncHistory: true,
|
||||
@@ -21,8 +20,8 @@ export const defaultSettings = {
|
||||
auth: true,
|
||||
httpUser: true,
|
||||
httpPassword: true,
|
||||
bearerToken: true
|
||||
}
|
||||
bearerToken: true,
|
||||
},
|
||||
}
|
||||
|
||||
export type SettingsType = typeof defaultSettings
|
||||
@@ -44,7 +43,10 @@ const dispatchers: Dispatchers<SettingsType> = {
|
||||
|
||||
return result
|
||||
},
|
||||
applySetting<K extends keyof SettingsType>(_currentState: SettingsType, { settingKey, value }: { settingKey: K, value: SettingsType[K] }) {
|
||||
applySetting<K extends keyof SettingsType>(
|
||||
_currentState: SettingsType,
|
||||
{ settingKey, value }: { settingKey: K; value: SettingsType[K] }
|
||||
) {
|
||||
if (!validKeys.includes(settingKey)) {
|
||||
console.log(`Ignoring non-existent setting key '${settingKey}' assignment`)
|
||||
return {}
|
||||
@@ -54,20 +56,21 @@ const dispatchers: Dispatchers<SettingsType> = {
|
||||
result[settingKey] = value
|
||||
|
||||
return result
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
export const settingsStore = new DispatchingStore(defaultSettings, dispatchers)
|
||||
|
||||
export function getSettingSubject<K extends keyof SettingsType>(settingKey: K): Observable<SettingsType[K]> {
|
||||
export function getSettingSubject<K extends keyof SettingsType>(
|
||||
settingKey: K
|
||||
): Observable<SettingsType[K]> {
|
||||
return settingsStore.subject$.pipe(pluck(settingKey), distinctUntilChanged())
|
||||
}
|
||||
|
||||
export function bulkApplySettings(settingsObj: Partial<SettingsType>) {
|
||||
settingsStore.dispatch({
|
||||
dispatcher: "bulkApplySettings",
|
||||
payload: settingsObj
|
||||
payload: settingsObj,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -75,8 +78,8 @@ export function toggleSetting(settingKey: KeysMatching<SettingsType, boolean>) {
|
||||
settingsStore.dispatch({
|
||||
dispatcher: "toggleSetting",
|
||||
payload: {
|
||||
settingKey
|
||||
}
|
||||
settingKey,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -85,7 +88,7 @@ export function applySetting<K extends keyof SettingsType>(settingKey: K, value:
|
||||
dispatcher: "applySetting",
|
||||
payload: {
|
||||
settingKey,
|
||||
value
|
||||
}
|
||||
value,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user