Singleton pattern for iFrames sharing the same origin

I have developed a web application that runs on multiple iframes within a parent window, similar to a modified version of GWT. Rather than each individual iframe accessing our backend service separately, I am attempting to have them share the data service through the use of the window.parent global object. I have implemented a singleton pattern for this purpose-

define(function(){
    var DocumentService = function () {

        //create namespace if it doesn't exist
        if (!parent.OurApp) {
           parent.OurApp= {};
        }
        //returning the singleton instance if already present
        if (parent.OurApp.DocumentService) {

            return parent.OurApp.DocumentService;
        }
        //assign the global instance
        parent.OurApp.DocumentService = this;

        //start fetching data upon instantiation
        this.items;
        this.getItems();
        this.startInterval();

    } 
    DocumentService.prototype.getItems = function(){
      //Call to Rest service here
      //this.items.push(response); some pseudo-code here
    };
    DocumentService.prototype.startInterval = function(){
      var self = this;
      this.intervalId = parent.setInterval(function(){
         console.log("Fetching items @ " +new Date());
         this.getItems();
      },300000);
    };
  //return new instance or singleton instance
  return new DocumentService();
});

This process works fine on initial load - getItems() loads data from the service and setInterval initiates a loop to run getItems at regular intervals. The changes to the items array can be monitored and tracked.

The challenge arises when an iframe is individually reloaded by right-clicking and selecting "reload frame", without refreshing the parent window. In this scenario, while the parent instantiation of the service continues to run, the threads seem to go out of sync when the iframe accesses the "parent" instance singleton by obtaining a new DocumentService(). This results in the frame no longer displaying console logs or reflecting changes in the items array. Any insights into why this occurs? Is there a flaw in my singleton pattern?

Answer №1

Although I couldn't find a complete solution to this issue, I ended up taking a different approach. Moving forward, I plan to utilize the native cross-frame communication methods such as postMessage.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Move the accordion slider to the top of the browser

I'm working with an accordion menu that has some long content. To make it more user-friendly, I want to add a slide effect when the accordion items are opened. Right now, if you open the first two menu items, the last item's content is cut off a ...

What is the best way to access and read the contents of a text file retrieved via axios?

I am looking to utilize a REST API to read the text from a file. I have been using marked to convert the txt file to markdown and display it in vue.js. However, I am unsure of how to actually retrieve the contents of the file. During my research, I came ...

Deactivating Touchable Opacity Sounds in React Native

Currently, I am in the process of developing an application, utilizing TouchableOpacity instead of a button. I am looking to disable the auditory feedback that occurs when the TouchableOpacity component is pressed. <TouchableOpacity activeOpacity={1} to ...

Solving an object in ui-router state using ui-sref

Dealing with a large JSON object in an Angular controller and wanting to pass it to the controller of a template that will be displayed in a ui-view. I am aware that parameters can be passed to states using ui-sref, but I do not want this object to be visi ...

The process of updating UseContext global state in React Native and ensuring that the change is reflected across all screens

Struggling with updating global state values using React useContext on different screens? Attempting to change theme color in the App, but changes only appear on the current screen and not carried over to others? Looking for assistance in resolving this ...

Guide on using Selenium and Python with the buster extension to easily navigate through ReCaptcha

I am currently automating certain processes using Selenium and encountering the challenge of solving Google ReCaptcha. The technology I use to tackle this issue is the browser Plugin Buster. To enter the Google ReCaptcha, I employ the following code: drive ...

Converting an object with a combination of different index types into a string representation

I'm dealing with a unique object structure that behaves similarly to an array, except it contains a mix of index types (numbers and strings). Here's an example: var myObj = []; myObj[0] = 'a'; myObj[1] = 'b'; myObj[2] = &apos ...

Record of Recaptcha actions is not showing up in the reports

My method of running the recaptcha script is as follows: let data = { action: 'homepage' }; grecaptcha.ready(() => { grecaptcha.execute('RECAPTCHASITEKEY', data).then((token) => { recaptcha_token = token; }); ...

invoke two JavaScript functions without displaying any message

I'm facing an issue with Ajax as it's not displaying the message I intend to show. Let me elaborate. Within my PHP code, there is an input tag: <input type="submit" id="login_button_add" name="submit" value="Add" onclick="add_building(); sho ...

The retrieved item has not been linked to the React state

After successfully fetching data on an object, I am attempting to assign it to the state variable movie. However, when I log it to the console, it shows as undefined. import React, {useState, useEffect} from "react"; import Topbar from '../H ...

Deleting a document by ObjectID in MongoDB with Node and Express without using Mongoose: A step-by-step guide

As a newcomer to backend development, I am currently using Node/Express/MongoDB with an EJS template for the frontend. I am in the process of creating a simple todo list app to practice CRUD operations without relying on Mongoose but solely native MongoDB. ...

jsTree unable to locate node using the provided ID

Implementing the jsTree on the webpage is not a problem for me. I have experimented with numerous suggestions from different sources. $('#myTree').jstree({ .... }) .on('loaded.jstree', function (e, dta) { var t = $('#myTree&a ...

What is the difference in speed between drawing on a transparent canvas and determining if a point is transparent compared to determining if a point is within a complex polygon?

I'm curious if anyone has done research on adding event support to a game engine. Specifically, I am working on incorporating pixel-perfect hover over 2D object event support into my own game engine. I am exploring different ways of achieving this eff ...

Navigating with jQuery Scrollbars

Looking to incorporate a bit of animation in jQuery, trying out the following line: window.parent.scroll(coord[0], coord[1]); The next block of code doesn't seem to be achieving what I had in mind. Do you have any suggestions? $(window.parent).anim ...

When a radio button is chosen, multiple text fields must be completed in order to validate the form

I am working with a set of 3 radio buttons. If the last option, labeled "In Home," is chosen, then all 4 of the text fields must be filled in for the form to validate. While I have found information on how to achieve this with checkboxes or a single text f ...

Issue with Braintree Integration - custom form failing to generate nonce

When I followed the code in the documentation, the nonce did not appear at the server side and I couldn't find any hidden input field for the nonce being submitted. I was only able to make it work with the drop-in form and could see the nonce on the ...

Looking to create an Ajax Control Toolkit AutoCompleteExtender with results that are "similar"?

The Ajax AutoCompleteExtender is all set up and functioning properly, linked to a webservice that fetches results from SQL. Now, I want to enhance the user experience by providing similar results in case they can't recall the exact name of what they& ...

How to trigger a jQuery function once a v-for loop has completed in VueJS?

Utilizing vue-resource, I successfully retrieve data from my API and incorporate it into my HTML. However, I am encountering an issue where I want to execute a jQuery function after the v-for loop has completed in order for jQuery to recognize elements in ...

Navigational elements, drawers, and flexible designs in Material-UI

I'm working on implementing a rechart in a component, but I've encountered an issue related to a flex tag. This is causing some problems as I don't have enough knowledge about CSS to find a workaround. In my nav style, I have display: flex, ...

Generate interactive tables using data from an XML file

Hello, I'm in the process of generating tables from an XML file using Node.js with the Express framework. I am utilizing npm modules xmldom and xmldoc for this task. The objective is to present these data tables on an ejs page. Here is the structure ...