Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit a50e233

Browse files
committed
Memory add public method to exploit memory
TODO: update tests
1 parent e725ddb commit a50e233

File tree

2 files changed

+61
-49
lines changed

2 files changed

+61
-49
lines changed

packages/core/src/Memory/Memory.ts

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -183,39 +183,73 @@ export class Memory {
183183
this._create(sliceKey, this._sliceKey);
184184
return this;
185185
}
186-
getRootVersionables(proxy: LoopObject | LoopArray | LoopSet): Set<AllowedObject> {
186+
getRootVersionables(proxy: AllowedObject): Set<AllowedObject> {
187187
const roots: Set<AllowedObject> = new Set();
188-
const pathToRoot = this._getRootVersionables(this._sliceKey, proxy);
189-
for (const path of pathToRoot) {
190-
roots.add(path.proxy);
188+
189+
const nodeID = proxy[memoryProxyPramsKey].ID;
190+
const pathList: [ProxyUniqID, string[]][] = [[nodeID, []]];
191+
while (pathList.length) {
192+
const path = pathList.pop();
193+
const [nodeID, pathToNode] = path;
194+
if (this._rootProxies[nodeID as number]) {
195+
roots.add(this._proxies[nodeID as number]);
196+
continue;
197+
}
198+
this._getProxyParentedPath(this._sliceKey, nodeID).forEach(path => {
199+
const parentNodeID = path.split(parentedPathSeparator, 1)[0];
200+
const partPath = path.slice(parentNodeID.length + 1);
201+
pathList.push([+parentNodeID, [partPath].concat(pathToNode)]);
202+
});
191203
}
192204
return roots;
193205
}
194-
getChangedVersionables(sliceKey: sliceKey = this._sliceKey): Map<AllowedObject, string[][]> {
206+
getParents(proxy: AllowedObject): Map<AllowedObject, string[][]> {
195207
const pathChanges = new Map<AllowedObject, string[][]>();
208+
const nodeID = proxy[memoryProxyPramsKey].ID;
209+
const pathList: [ProxyUniqID, string[]][] = [[nodeID, []]];
210+
while (pathList.length) {
211+
const path = pathList.pop();
212+
const [nodeID, pathToNode] = path;
213+
214+
const parentProxy: AllowedObject = this._proxies[nodeID as number];
215+
let paths: string[][] = pathChanges.get(parentProxy);
216+
if (!paths) {
217+
paths = [];
218+
pathChanges.set(parentProxy, paths);
219+
}
220+
paths.push(path[1]);
221+
222+
if (this._rootProxies[nodeID as number]) {
223+
continue;
224+
}
225+
this._getProxyParentedPath(this._sliceKey, nodeID).forEach(path => {
226+
const parentNodeID = path.split(parentedPathSeparator, 1)[0];
227+
const partPath = path.slice(parentNodeID.length + 1);
228+
pathList.push([+parentNodeID, [partPath].concat(pathToNode)]);
229+
});
230+
}
231+
return pathChanges;
232+
}
233+
getChanges(sliceKey: sliceKey = this._sliceKey): Map<AllowedObject, string[]> {
234+
const pathChanges = new Map<AllowedObject, string[]>();
196235
const slice = this._references[sliceKey].slice;
197236
for (const id in slice) {
198237
const item = slice[id];
199-
const proxy = this._proxies[id] as LoopObject | LoopArray | LoopSet;
200-
this._getRootVersionables(this._sliceKey, proxy).forEach(pathToRoot => {
201-
let paths: string[][];
202-
if (
203-
(item instanceof MemoryTypeObject || item instanceof MemoryTypeArray) &&
204-
Object.keys(item.props).length
205-
) {
206-
paths = [];
207-
for (const key in item.props) {
208-
paths.push(pathToRoot.path.concat([key]));
209-
}
210-
} else {
211-
paths = [pathToRoot.path];
212-
}
213-
if (pathChanges.get(pathToRoot.proxy)) {
214-
pathChanges.get(pathToRoot.proxy).push(...paths);
215-
} else {
216-
pathChanges.set(pathToRoot.proxy, paths);
217-
}
218-
});
238+
const proxy = this._proxies[id] as AllowedObject;
239+
let paths: string[];
240+
if (
241+
(item instanceof MemoryTypeObject || item instanceof MemoryTypeArray) &&
242+
Object.keys(item.props).length
243+
) {
244+
paths = Object.keys(item.props);
245+
} else {
246+
paths = [];
247+
}
248+
if (pathChanges.get(proxy)) {
249+
pathChanges.get(proxy).push(...paths);
250+
} else {
251+
pathChanges.set(proxy, paths);
252+
}
219253
}
220254
return pathChanges;
221255
}
@@ -403,28 +437,6 @@ export class Memory {
403437
// private
404438
/////////////////////////////////////////////////////
405439

406-
private _getRootVersionables(
407-
sliceKey: sliceKey,
408-
proxy: LoopObject | LoopArray | LoopSet,
409-
): { proxy: AllowedObject; path: string[] }[] {
410-
const roots: { proxy: AllowedObject; path: string[] }[] = [];
411-
const nodeID = proxy[memoryProxyPramsKey].ID;
412-
const pathList: [ProxyUniqID, string[]][] = [[nodeID, []]];
413-
while (pathList.length) {
414-
const path = pathList.pop();
415-
const [nodeID, pathToNode] = path;
416-
if (this._rootProxies[nodeID as number]) {
417-
roots.push({ proxy: this._proxies[nodeID as number], path: path[1] });
418-
continue;
419-
}
420-
this._getProxyParentedPath(sliceKey, nodeID).forEach(path => {
421-
const parentNodeID = path.split(parentedPathSeparator, 1)[0];
422-
const partPath = path.slice(parentNodeID.length + 1);
423-
pathList.push([+parentNodeID, [partPath].concat(pathToNode)]);
424-
});
425-
}
426-
return roots;
427-
}
428440
private _addSliceProxyParent(
429441
ID: ProxyUniqID,
430442
parentID: ProxyUniqID,

packages/core/src/Memory/test/memory.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,12 +2424,12 @@ describe('core', () => {
24242424
memory.create('test-1');
24252425
memory.switchTo('test-1');
24262426

2427-
expect(memory.getChangedVersionables('test-1').size).to.deep.equal(0);
2427+
expect(memory.getChanges('test-1').size).to.deep.equal(0);
24282428

24292429
obj.toto.titi.tata.m = 4;
24302430
obj.tutu.a = 6;
24312431

2432-
const pathChanges = memory.getChangedVersionables('test-1');
2432+
const pathChanges = memory.getChanges('test-1');
24332433
expect([...pathChanges.keys()]).to.deep.equal([obj.tutu, obj.toto]);
24342434
expect([...pathChanges.values()]).to.deep.equal([
24352435
[['a'], ['tata', 'm']],

0 commit comments

Comments
 (0)