@vnejs/compatibility.entities.units-with-scales 0.1.15
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/dist/index.d.ts +22 -0
- package/dist/index.js +38 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/package.json +42 -0
- package/src/index.ts +68 -0
- package/src/types.ts +18 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import "@vnejs/contracts.entities.units";
|
|
2
|
+
import "@vnejs/contracts.scales";
|
|
3
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
4
|
+
import { type ScaleState, type ScaleType } from "@vnejs/contracts.scales";
|
|
5
|
+
import type { UnitsDestroyedPayload, UnitsSpawnedPayload } from "@vnejs/contracts.entities.units";
|
|
6
|
+
type UnitScaleTemplate = Omit<ScaleState, "type">;
|
|
7
|
+
declare module "@vnejs/contracts.entities.units" {
|
|
8
|
+
interface UnitDefinition {
|
|
9
|
+
scales?: Partial<Record<ScaleType, UnitScaleTemplate>>;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
import type { UnitsScalesPluginConstants, UnitsScalesPluginEvents, UnitsScalesPluginParams, UnitsScalesPluginSettings, UnitsScalesState } from "./types.js";
|
|
13
|
+
export declare class UnitsScales extends ModuleCore<UnitsScalesPluginEvents, UnitsScalesPluginConstants, UnitsScalesPluginSettings, UnitsScalesPluginParams, UnitsScalesState> {
|
|
14
|
+
name: string;
|
|
15
|
+
subscribe: () => void;
|
|
16
|
+
onUnitsSpawned: ({ key, unit }?: UnitsSpawnedPayload) => Promise<(unknown[] | undefined)[]> | undefined;
|
|
17
|
+
onUnitsDestroyed: ({ key }?: UnitsDestroyedPayload) => Promise<(unknown[] | undefined)[]> | undefined;
|
|
18
|
+
emitDestroy: (key: string) => Promise<unknown[]> | undefined;
|
|
19
|
+
emitSpawn: (type: ScaleType, key: string, current: number, max: number) => Promise<unknown[]> | undefined;
|
|
20
|
+
getDefaultState: () => UnitsScalesState;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import "@vnejs/contracts.entities.units";
|
|
2
|
+
import "@vnejs/contracts.scales";
|
|
3
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
4
|
+
import { getScaleKey } from "@vnejs/contracts.scales";
|
|
5
|
+
import { regModule } from "@vnejs/shared";
|
|
6
|
+
export class UnitsScales extends ModuleCore {
|
|
7
|
+
name = "compatibility.entities.units-with-scales";
|
|
8
|
+
subscribe = () => {
|
|
9
|
+
this.on(this.EVENTS.UNITS.SPAWNED, this.onUnitsSpawned);
|
|
10
|
+
this.on(this.EVENTS.UNITS.DESTROYED, this.onUnitsDestroyed);
|
|
11
|
+
};
|
|
12
|
+
onUnitsSpawned = ({ key = "", unit = {} } = {}) => {
|
|
13
|
+
if (!key || !unit.scales)
|
|
14
|
+
return;
|
|
15
|
+
const scaleEntries = Object.entries(unit.scales);
|
|
16
|
+
const scaleKeys = scaleEntries.map(([scaleType]) => getScaleKey(this.PARAMS.ENTITIES.TYPES.UNIT, key, scaleType));
|
|
17
|
+
if (!scaleKeys.length)
|
|
18
|
+
return;
|
|
19
|
+
this.state[key] = scaleKeys;
|
|
20
|
+
return Promise.all(scaleEntries.map(([scaleType, entry]) => entry && this.emitSpawn(scaleType, key, entry.current, entry.max)));
|
|
21
|
+
};
|
|
22
|
+
onUnitsDestroyed = ({ key = "" } = {}) => {
|
|
23
|
+
if (!key)
|
|
24
|
+
return;
|
|
25
|
+
const scaleKeys = this.state[key];
|
|
26
|
+
if (!scaleKeys?.length)
|
|
27
|
+
return;
|
|
28
|
+
delete this.state[key];
|
|
29
|
+
return Promise.all(scaleKeys.map(this.emitDestroy));
|
|
30
|
+
};
|
|
31
|
+
emitDestroy = (key) => this.emit(this.EVENTS.SCALES.DESTROY, { key });
|
|
32
|
+
emitSpawn = (type, key, current, max) => {
|
|
33
|
+
const scaleKey = getScaleKey(this.PARAMS.ENTITIES.TYPES.UNIT, key, type);
|
|
34
|
+
return this.emit(this.EVENTS.SCALES.SPAWN, { key: scaleKey, type, current, max });
|
|
35
|
+
};
|
|
36
|
+
getDefaultState = () => ({});
|
|
37
|
+
}
|
|
38
|
+
regModule(UnitsScales);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { Params as EntitiesParams, PluginName as EntitiesPluginName } from "@vnejs/contracts.entities";
|
|
3
|
+
import type { Params as ScalesParams, PluginName as ScalesPluginName, SubscribeEvents as ScalesSubscribeEvents } from "@vnejs/contracts.scales";
|
|
4
|
+
import type { PluginName as UnitsPluginName, SubscribeEvents as UnitsSubscribeEvents } from "@vnejs/contracts.entities.units";
|
|
5
|
+
export type UnitsScalesPluginEvents = ModuleCoreEvents & Record<UnitsPluginName, UnitsSubscribeEvents> & Record<ScalesPluginName, ScalesSubscribeEvents>;
|
|
6
|
+
export type UnitsScalesPluginConstants = ModuleCoreConstants;
|
|
7
|
+
export type UnitsScalesPluginSettings = ModuleCoreSettings;
|
|
8
|
+
export type UnitsScalesPluginParams = ModuleCoreParams & Record<EntitiesPluginName, EntitiesParams> & Record<ScalesPluginName, ScalesParams>;
|
|
9
|
+
export type UnitsScalesState = Record<string, string[]>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/compatibility.entities.units-with-scales",
|
|
3
|
+
"version": "0.1.15",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@vnejs/module.core": "~0.1.0",
|
|
34
|
+
"@vnejs/contracts.entities": "~0.1.0",
|
|
35
|
+
"@vnejs/contracts.entities.units": "~0.1.0",
|
|
36
|
+
"@vnejs/contracts.scales": "~0.1.0",
|
|
37
|
+
"@vnejs/shared": "~0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@vnejs/configs.ts-common": "~0.1.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import "@vnejs/contracts.entities.units";
|
|
2
|
+
import "@vnejs/contracts.scales";
|
|
3
|
+
|
|
4
|
+
import { ModuleCore } from "@vnejs/module.core";
|
|
5
|
+
import { getScaleKey, type ScaleState, type ScaleType, type ScalesSpawnPayload } from "@vnejs/contracts.scales";
|
|
6
|
+
import type { UnitsDestroyedPayload, UnitsSpawnedPayload } from "@vnejs/contracts.entities.units";
|
|
7
|
+
import { regModule } from "@vnejs/shared";
|
|
8
|
+
|
|
9
|
+
type UnitScaleTemplate = Omit<ScaleState, "type">;
|
|
10
|
+
|
|
11
|
+
declare module "@vnejs/contracts.entities.units" {
|
|
12
|
+
interface UnitDefinition {
|
|
13
|
+
scales?: Partial<Record<ScaleType, UnitScaleTemplate>>;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
import type { UnitsScalesPluginConstants, UnitsScalesPluginEvents, UnitsScalesPluginParams, UnitsScalesPluginSettings, UnitsScalesState } from "./types.js";
|
|
18
|
+
|
|
19
|
+
export class UnitsScales extends ModuleCore<
|
|
20
|
+
UnitsScalesPluginEvents,
|
|
21
|
+
UnitsScalesPluginConstants,
|
|
22
|
+
UnitsScalesPluginSettings,
|
|
23
|
+
UnitsScalesPluginParams,
|
|
24
|
+
UnitsScalesState
|
|
25
|
+
> {
|
|
26
|
+
name = "compatibility.entities.units-with-scales";
|
|
27
|
+
|
|
28
|
+
subscribe = () => {
|
|
29
|
+
this.on(this.EVENTS.UNITS.SPAWNED, this.onUnitsSpawned);
|
|
30
|
+
this.on(this.EVENTS.UNITS.DESTROYED, this.onUnitsDestroyed);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
onUnitsSpawned = ({ key = "", unit = {} }: UnitsSpawnedPayload = {}) => {
|
|
34
|
+
if (!key || !unit.scales) return;
|
|
35
|
+
|
|
36
|
+
const scaleEntries = Object.entries(unit.scales);
|
|
37
|
+
const scaleKeys = scaleEntries.map(([scaleType]) => getScaleKey(this.PARAMS.ENTITIES.TYPES.UNIT, key, scaleType));
|
|
38
|
+
|
|
39
|
+
if (!scaleKeys.length) return;
|
|
40
|
+
|
|
41
|
+
this.state[key] = scaleKeys;
|
|
42
|
+
|
|
43
|
+
return Promise.all(scaleEntries.map(([scaleType, entry]) => entry && this.emitSpawn(scaleType as ScaleType, key, entry.current, entry.max)));
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
onUnitsDestroyed = ({ key = "" }: UnitsDestroyedPayload = {}) => {
|
|
47
|
+
if (!key) return;
|
|
48
|
+
|
|
49
|
+
const scaleKeys = this.state[key];
|
|
50
|
+
|
|
51
|
+
if (!scaleKeys?.length) return;
|
|
52
|
+
|
|
53
|
+
delete this.state[key];
|
|
54
|
+
|
|
55
|
+
return Promise.all(scaleKeys.map(this.emitDestroy));
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
emitDestroy = (key: string) => this.emit(this.EVENTS.SCALES.DESTROY, { key });
|
|
59
|
+
emitSpawn = (type: ScaleType, key: string, current: number, max: number) => {
|
|
60
|
+
const scaleKey = getScaleKey(this.PARAMS.ENTITIES.TYPES.UNIT, key, type);
|
|
61
|
+
|
|
62
|
+
return this.emit(this.EVENTS.SCALES.SPAWN, { key: scaleKey, type, current, max } satisfies ScalesSpawnPayload);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
getDefaultState = (): UnitsScalesState => ({});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
regModule(UnitsScales);
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
|
|
2
|
+
import type { Params as EntitiesParams, PluginName as EntitiesPluginName } from "@vnejs/contracts.entities";
|
|
3
|
+
import type { Params as ScalesParams, PluginName as ScalesPluginName, SubscribeEvents as ScalesSubscribeEvents } from "@vnejs/contracts.scales";
|
|
4
|
+
import type { PluginName as UnitsPluginName, SubscribeEvents as UnitsSubscribeEvents } from "@vnejs/contracts.entities.units";
|
|
5
|
+
|
|
6
|
+
export type UnitsScalesPluginEvents = ModuleCoreEvents &
|
|
7
|
+
Record<UnitsPluginName, UnitsSubscribeEvents> &
|
|
8
|
+
Record<ScalesPluginName, ScalesSubscribeEvents>;
|
|
9
|
+
|
|
10
|
+
export type UnitsScalesPluginConstants = ModuleCoreConstants;
|
|
11
|
+
|
|
12
|
+
export type UnitsScalesPluginSettings = ModuleCoreSettings;
|
|
13
|
+
|
|
14
|
+
export type UnitsScalesPluginParams = ModuleCoreParams &
|
|
15
|
+
Record<EntitiesPluginName, EntitiesParams> &
|
|
16
|
+
Record<ScalesPluginName, ScalesParams>;
|
|
17
|
+
|
|
18
|
+
export type UnitsScalesState = Record<string, string[]>;
|