Skip to content
This repository has been archived by the owner on Feb 11, 2023. It is now read-only.

Commit

Permalink
Avoid duplicated re-rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Mar 3, 2018
1 parent fc4750e commit be7cb8f
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 708 deletions.
372 changes: 191 additions & 181 deletions dist/vue-relay.common.js

Large diffs are not rendered by default.

372 changes: 191 additions & 181 deletions dist/vue-relay.esm.js

Large diffs are not rendered by default.

372 changes: 191 additions & 181 deletions dist/vue-relay.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vue-relay.min.js

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions src/VueRelayFragmentContainer.js
Expand Up @@ -32,7 +32,24 @@ const createContainerWithFragments = function (fragments) {
})
}
},
beforeUpdate () {
methods: {
setState (state) {
this.state = Object.freeze({
...this.state,
...state
})
},
_handleFragmentDataUpdate () {
this.setState({
data: this.state.resolver.resolve(),
relayProp: {
isLoading: this.state.resolver.isLoading(),
environment: this.state.relayProp.environment
}
})
}
},
watch: { ...Object.keys(fragments).map((key) => ({ [key]: function () {
const {
createFragmentSpecResolver,
getDataIDsFromObject
Expand Down Expand Up @@ -85,26 +102,9 @@ const createContainerWithFragments = function (fragments) {
})
}
}
},
} })) },
beforeDestroy () {
this.state.resolver.dispose()
},
methods: {
setState (state) {
this.state = Object.freeze({
...this.state,
...state
})
},
_handleFragmentDataUpdate () {
this.setState({
data: this.state.resolver.resolve(),
relayProp: {
isLoading: this.state.resolver.isLoading(),
environment: this.state.relayProp.environment
}
})
}
}
}
}
Expand Down
102 changes: 51 additions & 51 deletions src/VueRelayPaginationContainer.js
Expand Up @@ -147,57 +147,6 @@ const createContainerWithFragments = function (fragments, connectionConfig) {
})
}
},
beforeUpdate () {
const {
createFragmentSpecResolver,
getDataIDsFromObject
} = relay.environment.unstable_internal

const prevIDs = getDataIDsFromObject(fragments, this.state.prevProps)
const nextIDs = getDataIDsFromObject(fragments, this.$props)

// If the environment has changed or props point to new records then
// previously fetched data and any pending fetches no longer apply:
// - Existing references are on the old environment.
// - Existing references are based on old variables.
// - Pending fetches are for the previous records.
if (
this.state.relayEnvironment !== relay.environment ||
this.state.relayVariables !== relay.variables ||
!areEqual(prevIDs, nextIDs)
) {
this._release()

this.context.relay.environment = relay.environment
this.context.relay.variables = relay.variables

const resolver = createFragmentSpecResolver(
relay,
this.$options.name,
fragments,
this.$props,
this._handleFragmentDataUpdate
)

this.setState({
prevProps: this.$props,
relayEnvironment: relay.environment,
relayVariables: relay.variables,
relayProp: this._buildRelayProp(relay),
localVariables: null,
resolver
})
} else if (!this.state.localVariables) {
this.state.resolver.setProps(this.$props)
}
const data = this.state.resolver.resolve()
if (data !== this.state.data) {
this.setState({ data })
}
},
beforeDestroy () {
this._release()
},
methods: {
setState (state) {
this.state = Object.freeze({
Expand Down Expand Up @@ -492,6 +441,57 @@ const createContainerWithFragments = function (fragments, connectionConfig) {
this.state.queryFetcher.dispose()
}
}
},
watch: { ...Object.keys(fragments).map((key) => ({ [key]: function () {
const {
createFragmentSpecResolver,
getDataIDsFromObject
} = relay.environment.unstable_internal

const prevIDs = getDataIDsFromObject(fragments, this.state.prevProps)
const nextIDs = getDataIDsFromObject(fragments, this.$props)

// If the environment has changed or props point to new records then
// previously fetched data and any pending fetches no longer apply:
// - Existing references are on the old environment.
// - Existing references are based on old variables.
// - Pending fetches are for the previous records.
if (
this.state.relayEnvironment !== relay.environment ||
this.state.relayVariables !== relay.variables ||
!areEqual(prevIDs, nextIDs)
) {
this._release()

this.context.relay.environment = relay.environment
this.context.relay.variables = relay.variables

const resolver = createFragmentSpecResolver(
relay,
this.$options.name,
fragments,
this.$props,
this._handleFragmentDataUpdate
)

this.setState({
prevProps: this.$props,
relayEnvironment: relay.environment,
relayVariables: relay.variables,
relayProp: this._buildRelayProp(relay),
localVariables: null,
resolver
})
} else if (!this.state.localVariables) {
this.state.resolver.setProps(this.$props)
}
const data = this.state.resolver.resolve()
if (data !== this.state.data) {
this.setState({ data })
}
} })) },
beforeDestroy () {
this._release()
}
}
}
Expand Down
66 changes: 34 additions & 32 deletions src/VueRelayQueryRenderer.js
Expand Up @@ -106,20 +106,22 @@ const fetchQueryAndComputeStateFromProps = function (props, queryFetcher, retryC
}
}

const props = {
cacheConfig: {},
dataFrom: {},
environment: {
required: true
},
query: {},
variables: {
type: Object,
default: () => ({})
}
}

export default {
name: 'relay-query-renderer',
props: {
cacheConfig: {},
dataFrom: {},
environment: {
required: true
},
query: {},
variables: {
type: Object,
default: () => ({})
}
},
props,
data () {
const handleDataChange = ({ error, snapshot }) => {
this.setState({ renderProps: getRenderProps(error, snapshot, queryFetcher, retryCallbacks) })
Expand Down Expand Up @@ -154,16 +156,24 @@ export default {
}
}),
state: Object.freeze({
prevPropsEnvironment: this.$props.environment,
prevPropsVariables: this.$props.variables,
prevQuery: this.$props.query,
prevPropsEnvironment: this.environment,
prevPropsVariables: this.variables,
prevQuery: this.query,
queryFetcher,
retryCallbacks,
...state
})
}
},
beforeUpdate () {
methods: {
setState (state) {
this.state = Object.freeze({
...this.state,
...state
})
}
},
watch: { ...Object.keys(props).map((key) => ({ [key]: function () {
if (
this.state.prevQuery !== this.query ||
this.state.prevPropsEnvironment !== this.environment ||
Expand All @@ -188,9 +198,12 @@ export default {
...state
})
}
},
beforeDestroy () {
this.state.queryFetcher.dispose()
} })) },
render (h) {
if (process.env.NODE_ENV !== 'production') {
require('relay-runtime/lib/deepFreeze')(this.state.renderProps)
}
return h(this.component)
},
created () {
this.component = {
Expand All @@ -207,18 +220,7 @@ export default {
}
}
},
render (h) {
if (process.env.NODE_ENV !== 'production') {
require('relay-runtime/lib/deepFreeze')(this.state.renderProps)
}
return h(this.component)
},
methods: {
setState (state) {
this.state = Object.freeze({
...this.state,
...state
})
}
beforeDestroy () {
this.state.queryFetcher.dispose()
}
}
102 changes: 51 additions & 51 deletions src/VueRelayRefetchContainer.js
Expand Up @@ -43,57 +43,6 @@ const createContainerWithFragments = function (fragments, taggedNode) {
})
}
},
beforeUpdate () {
const {
createFragmentSpecResolver,
getDataIDsFromObject
} = relay.environment.unstable_internal

const prevIDs = getDataIDsFromObject(fragments, this.state.prevProps)
const nextIDs = getDataIDsFromObject(fragments, this.$props)

// If the environment has changed or props point to new records then
// previously fetched data and any pending fetches no longer apply:
// - Existing references are on the old environment.
// - Existing references are based on old variables.
// - Pending fetches are for the previous records.
if (
this.state.relayEnvironment !== relay.environment ||
this.state.relayVariables !== relay.variables ||
!areEqual(prevIDs, nextIDs)
) {
this._release()

this.context.relay.environment = relay.environment
this.context.relay.variables = relay.variables

const resolver = createFragmentSpecResolver(
relay,
this.$options.name,
fragments,
this.$props,
this._handleFragmentDataUpdate
)

this.setState({
prevProps: this.$props,
relayEnvironment: relay.environment,
relayVariables: relay.variables,
relayProp: this._buildRelayProp(relay),
localVariables: null,
resolver
})
} else if (!this.state.localVariables) {
this.state.resolver.setProps(this.$props)
}
const data = this.state.resolver.resolve()
if (data !== this.state.data) {
this.setState({ data })
}
},
beforeDestroy () {
this._release()
},
methods: {
setState (state) {
this.state = Object.freeze({
Expand Down Expand Up @@ -227,6 +176,57 @@ const createContainerWithFragments = function (fragments, taggedNode) {
this.state.queryFetcher.dispose()
}
}
},
watch: { ...Object.keys(fragments).map((key) => ({ [key]: function () {
const {
createFragmentSpecResolver,
getDataIDsFromObject
} = relay.environment.unstable_internal

const prevIDs = getDataIDsFromObject(fragments, this.state.prevProps)
const nextIDs = getDataIDsFromObject(fragments, this.$props)

// If the environment has changed or props point to new records then
// previously fetched data and any pending fetches no longer apply:
// - Existing references are on the old environment.
// - Existing references are based on old variables.
// - Pending fetches are for the previous records.
if (
this.state.relayEnvironment !== relay.environment ||
this.state.relayVariables !== relay.variables ||
!areEqual(prevIDs, nextIDs)
) {
this._release()

this.context.relay.environment = relay.environment
this.context.relay.variables = relay.variables

const resolver = createFragmentSpecResolver(
relay,
this.$options.name,
fragments,
this.$props,
this._handleFragmentDataUpdate
)

this.setState({
prevProps: this.$props,
relayEnvironment: relay.environment,
relayVariables: relay.variables,
relayProp: this._buildRelayProp(relay),
localVariables: null,
resolver
})
} else if (!this.state.localVariables) {
this.state.resolver.setProps(this.$props)
}
const data = this.state.resolver.resolve()
if (data !== this.state.data) {
this.setState({ data })
}
} })) },
beforeDestroy () {
this._release()
}
}
}
Expand Down

0 comments on commit be7cb8f

Please sign in to comment.