@gez/class-state
Advanced tools
Comparing version 0.0.9 to 0.0.12
@@ -77,2 +77,5 @@ import { type State, type StateContext } from './create'; | ||
export declare function connectState(state: State): <T extends StoreConstructor>(Store: T, name: string, cacheKey?: string) => StoreInstance<InstanceType<T>>; | ||
export declare function connectStore<T extends StoreConstructor>(Store: T, name: string, ...params: ConstructorParameters<T>): StoreInstance<InstanceType<T>>; | ||
/** | ||
* 连接外部的 store,如果没有,则实例化 Store 类作为默认值返回 | ||
*/ | ||
export declare function foreignStore<T extends StoreConstructor>(Store: T, name: string, cacheKey?: string): InstanceType<T>; |
export { createState, type State } from './create'; | ||
export { connectState, connectStore, type StoreConstructor, type StoreContext, type StoreInstance, type StoreParams, LIFE_CYCLE_CREATED, LIFE_CYCLE_DISPOSE } from './connect'; | ||
export { connectState, foreignStore, type StoreConstructor, type StoreContext, type StoreInstance, type StoreParams, LIFE_CYCLE_CREATED, LIFE_CYCLE_DISPOSE } from './connect'; |
@@ -16,5 +16,6 @@ { | ||
"devDependencies": { | ||
"@gez/lint": "0.0.9", | ||
"@gez/lint": "0.0.12", | ||
"@types/node": "20.12.12", | ||
"@vitest/coverage-v8": "1.6.0", | ||
"@vue/compiler-dom": "^3.4.27", | ||
"eslint": "8.57.0", | ||
@@ -27,3 +28,3 @@ "stylelint": "16.5.0", | ||
}, | ||
"version": "0.0.9", | ||
"version": "0.0.12", | ||
"type": "module", | ||
@@ -46,3 +47,3 @@ "private": false, | ||
], | ||
"gitHead": "5d769669f9d75cfbbae1c3df7e6c71fca8214634" | ||
"gitHead": "ee7626290d17f5085c2d0d032a895ba55ee292a6" | ||
} |
import { assert, test } from 'vitest'; | ||
import { connectState, connectStore } from './connect'; | ||
import { connectState, foreignStore } from './connect'; | ||
import { createState } from './create'; | ||
@@ -44,3 +44,2 @@ | ||
assert.strictEqual(user.name, 'jack'); | ||
// @ts-expect-error test | ||
assert.strictEqual(state.value.user.name, user.name); | ||
@@ -50,3 +49,2 @@ | ||
assert.strictEqual(user.age, 20); | ||
// @ts-expect-error test | ||
assert.strictEqual(state.value.user.age, user.age); | ||
@@ -56,5 +54,3 @@ | ||
assert.strictEqual(user.text, 'jack is 20 years old.'); | ||
// @ts-expect-error test | ||
assert.strictEqual(state.value.user.text, user.text); | ||
// @ts-expect-error test | ||
assert.isUndefined(state.value.user.online); | ||
@@ -240,3 +236,3 @@ | ||
public get blog() { | ||
return connectStore(Blog, 'blog'); | ||
return foreignStore(Blog, 'blog'); | ||
} | ||
@@ -243,0 +239,0 @@ |
@@ -173,5 +173,6 @@ /* eslint-disable @typescript-eslint/no-this-alias */ | ||
} | ||
currentStateContext = storeContext._stateContext; | ||
const result = Reflect.get(target, p, receiver); | ||
currentStateContext = null; | ||
const result = applyStateContext( | ||
storeContext._stateContext, | ||
() => Reflect.get(target, p, receiver) | ||
); | ||
if ( | ||
@@ -235,2 +236,6 @@ typeof result === 'function' && | ||
function getFullPath(name: string, cacheKey?: string) { | ||
return typeof cacheKey === 'string' ? name + '/' + cacheKey : name; | ||
} | ||
export function connectState(state: State) { | ||
@@ -243,8 +248,10 @@ const stateContext = getStateContext(state); | ||
): StoreInstance<InstanceType<T>> => { | ||
const fullPath = | ||
typeof cacheKey === 'string' ? name + '/' + cacheKey : name; | ||
const fullPath = getFullPath(name, cacheKey); | ||
let storeContext: StoreContext<InstanceType<T>> | null = | ||
stateContext.get(fullPath); | ||
if (!storeContext) { | ||
const store = new Store(cacheKey); | ||
const store = applyStateContext( | ||
stateContext, | ||
() => new Store(cacheKey) | ||
); | ||
let storeState; | ||
@@ -267,12 +274,20 @@ if (fullPath in state.value) { | ||
} | ||
export function connectStore<T extends StoreConstructor>( | ||
/** | ||
* 连接外部的 store,如果没有,则实例化 Store 类作为默认值返回 | ||
*/ | ||
export function foreignStore<T extends StoreConstructor>( | ||
Store: T, | ||
name: string, | ||
...params: ConstructorParameters<T> | ||
) { | ||
cacheKey?: string | ||
): InstanceType<T> { | ||
if (!currentStateContext) { | ||
throw new Error('No state context found'); | ||
} | ||
return connectState(currentStateContext.state)(Store, name, ...params); | ||
const fullPath = getFullPath(name, cacheKey); | ||
const storeContext: StoreContext<InstanceType<T>> | null = | ||
currentStateContext.get(fullPath); | ||
if (storeContext) { | ||
return storeContext.get(); | ||
} | ||
return new Store(cacheKey); | ||
} | ||
@@ -285,1 +300,12 @@ | ||
} | ||
function applyStateContext<T = void>( | ||
stateContext: StateContext | null, | ||
cb: () => T | ||
) { | ||
const prev = currentStateContext; | ||
currentStateContext = stateContext; | ||
const value = cb(); | ||
currentStateContext = prev; | ||
return value; | ||
} |
export { createState, type State } from './create'; | ||
export { | ||
connectState, | ||
connectStore, | ||
foreignStore, | ||
type StoreConstructor, | ||
@@ -6,0 +6,0 @@ type StoreContext, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81770
2767
10