Skip to content

Commit dd67133

Browse files
committed
impl lightning worker component / add proxy js resource
1 parent ac5d100 commit dd67133

File tree

6 files changed

+118
-7
lines changed

6 files changed

+118
-7
lines changed
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
<aura:component >
2-
<aura:attribute name="proxy" type="String" required="true" default="" />
3-
<lightning:container class="invisible" src="{!v.proxy}" />
1+
<aura:component access="global">
2+
<aura:attribute name="proxy" type="String" required="true" default="" access="global" />
3+
<aura:handler name="init" value="{!this}" action="{!c.init}" />
4+
<ltng:require
5+
scripts="{!$Resource.LightningWebWorkerProxyJS + '/client.js'}"
6+
afterScriptsLoaded="{!c.initScript}"
7+
/>
8+
<lightning:container
9+
aura:id="lightning-container"
10+
class="invisible"
11+
src="{!v.proxy}"
12+
onmessage="{!c.handleMessage}"
13+
onerror="{!c.handleError}"
14+
/>
415
</aura:component>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
.THIS {
2-
width: 0;
3-
height: 0;
2+
width: 0px;
3+
height: 0px;
44
}
55
.THIS iframe.invisible {
6-
width: 0;
7-
height: 0;
6+
width: 0px;
7+
height: 0px;
88
outline: none;
99
visibility: hidden;
1010
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
({
2+
init: function (cmp, evt, helper) {
3+
window.Worker = helper.declareWorker(cmp);
4+
},
5+
initScript: function (cmp, evt, helper) {
6+
helper.scriptLoaded = true;
7+
if (helper.containerLoaded) {
8+
helper.initWorker(cmp);
9+
}
10+
},
11+
handleMessage: function (cmp, evt, helper) {
12+
var payload = evt.getParams().payload;
13+
if (payload.name === "server:ready" && !helper.containerLoaded) {
14+
helper.containerLoaded = true;
15+
if (helper.scriptLoaded) {
16+
helper.initWorker(cmp);
17+
}
18+
} else if (payload.name === "server:message") {
19+
helper.dispatchWorkerMessage(cmp, payload.value);
20+
}
21+
},
22+
handleError: function () {
23+
console.log("handleError", arguments);
24+
},
25+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
({
2+
scriptLoaded: false,
3+
4+
containerLoaded: false,
5+
6+
declareWorker: function (cmp) {
7+
var id = 0;
8+
window._ltngWebWorkers = {};
9+
var container = cmp.find("lightning-container");
10+
var Worker = function (url, options) {
11+
var instanceId = String(++id);
12+
this.instanceId = instanceId;
13+
this.url = url;
14+
this.options = options;
15+
this.requestQueue = [];
16+
this.client = null;
17+
// if the worker client script is already loaded
18+
if (window.LightningWebWorkerClient) {
19+
this.client = LightningWebWorkerClient.create(this, container);
20+
}
21+
window._ltngWebWorkers[instanceId] = this;
22+
};
23+
Worker.prototype.postMessage = function () {
24+
if (this.client) {
25+
this.client.postMessage.apply(this.client, arguments);
26+
} else {
27+
this.requestQueue.push(["postMessage", arguments]);
28+
}
29+
};
30+
Worker.prototype.addEventListener = function () {
31+
if (this.client) {
32+
this.client.addEventListener.apply(this.client, arguments);
33+
} else {
34+
this.requestQueue.push(["addEventListener", arguments]);
35+
}
36+
};
37+
return Worker;
38+
},
39+
40+
initWorker: function (cmp) {
41+
if (!window.LightningWebWorkerClient) {
42+
console.error("load error: LightningWebWorkerClient is not found");
43+
return;
44+
}
45+
var container = cmp.find("lightning-container");
46+
container.message({ name: "worker:init", value: Date.now().toString() });
47+
for (var instanceId in window._ltngWebWorkers) {
48+
var instance = window._ltngWebWorkers[instanceId];
49+
if (!instance.client) {
50+
instance.client = LightningWebWorkerClient.create(instance, container);
51+
}
52+
}
53+
},
54+
55+
dispatchWorkerMessage: function (cmp, message) {
56+
if (!window.LightningWebWorkerClient) {
57+
console.error("load error: LightningWebWorkerClient is not found");
58+
return;
59+
}
60+
const instanceId = message.instanceId;
61+
var instance = window._ltngWebWorkers[instanceId];
62+
var e = new MessageEvent(message.type, {
63+
data: message.data,
64+
origin: "",
65+
});
66+
instance.client.receiveEvent(e);
67+
},
68+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<StaticResource xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<cacheControl>Private</cacheControl>
4+
<contentType>application/zip</contentType>
5+
</StaticResource>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.js
2+
*.js.map

0 commit comments

Comments
 (0)