-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix connection issue in service worker
- Loading branch information
1 parent
dd7c5f1
commit 25d2ae6
Showing
4 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
describe('Test service worker', function () { | ||
it('register service worker', function () { | ||
var __waitForSWState = function(registration, desiredState) { | ||
return new Promise((resolve, reject) => { | ||
let serviceWorker = registration.installing; | ||
|
||
if (!serviceWorker) { | ||
return reject(new Error('The service worker is not installing. ' + | ||
'Is the test environment clean?')); | ||
} | ||
|
||
const stateListener = (evt) => { | ||
if (evt.target.state === desiredState) { | ||
serviceWorker.removeEventListener('statechange', stateListener); | ||
return resolve(); | ||
} | ||
|
||
if (evt.target.state === 'redundant') { | ||
serviceWorker.removeEventListener('statechange', stateListener); | ||
|
||
return reject(new Error('Installing service worker became redundant')); | ||
} | ||
}; | ||
|
||
serviceWorker.addEventListener('statechange', stateListener); | ||
}); | ||
} | ||
return navigator.serviceWorker.register("/test/static/sw.js") | ||
.then(function (reg ) { | ||
return __waitForSWState(reg,'installed'); | ||
}) | ||
|
||
}) | ||
|
||
it('check if connection is created',function(){ | ||
window.caches.match('/__test/example') | ||
.then((response) => { | ||
if (!response) { | ||
throw new Error('Eek, no response was found in the cache.'); | ||
} | ||
|
||
return response.text(); | ||
}) | ||
.then((responseText) => { | ||
if (responseText !== 'Hello, World!') { | ||
throw new Error(`The response text was wrong!: '${responseText}'`); | ||
} | ||
}); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
importScripts('/output/jsstore.js'); | ||
importScripts('/output/jsstore.worker.js'); | ||
|
||
self.addEventListener('fetch', function (event) { | ||
console.log("fetch event:", event.request.url); | ||
}); | ||
|
||
var dbName = "ServiceWorkerDemo"; | ||
|
||
function initDb() { | ||
var connection = new JsStore.Connection(); | ||
return connection.initDb(getDbSchema()).then(function(isDbCreated){ | ||
if (isDbCreated) { | ||
console.log('db created'); | ||
} | ||
else { | ||
console.log('db opened'); | ||
} | ||
return connection; | ||
}) | ||
} | ||
|
||
function getDbSchema() { | ||
var table = { | ||
name: 'Student', | ||
columns: { | ||
id: { | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
name: { | ||
notNull: true, | ||
dataType: 'string' | ||
}, | ||
gender: { | ||
dataType: 'string', | ||
default: 'male' | ||
}, | ||
country: { | ||
notNull: true, | ||
dataType: 'string' | ||
}, | ||
city: { | ||
dataType: 'string', | ||
notNull: true | ||
} | ||
} | ||
} | ||
|
||
var db = { | ||
name: dbName, | ||
tables: [table] | ||
} | ||
return db; | ||
} | ||
|
||
// throw "ss" | ||
|
||
self.addEventListener('install', (event) => { | ||
const promiseChain = caches.open('test-cache') | ||
.then((openCache) => { | ||
initDb().then(function (connection) { | ||
return connection.terminate(); | ||
}).then(() => { | ||
return openCache.put( | ||
new Request('/__test/example'), | ||
new Response('Hello, World!') | ||
); | ||
}) | ||
}); | ||
event.waitUntil(promiseChain); | ||
}); |