Skip to content

Commit 3131828

Browse files
committed
Reimplement plugin cache
1 parent e7978c0 commit 3131828

File tree

2 files changed

+175
-168
lines changed

2 files changed

+175
-168
lines changed

src/specmap/index.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class SpecMap {
8383
// We might consider making this (traversing & application) configurable later.
8484
function createKeyBasedPlugin(pluginObj) {
8585
return function* (patches, specmap) {
86-
const traversedRefs = {}
86+
const resultCache = {}
8787

8888
for (const patch of patches.filter(lib.isAdditiveMutation)) {
8989
yield* traverse(patch.value, patch.path, patch)
@@ -109,20 +109,24 @@ class SpecMap {
109109
// If the object has a meta '$$ref' - and store this $$ref
110110
// in a lookaside to prevent future traversals of this $ref's tree.
111111
const objRef = obj.$$ref
112-
const traversed = specmap.allowMetaPatches && traversedRefs[obj.$$ref]
113-
114-
if (!traversed) {
115-
if (isObj) {
116-
// Only store the ref if it exists
117-
if (specmap.allowMetaPatches && objRef) {
118-
traversedRefs[objRef] = true
119-
}
120-
yield* traverse(val, updatedPath, patch)
121-
}
112+
113+
if (isObj) {
114+
// console.log('recursive case', {obj, path, patch})
115+
yield* traverse(val, updatedPath, patch)
122116
}
123117

124118
if (!isRootProperties && key === pluginObj.key) {
125-
yield pluginObj.plugin(val, key, updatedPath, specmap, patch)
119+
// console.log('base case', {obj, path, patch})
120+
const stringifiedPath = path.join('/')
121+
122+
if (resultCache[stringifiedPath]) {
123+
yield resultCache[stringifiedPath]
124+
}
125+
else {
126+
const res = pluginObj.plugin(val, key, updatedPath, specmap, patch)
127+
resultCache[stringifiedPath] = res
128+
yield res
129+
}
126130
}
127131
}
128132
}

0 commit comments

Comments
 (0)