Skip to content

Instantly share code, notes, and snippets.

@AleksueiR
Last active April 22, 2016 18:51
Show Gist options
  • Save AleksueiR/e33b0e214585af4de856201257a11fe2 to your computer and use it in GitHub Desktop.
Save AleksueiR/e33b0e214585af4de856201257a11fe2 to your computer and use it in GitHub Desktop.
Global registry "sync" map proxy
// global-registry.js
// "private" registry of map proxies
const _mapRegistry = [];
window.RV._mapRegistry = _mapRegistry;
const mapProxy = {
_appInstance: null,
_proxy(action, ...args) {
this._appInstance.then(appInstance =>
appInstance[action](...args)
);
}
addRcsLayer(keys) {
this._proxy('addRcsLayer', keys);
},
switchLanguage(lang) {
this._proxy('switchLanguage', lang);
},
_init(id) {
this._appInstance = new Promise((resolve, reject) {
// store a callback function in the proxy object itself for map instances to call upon readiness
this._registerMap = appInstance => {
// store actual instance of the map; after this point, all queued calls to `addRcsLayer`, `switchLanguage`, etc. will trigger
this._appInstance = appInstance;
resolve();
});
}
};
// external "sync" function to retrieve a map instance
// in reality it returns a map proxy queueing calls to the map until it's ready
window.RV.getMap = getMap;
function getMap(id) {
return _mapRegistry[id];
}
// app-seed.js
// move some app-seed code into the injector (can read configs here as well)
nodes.forEach(node => {
const id = ???; // id of a map instance
_mapRegistry[id] = Object.create(mapProxy)._init(id);
});
// core.run.js
// somewhere in core.run.js
$rootScope.$on(events.rvReady, () => {
globalRegistry._mapRegistry[$rootElement.attr('id')]._registerMap(service);
});
@spencerwahl
Copy link

const mapProxy = {
        _appInstance: null,

        _proxy(action, ...args) {
            this._appInstance.then(appInstance =>
                appInstance[action](...args)
            );
        },

        addRcsLayer(keys) {
            this._proxy('addRcsLayer', keys);
        },

        switchLanguage(lang) {
            this._proxy('switchLanguage', lang);
        },

        _init(id) {
            this._appInstance = new Promise((resolve, reject) => {
                // store a callback function in the proxy object itself for map instances to call upon readiness
                this._registerMap = appInstance => {
                    // store actual instance of the map; after this point, all queued calls to `addRcsLayer`, `switchLanguage`, etc. will trigger
                    this._appInstance = appInstance;
                    resolve();
                }
            });
        }
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment