This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1159474 - merge pull request #29797 from gaye:bug-1159474-db.js-i…
…n-worker to mozilla-b2g:master
- Loading branch information
Showing
141 changed files
with
5,250 additions
and
256 deletions.
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
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
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 |
---|---|---|
@@ -1,88 +1,191 @@ | ||
define(function(require, exports, module) { | ||
'use strict'; | ||
importScripts('../ext/alameda.js'); | ||
|
||
require.config({ | ||
baseUrl: '/js/backend', | ||
paths: { | ||
ext: '/js/ext' | ||
} | ||
}); | ||
|
||
require(['ext/threads'], threads => { | ||
var service = threads.service('calendar'); | ||
var Db = require('db'); | ||
var co = require('ext/co'); | ||
//var object = require('common/object'); | ||
var threads = require('ext/threads'); | ||
|
||
service.method('start', () => { | ||
// TODO | ||
console.log('start'); | ||
}); | ||
|
||
service.method('accounts/create', details => { | ||
// TODO | ||
console.log('accounts/create', details); | ||
}); | ||
var db = new Db('b2g-calendar'); | ||
var service = threads.service('calendar'); | ||
var loadDb; | ||
|
||
service.method('accounts/update', details => { | ||
// TODO | ||
console.log('accounts/update', details); | ||
}); | ||
|
||
service.method('accounts/remove', account => { | ||
// TODO | ||
console.log('accounts/remove', account); | ||
}); | ||
|
||
service.method('accounts/sync', account => { | ||
// TODO | ||
console.log('accounts/sync', account); | ||
}); | ||
|
||
service.method('events/create', (calendarId, details) => { | ||
// TODO | ||
console.log('events/create', calendarId, details); | ||
}); | ||
|
||
service.method('events/update', event => { | ||
// TODO | ||
console.log('events/update', event); | ||
}); | ||
function start() { | ||
if (loadDb != null) { | ||
return loadDb; | ||
} | ||
|
||
service.method('events/remove', event => { | ||
// TODO | ||
console.log('events/remove', event); | ||
loadDb = db.load(); | ||
return loadDb; | ||
} | ||
|
||
function method(endpoint, handler) { | ||
service.method(endpoint, () => { | ||
var args = Array.slice(arguments); | ||
return co(function *() { | ||
yield start(); | ||
return handler.apply(null, args); | ||
}); | ||
}); | ||
} | ||
|
||
service.method('settings/set', (start, end) => { | ||
// TODO | ||
console.log('settings/set', start, end); | ||
function stream(endpoint, handler) { | ||
service.stream(endpoint, function *() { | ||
var args = Array.slice(arguments); | ||
yield start(); | ||
handler.apply(null, args); | ||
}); | ||
|
||
service.stream('accounts/list', stream => { | ||
// TODO | ||
console.log('accounts/list', stream); | ||
} | ||
|
||
function echo() { | ||
return Array.slice(arguments); | ||
} | ||
|
||
/** | ||
* This will not work until the sync controller | ||
* (1) is not a class anymore | ||
* (2) doesn't need the app ns/object | ||
*/ | ||
function saveAccount(details) { | ||
return co(function *() { | ||
var store = db.getStore('Account'); | ||
var account; | ||
try { | ||
account = yield store.verifyAndPersist(details); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
|
||
yield syncAccount(account); | ||
return account; | ||
}); | ||
} | ||
|
||
service.stream('accounts/get', (stream, id) => { | ||
// TODO | ||
console.log('accounts/get', stream, id); | ||
function removeAccount(id) { | ||
return co(function *() { | ||
var store = db.getStore('Account'); | ||
yield store.remove(id); | ||
}); | ||
|
||
service.stream('calendars/list', stream => { | ||
// TODO | ||
console.log('calendars/list', stream); | ||
} | ||
|
||
/** | ||
* This will not work until the sync controller | ||
* (1) is not a class anymore | ||
* (2) doesn't need the app ns/object | ||
*/ | ||
function syncAccount(account) { | ||
console.log('syncAccount', account); | ||
/* | ||
return co(function *() { | ||
var store = db.getStore('Calendar'); | ||
var calendars; | ||
try { | ||
calendars = yield store.remotesByAccount(account._id); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
var syncCalendar = syncController.calendar.bind(null, account); | ||
yield Promise.all(object.map(calendars, syncCalendar)); | ||
}); | ||
|
||
service.stream('events/get', (stream, id) => { | ||
// TODO | ||
console.log('events/get', stream, id); | ||
*/ | ||
} | ||
|
||
function saveEvent(exists, event) { | ||
return co(function *() { | ||
var store = db.getStore('Event'); | ||
var provider = yield store.providerFor(event); | ||
var capabilities; | ||
try { | ||
capabilities = yield provider.eventCapabilities(event.data); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
|
||
var capability = exists ? 'canUpdate' : 'canCreate'; | ||
if (!capabilities[capability]) { | ||
return Promise.reject(new Error('User not allowed to perform operation')); | ||
} | ||
|
||
var method = exists ? 'updateEvent' : 'createEvent'; | ||
try { | ||
yield provider[method](event.data); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}); | ||
|
||
service.stream('busytimes/list', (stream, day) => { | ||
// TODO | ||
console.log('busytimes/list', stream, day); | ||
} | ||
|
||
function removeEvent(event) { | ||
return co(function *() { | ||
var store = db.getStore('Event'); | ||
var provider = yield store.providerFor(event); | ||
var capabilities; | ||
try { | ||
yield provider.eventCapabilities(event.data); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
|
||
if (!capabilities.canDelete) { | ||
return Promise.reject(new Error('User not allowed to perform operation')); | ||
} | ||
|
||
yield provider.deleteEvent(event.data); | ||
}); | ||
} | ||
|
||
function setSetting(key, value) { | ||
var store = db.getStore('Setting'); | ||
return store.set(key, value); | ||
} | ||
|
||
function listAccounts(stream) { | ||
// TODO | ||
console.log('accounts/list', stream); | ||
} | ||
|
||
function getAccount(stream, id) { | ||
// TODO | ||
console.log('accounts/get', stream, id); | ||
} | ||
|
||
function listCalendars(stream) { | ||
// TODO | ||
console.log('calendars/list', stream); | ||
} | ||
|
||
function getEvent(stream, id) { | ||
// TODO | ||
console.log('events/get', stream, id); | ||
} | ||
|
||
function listBusytimes(stream, day) { | ||
// TODO | ||
console.log('busytimes/list', stream, day); | ||
} | ||
|
||
function getSetting(stream, key) { | ||
// TODO | ||
console.log('settings/set', stream, key); | ||
} | ||
|
||
method('echo', echo); | ||
method('accounts/create', saveAccount); | ||
method('accounts/update', saveAccount); | ||
method('accounts/remove', removeAccount); | ||
method('accounts/sync', syncAccount); | ||
method('events/create', saveEvent.bind(null, true)); | ||
method('events/update', saveEvent.bind(null, true)); | ||
method('events/remove', removeEvent); | ||
method('settings/set', setSetting); | ||
stream('accounts/list', listAccounts); | ||
stream('accounts/get', getAccount); | ||
stream('calendars/list', listCalendars); | ||
stream('events/get', getEvent); | ||
stream('busytimes/list', listBusytimes); | ||
stream('settings/get', getSetting); | ||
|
||
exports.start = start; | ||
|
||
service.stream('settings/get', (stream, key) => { | ||
// TODO | ||
console.log('settings/set', stream, key); | ||
}); | ||
}); |
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,12 @@ | ||
'use strict'; | ||
importScripts('../ext/alameda.js'); | ||
|
||
require.config({ | ||
baseUrl: '/js/backend', | ||
paths: { | ||
common: '/js/common', | ||
ext: '/js/ext' | ||
} | ||
}); | ||
|
||
require(['calendar_service'], service => service.start()); |
Oops, something went wrong.