update
This commit is contained in:
31
src/y.js
31
src/y.js
@@ -22,6 +22,10 @@ Y.extend = function (name, value) {
|
||||
|
||||
Y.requestModules = requestModules
|
||||
function requestModules (modules) {
|
||||
// determine if this module was compiled for es5 or es6 (y.js vs. y.es6)
|
||||
// if Insert.execute is a Function, then it isnt a generator..
|
||||
// then load the es5(.js) files..
|
||||
var extention = Y.Struct.Insert.execute.constructor === Function ? '.js' : '.es6'
|
||||
var promises = []
|
||||
for (var i = 0; i < modules.length; i++) {
|
||||
var modulename = 'y-' + modules[i].toLowerCase()
|
||||
@@ -33,7 +37,7 @@ function requestModules (modules) {
|
||||
// module does not exist
|
||||
if (typeof window !== 'undefined') {
|
||||
var imported = document.createElement('script')
|
||||
imported.src = Y.sourceDir + '/' + modulename + '/' + modulename + '.js'
|
||||
imported.src = Y.sourceDir + '/' + modulename + '/' + modulename + extention
|
||||
document.head.appendChild(imported)
|
||||
|
||||
let requireModule = {}
|
||||
@@ -69,26 +73,28 @@ type DbOptions = MemoryOptions | IndexedDBOptions
|
||||
type WebRTCOptions = {
|
||||
name: 'webrtc',
|
||||
room: string
|
||||
}
|
||||
}
|
||||
type WebsocketsClientOptions = {
|
||||
name: 'websockets-client',
|
||||
room: string
|
||||
}
|
||||
type ConnectionOptions = WebRTCOptions | WebsocketsClientOptions
|
||||
|
||||
type TypesOptions = Array<'array'|'map'|'text'>
|
||||
|
||||
type YOptions = {
|
||||
connector: ConnectionOptions,
|
||||
db: DbOptions,
|
||||
types: TypesOptions,
|
||||
sourceDir: string
|
||||
types: Array<TypeName>,
|
||||
sourceDir: string,
|
||||
share: {[key: string]: TypeName}
|
||||
}
|
||||
*/
|
||||
|
||||
function Y (opts/* :YOptions */) /* :Promise<YConfig> */ {
|
||||
opts.types = opts.types != null ? opts.types : []
|
||||
var modules = [opts.db.name, opts.connector.name].concat(opts.types)
|
||||
for (var name in opts.share) {
|
||||
modules.push(opts.share[name])
|
||||
}
|
||||
Y.sourceDir = opts.sourceDir
|
||||
return Y.requestModules(modules).then(function () {
|
||||
return new Promise(function (resolve) {
|
||||
@@ -105,19 +111,18 @@ class YConfig {
|
||||
/* ::
|
||||
db: Y.AbstractDatabase;
|
||||
connector: Y.AbstractConnector;
|
||||
share: {[key: string]: any};
|
||||
*/
|
||||
constructor (opts, callback) {
|
||||
this.db = new Y[opts.db.name](this, opts.db)
|
||||
this.connector = new Y[opts.connector.name](this, opts.connector)
|
||||
var share = {}
|
||||
this.share = share
|
||||
this.db.requestTransaction(function * requestTransaction () {
|
||||
// create initial Map type
|
||||
this.store._temporaryUserIdGenerator = function () {
|
||||
return ['_', 0]
|
||||
// create shared object
|
||||
for (var propertyname in opts.share) {
|
||||
share[propertyname] = yield* this.getType(['_', opts.share[propertyname] + '_' + propertyname])
|
||||
}
|
||||
var typeid = yield* Y.Map.createType.call(this)
|
||||
var type = yield* this.getType(typeid)
|
||||
this.store.y.root = type
|
||||
this.store._temporaryUserIdGenerator = null
|
||||
setTimeout(callback, 0)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user