Skip to content

Commit 18d698b

Browse files
authored
Add @internal namespace for auto-assigned node IDs. Fixes #31 (#32)
* Add @internal namespace for auto-assigned node IDs. Fixes #31 * Add test for ID collisions
1 parent f0d33ea commit 18d698b

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/common/JSONImporter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ define([
1313
constructor(core, rootNode) {
1414
this.core = core;
1515
this.rootNode = rootNode;
16+
this._nodeIDCounter = 1;
1617
}
1718

1819
async _metaDictToGuids(meta_dict){
@@ -283,7 +284,7 @@ define([
283284

284285
async createNode(parent, state={}, base) {
285286
if (!state.id) {
286-
state.id = `@id:${Date.now() + Math.floor(100*Math.random())}`;
287+
state.id = `@internal:${this._nodeIDCounter++}`;
287288
}
288289
const idString = state.id;
289290
const fco = await this.core.loadByPath(this.rootNode, '/1');
@@ -776,7 +777,7 @@ define([
776777
.find(child => core.getAttribute(child, attr) === value);
777778
}
778779

779-
if (this.tag === '@id') {
780+
if (this.tag === '@id' || this.tag === '@internal') {
780781
return null;
781782
}
782783

@@ -877,7 +878,7 @@ define([
877878

878879
isAbsolute() {
879880
return this.tag === '@meta' || this.tag === '@path' ||
880-
this.tag === '@id' || this.tag === '@guid';
881+
this.tag === '@id' || this.tag === '@guid' || this.tag === '@internal';
881882
}
882883
}
883884

test/common/JSONImporter.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,17 @@ describe('JSONImporter', function () {
11541154
);
11551155
});
11561156

1157+
it('should not have collisions for auto-gen IDs', async function() {
1158+
const fco = await core.loadByPath(root, '/1');
1159+
const container = core.createNode({base: fco, parent: root});
1160+
const childCount = 1000;
1161+
const schema = {
1162+
children: [...new Array(childCount)].map(() => ({})),
1163+
};
1164+
await importer.apply(container, schema);
1165+
assert.equal(childCount, core.getChildrenPaths(container).length);
1166+
});
1167+
11571168
describe('prepare', function() {
11581169
it('should add @meta node to META', async function() {
11591170
const selector = new Importer.NodeSelector('@meta:TestMeta');

0 commit comments

Comments
 (0)