Skip to content

Commit 3a42938

Browse files
committed
fix(edward) loadremote (#257)
1 parent f553091 commit 3a42938

File tree

10 files changed

+119
-354
lines changed

10 files changed

+119
-354
lines changed

bower.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"modules"
1818
],
1919
"dependencies": {
20-
"loadremote": "~1.2.0",
2120
"ace-builds": "^1.4.6"
2221
}
2322
}

client/edward.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* global ace */
22
/* global join */
3-
/* global loadRemote */
43

54
'use strict';
65

@@ -10,6 +9,7 @@ const {createPatch} = require('daffy');
109
const exec = require('execon');
1110
const Emitify = require('emitify');
1211
const load = require('load.js');
12+
const loadremote = require('./loadremote');
1313
const wraptile = require('wraptile');
1414
const smalltalk = require('smalltalk');
1515
const jssha = require('jssha');
@@ -110,14 +110,14 @@ Edward.prototype._init = function(fn) {
110110

111111
exec.series([
112112
loadFiles,
113-
(callback) => {
114-
loadRemote('socket', {
113+
async (callback) => {
114+
await loadremote('socket', {
115115
name : 'io',
116116
prefix: this._SOCKET_PATH,
117-
}, (error) => {
118-
!error && initSocket();
119-
callback();
120117
});
118+
119+
initSocket();
120+
callback();
121121
},
122122
async () => {
123123
this._Emitter = Emitify();
@@ -574,9 +574,8 @@ Edward.prototype._loadFiles = function(callback) {
574574
const DIR = this._DIR;
575575

576576
exec.series([
577-
function(callback) {
577+
async function(callback) {
578578
const obj = {
579-
loadRemote : getModulePath('loadremote', 'lib'),
580579
join : '/join/join.js',
581580
};
582581

@@ -588,29 +587,32 @@ Edward.prototype._loadFiles = function(callback) {
588587
return PREFIX + obj[name];
589588
});
590589

591-
exec.if(!scripts.length, callback, () => {
592-
load.parallel(scripts, callback);
593-
});
590+
if (scripts.lengths)
591+
return callback();
592+
593+
await load.parallel(scripts);
594+
callback();
594595
},
595596

596-
function(callback) {
597-
loadRemote.load('ace', {prefix: PREFIX}, callback);
597+
async function(callback) {
598+
await loadremote('ace', {prefix: PREFIX});
599+
callback();
598600
},
599601

600-
function(callback) {
602+
async function(callback) {
601603
const ace = DIR + 'ace-builds/src-min/';
602604
const url = PREFIX + join([
603605
'language_tools',
604606
'searchbox',
605607
'modelist',
606608
].map((name) => {
607609
return 'ext-' + name;
608-
})
609-
.map((name) => {
610-
return ace + name + '.js';
611-
}));
610+
}).map((name) => {
611+
return ace + name + '.js';
612+
}));
612613

613-
load.js(url, callback);
614+
await load.js(url);
615+
callback();
614616
},
615617

616618
function() {

client/loadremote.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
'use strict';
2+
3+
const load = require('load.js');
4+
const tryToCatch = require('try-to-catch');
5+
const once = require('once');
6+
7+
const loadModules = once(async (prefix) => {
8+
const url = prefix + '/modules.json';
9+
return await load.json(url);
10+
});
11+
12+
const loadOptions = once(async (prefix) => {
13+
const url = prefix + '/options.json';
14+
return await load.json(url);
15+
});
16+
17+
const on = async (remote) => {
18+
const [error] = await tryToCatch(load.parallel, remote);
19+
20+
if (error)
21+
await off();
22+
};
23+
24+
const off = async function(local) {
25+
await load.parallel(local);
26+
};
27+
28+
module.exports = async (name, options = {}) => {
29+
const o = options;
30+
31+
const {
32+
prefix = '',
33+
} = o;
34+
35+
if (o.name && global[o.name])
36+
return;
37+
38+
const promises = [
39+
loadModules(prefix),
40+
loadOptions(prefix),
41+
];
42+
43+
const [error, [modules, config]] = await tryToCatch(Promise.all.bind(Promise), promises);
44+
45+
let remoteTmpls;
46+
let local;
47+
48+
if (error)
49+
return alert(`Error: could not load module or config: ${error.message}`);
50+
51+
const online = config.online && navigator.onLine;
52+
const module = binom(name, modules);
53+
const isArray = Array.isArray(module.local);
54+
const {version} = module;
55+
56+
if (isArray) {
57+
remoteTmpls = module.remote;
58+
local = module.local;
59+
} else {
60+
remoteTmpls = [module.remote];
61+
local = [module.local];
62+
}
63+
64+
local = local.map((url) => {
65+
return prefix + url;
66+
});
67+
68+
const remote = remoteTmpls.map((tmpl) => {
69+
return tmpl.replace(/{{\sversion\s}}/g, version);
70+
});
71+
72+
if (!online)
73+
return await off(local);
74+
75+
await on(remote);
76+
};
77+
78+
function binom(name, array) {
79+
let ret;
80+
81+
if (typeof name !== 'string')
82+
throw Error('name should be string!');
83+
84+
if (!Array.isArray(array))
85+
throw Error('array should be array!');
86+
87+
array.some((item) => {
88+
const is = item.name === name;
89+
90+
if (is)
91+
ret = item;
92+
93+
return is;
94+
});
95+
96+
return ret;
97+
}
98+

modules/loadremote/.bower.json

Lines changed: 0 additions & 37 deletions
This file was deleted.

modules/loadremote/ChangeLog

Lines changed: 0 additions & 23 deletions
This file was deleted.

modules/loadremote/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

modules/loadremote/README.md

Lines changed: 0 additions & 53 deletions
This file was deleted.

modules/loadremote/bower.json

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)