From de3faed0a2b635ba3a5758902255f90d22889688 Mon Sep 17 00:00:00 2001 From: Val Erastov Date: Thu, 20 Dec 2018 16:10:04 -0800 Subject: [PATCH] keep all model id generators at same place --- web/app/cad/craft/craftPlugin.js | 4 ++-- web/app/cad/model/mdatum.js | 5 ++--- web/app/cad/model/mobject.js | 11 ++++++++++- web/app/cad/model/mshell.js | 6 ++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/web/app/cad/craft/craftPlugin.js b/web/app/cad/craft/craftPlugin.js index d44aaaa1..4f6fbe7c 100644 --- a/web/app/cad/craft/craftPlugin.js +++ b/web/app/cad/craft/craftPlugin.js @@ -4,6 +4,7 @@ import {MShell} from '../model/mshell'; import {MDatum} from '../model/mdatum'; import materializeParams from './materializeParams'; import CadError from '../../utils/errors'; +import {MObjectIdGenerator} from '../model/mobject'; export function activate({streams, services}) { @@ -87,8 +88,7 @@ export function activate({streams, services}) { if (isAdditiveChange(prev, curr)) { beginIndex = prev.pointer + 1; } else { - MShell.ID_COUNTER = 0; - MDatum.ID_COUNTER = 0; + MObjectIdGenerator.reset(); beginIndex = 0; streams.craft.models.next([]); } diff --git a/web/app/cad/model/mdatum.js b/web/app/cad/model/mdatum.js index dc540360..8dc1cdc0 100644 --- a/web/app/cad/model/mdatum.js +++ b/web/app/cad/model/mdatum.js @@ -1,12 +1,11 @@ -import {MObject} from './mobject'; +import {MObject, MObjectIdGenerator} from './mobject'; export class MDatum extends MObject { static TYPE = 'datum'; - static ID_COUNTER = 0; // TODO: reset the counter constructor(csys) { - super(MDatum.TYPE, 'D:' + (MDatum.ID_COUNTER++)); + super(MDatum.TYPE, 'D:' + MObjectIdGenerator.next(MDatum.TYPE)); this.csys = csys; this.xAxis = new MDatumAxis(this.id + '/X', this.csys.origin, this.csys.x); this.yAxis = new MDatumAxis(this.id + '/Y', this.csys.origin, this.csys.y); diff --git a/web/app/cad/model/mobject.js b/web/app/cad/model/mobject.js index d9873930..14c83040 100644 --- a/web/app/cad/model/mobject.js +++ b/web/app/cad/model/mobject.js @@ -10,6 +10,15 @@ export class MObject { this.TYPE = TYPE; this.id = id; } - } +const ID_REGISTRY = new Map(); + +export const MObjectIdGenerator = { + next: entityType => { + const id = ID_REGISTRY.get(entityType) || 0; + ID_REGISTRY.set(entityType, id + 1); + return id; + }, + reset: () => ID_REGISTRY.clear() +}; diff --git a/web/app/cad/model/mshell.js b/web/app/cad/model/mshell.js index 8d3e5f1c..65f481ce 100644 --- a/web/app/cad/model/mshell.js +++ b/web/app/cad/model/mshell.js @@ -1,4 +1,4 @@ -import {MObject} from './mobject'; +import {MObject, MObjectIdGenerator} from './mobject'; import {MBrepFace, MFace} from './mface'; import {MEdge} from './medge'; import {MVertex} from './mvertex'; @@ -8,10 +8,8 @@ export class MShell extends MObject { static TYPE = 'shell'; - static ID_COUNTER = 0; - constructor() { - super(MShell.TYPE, 'S:' + (MShell.ID_COUNTER++)) + super(MShell.TYPE, 'S:' + MObjectIdGenerator.next(MShell.TYPE)) } shell;