Skip to content

Commit

Permalink
Merge branch 'insert-track' into バージョン0.20に向けたデザイン等張性
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Jul 30, 2024
2 parents a4820f6 + ab51ff1 commit b5d3b75
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
30 changes: 23 additions & 7 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1048,19 +1048,27 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
},
},

REGISTER_TRACK: {
mutation(state, { trackId, track }) {
INSERT_TRACK: {
/**
* トラックを挿入する。
* prevTrackIdがundefinedの場合は最後に追加する。
*/
mutation(state, { trackId, track, prevTrackId }) {
const index =
prevTrackId != undefined
? state.trackOrder.indexOf(prevTrackId) + 1
: state.trackOrder.length;
state.tracks.set(trackId, track);
state.trackOrder.push(trackId);
state.trackOrder.splice(index, 0, trackId);
},
action({ state, commit, dispatch }, { trackId, track }) {
action({ state, commit, dispatch }, { trackId, track, prevTrackId }) {
if (state.tracks.has(trackId)) {
throw new Error(`Track ${trackId} is already registered.`);
}
if (!isValidTrack(track)) {
throw new Error("The track is invalid.");
}
commit("REGISTER_TRACK", { trackId, track });
commit("INSERT_TRACK", { trackId, track, prevTrackId });

dispatch("RENDER");
},
Expand Down Expand Up @@ -2635,7 +2643,11 @@ export const singingCommandStore = transformCommandStore(

COMMAND_ADD_TRACK: {
mutation(draft, { trackId, track }) {
singingStore.mutations.REGISTER_TRACK(draft, { trackId, track });
singingStore.mutations.INSERT_TRACK(draft, {
trackId,
track,
prevTrackId: undefined,
});
},
async action({ getters, dispatch, commit }) {
const { trackId, track } = await dispatch("CREATE_TRACK");
Expand Down Expand Up @@ -2741,7 +2753,11 @@ export const singingCommandStore = transformCommandStore(
if (overwrite) {
singingStore.mutations.SET_TRACK(draft, { track, trackId });
} else {
singingStore.mutations.REGISTER_TRACK(draft, { track, trackId });
singingStore.mutations.INSERT_TRACK(draft, {
track,
trackId,
prevTrackId: undefined,
});
}
}
},
Expand Down
14 changes: 11 additions & 3 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,9 +1163,17 @@ export type SingingStoreTypes = {
action(): { trackId: TrackId; track: Track };
};

REGISTER_TRACK: {
mutation: { trackId: TrackId; track: Track };
action(payload: { trackId: TrackId; track: Track }): void;
INSERT_TRACK: {
mutation: {
trackId: TrackId;
track: Track;
prevTrackId: TrackId | undefined;
};
action(payload: {
trackId: TrackId;
track: Track;
prevTrackId: TrackId | undefined;
}): void;
};

DELETE_TRACK: {
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/store/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { toRaw } from "vue";
import { store } from "@/store";
import { AudioKey } from "@/type/preload";
import { resetMockMode, uuid4 } from "@/helpers/random";
import { cloneWithUnwrapProxy } from "@/helpers/cloneWithUnwrapProxy";

const initialState = structuredClone(toRaw(store.state));
const initialState = cloneWithUnwrapProxy(store.state);
beforeEach(() => {
store.replaceState(initialState);

resetMockMode();
store.replaceState(initialState);
});

test("コマンド実行で履歴が作られる", async () => {
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/store/singing.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { store } from "@/store";
import { TrackId } from "@/type/preload";
import { resetMockMode, uuid4 } from "@/helpers/random";
import { cloneWithUnwrapProxy } from "@/helpers/cloneWithUnwrapProxy";
import { createDefaultTrack } from "@/sing/domain";

const initialState = cloneWithUnwrapProxy(store.state);
beforeEach(() => {
resetMockMode();
store.replaceState(initialState);
});

test("INSERT_TRACK", () => {
const dummyTrack = createDefaultTrack();

// 最後尾に追加
// NOTE: 最初から1つトラックが登録されている
const trackId1 = TrackId(uuid4());
store.commit("INSERT_TRACK", {
trackId: trackId1,
track: dummyTrack,
prevTrackId: undefined,
});
expect(store.state.trackOrder.slice(1)).toEqual([trackId1]);

// 途中に追加
const trackId2 = TrackId(uuid4());
store.commit("INSERT_TRACK", {
trackId: trackId2,
track: dummyTrack,
prevTrackId: store.state.trackOrder[0],
});
expect(store.state.trackOrder.slice(1)).toEqual([trackId2, trackId1]);
});

0 comments on commit b5d3b75

Please sign in to comment.