From efe13aac32b44ec86cc60cadca28cf4546debb3f Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Mon, 19 Aug 2019 08:18:43 -0700 Subject: [PATCH] Share uids between maki objects and state tree --- modern/src/Actions.ts | 5 ++++- modern/src/initializeStateTree.ts | 10 +--------- modern/src/runtime/MakiObject.js | 14 ++++++-------- modern/src/utils.js | 6 ++++++ 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/modern/src/Actions.ts b/modern/src/Actions.ts index 7e13ab4d..3c0b7ad2 100644 --- a/modern/src/Actions.ts +++ b/modern/src/Actions.ts @@ -35,10 +35,13 @@ export function gotSkinBlob(blob: Blob, store: ModernStore) { function gotSkinZip(zip: JSZip, store: ModernStore) { return async dispatch => { - const xmlTree = await Utils.inlineIncludes( + const rawXmlTree = await Utils.inlineIncludes( await Utils.readXml(zip, "skin.xml"), zip ); + const xmlTree = Utils.mapTree(rawXmlTree, node => { + return { ...node, uid: Utils.getId() }; + }); dispatch(setXmlTree(xmlTree)); diff --git a/modern/src/initializeStateTree.ts b/modern/src/initializeStateTree.ts index e127f717..4b95f25d 100644 --- a/modern/src/initializeStateTree.ts +++ b/modern/src/initializeStateTree.ts @@ -1,15 +1,7 @@ import { XmlTree } from "./types"; -import * as Utils from "./utils"; - -function mapTree(node, cb) { - const children = node.children || []; - return cb({ ...node, children: children.map(child => mapTree(child, cb)) }); -} export default async function initializeStateTree( xmlTree: XmlTree ): Promise { - return mapTree(xmlTree, node => { - return { ...node, uid: Utils.getId() }; - }); + return xmlTree; } diff --git a/modern/src/runtime/MakiObject.js b/modern/src/runtime/MakiObject.js index 31f4a989..5e19aa56 100644 --- a/modern/src/runtime/MakiObject.js +++ b/modern/src/runtime/MakiObject.js @@ -1,17 +1,15 @@ import Emitter from "../Emitter"; -import { - findElementById, - findGroupDefById, - unimplementedWarning, -} from "../utils"; +import * as Utils from "../utils"; class MakiObject { constructor(node, parent, annotations = {}, store) { this._store = store; if (node) { + this._uid = node.uid; this.attributes = node.attributes || {}; this.name = node.name; } else { + this._uid = Utils.getId(); // When dynamically creating an object with `new` we have no underlying node this.attributes = {}; this.name = this.getclassname().toLowerCase(); @@ -56,7 +54,7 @@ class MakiObject { } js_imageLookup(id) { - const element = findElementById(this, id); + const element = Utils.findElementById(this, id); if (element) { return element.js_annotations; } @@ -65,7 +63,7 @@ class MakiObject { } js_groupdefLookup(id) { - const groupdef = findGroupDefById(this, id); + const groupdef = Utils.findGroupDefById(this, id); if (groupdef) { return groupdef; } @@ -91,7 +89,7 @@ class MakiObject { } onnotify(command, param, a, b) { - unimplementedWarning("onnotify"); + Utils.unimplementedWarning("onnotify"); return 0; } } diff --git a/modern/src/utils.js b/modern/src/utils.js index ae3f523a..1939694a 100644 --- a/modern/src/utils.js +++ b/modern/src/utils.js @@ -5,6 +5,12 @@ export function getId() { return i++; } +// Depth-first tree map +export function mapTree(node, cb) { + const children = node.children || []; + return cb({ ...node, children: children.map(child => mapTree(child, cb)) }); +} + export function isPromise(obj) { return obj && typeof obj.then === "function"; }