Skip to content

Commit

Permalink
feat: sceneMapping support function
Browse files Browse the repository at this point in the history
  • Loading branch information
tangshuang committed Mar 8, 2023
1 parent 060f234 commit d5ad7d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,14 +796,16 @@ export declare function createMetaRef<T extends Meta[]>(create: (...args: Meta[]
*/
export declare function createAsyncMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>, asyncGetter: () => Promise<Partial<Attrs<T, I, M, U>>>): AsyncMeta<T, I, M, U>

type SceneMapping<T, I, M, U> = {
[sceneCode: string]: Partial<Attrs<T, I, M, U>> | (() => Partial<Attrs<T, I, M, U>>) | (() => Promise<Partial<Attrs<T, I, M, U>>>)
}

/**
* create a scene meta, which can be switch to certain scene by Model#Scene(sceneCode)
* @param attrs
* @param mapping
*/
export declare function createSceneMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>, mapping: {
[sceneCode: string]: Partial<Attrs<T, I, M, U>> | (() => Partial<Attrs<T, I, M, U>>) | (() => Promise<Partial<Attrs<T, I, M, U>>>)
}): SceneMeta<T, I, M, U>
export declare function createSceneMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>, mapping: SceneMapping<T, I, M, U> | (() => SceneMapping<T, I, M, U>)): SceneMeta<T, I, M, U>

/**
* create a state meta, whose disabled is force set to be true
Expand All @@ -816,9 +818,7 @@ export declare function createStateMeta<T = any, I = T, M extends Model = Model,
* @param attrs
* @param mapping
*/
export declare function createSceneStateMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Omit<Attrs<T, I, M, U>, 'default' | 'drop' | 'to' | 'map' | 'state'> & { value: T }, mapping: {
[sceneCode: string]: Partial<Attrs<T, I, M, U>> | (() => Partial<Attrs<T, I, M, U>>) | (() => Promise<Partial<Attrs<T, I, M, U>>>)
}): SceneMeta<T, I, M, U>
export declare function createSceneStateMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Omit<Attrs<T, I, M, U>, 'default' | 'drop' | 'to' | 'map' | 'state'> & { value: T }, mapping: SceneMapping<T, I, M, U> | (() => SceneMapping<T, I, M, U>)): SceneMeta<T, I, M, U>

/**
* use field value type from a meta
Expand Down
4 changes: 2 additions & 2 deletions src/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function createAsyncMeta(defaultAttrs, asyncGetter) {
export function createSceneMeta(defaultAttrs, sceneMapping) {
class ThisSceneMeta extends SceneMeta {
defineScenes() {
return sceneMapping
return typeof sceneMapping === 'function' ? sceneMapping() : sceneMapping
}
}
Object.assign(ThisSceneMeta, defaultAttrs)
Expand All @@ -105,7 +105,7 @@ export function createStateMeta(attrs) {
export function createSceneStateMeta(defaultAttrs, sceneMapping) {
class ThisSceneStateMeta extends SceneStateMeta {
defineScenes() {
return sceneMapping
return typeof sceneMapping === 'function' ? sceneMapping() : sceneMapping
}
}
Object.assign(ThisSceneStateMeta, defaultAttrs)
Expand Down

0 comments on commit d5ad7d1

Please sign in to comment.