Skip to content

Commit

Permalink
Merge pull request #215 from wechat-miniprogram/feat-selector-query-c…
Browse files Browse the repository at this point in the history
…ontext

feat(miniprogram-adapter): add context to selector query
  • Loading branch information
LastLeaf authored Nov 19, 2024
2 parents e6c25eb + df6a8d6 commit aa37d0b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions glass-easel-miniprogram-adapter/src/selector_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type SelectorQueryFields = {
size?: boolean
scrollOffset?: boolean
properties?: string[]
context?: boolean
// TODO support computedStyle
}

Expand Down Expand Up @@ -50,6 +51,10 @@ export type ScrollOffset = {
scrollHeight: number
}

export type ContextResult = {
context: unknown
}

const joinAsync = <T>(inits: ((cb: (ret: T) => void) => void)[], cb: (rets: T[]) => void) => {
let initDone = false
let jobCount = inits.length
Expand Down Expand Up @@ -137,6 +142,15 @@ class NodesRef<S extends boolean> {
)
return this._$sq
}

context(
cb: (res: S extends true ? ContextResult : any[]) => void = () => {
/* empty */
},
) {
this._$sq._$push(this._$sel, this._$comp, this._$single, { context: true }, cb)
return this._$sq
}
}

// main class
Expand Down Expand Up @@ -298,6 +312,16 @@ export class SelectorQuery {
done(undefined)
}
},
(done) => {
if (fields.context) {
elem.getContext((ctx) => {
res.context = ctx
done(undefined)
})
} else {
done(undefined)
}
},
],
() => {
if (fields.properties) {
Expand Down
26 changes: 26 additions & 0 deletions glass-easel-miniprogram-adapter/tests/selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,32 @@ describe('selector query', () => {
.register()
})

const ab = env.associateBackend()
const root = ab.createRoot('body', codeSpace, 'path/to/comp')
glassEasel.Element.pretendAttached(root.getComponent())
}))

test('query context', () =>
new Promise<undefined>((resolve) => {
const env = new MiniProgramEnv()
const codeSpace = env.createCodeSpace('', true)

codeSpace.addComponentStaticConfig('path/to/comp', {})
codeSpace.addCompiledTemplate('path/to/comp', tmpl(''))
codeSpace.componentEnv('path/to/comp', ({ Component }) => {
Component()
.lifetime('attached', function () {
this.createSelectorQuery()
.selectViewport()
.context((res) => {
expect(res.context).toBeUndefined()
resolve(undefined)
})
.exec()
})
.register()
})

const ab = env.associateBackend()
const root = ab.createRoot('body', codeSpace, 'path/to/comp')
glassEasel.Element.pretendAttached(root.getComponent())
Expand Down

0 comments on commit aa37d0b

Please sign in to comment.