Convert browser module to use ES6 modules

This commit is contained in:
Scott Kyle 2016-02-28 12:36:30 -08:00
parent 44a29d8dd4
commit b1f656a252
11 changed files with 72 additions and 122 deletions

View File

@ -1,13 +1,5 @@
{
"env": {
"commonjs": true,
"browser": true,
"es6": true
},
"ecmaFeatures": {
"forOf": false
},
"globals": {
"global": true
"commonjs": true
}
}

18
lib/browser/.eslintrc Normal file
View File

@ -0,0 +1,18 @@
{
"env": {
"es6": true,
"worker": true
},
"globals": {
"global": true
},
"parserOptions": {
"ecmaFeatures": {
"forOf": false
},
"sourceType": "module"
},
"rules": {
"strict": 0
}
}

View File

@ -23,12 +23,7 @@ const CHAR_MAP = {};
Array.from(CHARS, (char, i) => CHAR_MAP[char] = i);
module.exports = {
decode,
encode,
};
function decode(base64) {
export function decode(base64) {
let length = base64.length;
let byteCount = length * 0.75;
@ -56,7 +51,7 @@ function decode(base64) {
return buffer;
}
function encode(data) {
export function encode(data) {
var byteOffset = 0;
var buffer;

View File

@ -18,9 +18,9 @@
'use strict';
let keys = {};
let objectTypes = {};
let propTypes = {};
export const keys = {};
export const objectTypes = {};
export const propTypes = {};
[
'id',
@ -62,9 +62,3 @@ let propTypes = {};
enumerable: true,
});
});
module.exports = {
keys,
objectTypes,
propTypes
};

View File

@ -18,21 +18,20 @@
'use strict';
const constants = require('./constants');
const lists = require('./lists');
const objects = require('./objects');
const results = require('./results');
const rpc = require('./rpc');
const util = require('./util');
import { keys, propTypes, objectTypes } from './constants';
import * as lists from './lists';
import * as objects from './objects';
import * as results from './results';
import * as rpc from './rpc';
import * as util from './util';
const {keys, propTypes, objectTypes} = constants;
const listenersKey = Symbol();
rpc.registerTypeConverter(objectTypes.LIST, lists.create);
rpc.registerTypeConverter(objectTypes.OBJECT, objects.create);
rpc.registerTypeConverter(objectTypes.RESULTS, results.create);
class Realm {
export default class Realm {
constructor(config) {
let schemas = typeof config == 'object' && config.schema;
let constructors = {};
@ -165,5 +164,3 @@ Object.defineProperties(Realm, {
// The session ID refers to the Realm constructor object in the RPC server.
Realm[keys.id] = rpc.createSession();
module.exports = Realm;

View File

@ -18,27 +18,20 @@
'use strict';
const constants = require('./constants');
const util = require('./util');
const {objectTypes} = constants;
class list {}
module.exports = {
List,
create,
};
import { objectTypes } from './constants';
import { createList, createMethods } from './util';
export class List {}
// Non-mutating methods:
util.createMethods(List.prototype, objectTypes.LIST, [
createMethods(List.prototype, objectTypes.LIST, [
'filtered',
'sorted',
'snapshot',
]);
// Mutating methods:
util.createMethods(List.prototype, objectTypes.LIST, [
createMethods(List.prototype, objectTypes.LIST, [
'pop',
'shift',
'push',
@ -46,6 +39,6 @@ util.createMethods(List.prototype, objectTypes.LIST, [
'splice',
], true);
function create(realmId, info) {
return util.createList(List.prototype, realmId, info, true);
export function create(realmId, info) {
return createList(List.prototype, realmId, info, true);
}

View File

@ -18,18 +18,12 @@
'use strict';
const constants = require('./constants');
const util = require('./util');
import { keys } from './constants';
import { getterForProperty, setterForProperty } from './util';
const {keys} = constants;
const registeredConstructors = {};
module.exports = {
create,
registerConstructors,
};
function create(realmId, info) {
export function create(realmId, info) {
let schema = info.schema;
let constructor = (registeredConstructors[realmId] || {})[schema.name];
let object = constructor ? Object.create(constructor.prototype) : {};
@ -41,8 +35,8 @@ function create(realmId, info) {
schema.properties.forEach((name) => {
Object.defineProperty(object, name, {
enumerable: true,
get: util.getterForProperty(name),
set: util.setterForProperty(name),
get: getterForProperty(name),
set: setterForProperty(name),
});
});
@ -56,6 +50,6 @@ function create(realmId, info) {
return object;
}
function registerConstructors(realmId, constructors) {
export function registerConstructors(realmId, constructors) {
registeredConstructors[realmId] = constructors;
}

View File

@ -18,23 +18,17 @@
'use strict';
const constants = require('./constants');
const util = require('./util');
import { objectTypes } from './constants';
import { createList, createMethods } from './util';
class Results {}
export class Results {}
module.exports = {
Results,
create,
};
util.createMethods(Results.prototype, constants.objectTypes.RESULTS, [
createMethods(Results.prototype, objectTypes.RESULTS, [
'filtered',
'sorted',
'snapshot',
]);
function create(realmId, info) {
return util.createList(Results.prototype, realmId, info);
export function create(realmId, info) {
return createList(Results.prototype, realmId, info);
}

View File

@ -18,12 +18,11 @@
'use strict';
const base64 = require('./base64');
const constants = require('./constants');
import * as base64 from './base64';
import { keys, objectTypes } from './constants';
const DEVICE_HOST = 'localhost:8082';
const {keys, objectTypes} = constants;
const {id: idKey, realm: realmKey} = keys;
const typeConverters = {};
@ -38,35 +37,20 @@ if (XMLHttpRequest.__proto__ != global.XMLHttpRequestEventTarget) {
global.XMLHttpRequest = fakeXMLHttpRequest;
}
module.exports = {
registerTypeConverter,
createSession,
createRealm,
callMethod,
getProperty,
setProperty,
beginTransaction,
cancelTransaction,
commitTransaction,
clearTestState,
};
registerTypeConverter(objectTypes.DATA, (_, {value}) => base64.decode(value));
registerTypeConverter(objectTypes.DATE, (_, {value}) => new Date(value));
registerTypeConverter(objectTypes.DICT, deserializeDict);
function registerTypeConverter(type, handler) {
export function registerTypeConverter(type, handler) {
typeConverters[type] = handler;
}
function createSession() {
export function createSession() {
sessionId = sendRequest('create_session');
return sessionId;
}
function createRealm(args) {
export function createRealm(args) {
if (args) {
args = args.map((arg) => serialize(null, arg));
}
@ -74,7 +58,7 @@ function createRealm(args) {
return sendRequest('create_realm', {arguments: args});
}
function callMethod(realmId, id, name, args) {
export function callMethod(realmId, id, name, args) {
if (args) {
args = args.map((arg) => serialize(realmId, arg));
}
@ -83,29 +67,29 @@ function callMethod(realmId, id, name, args) {
return deserialize(realmId, result);
}
function getProperty(realmId, id, name) {
export function getProperty(realmId, id, name) {
let result = sendRequest('get_property', {realmId, id, name});
return deserialize(realmId, result);
}
function setProperty(realmId, id, name, value) {
export function setProperty(realmId, id, name, value) {
value = serialize(realmId, value);
sendRequest('set_property', {realmId, id, name, value});
}
function beginTransaction(realmId) {
export function beginTransaction(realmId) {
sendRequest('begin_transaction', {realmId});
}
function cancelTransaction(realmId) {
export function cancelTransaction(realmId) {
sendRequest('cancel_transaction', {realmId});
}
function commitTransaction(realmId) {
export function commitTransaction(realmId) {
sendRequest('commit_transaction', {realmId});
}
function clearTestState() {
export function clearTestState() {
sendRequest('clear_test_state');
}

View File

@ -18,22 +18,11 @@
'use strict';
const constants = require('./constants');
const rpc = require('./rpc');
import { keys } from './constants';
import * as rpc from './rpc';
const {keys} = constants;
let mutationListeners = {};
module.exports = {
clearMutationListeners,
fireMutationListeners,
createList,
createMethods,
createMethod,
getterForProperty,
setterForProperty,
};
function addMutationListener(realmId, callback) {
let listeners = mutationListeners[realmId] || (mutationListeners[realmId] = new Set());
listeners.add(callback);
@ -46,18 +35,18 @@ function removeMutationListener(realmId, callback) {
}
}
function clearMutationListeners() {
export function clearMutationListeners() {
mutationListeners = {};
}
function fireMutationListeners(realmId) {
export function fireMutationListeners(realmId) {
let listeners = mutationListeners[realmId];
if (listeners) {
listeners.forEach((cb) => cb());
}
}
function createList(prototype, realmId, info, mutable) {
export function createList(prototype, realmId, info, mutable) {
let list = Object.create(prototype);
let size = 0;
@ -129,7 +118,7 @@ function createList(prototype, realmId, info, mutable) {
return list;
}
function createMethods(prototype, type, methodNames, mutates) {
export function createMethods(prototype, type, methodNames, mutates) {
let props = {};
methodNames.forEach((name) => {
@ -141,7 +130,7 @@ function createMethods(prototype, type, methodNames, mutates) {
Object.defineProperties(prototype, props);
}
function createMethod(type, name, mutates) {
export function createMethod(type, name, mutates) {
return function() {
let realmId = this[keys.realm];
let id = this[keys.id];
@ -163,13 +152,13 @@ function createMethod(type, name, mutates) {
};
}
function getterForProperty(name) {
export function getterForProperty(name) {
return function() {
return rpc.getProperty(this[keys.realm], this[keys.id], name);
};
}
function setterForProperty(name) {
export function setterForProperty(name) {
return function(value) {
let realmId = this[keys.realm];

View File

@ -23,7 +23,7 @@ if (typeof Realm != 'undefined') {
module.exports = Realm; // eslint-disable-line no-undef
} else if (navigator.userAgent) {
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
module.exports = require('./browser');
module.exports = require('./browser').default; // (exported as ES6 module)
} else {
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
}