test: added

This commit is contained in:
alexander.akait 2024-06-21 17:01:58 +03:00
parent ffaad7132f
commit f36ae6c702
12 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import { emitter } from "./emitter.js";
function App() {
return emitter;
}
export default App;

View File

@ -0,0 +1,9 @@
import { TinyEmitter } from 'tiny-emitter'
const emitter = new TinyEmitter()
emitter.on('hello', () => console.log('hello[service]'))
export {
emitter,
}

View File

@ -0,0 +1,6 @@
it("should allow to import exposed modules sync", () => {
return import("./App").then(({ default: App }) => {
expect(App().e.hello).toBeDefined();
});
});

View File

@ -0,0 +1,67 @@
function E () {
// Keep this empty so it's easier to inherit from
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
}
E.prototype = {
on: function (name, callback, ctx) {
var e = this.e || (this.e = {});
(e[name] || (e[name] = [])).push({
fn: callback,
ctx: ctx
});
return this;
},
once: function (name, callback, ctx) {
var self = this;
function listener () {
self.off(name, listener);
callback.apply(ctx, arguments);
};
listener._ = callback
return this.on(name, listener, ctx);
},
emit: function (name) {
var data = [].slice.call(arguments, 1);
var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
var i = 0;
var len = evtArr.length;
for (i; i < len; i++) {
evtArr[i].fn.apply(evtArr[i].ctx, data);
}
return this;
},
off: function (name, callback) {
var e = this.e || (this.e = {});
var evts = e[name];
var liveEvents = [];
if (evts && callback) {
for (var i = 0, len = evts.length; i < len; i++) {
if (evts[i].fn !== callback && evts[i].fn._ !== callback)
liveEvents.push(evts[i]);
}
}
// Remove event from queue to prevent memory leak
// Suggested by https://github.com/lazd
// Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
(liveEvents.length)
? e[name] = liveEvents
: delete e[name];
return this;
}
};
module.exports = E;
module.exports.TinyEmitter = E;

View File

@ -0,0 +1,7 @@
{
"name": "tiny-emitter",
"version": "2.1.0",
"description": "A tiny (less than 1k) event emitter library",
"main": "index.js",
"license": "MIT"
}

View File

@ -0,0 +1,9 @@
{
"private": true,
"engines": {
"node": ">=10.13.0"
},
"dependencies": {
"tiny-emitter": "^2.1.0"
}
}

View File

@ -0,0 +1,26 @@
const { dependencies } = require("./package.json");
const { ModuleFederationPlugin } = require("../../../../").container;
/** @type {import("../../../../").Configuration} */
module.exports = {
optimization: {
chunkIds: "named",
moduleIds: "named"
},
plugins: [
new ModuleFederationPlugin({
name: "container",
filename: "container.js",
library: { type: "commonjs-module" },
exposes: {
"./emitter": {
name: "emitter",
import: "./emitter.js"
}
},
shared: {
...dependencies
}
})
]
};

View File

@ -0,0 +1,13 @@
import TinyEmitter from 'tiny-emitter'
it("should load the component from container", () => {
const emitter = new TinyEmitter()
emitter.on('hello', () => {})
expect(emitter.e.hello).toBeDefined();
return import('service/emitter').then(({ emitter }) => {
expect(emitter.e.hello).toBeDefined();
})
});

View File

@ -0,0 +1,66 @@
function E () {
// Keep this empty so it's easier to inherit from
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
}
E.prototype = {
on: function (name, callback, ctx) {
var e = this.e || (this.e = {});
(e[name] || (e[name] = [])).push({
fn: callback,
ctx: ctx
});
return this;
},
once: function (name, callback, ctx) {
var self = this;
function listener () {
self.off(name, listener);
callback.apply(ctx, arguments);
};
listener._ = callback
return this.on(name, listener, ctx);
},
emit: function (name) {
var data = [].slice.call(arguments, 1);
var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
var i = 0;
var len = evtArr.length;
for (i; i < len; i++) {
evtArr[i].fn.apply(evtArr[i].ctx, data);
}
return this;
},
off: function (name, callback) {
var e = this.e || (this.e = {});
var evts = e[name];
var liveEvents = [];
if (evts && callback) {
for (var i = 0, len = evts.length; i < len; i++) {
if (evts[i].fn !== callback && evts[i].fn._ !== callback)
liveEvents.push(evts[i]);
}
}
// Remove event from queue to prevent memory leak
// Suggested by https://github.com/lazd
// Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
(liveEvents.length)
? e[name] = liveEvents
: delete e[name];
return this;
}
};
module.exports = E;

View File

@ -0,0 +1,7 @@
{
"name": "tiny-emitter",
"version": "2.0.0",
"description": "A tiny (less than 1k) event emitter library",
"main": "index.js",
"license": "MIT"
}

View File

@ -0,0 +1,9 @@
{
"private": true,
"engines": {
"node": ">=10.13.0"
},
"dependencies": {
"tiny-emitter": "=2.0.0"
}
}

View File

@ -0,0 +1,25 @@
const { dependencies } = require("./package.json");
const { ModuleFederationPlugin } = require("../../../../").container;
/** @type {import("../../../../").Configuration} */
module.exports = {
optimization: {
chunkIds: "named",
moduleIds: "named"
},
plugins: [
new ModuleFederationPlugin({
remoteType: "commonjs-module",
remotes: {
service: "../0-eager-shared/container.js"
},
shared: {
"tiny-emitter": {
eager: true,
singleton: true,
requiredVersion: dependencies["tiny-emitter"]
}
}
})
]
};