OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 goog.provide('__crWeb.webUIModuleLoadNotifier'); | |
6 | |
7 /** | |
8 * A holder class for require.js contexts whose load is pending. | |
9 * @constructor | |
10 */ | |
11 var WebUIModuleLoadNotifier = function() { | |
12 this.lastUsedId_ = 0; | |
13 this.pendingContexts_ = new Map(); | |
14 }; | |
15 | |
16 /** | |
17 * Adds the given context to the list of pending contexts. | |
18 * @param {string} name Name of the module that will be loaded. | |
19 * @param {!Object} context Require.js context to hold. | |
20 * @return {string} Identifier for the added context, can be later used for | |
21 * {@code moduleLoadCompleted} and {@code moduleLoadFailed} arguments. | |
22 */ | |
23 WebUIModuleLoadNotifier.prototype.addPendingContext = function(name, context) { | |
24 this.lastUsedId_++; | |
25 var key = name + this.lastUsedId_; | |
26 this.pendingContexts_.set(key, context); | |
27 return String(this.lastUsedId_); | |
28 }; | |
29 | |
30 /** | |
31 * Signals that module has successfully loaded. | |
32 * @param {string} name Name of the module that has been loaded. | |
33 * @param {String} loadId Identifier returned from {@code addPendingContext}. | |
34 */ | |
35 WebUIModuleLoadNotifier.prototype.moduleLoadCompleted = function(name, loadId) { | |
36 this.popContext_(name, loadId).completeLoad(name); | |
37 }; | |
38 | |
39 /** | |
40 * Signals that module has failed to load. | |
41 * @param {string} name Name of the module that has failed to load. | |
42 * @param {String} loadId Identifier returned from {@code addPendingContext}. | |
43 */ | |
44 WebUIModuleLoadNotifier.prototype.moduleLoadFailed = function(name, loadId) { | |
45 this.popContext_(name, loadId).onScriptFailure(name); | |
46 }; | |
47 | |
48 /** | |
49 * Pops the pending context. | |
50 * @param {string} name Name of the module that has failed to load. | |
51 * @param {String} loadId Identifier returned from {@code addPendingContext}. | |
52 * @return {Object} Require.js context | |
53 */ | |
54 WebUIModuleLoadNotifier.prototype.popContext_ = function(name, loadId) { | |
55 var key = name + loadId; | |
56 var context = this.pendingContexts_.get(key); | |
57 this.pendingContexts_.delete(key); | |
58 return context; | |
59 }; | |
OLD | NEW |