From 426526355cb57d17fb6aad781123fb10966279f3 Mon Sep 17 00:00:00 2001 From: Berkeley Martinez Date: Tue, 1 Dec 2015 08:01:51 -0800 Subject: [PATCH] Fix initial load error --- client/commonFramework/update-preview.js | 18 +++++++++++++----- client/iFrameScripts.js | 5 +++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/client/commonFramework/update-preview.js b/client/commonFramework/update-preview.js index 8d2a20c735..fb4e36d605 100644 --- a/client/commonFramework/update-preview.js +++ b/client/commonFramework/update-preview.js @@ -1,6 +1,6 @@ window.common = (function(global) { const { - Rx: { Observable }, + Rx: { BehaviorSubject, Observable }, common = { init: [] } } = global; @@ -27,6 +27,11 @@ window.common = (function(global) { const iFrameScript$ = common.getScriptContent$('/js/iFrameScripts.js').shareReplay(); + // behavior subject allways remembers the last value + // we use this to determine if runPreviewTest$ is defined + // and prime it with false + common.previewReady$ = new BehaviorSubject(false); + // runPreviewTests$ should be set up in the preview window common.runPreviewTests$ = () => Observable.throw({ err: new Error('run preview not enabled') }); @@ -42,13 +47,16 @@ window.common = (function(global) { return iFrameScript$ .map(script => ``) .flatMap(script => { + // we make sure to override the last value in the + // subject to false here. + common.previewReady$.onNext(false); preview.open(); preview.write(libraryIncludes + code + '' + script); preview.close(); - return Observable.fromCallback($(preview).ready, $(preview))() - .first() - // delay is need here for first initial run - .delay(100); + // now we filter false values and wait for the first true + return common.previewReady$ + .filter(ready => ready) + .first(); }) .map(() => code); }; diff --git a/client/iFrameScripts.js b/client/iFrameScripts.js index b4c9523317..7bbaa3eb10 100644 --- a/client/iFrameScripts.js +++ b/client/iFrameScripts.js @@ -35,4 +35,9 @@ window.__$(function() { .map(tests => ({ ...rest, tests })); }; + // now that the runPreviewTest$ is defined + // we set the subject to true + // this will let the updatePreview + // script now that we are ready. + common.previewReady$.onNext(true); });