Skip to content

Commit

Permalink
Adding Models and base babel build architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
ScriptedAlchemy committed Mar 18, 2021
1 parent 14da567 commit 5bb5d10
Show file tree
Hide file tree
Showing 18 changed files with 1,867 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"useBuiltIns": "usage",
"corejs": 3
}
],
],
"plugins": [
"@babel/plugin-proposal-optional-chaining"
],
"env": {
"cjs": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": 10,
"browsers": [
">0.23%",
"iOS >= 9",
"Chrome >= 63",
"safari >= 9",
"IE >= 11"
]
},
"useBuiltIns": "usage",
"corejs": 3
}
]
]
}
}
}
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
env: {
browser: true,
es2020: true,
},
extends: ["standard", "plugin:prettier/recommended"],
parserOptions: {
ecmaVersion: 11,
sourceType: "module",
},
rules: {},
};
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/esm
/lib
*.log
yarn.lock
package-lock.json
node_modules
.idea
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.babelrc
.gitignore

34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@module-federation/aegis",
"version": "0.0.1",
"main": "index.js",
"repository": "[email protected]:module-federation/aegis.git",
"author": "Zack Jackson <[email protected]>",
"license": "MIT",
"scripts": {
"format": "yarn pretty -- --write",
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "babel --delete-dir-on-start -d esm/ src/ && cp -r ./src ./esm",
"build:cjs": "babel --delete-dir-on-start --env-name cjs -d lib/ src/ && cp -r ./webpack ./lib/webpack",
"prepublish": "yarn build"
},
"peerDependencies": {
"core-js": "^3"
},
"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/plugin-proposal-optional-chaining": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/preset-react": "^7.12.5",
"eslint": "^7.12.1",
"eslint-config-prettier": "^6.15.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.2",
"prettier": "^2.1.2"
}
}
16 changes: 16 additions & 0 deletions src/models/circuit-breaker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// "use strict";

// const BreakerStatus = {
// tripped: [],
// closed: [],
// }

// const CurcuitBreaker = function ({ observer, models }) {
// function
// return {
// getStatus(port) {
// const keys = Object.keys(BreakerStatus);
// keys.filter(key => )
// }
// };
// };
54 changes: 54 additions & 0 deletions src/models/compensate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import async from "../lib/async-error";
import domainEvents from "./domain-events";

/**
* Steps through the sequence of port calls
* in LIFO order executing their undo functions.
* @param {import('.').Model} model
* @returns {function():Promise<void>}
*/
export default async function compensate(model) {
try {
const updated = await model.update({ compensate: true });
const portFlow = model.getPortFlow();
const ports = model.getPorts();

await updated.emit(domainEvents.undoStarted(updated), "undo starting");

const undoResult = await portFlow.reduceRight(
async (_prev, port, index, arr) => {
if (ports[port].undo) {
console.log("calling undo on port: ", port);
const result = await async(ports[port].undo(updated));

if (result.ok) {
return arr.splice(0, index);
}
throw new Error("undo failed on port: ", port, result.error);
}
return arr.splice(0, index);
}
);

if (undoResult.length > 0) {
const lastPort = portFlow[undoResult.length - 1];
const msg = "undo incomplete, last port compensated " + lastPort;

const recordPort = await updated.update({
lastPort,
compensateResult: "INCOMPLETE",
});

await recordPort.emit(domainEvents.undoFailed(model), msg);
console.error(msg, updated);
return;
}

const compensateResult = "COMPLETE";
await updated.emit(domainEvents.undoWorked(model), compensateResult);
await updated.update({ compensateResult });
} catch (error) {
console.error(error);
await model.emit(domainEvents.undoFailed(model), error.message);
}
}
14 changes: 14 additions & 0 deletions src/models/domain-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const domainEvents = {
portTimeout: model => `portTimeout:${model.getName()}`,
portRetryWorked: model => `portRetryWorked:${model.getName()}`,
undoStarted: model => `undoStart:${model.getName()}`,
undoFailed: model => `undoFailed:${model.getName()}`,
undoWorked: model => `undoWorked:${model.getName()}`,
unauthorizedCommand: model => `unauthorizedCommand:${model.getName()}`,
addModel: eventName => `addModel:${eventName}`,
editModel: eventName => `editModel:${eventName}`,
};

export default domainEvents;
75 changes: 75 additions & 0 deletions src/models/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict'

import { withId, withTimestamp } from './mixins';
import asyncPipe from '../lib/async-pipe';
import uuid from '../lib/uuid';

/**
* @typedef {import('../models/model-factory').EventType} EventType
*/

/**
* @typedef {{
* factory: function(*):any,
* args: any,
* eventType: EventType,
* modelName: String
* }} options
*/

/**
* @typedef {Object} Event
* @property {EventType} eventType
* @property {String} eventName
* @property {String} eventTime
* @property {String} modelName
* @property {Object} modelData
* @property {String} id
*/

/**
* @namespace
*/
const Event = (() => {

/**
* @lends Event
* @namespace
* @class
* @param {options} options
* @returns {Promise<Readonly<Event>>}
*/
const Event = async ({
factory,
args,
eventType,
modelName
}) => Promise.resolve(
factory(args)
).then(event => ({
...event,
eventName: (eventType + modelName).toUpperCase(),
eventType,
modelName
}))

const makeEvent = asyncPipe(
Event,
withTimestamp('eventTime'),
withId('id', uuid),
Object.freeze
);

return {
/**
* Create event
* @param {options} options
* @returns {Promise<Readonly<Event>>}
*/
create: async (options) => makeEvent(options)
}
})();

export default Event;


Loading

0 comments on commit 5bb5d10

Please sign in to comment.