WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 42fc45e

Browse files
authored
Merge pull request #1 from taketo1113/raise-error-multi-start
Add to raise an error when executed multiple times
2 parents a5716ba + 5788c1d commit 42fc45e

File tree

4 files changed

+143
-7
lines changed

4 files changed

+143
-7
lines changed

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import confirm from "./confirm.js";
22
import method from "./method.js";
3+
import loadState from "./utils/loadState.js";
34

45
const start = function () {
56
startConfirm();
67
startMethod();
78
};
89

910
const startConfirm = function () {
11+
const moduleName = "confirm";
12+
loadState.checkLoaded(moduleName);
13+
14+
// start
1015
confirm.start();
16+
17+
loadState.setLoaded(moduleName);
1118
};
1219

1320
const startMethod = function () {
21+
const moduleName = "method";
22+
loadState.checkLoaded(moduleName);
23+
24+
// start
1425
method.start();
26+
27+
loadState.setLoaded(moduleName);
1528
};
1629

1730
export default { start, startConfirm, startMethod };

src/utils/loadState.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// State store
2+
// window.__ALT_UJS__: { loaded_confirm: true|falseloaded_method: true|false }
3+
//
4+
const setLoaded = function (module) {
5+
if (window.__ALT_UJS__ === undefined) {
6+
window.__ALT_UJS__ = {};
7+
}
8+
9+
window.__ALT_UJS__[key(module)] = true;
10+
};
11+
12+
const isLoaded = function (module) {
13+
if (window.__ALT_UJS__ === undefined) {
14+
return false;
15+
}
16+
17+
if (window.__ALT_UJS__[key(module)] === true) {
18+
return true;
19+
}
20+
21+
return false;
22+
};
23+
24+
// Raise an error to prevent multiple event listeners from being registered when alt-ujs is executed more than once.
25+
const checkLoaded = function (module) {
26+
if (isLoaded(module)) {
27+
throw new Error(`alt-ujs: ${module} module has already been loaded.`);
28+
}
29+
30+
return true;
31+
};
32+
33+
const key = function (module) {
34+
if (!module) {
35+
return;
36+
}
37+
38+
return "loaded_" + module;
39+
};
40+
41+
export default { setLoaded, checkLoaded };

test/index.test.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
1-
import { expect, test } from "vitest";
1+
import { expect, test, describe, afterEach } from "vitest";
22
import AltUjs from "../src/index.js";
33

4-
test("export start", async () => {
5-
expect(AltUjs.start).not.toBeNull();
4+
afterEach(async () => {
5+
window.__ALT_UJS__ = undefined;
66
});
77

8-
test("export startConfirm", async () => {
9-
expect(AltUjs.startConfirm).not.toBeNull();
8+
describe("#start", () => {
9+
test("export start", () => {
10+
expect(AltUjs.start).not.toBeNull();
11+
});
12+
13+
test("raise an error when start twice", () => {
14+
AltUjs.start();
15+
16+
expect(() => AltUjs.start()).toThrowError(
17+
"alt-ujs: confirm module has already been loaded.",
18+
);
19+
});
20+
});
21+
22+
describe("#startConfirm", () => {
23+
test("export startConfirm", () => {
24+
expect(AltUjs.startConfirm).not.toBeNull();
25+
});
26+
27+
test("raise an error when start twice", () => {
28+
AltUjs.startConfirm();
29+
30+
expect(() => AltUjs.startConfirm()).toThrowError(
31+
"alt-ujs: confirm module has already been loaded.",
32+
);
33+
});
1034
});
1135

12-
test("export startMethod", async () => {
13-
expect(AltUjs.startMethod).not.toBeNull();
36+
describe("#startMethod", () => {
37+
test("export startMethod", () => {
38+
expect(AltUjs.startMethod).not.toBeNull();
39+
});
40+
41+
test("raise an error when start twice", () => {
42+
AltUjs.startMethod();
43+
44+
expect(() => AltUjs.startMethod()).toThrowError(
45+
"alt-ujs: method module has already been loaded.",
46+
);
47+
});
1448
});

test/utils/loadState.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { expect, test, describe, afterEach } from "vitest";
2+
import loadState from "../../src/utils/loadState.js";
3+
4+
const moduleName = "test_module";
5+
6+
afterEach(async () => {
7+
window.__ALT_UJS__ = undefined;
8+
});
9+
10+
describe("#setLoaded", () => {
11+
test("set the loaded state for a single module", () => {
12+
loadState.setLoaded(moduleName);
13+
14+
expect(window.__ALT_UJS__.loaded_test_module).toBe(true);
15+
});
16+
17+
test("set the loaded state for a multiple module", () => {
18+
loadState.setLoaded(moduleName);
19+
loadState.setLoaded("test2");
20+
21+
expect(window.__ALT_UJS__.loaded_test_module).toBe(true);
22+
expect(window.__ALT_UJS__.loaded_test2).toBe(true);
23+
});
24+
25+
test("return undefined when no loaded state is set", () => {
26+
expect(window.__ALT_UJS__).toBeUndefined();
27+
});
28+
});
29+
30+
describe("#checkLoaded", () => {
31+
test("raise an error when the module has already been loaded", () => {
32+
loadState.setLoaded(moduleName);
33+
34+
expect(() => loadState.checkLoaded(moduleName)).toThrowError(
35+
"alt-ujs: test_module module has already been loaded.",
36+
);
37+
});
38+
39+
test("return true for a different module's loaded state", () => {
40+
loadState.setLoaded("test2");
41+
42+
expect(loadState.checkLoaded(moduleName)).toBe(true);
43+
});
44+
45+
test("return true when no loaded state is set for the module", () => {
46+
expect(loadState.checkLoaded(moduleName)).toBe(true);
47+
});
48+
});

0 commit comments

Comments
 (0)