Skip to content

Commit 06afbd5

Browse files
committed
Improved internal HTTP server and moved static files into Cache instance.
This change introduce the "cache" which is only filled / updated if the local cache version is lower then the current version. The cache is used by the HTTP server and several other components. The main benefit of this change is the lower payload for the sandbox (some urls instead of lots of data) and the faster loading of the application. The cache is using IndexDB for storing the data which also lowers the memory usage for storing these data.
1 parent e25d3dd commit 06afbd5

File tree

21 files changed

+444
-281
lines changed

21 files changed

+444
-281
lines changed

build/cwc/gss.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,16 @@ closureBuilder.build({
4545
]),
4646
out: 'genfiles/core/css/dialog.css',
4747
});
48+
49+
50+
/**
51+
* Runner Style Sheets files
52+
*/
53+
closureBuilder.build({
54+
name: 'CwC runner Style Sheet',
55+
prefix: 'cwc-',
56+
srcs: glob([
57+
'src/frameworks/internal/runner/runner.gss',
58+
]),
59+
out: 'genfiles/core/css/runner.css',
60+
});

build/externs/chrome.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,18 @@ chrome.sockets.tcp.onReceive = function() {};
194194
/** @type {Function} */
195195
chrome.sockets.tcp.onReceive.addListener = function() {};
196196

197+
/** @type {Function} */
198+
chrome.sockets.tcp.onReceive.removeListener = function() {};
199+
197200
/** @type {Function} */
198201
chrome.sockets.tcp.onReceiveError = function() {};
199202

200203
/** @type {Function} */
201204
chrome.sockets.tcp.onReceiveError.addListener = function() {};
202205

206+
/** @type {Function} */
207+
chrome.sockets.tcp.onReceiveError.removeListener = function() {};
208+
203209
/** @type {Function} */
204210
chrome.sockets.tcp.setKeepAlive = function() {};
205211

@@ -215,9 +221,15 @@ chrome.sockets.tcpServer.create = function() {};
215221
/** @type {Function} */
216222
chrome.sockets.tcpServer.onAccept.addListener = function() {};
217223

224+
/** @type {Function} */
225+
chrome.sockets.tcpServer.onAccept.removeListener = function() {};
226+
218227
/** @type {Function} */
219228
chrome.sockets.tcpServer.onAcceptError.addListener = function() {};
220229

230+
/** @type {Function} */
231+
chrome.sockets.tcpServer.onAcceptError.removeListener = function() {};
232+
221233

222234
/** @type {Function} */
223235
chrome.system = function() {};

src/cache/cache.js

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ cwc.Cache = function(helper) {
5757
this.log_ = new cwc.utils.Logger(this.name);
5858

5959
/** @private {!string} */
60-
this.version_ = '2';
60+
this.version_ = '3';
6161
};
6262

6363

@@ -77,10 +77,13 @@ cwc.Cache.prototype.prepare = function() {
7777
* @param {!string|number} version
7878
*/
7979
cwc.Cache.prototype.update = function(version) {
80-
if (this.version_ >= version) {
81-
this.log_.info('No need for updates ...');
80+
if (version && this.version_ <= version) {
81+
this.log_.info('No updates needed', version, '>=', this.version_);
82+
return;
8283
}
84+
8385
this.log_.info('Updating Cache to version', this.version_);
86+
this.database_.clearFiles();
8487

8588
this.log_.info('Loading external frameworks ...');
8689
this.loadFiles(cwc.framework.External);
@@ -121,40 +124,13 @@ cwc.Cache.prototype.loadFiles = function(files) {
121124
/**
122125
* @param {string!} name
123126
* @param {string!} content
124-
* @param {boolean=} optimize
125127
*/
126-
cwc.Cache.prototype.addFile = function(name, content, optimize = false) {
128+
cwc.Cache.prototype.addFile = function(name, content) {
127129
if (!content) {
128130
this.log_.error('Received empty content for', name);
129131
return;
130132
}
131-
132-
// Add file to server instance if available.
133-
let serverInstance = this.helper.getInstance('server');
134-
if (serverInstance) {
135-
serverInstance.addFile(name, content);
136-
}
137-
138-
if (optimize && !name.includes('.min.') && content.length > 1000) {
139-
// Try to optimize unminimized code by removing comments and white-spaces.
140-
let originalContentLength = content.length;
141-
content = cwc.Cache.optimizeContent(content);
142-
if (originalContentLength > content.length) {
143-
let optimized = Math.ceil(((originalContentLength - content.length) /
144-
originalContentLength) * 100);
145-
if (optimized >= 5) {
146-
this.log_.info('Optimized content from', originalContentLength, 'to',
147-
content.length, 'by', optimized, '%');
148-
}
149-
}
150-
}
151-
let mimeType = cwc.utils.mime.getTypeByExtension(name);
152-
let fileContent = this.rendererHelper.getDataUrl(content, mimeType);
153-
if (!fileContent) {
154-
this.log_.error('Received empty file for', name);
155-
return;
156-
}
157-
this.database_.addFile(name, fileContent);
133+
this.database_.addFile(name, content);
158134
};
159135

160136

src/file/file.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ cwc.file.File = function(name, content, type, size) {
5252
};
5353

5454

55+
/**
56+
* @param {!string} content
57+
*/
58+
cwc.file.File.prototype.setContent = function(content) {
59+
this.content_ = content;
60+
};
61+
62+
5563
/**
5664
* @return {string}
5765
*/
@@ -60,6 +68,17 @@ cwc.file.File.prototype.getContent = function() {
6068
};
6169

6270

71+
/**
72+
* @return {string}
73+
*/
74+
cwc.file.File.prototype.getRawContent = function() {
75+
if (this.content_.includes('data:')) {
76+
return atob(this.content_.split(',')[1]);
77+
}
78+
return atob(this.content_);
79+
};
80+
81+
6382
/**
6483
* @return {string}
6584
*/

src/file_format/file_format.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ cwc.fileFormat.File.prototype.setFilesData = function(data) {
222222
};
223223

224224

225+
/**
226+
* @return {Object}
227+
*/
228+
cwc.fileFormat.File.prototype.getFileData = function() {
229+
return this.files_.getFiles();
230+
};
231+
232+
225233
/**
226234
* @return {!cwc.file.Files}
227235
*/

src/file_handler/file_loader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ cwc.fileHandler.FileLoader.prototype.loadCWCFile = function(file,
211211
}
212212

213213
// Handle library files
214-
// let cacheInstance = this.helper.getInstance('cache');
214+
let cacheInstance = this.helper.getInstance('cache');
215+
cacheInstance.clearLibraryFiles();
216+
let fileContent = file.getFileData();
217+
for (let entry in fileContent) {
218+
if (Object.prototype.hasOwnProperty.call(fileContent, entry)) {
219+
let content = fileContent[entry];
220+
cacheInstance.addLibraryFile(content.getName(), content.getRawContent());
221+
}
222+
}
215223

216224
modeInstance.postMode();
217225
};

src/frameworks/frameworks.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ goog.provide('cwc.framework.StyleSheet');
3131
*/
3232
cwc.framework.External = {
3333
BRYTHON: {
34-
CORE: 'frameworks/external/brython.js',
35-
STDLIB: 'frameworks/external/brython_stdlib.js',
34+
CORE: '/frameworks/external/brython.js',
35+
STDLIB: '/frameworks/external/brython_stdlib.js',
3636
},
37-
COFFEESCRIPT: 'frameworks/external/coffee-script.js',
37+
COFFEESCRIPT: '/frameworks/external/coffee-script.js',
3838
JQUERY: {
39-
V3_X: 'frameworks/external/jquery.min.js',
40-
V2_2_4: 'frameworks/external/jquery-2.2.4.min.js',
39+
V3_X: '/frameworks/external/jquery.min.js',
40+
V2_2_4: '/frameworks/external/jquery-2.2.4.min.js',
4141
},
42-
JQUERY_TURTLE: 'frameworks/external/jquery-turtle.js',
43-
PHASER: 'frameworks/external/phaser.min.js',
42+
JQUERY_TURTLE: '/frameworks/external/jquery-turtle.js',
43+
PHASER: '/frameworks/external/phaser.min.js',
4444
SKULPT: {
45-
CORE: 'frameworks/external/skulpt.min.js',
46-
STDLIB: 'frameworks/external/skulpt-stdlib.js',
45+
CORE: '/frameworks/external/skulpt.min.js',
46+
STDLIB: '/frameworks/external/skulpt-stdlib.js',
4747
},
4848
THREE_JS: {
49-
CORE: 'frameworks/external/three.min.js',
49+
CORE: '/frameworks/external/three.min.js',
5050
},
5151
};
5252

@@ -56,18 +56,18 @@ cwc.framework.External = {
5656
* @enum {!Object.<string>|string}
5757
*/
5858
cwc.framework.Internal = {
59-
ARDUINO: 'frameworks/internal/arduino_framework.js',
60-
EV3: 'frameworks/internal/ev3_framework.js',
61-
MBOT: 'frameworks/internal/mbot_framework.js',
62-
MBOT_RANGER: 'frameworks/internal/mbot_ranger_framework.js',
63-
PHASER: 'frameworks/internal/phaser_framework.js',
64-
PYTHON2: 'frameworks/internal/python2_framework.js',
65-
PYTHON3: 'frameworks/internal/python3_framework.js',
66-
RASPBERRY_PI: 'frameworks/internal/raspberry_pi_framework.js',
67-
RUNNER: 'frameworks/internal/runner_framework.js',
68-
SIMPLE: 'frameworks/internal/simple_framework.js',
69-
SPHERO: 'frameworks/internal/sphero_framework.js',
70-
TURTLE: 'frameworks/internal/turtle_framework.js',
59+
ARDUINO: '/frameworks/internal/arduino_framework.js',
60+
EV3: '/frameworks/internal/ev3_framework.js',
61+
MBOT: '/frameworks/internal/mbot_framework.js',
62+
MBOT_RANGER: '/frameworks/internal/mbot_ranger_framework.js',
63+
PHASER: '/frameworks/internal/phaser_framework.js',
64+
PYTHON2: '/frameworks/internal/python2_framework.js',
65+
PYTHON3: '/frameworks/internal/python3_framework.js',
66+
RASPBERRY_PI: '/frameworks/internal/raspberry_pi_framework.js',
67+
RUNNER: '/frameworks/internal/runner_framework.js',
68+
SIMPLE: '/frameworks/internal/simple_framework.js',
69+
SPHERO: '/frameworks/internal/sphero_framework.js',
70+
TURTLE: '/frameworks/internal/turtle_framework.js',
7171
};
7272

7373

@@ -76,5 +76,6 @@ cwc.framework.Internal = {
7676
* @enum {!Object.<string>|string}
7777
*/
7878
cwc.framework.StyleSheet = {
79-
DIALOG: 'css/dialog.css',
79+
DIALOG: '/css/dialog.css',
80+
RUNNER: '/css/runner.css',
8081
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2018 The Coding with Chrome Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
* {
19+
margin:0;
20+
padding:0;
21+
}
22+
23+
html, body {
24+
width:100%;
25+
height:100%;
26+
}
27+
28+
body {
29+
overflow: hidden;
30+
}
31+
32+
canvas {
33+
display: block;
34+
}
35+
36+
#runner:before {
37+
content: '';
38+
box-sizing: border-box;
39+
position: absolute;
40+
top: 3px;
41+
left: 5px;
42+
width: 40px;
43+
height: 40px;
44+
border-radius: 50%;
45+
border: 4px solid transparent;
46+
border-top-color: rgb(63, 81, 181);
47+
border-bottom-color: rgb(63, 81, 181);
48+
animation: spinner 1s infinite;
49+
}
50+
51+
@keyframes spinner {
52+
to {transform: rotate(360deg);}
53+
}

0 commit comments

Comments
 (0)