瀏覽代碼

Merge pull request #3960 from RaananW/viewer-catch-errors

Viewer introducing disposeAll for singleton-context disposal
Raanan Weber 7 年之前
父節點
當前提交
37f903f8b5

+ 13 - 8
Viewer/src/configuration/loader.ts

@@ -11,7 +11,7 @@ export class ConfigurationLoader {
 
     private loadRequests: Array<IFileRequest>;
 
-    constructor() {
+    constructor(private _enableCache: boolean = false) {
         this.configurationCache = {};
         this.loadRequests = [];
     }
@@ -78,20 +78,25 @@ export class ConfigurationLoader {
 
     private loadFile(url: string): Promise<any> {
         let cacheReference = this.configurationCache;
-        if (cacheReference[url]) {
+        if (this._enableCache && cacheReference[url]) {
             return Promise.resolve(cacheReference[url]);
         }
 
         return new Promise((resolve, reject) => {
-            let fileRequest = Tools.LoadFile(url, resolve, undefined, undefined, false, (request, error: any) => {
+            let fileRequest = Tools.LoadFile(url, (result) => {
+                let idx = this.loadRequests.indexOf(fileRequest);
+                if (idx !== -1)
+                    this.loadRequests.splice(idx, 1);
+                if (this._enableCache) cacheReference[url] = result;
+                resolve(result);
+            }, undefined, undefined, false, (request, error: any) => {
+                let idx = this.loadRequests.indexOf(fileRequest);
+                if (idx !== -1)
+                    this.loadRequests.splice(idx, 1);
                 reject(error);
             });
             this.loadRequests.push(fileRequest);
         });
     }
 
-}
-
-export let configurationLoader = new ConfigurationLoader();
-
-export default configurationLoader;
+}

+ 4 - 0
Viewer/src/configuration/mappers.ts

@@ -118,6 +118,10 @@ export class MapperManager {
         this.mappers[type] = mapper;
     }
 
+    public dispose() {
+        this.mappers = {};
+    }
+
 }
 
 export let mapperManager = new MapperManager();

+ 4 - 0
Viewer/src/eventManager.ts

@@ -37,4 +37,8 @@ export class EventManager {
             callbackDef.callback(data);
         });
     }
+
+    public dispose() {
+        this.callbacksContainer = {};
+    }
 }

+ 6 - 1
Viewer/src/index.ts

@@ -29,5 +29,10 @@ function init(event) {
     InitTags();
 }
 
+function disposeAll() {
+    viewerManager.dispose();
+    mapperManager.dispose();
+}
+
 // public API for initialization
-export { InitTags, DefaultViewer, AbstractViewer, viewerManager, mapperManager };
+export { InitTags, DefaultViewer, AbstractViewer, viewerManager, mapperManager, disposeAll };

+ 1 - 0
Viewer/src/templateManager.ts

@@ -164,6 +164,7 @@ export class TemplateManager {
             this.templates[template].dispose();
         });
         this.templates = {};
+        this.eventManager.dispose();
 
         this.onInit.clear();
         this.onAllLoaded.clear();

+ 6 - 0
Viewer/src/viewer/viewerManager.ts

@@ -58,6 +58,12 @@ export class ViewerManager {
         this.onViewerAdded && this.onViewerAdded(viewer);
         this.onViewerAddedObservable.notifyObservers(viewer);
     }
+
+    public dispose() {
+        for (let id in this.viewers) {
+            this.viewers[id].dispose();
+        }
+    }
 }
 
 export let viewerManager = new ViewerManager();

+ 2 - 0
dist/preview release/viewer/babylon.viewer.d.ts

@@ -2,6 +2,8 @@ declare module BabylonViewer {
 
     export let disableInit: boolean;
 
+    export function disposeAll();
+
     export interface ITemplateConfiguration {
         location?: string;
         html?: string;

文件差異過大導致無法顯示
+ 10 - 10
dist/preview release/viewer/babylon.viewer.js


+ 32 - 5
dist/preview release/viewer/babylon.viewer.max.js

@@ -93272,6 +93272,9 @@ var MapperManager = (function () {
     MapperManager.prototype.registerMapper = function (type, mapper) {
         this.mappers[type] = mapper;
     };
+    MapperManager.prototype.dispose = function () {
+        this.mappers = {};
+    };
     MapperManager.DefaultMapper = 'json';
     return MapperManager;
 }());
@@ -93356,6 +93359,11 @@ var ViewerManager = (function () {
         this.onViewerAdded && this.onViewerAdded(viewer);
         this.onViewerAddedObservable.notifyObservers(viewer);
     };
+    ViewerManager.prototype.dispose = function () {
+        for (var id in this.viewers) {
+            this.viewers[id].dispose();
+        }
+    };
     return ViewerManager;
 }());
 exports.ViewerManager = ViewerManager;
@@ -94602,6 +94610,11 @@ function init(event) {
         return;
     initializer_1.InitTags();
 }
+function disposeAll() {
+    viewerManager_1.viewerManager.dispose();
+    mappers_1.mapperManager.dispose();
+}
+exports.disposeAll = disposeAll;
 
 
 /***/ }),
@@ -94742,6 +94755,7 @@ var TemplateManager = (function () {
             _this.templates[template].dispose();
         });
         this.templates = {};
+        this.eventManager.dispose();
         this.onInit.clear();
         this.onAllLoaded.clear();
         this.onEventTriggered.clear();
@@ -95078,6 +95092,9 @@ var EventManager = (function () {
             callbackDef.callback(data);
         });
     };
+    EventManager.prototype.dispose = function () {
+        this.callbacksContainer = {};
+    };
     return EventManager;
 }());
 exports.EventManager = EventManager;
@@ -95095,7 +95112,9 @@ var types_1 = __webpack_require__(23);
 var deepmerge = __webpack_require__(13);
 var babylonjs_1 = __webpack_require__(0);
 var ConfigurationLoader = (function () {
-    function ConfigurationLoader() {
+    function ConfigurationLoader(_enableCache) {
+        if (_enableCache === void 0) { _enableCache = false; }
+        this._enableCache = _enableCache;
         this.configurationCache = {};
         this.loadRequests = [];
     }
@@ -95154,11 +95173,21 @@ var ConfigurationLoader = (function () {
     ConfigurationLoader.prototype.loadFile = function (url) {
         var _this = this;
         var cacheReference = this.configurationCache;
-        if (cacheReference[url]) {
+        if (this._enableCache && cacheReference[url]) {
             return Promise.resolve(cacheReference[url]);
         }
         return new Promise(function (resolve, reject) {
-            var fileRequest = babylonjs_1.Tools.LoadFile(url, resolve, undefined, undefined, false, function (request, error) {
+            var fileRequest = babylonjs_1.Tools.LoadFile(url, function (result) {
+                var idx = _this.loadRequests.indexOf(fileRequest);
+                if (idx !== -1)
+                    _this.loadRequests.splice(idx, 1);
+                if (_this._enableCache)
+                    cacheReference[url] = result;
+                resolve(result);
+            }, undefined, undefined, false, function (request, error) {
+                var idx = _this.loadRequests.indexOf(fileRequest);
+                if (idx !== -1)
+                    _this.loadRequests.splice(idx, 1);
                 reject(error);
             });
             _this.loadRequests.push(fileRequest);
@@ -95167,8 +95196,6 @@ var ConfigurationLoader = (function () {
     return ConfigurationLoader;
 }());
 exports.ConfigurationLoader = ConfigurationLoader;
-exports.configurationLoader = new ConfigurationLoader();
-exports.default = exports.configurationLoader;
 
 
 /***/ }),