Skip to content

Commit

Permalink
feat: new effectScope API
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmingshan committed Sep 19, 2021
1 parent 6076e6f commit fb82da3
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 70 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const config = {
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/promise-function-async': 'off',
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'@typescript-eslint/prefer-optional-chain': 'off',
'@typescript-eslint/ban-types': [
'error',
{ extendDefaults: false, types },
Expand Down
6 changes: 3 additions & 3 deletions packages/wechat/__tests__/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('component', () => {
}
})
component.lifetimes.attached.call(component)
expect(component.__effects__.length).toBe(3)
expect(component.__scope__.effects.length).toBe(3)

component.increment()
component.lifetimes.detached.call(component)
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('component', () => {
expect(dummy!).toBe(0)
expect(component.data.count).toBe(0)
// The other is `count` sync watcher
expect(component.__effects__.length).toBe(2)
expect(component.__scope__.effects.length).toBe(2)

component.increment()
await nextTick()
Expand All @@ -236,7 +236,7 @@ describe('component', () => {
await nextTick()
expect(dummy!).toBe(1)
expect(component.data.count).toBe(2)
expect(component.__effects__.length).toBe(1)
expect(component.__scope__.effects.length).toBe(1)
})

it('props', async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/wechat/__tests__/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('page', () => {
}
})
page.onLoad()
expect(page.__effects__.length).toBe(3)
expect(page.__scope__.effects.length).toBe(3)

page.increment()
page.onUnload()
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('page', () => {
expect(dummy!).toBe(0)
expect(page.data.count).toBe(0)
// The other is `count` sync watcher
expect(page.__effects__.length).toBe(2)
expect(page.__scope__.effects.length).toBe(2)

page.increment()
await nextTick()
Expand All @@ -226,7 +226,7 @@ describe('page', () => {
await nextTick()
expect(dummy!).toBe(1)
expect(page.data.count).toBe(2)
expect(page.__effects__.length).toBe(1)
expect(page.__scope__.effects.length).toBe(1)
})

it('onLoad', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/wechat/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Bindings, AppInstance, setCurrentApp } from './instance'
import {
Bindings,
AppInstance,
setCurrentApp,
unsetCurrentApp,
} from './instance'
import { isFunction, toHiddenField } from './utils'

export type AppSetup = (
Expand Down Expand Up @@ -57,7 +62,7 @@ export function createApp(optionsOrSetup: any): void {
})
}

setCurrentApp(null)
unsetCurrentApp()

if (originOnLaunch !== undefined) {
originOnLaunch.call(this, options)
Expand Down
19 changes: 11 additions & 8 deletions packages/wechat/src/component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { shallowReactive, shallowReadonly } from '@vue/reactivity'
import { shallowReactive, shallowReadonly, EffectScope } from '@vue/reactivity'
import { PageLifecycle, Config } from './page'
import { deepToRaw, deepWatch } from './shared'
import { Bindings, ComponentInstance, setCurrentComponent } from './instance'
import {
Bindings,
ComponentInstance,
setCurrentComponent,
unsetCurrentComponent,
} from './instance'
import { isFunction, toHiddenField } from './utils'

export type ComponentContext = WechatMiniprogram.Component.InstanceProperties &
Expand Down Expand Up @@ -128,6 +133,8 @@ export function defineComponent(optionsOrSetup: any, config?: Config): string {
options.lifetimes[ComponentLifecycle.ATTACHED] = function (
this: ComponentInstance
) {
this.__scope__ = new EffectScope()

setCurrentComponent(this)
const rawProps: Record<string, any> = {}
if (properties) {
Expand Down Expand Up @@ -174,7 +181,7 @@ export function defineComponent(optionsOrSetup: any, config?: Config): string {
})
}

setCurrentComponent(null)
unsetCurrentComponent()

if (originAttached !== undefined) {
originAttached.call(this)
Expand All @@ -190,11 +197,7 @@ export function defineComponent(optionsOrSetup: any, config?: Config): string {
) {
detached.call(this)

if (this.__effects__) {
this.__effects__.forEach((effect) => {
effect.stop()
})
}
this.__scope__.stop()
}

const originReady =
Expand Down
32 changes: 0 additions & 32 deletions packages/wechat/src/computed.ts

This file was deleted.

12 changes: 10 additions & 2 deletions packages/wechat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export {
// Core
computed,
reactive,
ref,
readonly,
Expand All @@ -22,8 +23,16 @@ export {
shallowReadonly,
markRaw,
toRaw,
// Effect
effect,
stop,
ReactiveEffect,
// Effect scope
effectScope,
EffectScope,
getCurrentScope,
onScopeDispose,
} from '@vue/reactivity'
export { computed } from './computed'
export { watch, watchEffect } from './watch'
export { nextTick } from './scheduler'
export { provide, inject } from './inject'
Expand Down Expand Up @@ -58,7 +67,6 @@ export {
// Types -----------------------------------------------------------------------

export {
ReactiveEffect,
ReactiveEffectOptions,
DebuggerEvent,
TrackOpTypes,
Expand Down
36 changes: 30 additions & 6 deletions packages/wechat/src/instance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactiveEffect } from '@vue/reactivity'
import { EffectScope } from '@vue/reactivity'

export type Bindings = Record<string, any> | void

Expand All @@ -11,7 +11,7 @@ export type PageInstance = WechatMiniprogram.Page.InstanceProperties &
__isInjectedShareToTimelineHook__?: () => true
__isInjectedFavoritesHook__?: () => true
__listenPageScroll__?: () => true
__effects__?: ReactiveEffect[]
__scope__: EffectScope
}

export type ComponentInstance = WechatMiniprogram.Component.InstanceProperties &
Expand All @@ -21,7 +21,7 @@ export type ComponentInstance = WechatMiniprogram.Component.InstanceProperties &
__isInjectedShareToTimelineHook__?: () => true
__isInjectedFavoritesHook__?: () => true
__listenPageScroll__?: () => true
__effects__?: ReactiveEffect[]
__scope__: EffectScope
__props__: undefined | Record<string, any>
}

Expand All @@ -35,14 +35,38 @@ export function getCurrentInstance(): PageInstance | ComponentInstance | null {
return currentPage || currentComponent
}

export function setCurrentApp(page: AppInstance | null): void {
export function setCurrentApp(page: AppInstance): void {
currentApp = page
}

export function setCurrentPage(page: PageInstance | null): void {
export function unsetCurrentApp(): void {
currentApp = null
}

export function setCurrentPage(page: PageInstance): void {
currentPage = page
page.__scope__.on()
}

export function unsetCurrentPage(): void {
/* istanbul ignore else */
if (currentPage) {
currentPage.__scope__.off()
}

currentPage = null
}

export function setCurrentComponent(component: ComponentInstance | null): void {
export function setCurrentComponent(component: ComponentInstance): void {
currentComponent = component
component.__scope__.on()
}

export function unsetCurrentComponent(): void {
/* istanbul ignore else */
if (currentComponent) {
currentComponent.__scope__.off()
}

currentComponent = null
}
18 changes: 11 additions & 7 deletions packages/wechat/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Bindings, PageInstance, setCurrentPage } from './instance'
import { EffectScope } from '@vue/reactivity'
import {
Bindings,
PageInstance,
setCurrentPage,
unsetCurrentPage,
} from './instance'
import { deepToRaw, deepWatch } from './shared'
import { isFunction, toHiddenField } from './utils'

Expand Down Expand Up @@ -78,6 +84,8 @@ export function definePage(optionsOrSetup: any, config?: Config): void {

const originOnLoad = options[PageLifecycle.ON_LOAD]
options[PageLifecycle.ON_LOAD] = function (this: PageInstance, query: Query) {
this.__scope__ = new EffectScope()

setCurrentPage(this)
const context: PageContext = {
is: this.is,
Expand Down Expand Up @@ -107,7 +115,7 @@ export function definePage(optionsOrSetup: any, config?: Config): void {
})
}

setCurrentPage(null)
unsetCurrentPage()

if (originOnLoad !== undefined) {
originOnLoad.call(this, query)
Expand All @@ -118,11 +126,7 @@ export function definePage(optionsOrSetup: any, config?: Config): void {
options[PageLifecycle.ON_UNLOAD] = function (this: PageInstance) {
onUnload.call(this)

if (this.__effects__) {
this.__effects__.forEach((effect) => {
effect.stop()
})
}
this.__scope__.stop()
}

if (options[PageLifecycle.ON_PAGE_SCROLL] || config.listenPageScroll) {
Expand Down
12 changes: 5 additions & 7 deletions packages/wechat/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
EffectScheduler,
} from '@vue/reactivity'
import { queueJob, SchedulerJob } from './scheduler'
import { recordInstanceBoundEffect } from './computed'
import { getCurrentInstance } from './instance'
import {
isArray,
Expand Down Expand Up @@ -280,16 +279,16 @@ function doWatch(
}
}

const effect = new ReactiveEffect(getter, scheduler)
const instance = getCurrentInstance()
const scope = instance && instance.__scope__
const effect = new ReactiveEffect(getter, scheduler, scope)

/* istanbul ignore else */
if (__DEV__) {
effect.onTrack = onTrack
effect.onTrigger = onTrigger
}

recordInstanceBoundEffect(effect)

// Initial run
if (cb) {
if (immediate) {
Expand All @@ -301,11 +300,10 @@ function doWatch(
effect.run()
}

const instance = getCurrentInstance()
return () => {
effect.stop()
if (instance) {
remove(instance.__effects__!, effect)
if (scope) {
remove(scope.effects, effect)
}
}
}
Expand Down

0 comments on commit fb82da3

Please sign in to comment.