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

Commit 1763ffd

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

File tree

2 files changed

+65
-49
lines changed

2 files changed

+65
-49
lines changed

packages/core/src/Memory/Memory.ts

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -183,39 +183,77 @@ export class Memory {
183183
this._create(sliceKey, this._sliceKey);
184184
return this;
185185
}
186-
getRootVersionables(proxy: LoopObject | LoopArray | LoopSet): Set<AllowedObject> {
186+
isLinkeToMemory(proxy: AllowedObject): boolean {
187+
const params = proxy[memoryProxyPramsKey] as VersionableParams;
188+
return params.memory === this._memoryVersionable;
189+
}
190+
getRootVersionables(proxy: AllowedObject): Set<AllowedObject> {
187191
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);
192+
193+
const nodeID = proxy[memoryProxyPramsKey].ID;
194+
const pathList: [ProxyUniqID, string[]][] = [[nodeID, []]];
195+
while (pathList.length) {
196+
const path = pathList.pop();
197+
const [nodeID, pathToNode] = path;
198+
if (this._rootProxies[nodeID as number]) {
199+
roots.add(this._proxies[nodeID as number]);
200+
continue;
201+
}
202+
this._getProxyParentedPath(this._sliceKey, nodeID).forEach(path => {
203+
const parentNodeID = path.split(parentedPathSeparator, 1)[0];
204+
const partPath = path.slice(parentNodeID.length + 1);
205+
pathList.push([+parentNodeID, [partPath].concat(pathToNode)]);
206+
});
191207
}
192208
return roots;
193209
}
194-
getChangedVersionables(sliceKey: sliceKey = this._sliceKey): Map<AllowedObject, string[][]> {
210+
getParents(proxy: AllowedObject): Map<AllowedObject, string[][]> {
195211
const pathChanges = new Map<AllowedObject, string[][]>();
212+
const nodeID = proxy[memoryProxyPramsKey].ID;
213+
const pathList: [ProxyUniqID, string[]][] = [[nodeID, []]];
214+
while (pathList.length) {
215+
const path = pathList.pop();
216+
const [nodeID, pathToNode] = path;
217+
218+
const parentProxy: AllowedObject = this._proxies[nodeID as number];
219+
let paths: string[][] = pathChanges.get(parentProxy);
220+
if (!paths) {
221+
paths = [];
222+
pathChanges.set(parentProxy, paths);
223+
}
224+
paths.push(path[1]);
225+
226+
if (this._rootProxies[nodeID as number]) {
227+
continue;
228+
}
229+
this._getProxyParentedPath(this._sliceKey, nodeID).forEach(path => {
230+
const parentNodeID = path.split(parentedPathSeparator, 1)[0];
231+
const partPath = path.slice(parentNodeID.length + 1);
232+
pathList.push([+parentNodeID, [partPath].concat(pathToNode)]);
233+
});
234+
}
235+
return pathChanges;
236+
}
237+
getChanges(sliceKey: sliceKey = this._sliceKey): Map<AllowedObject, string[]> {
238+
const pathChanges = new Map<AllowedObject, string[]>();
196239
const slice = this._references[sliceKey].slice;
197240
for (const id in slice) {
198241
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-
});
242+
const proxy = this._proxies[id] as AllowedObject;
243+
let paths: string[];
244+
if (
245+
(item instanceof MemoryTypeObject || item instanceof MemoryTypeArray) &&
246+
Object.keys(item.props).length
247+
) {
248+
paths = Object.keys(item.props);
249+
} else {
250+
paths = [];
251+
}
252+
if (pathChanges.get(proxy)) {
253+
pathChanges.get(proxy).push(...paths);
254+
} else {
255+
pathChanges.set(proxy, paths);
256+
}
219257
}
220258
return pathChanges;
221259
}
@@ -403,28 +441,6 @@ export class Memory {
403441
// private
404442
/////////////////////////////////////////////////////
405443

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-
}
428444
private _addSliceProxyParent(
429445
ID: ProxyUniqID,
430446
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)