Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(stores): change store state interface #328

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/vlossom/src/stores/__tests__/option-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('option store', () => {
const store = new OptionStore();

// when
const result = store.getState();
const result = store.getOptions();

// then
expect(result).toEqual({
Expand All @@ -28,7 +28,7 @@ describe('option store', () => {
store.setTheme('dark');

// then
expect(store.getState().theme).toEqual('dark');
expect(store.getOptions().theme).toEqual('dark');
});
});

Expand All @@ -41,7 +41,7 @@ describe('option store', () => {
store.setGlobalColorScheme({ default: 'red' });

// then
expect(store.getState().globalColorScheme).toEqual({ default: 'red' });
expect(store.getOptions().globalColorScheme).toEqual({ default: 'red' });
});

describe('getGlobalColorScheme', () => {
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('option store', () => {
store.registerStyleSet(styleSet);

// then
expect(store.getState().styleSets).toEqual(styleSet);
expect(store.getOptions().styleSets).toEqual(styleSet);
});

it('styleSet을 등록할 수 있다 (기존에 등록된 styleSet이 있을 경우)', () => {
Expand All @@ -114,7 +114,7 @@ describe('option store', () => {
store.registerStyleSet(styleSet2);

// then
expect(store.getState().styleSets).toEqual({
expect(store.getOptions().styleSets).toEqual({
VsButton: {
primary: {
fontColor: 'red',
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('option store', () => {
store.setGlobalRadiusRatio(0.5);

// then
expect(store.getState().globalRadiusRatio).toEqual(0.5);
expect(store.getOptions().globalRadiusRatio).toEqual(0.5);
expect(setPropertySpy).toBeCalledWith('--vs-radius-ratio', '0.5');
});

Expand Down
23 changes: 12 additions & 11 deletions packages/vlossom/src/stores/option-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive } from 'vue';
import { readonly, Ref, ref } from 'vue';
import { VsComponent } from '@/declaration';
import { utils } from '@/utils';

Expand All @@ -12,27 +12,28 @@ interface OptionStoreState {
}

export class OptionStore {
private state: OptionStoreState = reactive({
private _options: Ref<OptionStoreState> = ref({
theme: 'light',
globalColorScheme: {},
styleSets: {},
globalRadiusRatio: 1,
});
public options = readonly(this._options);

getState() {
return this.state;
getOptions() {
return this._options.value;
}

setTheme(theme: 'light' | 'dark') {
this.state.theme = theme;
this._options.value.theme = theme;
}

setGlobalColorScheme(colorScheme: GlobalColorScheme) {
this.state.globalColorScheme = colorScheme;
this._options.value.globalColorScheme = colorScheme;
}

getGlobalColorScheme(component: VsComponent | string) {
return this.state.globalColorScheme[component] || this.state.globalColorScheme.default;
return this._options.value.globalColorScheme[component] || this._options.value.globalColorScheme.default;
}

setGlobalRadiusRatio(radiusRatio: number) {
Expand All @@ -41,23 +42,23 @@ export class OptionStore {
return;
}

this.state.globalRadiusRatio = radiusRatio;
this._options.value.globalRadiusRatio = radiusRatio;
document.documentElement.style.setProperty('--vs-radius-ratio', radiusRatio.toString());
}

registerStyleSet(styleSet: StyleSet) {
Object.entries(styleSet).forEach(([key, value]) => {
const styleSets = this.state.styleSets[key as keyof StyleSet];
const styleSets = this._options.value.styleSets[key as keyof StyleSet];

if (!styleSets) {
this.state.styleSets[key as keyof StyleSet] = value;
this._options.value.styleSets[key as keyof StyleSet] = value;
} else {
Object.assign(styleSets, value);
}
});
}

getStyleSet(component: VsComponent | string, styleSetName: string) {
return this.state.styleSets[component as keyof StyleSet]?.[styleSetName];
return this._options.value.styleSets[component as keyof StyleSet]?.[styleSetName];
}
}
29 changes: 15 additions & 14 deletions packages/vlossom/src/stores/overlay-callback-store.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { reactive, Ref } from 'vue';
import { ref, Ref, readonly } from 'vue';
import { OverlayCallbacks, VS_OVERLAY_CLOSE, VS_OVERLAY_OPEN } from '@/declaration';

export class OverlayCallbackStore {
// overlay tuple: [id, { [eventName: callback }]
public overlays: [string, Ref<OverlayCallbacks>][] = reactive([]);
private _overlays: Ref<[string, Ref<OverlayCallbacks>][]> = ref([]);
public overlays = readonly(this._overlays);

constructor() {
document.addEventListener('keydown', (event: KeyboardEvent) => {
if (this.overlays.length === 0) {
if (this._overlays.value.length === 0) {
return;
}

const keyEventName = `key-${event.key}`;
const [lastOverlayId, callbacks] = this.overlays[this.overlays.length - 1];
const [lastOverlayId, callbacks] = this._overlays.value[this._overlays.value.length - 1];
if (!callbacks.value[keyEventName]) {
return;
}
Expand All @@ -25,46 +26,46 @@ export class OverlayCallbackStore {
}

getLastOverlayId() {
return this.overlays.length > 0 ? this.overlays[this.overlays.length - 1][0] : '';
return this._overlays.value.length > 0 ? this._overlays.value[this._overlays.value.length - 1][0] : '';
}

async run<T = void>(id: string, eventName: string, ...args: any[]): Promise<T | void> {
const index = this.overlays.findIndex(([overlayId]) => overlayId === id);
const index = this._overlays.value.findIndex(([overlayId]) => overlayId === id);
if (index === -1) {
return;
}
const [, callbacks] = this.overlays[index];
const [, callbacks] = this._overlays.value[index];
return await callbacks.value[eventName]?.(...args);
}

push(id: string, callbacks: Ref<OverlayCallbacks>) {
this.overlays.push([id, callbacks]);
this._overlays.value.push([id, callbacks]);
this.run(id, VS_OVERLAY_OPEN);
return this.run(id, 'open');
}

pop(...args: any[]) {
const [targetId] = this.overlays[this.overlays.length - 1];
const [targetId] = this._overlays.value[this._overlays.value.length - 1];
this.run(targetId, VS_OVERLAY_CLOSE, ...args);
const result = this.run(targetId, 'close', ...args);
this.overlays.pop();
this._overlays.value.pop();
return result;
}

remove(id: string, ...args: any[]) {
const index = this.overlays.findIndex(([stackId]) => stackId === id);
const index = this._overlays.value.findIndex(([stackId]) => stackId === id);
if (index === -1) {
return;
}
const [targetId] = this.overlays[index];
const [targetId] = this._overlays.value[index];
this.run(targetId, VS_OVERLAY_CLOSE, ...args);
const result = this.run(targetId, 'close', ...args);
this.overlays.splice(index, 1);
this._overlays.value.splice(index, 1);
return result;
}

clear(...args: any[]) {
while (this.overlays.length > 0) {
while (this._overlays.value.length > 0) {
this.pop(...args);
}
}
Expand Down
18 changes: 10 additions & 8 deletions packages/vlossom/src/stores/overlay-stack-store.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { computed, ComputedRef, Ref, ref } from 'vue';
import { computed, ComputedRef, Ref, ref, readonly } from 'vue';

export class OverlayStackStore<T extends { id?: string; container?: string }> {
public readonly items: Ref<T[]> = ref([]);
public readonly itemsByContainer: ComputedRef<{ [container: string]: T[] }> = computed(() => {
private _items: Ref<T[]> = ref([]);
public items = readonly(this._items);

public itemsByContainer: ComputedRef<{ [container: string]: T[] }> = computed(() => {
const result: { [container: string]: T[] } = {};
this.items.value.forEach((modal) => {
this._items.value.forEach((modal) => {
const { container = 'body' } = modal;
if (!result[container]) {
result[container] = [];
Expand All @@ -19,18 +21,18 @@ export class OverlayStackStore<T extends { id?: string; container?: string }> {
return;
}

this.items.value.push(options);
this._items.value.push(options);
}

pop() {
this.items.value.pop();
this._items.value.pop();
}

remove(id: string) {
this.items.value = this.items.value.filter(({ id: modalId }) => modalId !== id);
this._items.value = this._items.value.filter(({ id: modalId }) => modalId !== id);
}

clear() {
this.items.value = [];
this._items.value = [];
}
}
8 changes: 4 additions & 4 deletions packages/vlossom/src/vlossom-framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Vlossom {
}

get theme() {
return store.option.getState().theme;
return store.option.getOptions().theme;
}

set theme(value) {
Expand All @@ -68,19 +68,19 @@ export class Vlossom {
}

get colorScheme() {
return store.option.getState().globalColorScheme;
return store.option.getOptions().globalColorScheme;
}

set colorScheme(colorScheme) {
store.option.setGlobalColorScheme(colorScheme);
}

get styleSets() {
return store.option.getState().styleSets;
return store.option.getOptions().styleSets;
}

get radiusRatio() {
return store.option.getState().globalRadiusRatio;
return store.option.getOptions().globalRadiusRatio;
}

set radiusRatio(radiusRatio: number) {
Expand Down