This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
sw.js
69 lines (59 loc) · 2.14 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Service Worker
"use strict";
importScripts('common/common.js');
importScripts('polyfill/ballista-polyfill.js');
// Opens |file| for editing in an external editor.
function editFile(file, port) {
navigator.actions.performAction(
{verb: 'open', bidirectional: true, type: file.type}, {file: file})
.then(action => {
console.log('Action started:', action);
port.postMessage({type: 'update', openState: true});
var onUpdate = event => {
// Only respond to updates to the current action.
if (event.id != action.id)
return;
// Can be called multiple times for a single action.
// |event.data.file| is a new File with updated text.
port.postMessage(
{type: 'update', openState: !event.done, file: event.data.file});
if (event.done) {
console.log('Action completed:', action);
navigator.actions.removeEventListener('update', onUpdate);
} else {
console.log('Action updated:', action);
}
};
navigator.actions.addEventListener('update', onUpdate);
});
}
// For testing/debugging purposes: send an "update" event to an action with a
// dummy file contents.
function debugCloseAction(id) {
var evt = new Event('update');
evt.id = id;
evt.data = {};
var contents = 'Updated file contents.';
evt.data.file = new File([contents], '');
evt.done = true;
navigator.actions.dispatchEvent(evt);
}
self.addEventListener('message', event => {
var data = event.data;
var type = data.type;
if (type == 'open')
editFile(data.file, data.port);
});