mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
chore(packages): standardize conventions (#1279)
This commit is contained in:
@@ -23,9 +23,6 @@ const createConfig = (mode: BuildMode): UserConfig => ({
|
||||
clean: true,
|
||||
hash: false,
|
||||
unbundle: true,
|
||||
alias: {
|
||||
'@': new URL('./src/core', import.meta.url).pathname,
|
||||
},
|
||||
outDir: `dist/${mode}`,
|
||||
define: {
|
||||
__DEV__: mode === 'dev' ? 'true' : 'false',
|
||||
|
||||
@@ -12,6 +12,19 @@ export function combine<Target, const Slices extends Slice<Target, any>[]>(
|
||||
return {
|
||||
state: (ctx: StateContext<Target>) => {
|
||||
const states = slices.map((slice) => slice.state(ctx));
|
||||
|
||||
if (__DEV__) {
|
||||
const seen = new Set<string>();
|
||||
for (const state of states) {
|
||||
for (const key of Object.keys(state as object)) {
|
||||
if (seen.has(key)) {
|
||||
console.warn(`[vjs-store] combine(): duplicate state key "${key}" — later slice overwrites earlier one`);
|
||||
}
|
||||
seen.add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Object.assign({}, ...states) as UnionSliceState<Slices>;
|
||||
},
|
||||
|
||||
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
declare const __DEV__: boolean;
|
||||
@@ -0,0 +1,67 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { combine } from '../combine';
|
||||
import { defineSlice } from '../slice';
|
||||
import { createStore } from '../store';
|
||||
|
||||
class MockTarget extends EventTarget {
|
||||
value = 0;
|
||||
}
|
||||
|
||||
const slice = defineSlice<MockTarget>();
|
||||
|
||||
describe('combine', () => {
|
||||
it('merges state from multiple slices', () => {
|
||||
const a = slice({ state: () => ({ count: 0 }) });
|
||||
const b = slice({ state: () => ({ label: 'hello' }) });
|
||||
|
||||
const store = createStore<MockTarget>()(combine(a, b));
|
||||
|
||||
expect(store.state).toMatchObject({ count: 0, label: 'hello' });
|
||||
});
|
||||
|
||||
it('calls attach for each slice', () => {
|
||||
const attachA = vi.fn();
|
||||
const attachB = vi.fn();
|
||||
|
||||
const a = slice({ state: () => ({ count: 0 }), attach: attachA });
|
||||
const b = slice({ state: () => ({ label: '' }), attach: attachB });
|
||||
|
||||
const store = createStore<MockTarget>()(combine(a, b));
|
||||
store.attach(new MockTarget());
|
||||
|
||||
expect(attachA).toHaveBeenCalledOnce();
|
||||
expect(attachB).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('catches and reports attach errors via onError callback', () => {
|
||||
const error = new Error('attach failed');
|
||||
const onError = vi.fn();
|
||||
|
||||
const a = slice({
|
||||
state: () => ({ count: 0 }),
|
||||
attach: () => {
|
||||
throw error;
|
||||
},
|
||||
});
|
||||
const b = slice({ state: () => ({ label: '' }) });
|
||||
|
||||
const store = createStore<MockTarget>()(combine(a, b), { onError });
|
||||
store.attach(new MockTarget());
|
||||
|
||||
expect(onError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('warns on duplicate state keys in __DEV__ mode', () => {
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||
|
||||
const a = slice({ state: () => ({ count: 0 }) });
|
||||
const b = slice({ state: () => ({ count: 1 }) });
|
||||
|
||||
createStore<MockTarget>()(combine(a, b));
|
||||
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringContaining('duplicate state key "count"'));
|
||||
|
||||
warn.mockRestore();
|
||||
});
|
||||
});
|
||||
@@ -17,9 +17,6 @@ const createConfig = (mode: BuildMode): UserConfig => ({
|
||||
clean: true,
|
||||
hash: false,
|
||||
unbundle: true,
|
||||
alias: {
|
||||
'@': new URL('./src/core', import.meta.url).pathname,
|
||||
},
|
||||
outDir: `dist/${mode}`,
|
||||
define: {
|
||||
__DEV__: mode === 'dev' ? 'true' : 'false',
|
||||
|
||||
@@ -20,8 +20,5 @@ export default defineConfig({
|
||||
clean: true,
|
||||
hash: false,
|
||||
unbundle: true,
|
||||
alias: {
|
||||
'@': new URL('./src', import.meta.url).pathname,
|
||||
},
|
||||
dts: true,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user