From e16195cb542f5467ec003f6da6a442cefa4bf294 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Mon, 14 Aug 2017 15:39:17 +0200 Subject: [PATCH] implement timeout for creating Yjs instance --- examples/textarea/index.js | 7 +++++-- src/y.js | 12 +++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/textarea/index.js b/examples/textarea/index.js index d58ab9d1..e40bba32 100644 --- a/examples/textarea/index.js +++ b/examples/textarea/index.js @@ -12,17 +12,20 @@ Y({ connector: { name: 'websockets-client', room: 'Textarea-example', - url: url || 'http://127.0.0.1:1234' + url: 'https://yjs-v13.herokuapp.com/' }, sourceDir: '/bower_components', share: { textarea: 'Text', // y.share.textarea is of type Y.Text test: 'Array' - } + }, + 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/src/y.js b/src/y.js index 0dd86e43..f96594a7 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) }