-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix server side memory leak (fixes #40)
- Loading branch information
Showing
7 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function filterVariables(component, variableMapping) { | ||
const result = {}; | ||
for (const [name, value] of Object.entries(variableMapping)) { | ||
if (component.hasVariable(name)) { | ||
result[name] = value; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export default class AggregateContainer { | ||
constructor(fragmentSpecs) { | ||
this.fragmentSpecs = fragmentSpecs; | ||
} | ||
|
||
getFragmentNames() { | ||
return Object.keys(this.fragmentSpecs); | ||
} | ||
|
||
getFragment(fragmentName, variableMapping) { | ||
const { component, queryName } = this.fragmentSpecs[fragmentName]; | ||
|
||
return component.getFragment(queryName, filterVariables(component, variableMapping)); | ||
} | ||
|
||
hasFragment(fragmentName) { | ||
return !!this.fragmentSpecs[fragmentName]; | ||
} | ||
|
||
hasVariable(variableName) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import QueryAggregator from 'react-router-relay/lib/QueryAggregator'; | ||
import getAggregateContainer from './getAggregateContainer'; | ||
|
||
export default class IsomorphicQueryAggregator extends QueryAggregator { | ||
updateQueryConfig(routerProps) { | ||
super.updateQueryConfig(routerProps); | ||
|
||
this.Container = getAggregateContainer(this.fragmentSpecs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import AggregateContainer from './AggregateContainer'; | ||
|
||
const containerIds = new WeakMap; | ||
let nextContainerId = 0; | ||
|
||
function getContainerId(container) { | ||
let id = containerIds.get(container); | ||
if (id === void 0) { | ||
id = nextContainerId++; | ||
containerIds.set(container, id); | ||
} | ||
|
||
return id; | ||
} | ||
|
||
function getCacheKey(fragmentSpecs) { | ||
return JSON.stringify( | ||
Object.entries(fragmentSpecs) | ||
.map(([fragmentName, { component, queryName }]) => | ||
[fragmentName, getContainerId(component), queryName] | ||
) | ||
.sort(([a], [b]) => a.localeCompare(b)) | ||
); | ||
} | ||
|
||
const containerCache = new Map; | ||
|
||
export default function getAggregateContainer(fragmentSpecs) { | ||
const cacheKey = getCacheKey(fragmentSpecs); | ||
|
||
let container = containerCache.get(cacheKey); | ||
if (!container) { | ||
container = new AggregateContainer(fragmentSpecs); | ||
containerCache.set(cacheKey, container); | ||
} | ||
|
||
return container; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters