@vnejs/plugins.feature.wallet 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/const/const.js +1 -0
- package/const/events.js +7 -0
- package/index.js +8 -0
- package/modules/wallet.js +61 -0
- package/package.json +17 -0
package/const/const.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CURRENCIES = { MONEY: "money", DIAMONDS: "diamonds" };
|
package/const/events.js
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
|
|
3
|
+
import * as constants from "./const/const";
|
|
4
|
+
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
5
|
+
|
|
6
|
+
import { Wallet } from "./modules/wallet";
|
|
7
|
+
|
|
8
|
+
regPlugin("WALLET", { constants, events: SUBSCRIBE_EVENTS }, [Wallet]);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
window.Vne_Wallets = {};
|
|
4
|
+
|
|
5
|
+
export class Wallet extends Module {
|
|
6
|
+
name = "wallet";
|
|
7
|
+
|
|
8
|
+
subscribe = () => {
|
|
9
|
+
this.on(this.EVENTS.WALLET.REG, this.onWalletReg);
|
|
10
|
+
this.on(this.EVENTS.WALLET.ADD, this.onWalletAdd);
|
|
11
|
+
this.on(this.EVENTS.WALLET.SET, this.onWalletSet);
|
|
12
|
+
this.on(this.EVENTS.WALLET.RESET, this.onWalletReset);
|
|
13
|
+
|
|
14
|
+
this.on(this.EVENTS.STATE.SET, this.setStateFromGlobalState);
|
|
15
|
+
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
init = async () => {
|
|
19
|
+
this.shared.initialWallets = window.Vne_Wallets;
|
|
20
|
+
|
|
21
|
+
await Promise.all(Object.keys(this.shared.initialWallets).map(this.resetVallet));
|
|
22
|
+
await this.emit(this.EVENTS.SCENARIO.EXEC_REG, { module: this.name, handler: this.execHandler });
|
|
23
|
+
};
|
|
24
|
+
resetVallet = (id) => this.emit(this.EVENTS.WALLET.RESET, { id });
|
|
25
|
+
|
|
26
|
+
execHandler = async (line, args) => {
|
|
27
|
+
const [id, action] = line.split(" ");
|
|
28
|
+
|
|
29
|
+
if (action === "add") await this.emit(this.EVENTS.WALLET.ADD, { id, ...args });
|
|
30
|
+
if (action === "set") await this.emit(this.EVENTS.WALLET.SET, { id, ...args });
|
|
31
|
+
if (action === "reset") await this.emit(this.EVENTS.WALLET.RESET, { id });
|
|
32
|
+
|
|
33
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
onWalletReg = ({ id, ...currencies } = {}) => {
|
|
37
|
+
this.state[id] = { ...currencies };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
onWalletAdd = ({ id, ...currencies } = {}) => {
|
|
41
|
+
Object.keys(currencies).forEach((currency) => {
|
|
42
|
+
this.state[id][currency] += currencies[currency];
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.emit(this.EVENTS.WALLET.CHANGED);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
onWalletSet = ({ id, ...currencies } = {}) => {
|
|
49
|
+
this.state[id] = { ...this.state[id], ...currencies };
|
|
50
|
+
|
|
51
|
+
this.emit(this.EVENTS.WALLET.CHANGED);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
onWalletReset = ({ id } = {}) => {
|
|
55
|
+
this.state[id] = { ...this.shared.initialWallets[id] };
|
|
56
|
+
|
|
57
|
+
this.emit(this.EVENTS.WALLET.CHANGED);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
getDefaultState = () => ({});
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.feature.wallet",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
7
|
+
"publish:major:plugin": "npm run publish:major",
|
|
8
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
9
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
10
|
+
"publish:major": "npm version major && npm publish --access public",
|
|
11
|
+
"publish:minor": "npm version minor && npm publish --access public",
|
|
12
|
+
"publish:patch": "npm version patch && npm publish --access public"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"description": ""
|
|
17
|
+
}
|