From b1f656a25272502ad431896c4aa9fbd402485079 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Sun, 28 Feb 2016 12:36:30 -0800 Subject: [PATCH] Convert browser module to use ES6 modules --- lib/.eslintrc | 10 +--------- lib/browser/.eslintrc | 18 ++++++++++++++++++ lib/browser/base64.js | 9 ++------- lib/browser/constants.js | 12 +++--------- lib/browser/index.js | 17 +++++++---------- lib/browser/lists.js | 21 +++++++-------------- lib/browser/objects.js | 18 ++++++------------ lib/browser/results.js | 18 ++++++------------ lib/browser/rpc.js | 40 ++++++++++++---------------------------- lib/browser/util.js | 29 +++++++++-------------------- lib/index.js | 2 +- 11 files changed, 72 insertions(+), 122 deletions(-) create mode 100644 lib/browser/.eslintrc diff --git a/lib/.eslintrc b/lib/.eslintrc index 432f0525..e84df8c3 100644 --- a/lib/.eslintrc +++ b/lib/.eslintrc @@ -1,13 +1,5 @@ { "env": { - "commonjs": true, - "browser": true, - "es6": true - }, - "ecmaFeatures": { - "forOf": false - }, - "globals": { - "global": true + "commonjs": true } } diff --git a/lib/browser/.eslintrc b/lib/browser/.eslintrc new file mode 100644 index 00000000..3fb64fc9 --- /dev/null +++ b/lib/browser/.eslintrc @@ -0,0 +1,18 @@ +{ + "env": { + "es6": true, + "worker": true + }, + "globals": { + "global": true + }, + "parserOptions": { + "ecmaFeatures": { + "forOf": false + }, + "sourceType": "module" + }, + "rules": { + "strict": 0 + } +} diff --git a/lib/browser/base64.js b/lib/browser/base64.js index 24b65db3..49774b12 100644 --- a/lib/browser/base64.js +++ b/lib/browser/base64.js @@ -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; diff --git a/lib/browser/constants.js b/lib/browser/constants.js index 88551970..367ee967 100644 --- a/lib/browser/constants.js +++ b/lib/browser/constants.js @@ -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 -}; diff --git a/lib/browser/index.js b/lib/browser/index.js index 4e327bec..d160af58 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -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; diff --git a/lib/browser/lists.js b/lib/browser/lists.js index 38beba2f..1e95ca80 100644 --- a/lib/browser/lists.js +++ b/lib/browser/lists.js @@ -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); } diff --git a/lib/browser/objects.js b/lib/browser/objects.js index a6e34b1e..974b4916 100644 --- a/lib/browser/objects.js +++ b/lib/browser/objects.js @@ -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; } diff --git a/lib/browser/results.js b/lib/browser/results.js index da5dbbff..2cedcb97 100644 --- a/lib/browser/results.js +++ b/lib/browser/results.js @@ -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); } diff --git a/lib/browser/rpc.js b/lib/browser/rpc.js index f632dfd2..de010510 100644 --- a/lib/browser/rpc.js +++ b/lib/browser/rpc.js @@ -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'); } diff --git a/lib/browser/util.js b/lib/browser/util.js index 206c5549..cb5bc939 100644 --- a/lib/browser/util.js +++ b/lib/browser/util.js @@ -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]; diff --git a/lib/index.js b/lib/index.js index 69876115..ed3bf513 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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!'); }