diff --git a/examples/textarea/index.js b/examples/textarea/index.js index c3d9e6c4..3a8cd505 100644 --- a/examples/textarea/index.js +++ b/examples/textarea/index.js @@ -2,7 +2,6 @@ // eslint-disable-next-line let search = new URLSearchParams(location.search) -let url = search.get('url') // initialize a shared object. This function call returns a promise! Y({ @@ -13,16 +12,19 @@ Y({ name: 'websockets-client', room: 'Textarea-example', // url: '//localhost:1234', - url: 'https://yjs-v13.herokuapp.com/', + url: 'https://yjs-v13.herokuapp.com/' // options: { transports: ['websocket'], upgrade: false } }, share: { textarea: 'Text' - } + }, + timeout: 5000 // reject if no connection was established within 5 seconds }).then(function (y) { window.yTextarea = y // bind the textarea to a shared text element y.share.textarea.bind(document.getElementById('textfield')) // thats it.. +}).catch(() => { + console.log('Something went wrong while creating the instance..') }) diff --git a/package.json b/package.json index 283accdf..153cd2a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yjs", - "version": "13.0.0-13", + "version": "13.0.0-15", "description": "A framework for real-time p2p shared editing on any data", "main": "./y.node.js", "browser": "./y.js", diff --git a/src/y.js b/src/y.js index 2b4bc00e..ebc07591 100644 --- a/src/y.js +++ b/src/y.js @@ -139,10 +139,20 @@ export default function Y (opts/* :YOptions */) /* :Promise */ { opts.share = Y.utils.copyObject(opts.share) Y.requestModules(modules).then(function () { var yconfig = new YConfig(opts) + let resolved = false + if (opts.timeout != null && opts.timeout >= 0) { + setTimeout(function () { + if (!resolved) { + reject(new Error('Yjs init timeout')) + yconfig.destroy() + } + }, opts.timeout) + } yconfig.db.whenUserIdSet(function () { yconfig.init(function () { + resolved = true resolve(yconfig) - }) + }, reject) }) }).catch(reject) }