Skip to content

Commit e5bef1b

Browse files
committed
Wait for cached files to load
* Fixed start screen to wait for cache to be loaded prior to starting CwC * Maded various database functions async to support waiting for database operations to complete * Fixed cache.update(), returned promise now waits for all async calls within function * Made cache.loadFiles and cache.addFile async
1 parent 79ce5ff commit e5bef1b

File tree

4 files changed

+115
-37
lines changed

4 files changed

+115
-37
lines changed

src/cache/cache.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,60 +73,66 @@ cwc.Cache.prototype.prepare = async function() {
7373
/**
7474
* @param {!string|number} version
7575
*/
76-
cwc.Cache.prototype.update = function(version) {
76+
cwc.Cache.prototype.update = async function(version) {
7777
if (version && this.version <= version) {
7878
this.log_.info('No updates needed', version, '>=', this.version);
7979
return;
8080
}
8181

8282
this.log_.info('Updating Cache to version', this.version);
83-
this.database_.clear();
83+
await this.database_.clear();
8484

85+
let promises = [];
8586
this.log_.info('Loading external frameworks ...');
86-
this.loadFiles(cwc.framework.External);
87+
promises.push(this.loadFiles(cwc.framework.External));
8788

8889
this.log_.info('Loading internal frameworks ...');
89-
this.loadFiles(cwc.framework.Internal);
90+
promises.push(this.loadFiles(cwc.framework.Internal));
9091

9192
this.log_.info('Loading Style Sheets ...');
92-
this.loadFiles(cwc.framework.StyleSheet);
93+
promises.push(this.loadFiles(cwc.framework.StyleSheet));
9394

94-
this.database_.add('__version__', this.version);
95+
promises.push(this.database_.add('__version__', this.version));
96+
97+
await Promise.all(promises);
9598
};
9699

97100

98101
/**
99102
* Loads files into cache.
100103
* @param {!Object} files
101104
*/
102-
cwc.Cache.prototype.loadFiles = function(files) {
105+
cwc.Cache.prototype.loadFiles = async function(files) {
106+
let promises = [];
103107
for (let file of Object.keys(files)) {
104108
if (goog.isString(files[file])) {
105-
cwc.utils.Resources.getUriAsText('..' + files[file]).then((content) => {
106-
this.addFile(files[file], content);
107-
});
109+
promises.push(cwc.utils.Resources.getUriAsText('..' + files[file])
110+
.then((content) => {
111+
return this.addFile(files[file], content);
112+
}));
108113
} else {
109114
for (let subFile of Object.keys(files[file])) {
110-
cwc.utils.Resources.getUriAsText('..' + files[file][subFile]).then(
111-
(content) => {
112-
this.addFile(files[file][subFile], content);
113-
});
115+
promises.push(cwc.utils.Resources.getUriAsText('..' +
116+
files[file][subFile]).then((content) => {
117+
return this.addFile(files[file][subFile], content);
118+
}));
114119
}
115120
}
116121
}
122+
await Promise.all(promises);
117123
};
118124

119125

120126
/**
121127
* @param {string!} name
122128
* @param {string!} content
123129
*/
124-
cwc.Cache.prototype.addFile = function(name, content) {
130+
cwc.Cache.prototype.addFile = async function(name, content) {
125131
if (!content) {
126132
this.log_.error('Received empty content for', name);
127133
return;
128134
}
129-
this.database_.add(name, content);
135+
await this.database_.add(name, content);
130136
};
131137

132138

src/ui/builder.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ cwc.ui.Builder.prototype.decorateUI = function() {
282282
this.setProgressFunc('Prepare account support ...', this.prepareAccount);
283283
}
284284
this.setProgressFunc('Loading select screen ...', this.showSelectScreen);
285-
this.setProgressFunc('Loading cache ...', this.loadCache).then(() => {
285+
let startCwC = () => {
286286
// Done.
287287
this.setProgress('Starting Coding with Chrome', 100);
288288
this.loaded = true;
@@ -292,6 +292,12 @@ cwc.ui.Builder.prototype.decorateUI = function() {
292292
this.events_.clear();
293293
this.loadingScreen_.hideSecondsAfterStart(3000);
294294
resolve();
295+
};
296+
this.setProgressFunc('Loading cache ...', this.loadCache).then(() => {
297+
startCwC();
298+
}).catch((error) => {
299+
this.log_.error('Failed to load cache', error);
300+
startCwC();
295301
});
296302
});
297303
};

src/utils/database.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,18 @@ cwc.utils.Database.prototype.open = function(config = this.config_) {
108108
* @param {string} name
109109
* @param {!string|number} content
110110
* @param {string=} group
111+
* @async
111112
*/
112-
cwc.utils.Database.prototype.add = function(name, content, group) {
113-
this.open().then(() => {
113+
cwc.utils.Database.prototype.add = async function(name, content, group) {
114+
await this.open();
115+
await new Promise((resolve, reject) => {
114116
this.log_.info('Add', name, group ? ' in group ' + group : '');
115-
this.getObjectStore_(group)['add'](content, name);
117+
let addRequest = this.getObjectStore_(group)['add'](content, name);
118+
addRequest.onsuccess = resolve;
119+
addRequest.onerror = () => {
120+
this.log_.error('Failed to add', name, 'to group', group);
121+
reject();
122+
};
116123
});
117124
};
118125

@@ -121,16 +128,24 @@ cwc.utils.Database.prototype.add = function(name, content, group) {
121128
* Updates given record, or inserts a new record if not already exist.
122129
* @param {string=} group
123130
*/
124-
cwc.utils.Database.prototype.clear = function(
131+
cwc.utils.Database.prototype.clear = async function(
125132
group = this.defaultObjectStore_) {
126-
this.open().then(() => {
127-
if (this.existObjectStore_(group)) {
128-
this.log_.info('Clear group', group);
129-
this.getObjectStore_(group)['clear']();
130-
} else {
131-
this.log_.warn('ObjectStore', group, 'does not exists!');
132-
}
133-
});
133+
await this.open();
134+
if (this.existObjectStore_(group)) {
135+
this.log_.info('Clear group', group);
136+
let clearRequest = this.getObjectStore_(group)['clear']();
137+
await new Promise((resolve) => {
138+
clearRequest.onsuccess = () => {
139+
resolve();
140+
};
141+
clearRequest.onerror = () => {
142+
this.log_.warn('Failed to clear database');
143+
resolve();
144+
};
145+
});
146+
} else {
147+
this.log_.warn('ObjectStore', group, 'does not exists!');
148+
}
134149
};
135150

136151

@@ -163,14 +178,16 @@ cwc.utils.Database.prototype.get = function(name, group) {
163178
if (!name) {
164179
this.log_.error('Invalid name', name, '!');
165180
}
166-
return new Promise((resolve, reject) => {
167-
let result = this.getObjectStoreReadOnly_(group)['get'](name);
168-
result['onsuccess'] = (e) => {
169-
resolve(e.target.result);
170-
};
171-
result['onerror'] = (e) => {
172-
reject(e);
173-
};
181+
return this.open().then(() => {
182+
return new Promise((resolve, reject) => {
183+
let result = this.getObjectStoreReadOnly_(group)['get'](name);
184+
result['onsuccess'] = (e) => {
185+
resolve(e.target.result);
186+
};
187+
result['onerror'] = (e) => {
188+
reject(e);
189+
};
190+
});
174191
});
175192
};
176193

test/unit_tests/cache/cache_test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @fileoverview Cache tests.
3+
*
4+
* @license Copyright 2018 The Coding with Chrome Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @author [email protected] (Adam Carheden)
19+
*/
20+
goog.require('cwc.Cache');
21+
goog.require('cwc.utils.Helper');
22+
23+
describe('Cache', function() {
24+
let helper;
25+
beforeAll(function() {
26+
helper = new cwc.utils.Helper();
27+
});
28+
29+
let cache;
30+
beforeEach(function() {
31+
cache = new cwc.Cache(helper);
32+
});
33+
34+
it('prepare', function() {
35+
expect(typeof cache).toEqual('object');
36+
});
37+
38+
it('addFile', async function(done) {
39+
let name = 'hello.txt';
40+
let content = 'Hello, World!';
41+
await cache.addFile(name, content);
42+
expect(await cache.getFile(name)).toEqual(content);
43+
done();
44+
});
45+
46+
afterEach(function() {
47+
cache = undefined;
48+
});
49+
});

0 commit comments

Comments
 (0)