Skip to content

Commit

Permalink
RollCake SPA v0.0.3
Browse files Browse the repository at this point in the history
- Update RollCake MF Broker version to v0.0.4
- Remove page state feature
- Refactor page update logic
  • Loading branch information
aacgn committed Oct 29, 2020
1 parent 124b01f commit 52fddc8
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 68 deletions.
5 changes: 1 addition & 4 deletions examples/mf-sample/application-shell/src/broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ const buckets = [
}
];

export default new RollCakeMFBroker({
buckets: buckets,
state: {}
});
export default new RollCakeMFBroker(buckets);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CONTEXT_ATTRIBUTE } from "../../../../../src/index";
import { createElement, PUBLIC_BUS_PUBLISH_EVENT_TYPE, WINDOW_VARIABLE, } from "../../../../../src/index";
import { createElement, PUBLIC_BUS_PUBLISH_EVENT_TYPE, WINDOW_VARIABLE } from "../../../../../src/index";

const CommonHeader = () => createElement({
tag: 'div',
Expand All @@ -15,7 +15,7 @@ const CommonHeader = () => createElement({
textContent: 'Home',
eventListener: 'click',
eventHandler: () => {
window[WINDOW_VARIABLE.MF_BROKER][CONTEXT_ATTRIBUTE.BUS].publish(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, '/');
window[WINDOW_VARIABLE.ROLLCAKE][CONTEXT_ATTRIBUTE.BUS].publish(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, '/');
}
}
}),
Expand All @@ -30,7 +30,7 @@ const CommonHeader = () => createElement({
textContent: 'React',
eventListener: 'click',
eventHandler: () => {
window[WINDOW_VARIABLE.MF_BROKER][CONTEXT_ATTRIBUTE.BUS].publish(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, '/react');
window[WINDOW_VARIABLE.ROLLCAKE][CONTEXT_ATTRIBUTE.BUS].publish(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, '/react');
}
}
}),
Expand All @@ -40,7 +40,7 @@ const CommonHeader = () => createElement({
textContent: 'Vue',
eventListener: 'click',
eventHandler: () => {
window[WINDOW_VARIABLE.MF_BROKER][CONTEXT_ATTRIBUTE.BUS].publish(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, '/vue');
window[WINDOW_VARIABLE.ROLLCAKE][CONTEXT_ATTRIBUTE.BUS].publish(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, '/vue');
}
}
}),
Expand Down
5 changes: 1 addition & 4 deletions examples/render-sample/src/broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ import { RollCakeMFBroker } from "../../../src/index";

const buckets = [];

export default new RollCakeMFBroker({
buckets: buckets,
state: {}
});
export default new RollCakeMFBroker(buckets);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rollcakejs/rollcake-spa",
"version": "0.0.2",
"version": "0.0.3",
"description": "🖖 RollCakeSpa.js is a progressive, incrementally-adoptable JavaScript framework for building micro-frontends UI on the web.",
"main": "src/index.js",
"sideEffects": false,
Expand Down Expand Up @@ -41,7 +41,7 @@
"babel-eslint": "^10.1.0"
},
"dependencies": {
"@rollcakejs/rollcake-mf-broker": "0.0.3",
"@rollcakejs/rollcake-mf-broker": "0.0.4",
"@rollcakejs/rollcake-router": "0.0.1",
"loadash": "^1.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/core/create-element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Element from './element';

export function createElement(config = null) {
export function createElement(config = {}) {
const { tag, attr, props, children } = config;
return new Element(tag, attr, props, children);
}
6 changes: 3 additions & 3 deletions src/core/create-page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Page from './page';

export function createPage(config = null) {
const { state, onInit, onUpdate, onDestroy, content, loadingContent } = config;
return new Page(state, onInit, onUpdate, onDestroy, content, loadingContent);
export function createPage(config = {}) {
const { onInit, onUpdate, onDestroy, content, loadingContent } = config;
return new Page(onInit, onUpdate, onDestroy, content, loadingContent);
}
45 changes: 14 additions & 31 deletions src/core/page.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { WINDOW_VARIABLE, CONTEXT_ATTRIBUTE, INTERNAL_BUS_PUBLISH_EVENT_TYPE } from "@rollcakejs/rollcake-mf-broker";
import { ERROR_MESSAGE } from "../shared/constants";

class Page {
constructor(
state = null,
onInit = () => {},
onUpdate = () => {},
onDestroy = () => {},
content = null
) {
this.state = state;
this.onInit = onInit;
this.onUpdate = onUpdate;
this.onDestroy = onDestroy;
Expand All @@ -19,32 +16,30 @@ class Page {
this.onBeforeDestroy = () => {};

// strictly internal
this._rollCakeMFBroker = window[WINDOW_VARIABLE.ROLLCAKE];
this._entryDOMNode = null;
this._childDOMNode = null;
this._loadingContentDOMNode = null;
this._prevStateValue = null;

this._configuration();
this._watchIsStoreUpdatedEvent();
this._watchIsMicrofrontendFetchingEvent();
}

_configuration() {
if (this.state) {
const state = window[WINDOW_VARIABLE.MF_BROKER][CONTEXT_ATTRIBUTE.STATE];
_watchIsStoreUpdatedEvent() {
const bus = this._rollCakeMFBroker[CONTEXT_ATTRIBUTE.BUS];

if (state === null || state === undefined)
{
throw Error(ERROR_MESSAGE.STATE_NOT_INITIALIZED);
}

state[this.state.key] = this.state.value;
this._prevStateValue = JSON.parse(JSON.stringify(this.state.value));
bus.subscribe(INTERNAL_BUS_PUBLISH_EVENT_TYPE.IS_STORE_UPDATED, () => {
this._destroyContent();
this.onUpdate();
this._renderContent();
});
}

this._useStateReferenceCheckAndUpdate();
}

const bus = window[WINDOW_VARIABLE.MF_BROKER][CONTEXT_ATTRIBUTE.BUS];
_watchIsMicrofrontendFetchingEvent() {
const bus = this._rollCakeMFBroker[CONTEXT_ATTRIBUTE.BUS];

bus.subscribe(INTERNAL_BUS_PUBLISH_EVENT_TYPE.IS_FETCHING_MICFRONTEND, (isFetching) => {
bus.subscribe(INTERNAL_BUS_PUBLISH_EVENT_TYPE.IS_FETCHING_MICROFRONTEND, (isFetching) => {
if (isFetching && this.loadingContent && !this._loadingContentDOMNode) {
this._hideContent();
this._renderLoadingContent();
Expand All @@ -56,18 +51,6 @@ class Page {
});
}

_useStateReferenceCheckAndUpdate() {
setInterval(() => {
if (JSON.stringify(this._prevStateValue) !== JSON.stringify(this.state.value)) {
this._destroyContent();
this.onUpdate();
this._renderContent();

this._prevStateValue = JSON.parse(JSON.stringify(this.state.value));
}
});
}

render(entryDOMNode, loadingContent = null) {
this.onBeforeInit();
this._entryDOMNode = entryDOMNode;
Expand Down
20 changes: 8 additions & 12 deletions src/core/spa.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { PUBLIC_BUS_PUBLISH_EVENT_TYPE } from "../shared/constants";

class RollCakeSpa {
constructor(broker, router, entryDOMNode, loadingContent = null) {
this.broker = broker;
this.router = router;
this.entryDOMNode = entryDOMNode;

constructor(broker, router, entryDOMNode, loadingContent) {
// strictly internal
this._VPage = null;

this.broker.init();
broker.init();

this.router.init((props) => {
router.init((response) => {
if (this._VPage !== null)
{
this._VPage.destroy();
}
if (props && props.item) {
this._VPage = props.item();
this._VPage.render(this.entryDOMNode, loadingContent);
if (response && response.item) {
this._VPage = response.item();
this._VPage.render(entryDOMNode, loadingContent);
}
else {
this._VPage = null;
}
});

this.broker.bus().subscribe(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, (path) => {
this.router.navigateTo(path);
broker.getBus().subscribe(PUBLIC_BUS_PUBLISH_EVENT_TYPE.NAVIGATE_TO, (path) => {
router.navigateTo(path);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RollCakeRouter } from '@rollcakejs/rollcake-router';
import { RollCakeMFBroker } from '@rollcakejs/rollcake-mf-broker';

export { PUBLIC_BUS_PUBLISH_EVENT_TYPE } from './shared/constants';
export { WINDOW_VARIABLE, CONTEXT_ATTRIBUTE, CUSTOM_ELEMENT_TAG } from '@rollcakejs/rollcake-mf-broker';
export { WINDOW_VARIABLE, CONTEXT_ATTRIBUTE, CUSTOM_ELEMENT_TAG, LOCAL_STORAGE } from '@rollcakejs/rollcake-mf-broker';
export { NAVIGATION_MODE } from "@rollcakejs/rollcake-router";

export { RollCakeSpa, RollCakeRouter, RollCakeMFBroker };
6 changes: 0 additions & 6 deletions src/shared/constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export const ERROR_MESSAGE = {
BROKER_NOT_INTIALIZED: 'Please, refresh your screen. The broker was not initialized yet.',
STATE_NOT_INITIALIZED: 'Please, refresh your screen. The state was not initialized yet.',
STATE_KEY_ALREADY_BEING_USED: 'You cannot use this key, because it is already being used in another page declaration.',
EVENT_BUS_NOT_INITIALIZED: 'Please, refresh your screen. The event bus was not initialized yet.'
};
export const PUBLIC_BUS_PUBLISH_EVENT_TYPE = {
NAVIGATE_TO: 'navigate-to'
}

0 comments on commit 52fddc8

Please sign in to comment.