window.common = (function(global) { const { Rx: { BehaviorSubject, Observable }, common = { init: [] } } = global; var libraryIncludes = ` `; 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') }); common.updatePreview$ = function updatePreview$(code = '') { const previewFrame = document.getElementById('preview'); const preview = previewFrame.contentDocument || previewFrame.contentWindow.document; if (!preview) { return Observable.just(code); } 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(); // now we filter false values and wait for the first true return common.previewReady$ .filter(ready => ready) .first(); }) .map(() => code); }; return common; }(window));