@vnejs/plugins.canvas.child.bg 0.1.30

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.
@@ -0,0 +1 @@
1
+ import "@vnejs/contracts.canvas.child.bg";
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ import "@vnejs/contracts.canvas.child.bg";
2
+ import { regPlugin } from "@vnejs/shared";
3
+ import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.canvas.child.bg";
4
+ import { LayerBgChange } from "./modules/change.js";
5
+ import { LayerBgHide } from "./modules/hide.js";
6
+ import { LayerBgPreload } from "./modules/preload.js";
7
+ import { LayerBgShow } from "./modules/show.js";
8
+ import { LayerBgStaff } from "./modules/staff.js";
9
+ import { LayerBgState } from "./modules/state.js";
10
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS }, [
11
+ LayerBgShow,
12
+ LayerBgHide,
13
+ LayerBgStaff,
14
+ LayerBgState,
15
+ LayerBgChange,
16
+ LayerBgPreload,
17
+ ]);
@@ -0,0 +1,10 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
3
+ import type { BgChangePayload } from "../utils/bg.js";
4
+ export declare class LayerBgChange extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onBgChange: ({ id, name, layer, duration, args }?: BgChangePayload) => Promise<unknown[] | undefined>;
8
+ getBgId: (id: string | undefined, layer: string) => Promise<string>;
9
+ emitChildChange: (layer: string, childId: string, duration: number, props: Record<string, unknown>) => Promise<unknown[]> | undefined;
10
+ }
@@ -0,0 +1,34 @@
1
+ import { createPromiseResolve } from "@vnejs/helpers";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+ export class LayerBgChange extends ModuleCore {
4
+ name = "layer.bg.change";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.LAYER_BG.CHANGE, this.onBgChange);
7
+ };
8
+ onBgChange = async ({ id, name, layer, duration, args = {} } = {}) => {
9
+ if (!name || !layer)
10
+ return;
11
+ const realId = await this.getBgId(id, layer);
12
+ if (!realId)
13
+ return;
14
+ const clonedArgs = await this.emitClone(args);
15
+ const src = await this.emitOne(this.EVENTS.LAYER_BG.SRC_GET, { name, layer, args: clonedArgs });
16
+ const changeArgs = { src, isAllSrcRendered: Boolean(clonedArgs.parts), name, args: clonedArgs };
17
+ await this.emit(this.EVENTS.LAYER_BG.STATE_UPDATE, { id: realId, name, args: clonedArgs });
18
+ if (!duration || this.shared.viewForceAnimationSources.isNotEmpty())
19
+ return this.emitChildChange(layer, realId, 0, { ...changeArgs, transition: 0 });
20
+ const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
21
+ return this.emit(this.EVENTS.INTERACT.PUSH_POP, {
22
+ key: `${this.name}_${layer}_${name}`,
23
+ skipHandler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve),
24
+ handler: () => Promise.race([this.emitChildChange(layer, realId, Number(duration), { ...changeArgs, transition: duration }), skipPromise]),
25
+ });
26
+ };
27
+ getBgId = async (id, layer) => {
28
+ if (id)
29
+ return id;
30
+ const oldChilds = (await this.emitOne(this.EVENTS.LAYER_BG.STATE_GET, { layer }));
31
+ return oldChilds.length === 1 ? oldChilds[0].id : "";
32
+ };
33
+ emitChildChange = (layer, childId, duration, props) => this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration, props });
34
+ }
@@ -0,0 +1,10 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
3
+ import type { BgHidePayload, TransitionProps } from "../utils/bg.js";
4
+ export declare class LayerBgHide extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onBgHide: ({ layer, transition, duration }?: BgHidePayload) => Promise<void>;
8
+ getTransitionProps: (transition: string, duration: number) => TransitionProps;
9
+ emitStateRm: (id: string) => Promise<unknown[]> | undefined;
10
+ }
@@ -0,0 +1,30 @@
1
+ import { createPromiseResolve } from "@vnejs/helpers";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+ export class LayerBgHide extends ModuleCore {
4
+ name = "layer.bg.hide";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.LAYER_BG.HIDE, this.onBgHide);
7
+ };
8
+ onBgHide = async ({ layer, transition, duration } = {}) => {
9
+ const oldChildsId = (await this.emitOne(this.EVENTS.LAYER_BG.STATE_GET, { layer })).map((item) => item.id);
10
+ if (transition && duration && this.shared.viewForceAnimationSources.isEmpty()) {
11
+ const { initialProps = {}, finishProps = {} } = this.getTransitionProps(transition, duration);
12
+ const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
13
+ const key = `${this.name}_${layer}`;
14
+ await Promise.all(oldChildsId.map((childId) => this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration: 0, props: initialProps })));
15
+ await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
16
+ const promises = oldChildsId.map((childId) => this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration, props: finishProps }));
17
+ await Promise.race([Promise.all(promises), skipPromise]);
18
+ await this.emit(this.EVENTS.INTERACT.POP, { key });
19
+ }
20
+ await Promise.all(oldChildsId.map((childId) => this.emitStateRm(childId)));
21
+ await this.emit(this.EVENTS.LAYER.CHILDS_RM, { layer, childsIds: oldChildsId });
22
+ };
23
+ getTransitionProps = (transition, duration) => {
24
+ const transitions = this.PARAMS.LAYER_BG.TRANSITIONS_HIDE;
25
+ return transitions[transition]
26
+ ? transitions[transition](duration)
27
+ : transitions.opacity(duration);
28
+ };
29
+ emitStateRm = (id) => this.emit(this.EVENTS.LAYER_BG.STATE_RM, { id });
30
+ }
@@ -0,0 +1,9 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
3
+ import type { BgPreloadPayload } from "../utils/bg.js";
4
+ export declare class LayerBgPreload extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onPreload: ({ name, mediaType, priority, parts }?: BgPreloadPayload) => Promise<unknown[] | undefined>;
8
+ loadMedia: (name: string, args: Record<string, unknown>) => Promise<unknown[]> | undefined;
9
+ }
@@ -0,0 +1,19 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class LayerBgPreload extends ModuleCore {
3
+ name = "layer.bg.preload";
4
+ subscribe = () => {
5
+ this.on(this.EVENTS.LAYER_BG.PRELOAD, this.onPreload);
6
+ };
7
+ onPreload = async ({ name, mediaType, priority = this.CONST.MEDIA.PRIORITIES.BACKGROUND, parts } = {}) => {
8
+ if (!name)
9
+ return;
10
+ const [type, quality] = [this.CONST.MEDIA.TYPES.IMAGE, this.shared.settings[this.SETTINGS.LAYER.QUALITY]];
11
+ const args = { type, mediaType, priority, quality };
12
+ if (!parts)
13
+ return this.loadMedia(name, args);
14
+ const promises = [this.loadMedia(`${name}/index`, args)];
15
+ Object.entries(parts).forEach(([key, value]) => promises.push(this.loadMedia(`${name}/${key}/${value}`, args)));
16
+ return Promise.all(promises);
17
+ };
18
+ loadMedia = (name, args) => this.emit(this.EVENTS.MEDIA.LOAD, { name, ...args });
19
+ }
@@ -0,0 +1,10 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
3
+ import type { BgShowPayload, TransitionProps } from "../utils/bg.js";
4
+ export declare class LayerBgShow extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onBgShow: ({ name, layer, transition, duration, args, hideOldBgsOnShow }?: BgShowPayload) => Promise<[...(unknown[] | undefined)[], unknown[] | undefined] | undefined>;
8
+ getTransitionProps: (transition: string, duration: number) => TransitionProps;
9
+ emitStateRm: (id: string) => Promise<unknown[]> | undefined;
10
+ }
@@ -0,0 +1,40 @@
1
+ import { createPromiseResolve } from "@vnejs/helpers";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+ export class LayerBgShow extends ModuleCore {
4
+ name = "layer.bg.show";
5
+ subscribe = () => {
6
+ this.on(this.EVENTS.LAYER_BG.SHOW, this.onBgShow);
7
+ };
8
+ onBgShow = async ({ name, layer, transition, duration, args = {}, hideOldBgsOnShow = true } = {}) => {
9
+ const clonedArgs = await this.emitClone(args);
10
+ const shouldHideOldImages = hideOldBgsOnShow || clonedArgs.hideOldBgsOnShow;
11
+ const id = clonedArgs.id || (await this.emitOne(this.EVENTS.VENDORS.UID));
12
+ const oldChildsId = shouldHideOldImages
13
+ ? (await this.emitOne(this.EVENTS.LAYER_BG.STATE_GET, { layer })).map((item) => item.id)
14
+ : [];
15
+ const src = await this.emitOne(this.EVENTS.LAYER_BG.SRC_GET, { name, layer, args: clonedArgs });
16
+ const childCommon = { type: this.CONST.LAYER_BG.LAYER_CHILD_TYPE, id, src, isAllSrcRendered: Boolean(clonedArgs.parts), name, args };
17
+ await this.emit(this.EVENTS.LAYER_BG.STATE_ADD, { id, name, layer, args: clonedArgs });
18
+ if (!duration || !transition || this.shared.viewForceAnimationSources.isNotEmpty()) {
19
+ await this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: childCommon });
20
+ }
21
+ else {
22
+ const { initialProps = {}, finishProps = {} } = this.getTransitionProps(transition, duration);
23
+ const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
24
+ const [key, changeArgs] = [`${this.name}_${layer}_${name}`, { layer, childId: id, duration, props: finishProps }];
25
+ await this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: { ...childCommon, ...initialProps } });
26
+ await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
27
+ await Promise.race([this.emit(this.EVENTS.LAYER.CHILD_CHANGE, changeArgs), skipPromise]);
28
+ await this.emit(this.EVENTS.INTERACT.POP, { key });
29
+ }
30
+ if (shouldHideOldImages)
31
+ return Promise.all([...oldChildsId.map((childId) => this.emitStateRm(childId)), this.emit(this.EVENTS.LAYER.CHILDS_RM, { layer, childsIds: oldChildsId })]);
32
+ };
33
+ getTransitionProps = (transition, duration) => {
34
+ const transitions = this.PARAMS.LAYER_BG.TRANSITIONS_SHOW;
35
+ return transitions[transition]
36
+ ? transitions[transition](duration)
37
+ : transitions.opacity(duration);
38
+ };
39
+ emitStateRm = (id) => this.emit(this.EVENTS.LAYER_BG.STATE_RM, { id });
40
+ }
@@ -0,0 +1,17 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
3
+ import type { BgSrcGetPayload } from "../utils/bg.js";
4
+ export declare class LayerBgStaff extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onBgGetSrc: ({ name, args }?: BgSrcGetPayload) => Promise<string | {
8
+ url: string;
9
+ }[]>;
10
+ getBgSrcParts: (name: string, parts: Record<string, string>) => Promise<{
11
+ url: string;
12
+ }[]>;
13
+ getBgSrcOne: (name: string) => Promise<string>;
14
+ getBgMedia: (name: string) => Promise<{
15
+ src: string;
16
+ }>;
17
+ }
@@ -0,0 +1,25 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ import { applyArgsInjections } from "../utils/bg.js";
3
+ const mediaToSrcs = (q) => ({ url: q.src });
4
+ export class LayerBgStaff extends ModuleCore {
5
+ name = "layer.bg.staff";
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.LAYER_BG.SRC_GET, this.onBgGetSrc);
8
+ };
9
+ onBgGetSrc = async ({ name = "", args = {} } = {}) => {
10
+ const clonedArgs = await this.emitClone(args);
11
+ const injections = (await this.emitReals(this.EVENTS.LAYER_BG.ARGS_INJECT, { args: clonedArgs }));
12
+ const { parts } = applyArgsInjections(clonedArgs, injections);
13
+ return parts ? this.getBgSrcParts(name, parts) : this.getBgSrcOne(name);
14
+ };
15
+ getBgSrcParts = async (name, parts) => {
16
+ const partsPromises = Object.entries(parts).map(([partName, partValue]) => this.getBgMedia(`${name}/${partName}/${partValue}`));
17
+ return (await Promise.all([this.getBgMedia(`${name}/index`), ...partsPromises])).map(mediaToSrcs);
18
+ };
19
+ getBgSrcOne = async (name) => (await this.getBgMedia(name)).src;
20
+ getBgMedia = (name) => {
21
+ const [type, quality, mediaType] = [this.CONST.MEDIA.TYPES.IMAGE, this.shared.settings[this.SETTINGS.LAYER.QUALITY], "canvas"];
22
+ const mediaName = `${this.CONST.LAYER_BG.LAYER_CHILD_TYPE}/${name}`;
23
+ return this.emitOne(this.EVENTS.MEDIA.GET, { name: mediaName, mediaType, type, quality });
24
+ };
25
+ }
@@ -0,0 +1,29 @@
1
+ import { ModuleCore, type ModuleGlobalStateRegistry } from "@vnejs/module.core";
2
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
3
+ import type { BgRecord, BgState, BgStateIdPayload, BgStateLayerPayload } from "../utils/bg.js";
4
+ export declare class LayerBgState extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams, BgState> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ onBgStateUpdate: ({ id, ...image }?: BgStateIdPayload) => void;
8
+ onBgStateGet: ({ layer }?: BgStateLayerPayload) => BgRecord[];
9
+ onBgStateGetAll: () => BgRecord[];
10
+ onBgStateAdd: ({ id, name, layer, args }?: BgStateIdPayload) => void;
11
+ onBgStateRm: ({ id }?: BgStateIdPayload) => void;
12
+ onStateSet: (payload: ModuleGlobalStateRegistry) => Promise<void>;
13
+ onStateClear: () => Promise<void>;
14
+ onStatePreload: ({ [this.name]: state }: Record<string, BgState>) => Promise<(unknown[] | undefined)[]>;
15
+ onMemoryCleared: () => Promise<void>;
16
+ clearState: () => Promise<void>;
17
+ preloadState: (state: BgState) => Promise<(unknown[] | undefined)[]>;
18
+ cloneBgAndChangeWithNewId: (bg: BgRecord, id: string) => Promise<unknown>;
19
+ cloneBgAndChange: (bg: BgRecord) => Promise<unknown>;
20
+ cloneBgAndHide: (bg: BgRecord) => Promise<unknown>;
21
+ cloneBgAndShow: (bg: BgRecord) => Promise<unknown>;
22
+ emitEventWithBg: (bg: BgRecord, cb: (bg: BgRecord) => unknown) => unknown;
23
+ emitBgSrcGet: (bg: BgRecord) => Promise<unknown[]> | undefined;
24
+ emitBgChange: (bg: BgRecord) => Promise<unknown[]> | undefined;
25
+ emitBgHide: (bg: BgRecord) => Promise<unknown[]> | undefined;
26
+ emitBgShow: (bg: BgRecord) => Promise<unknown[]> | undefined;
27
+ getBgsByLayerFromState: (acc: Record<string, BgRecord[]>, img: BgRecord) => Record<string, BgRecord[]>;
28
+ getDefaultState: () => BgState;
29
+ }
@@ -0,0 +1,79 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+ export class LayerBgState extends ModuleCore {
3
+ name = "layer.bg.state";
4
+ subscribe = () => {
5
+ this.on(this.EVENTS.LAYER_BG.STATE_UPDATE, this.onBgStateUpdate);
6
+ this.on(this.EVENTS.LAYER_BG.STATE_GET, this.onBgStateGet);
7
+ this.on(this.EVENTS.LAYER_BG.STATE_GET_ALL, this.onBgStateGetAll);
8
+ this.on(this.EVENTS.LAYER_BG.STATE_ADD, this.onBgStateAdd);
9
+ this.on(this.EVENTS.LAYER_BG.STATE_RM, this.onBgStateRm);
10
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
11
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
12
+ this.on(this.EVENTS.STATE.PRELOAD, this.onStatePreload);
13
+ this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
14
+ };
15
+ onBgStateUpdate = ({ id, ...image } = {}) => {
16
+ this.state.bgs = this.state.bgs.map((e) => (e.id === id ? { ...e, ...image } : e));
17
+ };
18
+ onBgStateGet = ({ layer } = {}) => this.state.bgs.filter((e) => e.layer === layer);
19
+ onBgStateGetAll = () => this.state.bgs;
20
+ onBgStateAdd = ({ id, name, layer, args } = {}) => {
21
+ this.state.bgs.push({ id: id, name, layer, args });
22
+ };
23
+ onBgStateRm = ({ id } = {}) => {
24
+ this.state.bgs = this.state.bgs.filter((e) => e.id !== id);
25
+ };
26
+ onStateSet = async (payload) => {
27
+ const clonedStateOld = await this.emitClone(this.state);
28
+ const clonedStateNew = await this.emitClone(this.getStateSlice(payload));
29
+ await this.preloadState(clonedStateNew);
30
+ const oldBgsByLayer = clonedStateOld.bgs.reduce(this.getBgsByLayerFromState, {});
31
+ const newBgsByLayer = clonedStateNew.bgs.reduce(this.getBgsByLayerFromState, {});
32
+ const [promises, uniqLayers, oldLayers, newLayers] = [[], [], Object.keys(oldBgsByLayer), Object.keys(newBgsByLayer)];
33
+ oldLayers.forEach((layer) => uniqLayers.push(layer));
34
+ newLayers.forEach((layer) => !uniqLayers.includes(layer) && uniqLayers.push(layer));
35
+ uniqLayers.forEach((layer) => {
36
+ const oldBgs = oldBgsByLayer[layer] || [];
37
+ const newBgs = newBgsByLayer[layer] || [];
38
+ const bgsToChange = oldBgs.slice(0, newBgs.length);
39
+ const bgsToShow = newBgs.slice(oldBgs.length);
40
+ const bgsToHide = oldBgs.slice(newBgs.length);
41
+ promises.push(...bgsToChange.map((img, i) => this.cloneBgAndChangeWithNewId(newBgs[i], img.id)));
42
+ promises.push(...bgsToShow.map(this.cloneBgAndShow));
43
+ promises.push(...bgsToHide.map(this.cloneBgAndHide));
44
+ });
45
+ this.shared.viewForceAnimationSources.add(`${this.name}.set`);
46
+ await Promise.all(promises);
47
+ this.shared.viewForceAnimationSources.rm(`${this.name}.set`);
48
+ };
49
+ onStateClear = () => this.clearState();
50
+ onStatePreload = ({ [this.name]: state }) => this.preloadState(state);
51
+ onMemoryCleared = async () => {
52
+ await this.preloadState(this.state);
53
+ this.shared.viewForceAnimationSources.add(`${this.name}.memory`);
54
+ await Promise.all(this.state.bgs.map(this.cloneBgAndChange));
55
+ this.shared.viewForceAnimationSources.rm(`${this.name}.memory`);
56
+ };
57
+ clearState = async () => {
58
+ this.shared.viewForceAnimationSources.add(`${this.name}.clear`);
59
+ await Promise.all(this.state.bgs.map(this.cloneBgAndHide));
60
+ this.shared.viewForceAnimationSources.rm(`${this.name}.clear`);
61
+ };
62
+ preloadState = (state) => Promise.all(state.bgs.map(this.emitBgSrcGet));
63
+ cloneBgAndChangeWithNewId = async (bg, id) => this.emitEventWithBg({ ...(await this.emitClone(bg)), id }, this.emitBgChange);
64
+ cloneBgAndChange = async (bg) => this.emitEventWithBg(await this.emitClone(bg), this.emitBgChange);
65
+ cloneBgAndHide = async (bg) => this.emitEventWithBg(await this.emitClone(bg), this.emitBgHide);
66
+ cloneBgAndShow = async (bg) => this.emitEventWithBg(await this.emitClone(bg), this.emitBgShow);
67
+ emitEventWithBg = (bg, cb) => cb(bg);
68
+ emitBgSrcGet = (bg) => this.emit(this.EVENTS.LAYER_BG.SRC_GET, bg);
69
+ emitBgChange = (bg) => this.emit(this.EVENTS.LAYER_BG.CHANGE, bg);
70
+ emitBgHide = (bg) => this.emit(this.EVENTS.LAYER_BG.HIDE, bg);
71
+ emitBgShow = (bg) => this.emit(this.EVENTS.LAYER_BG.SHOW, bg);
72
+ getBgsByLayerFromState = (acc, img) => {
73
+ if (!acc[img.layer])
74
+ acc[img.layer] = [];
75
+ acc[img.layer].push(img);
76
+ return acc;
77
+ };
78
+ getDefaultState = () => ({ bgs: [] });
79
+ }
@@ -0,0 +1,7 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ import type { Constants, Params, PluginName, SubscribeEvents } from "@vnejs/contracts.canvas.child.bg";
3
+ import type { PluginName as LayerPluginName, SettingsKeys as LayerSettingsKeys, SubscribeEvents as LayerSubscribeEvents } from "@vnejs/contracts.canvas.layer";
4
+ export type BgPluginEvents = ModuleCoreEvents & Record<PluginName, SubscribeEvents> & Record<LayerPluginName, LayerSubscribeEvents>;
5
+ export type BgPluginConstants = ModuleCoreConstants & Record<PluginName, Constants>;
6
+ export type BgPluginSettings = ModuleCoreSettings & Record<LayerPluginName, LayerSettingsKeys>;
7
+ export type BgPluginParams = ModuleCoreParams & Record<PluginName, Params>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,57 @@
1
+ import type { BgArgs, BgPreloadPayload } from "@vnejs/contracts.canvas.child.bg";
2
+ export type { BgArgs, BgPreloadPayload };
3
+ export type BgArgsInject = Partial<BgArgs>;
4
+ export type BgArgsInjectPayload = {
5
+ args?: BgArgs;
6
+ };
7
+ export declare const applyArgsInjections: (base: BgArgs, injections: BgArgsInject[]) => BgArgs;
8
+ export type BgRecord = {
9
+ id: string;
10
+ name?: string;
11
+ layer?: string;
12
+ args?: BgArgs;
13
+ transition?: string;
14
+ duration?: number;
15
+ hideOldBgsOnShow?: boolean;
16
+ };
17
+ export type BgState = {
18
+ bgs: BgRecord[];
19
+ };
20
+ export type BgShowPayload = {
21
+ name?: string;
22
+ layer?: string;
23
+ transition?: string;
24
+ duration?: number;
25
+ args?: BgArgs;
26
+ hideOldBgsOnShow?: boolean;
27
+ };
28
+ export type BgHidePayload = {
29
+ layer?: string;
30
+ transition?: string;
31
+ duration?: number;
32
+ };
33
+ export type BgChangePayload = {
34
+ id?: string;
35
+ name?: string;
36
+ layer?: string;
37
+ duration?: number;
38
+ args?: BgArgs;
39
+ };
40
+ export type BgSrcGetPayload = {
41
+ name?: string;
42
+ layer?: string;
43
+ args?: BgArgs;
44
+ };
45
+ export type BgStateLayerPayload = {
46
+ layer?: string;
47
+ };
48
+ export type BgStateIdPayload = {
49
+ id?: string;
50
+ name?: string;
51
+ layer?: string;
52
+ args?: BgArgs;
53
+ };
54
+ export type TransitionProps = {
55
+ initialProps?: Record<string, unknown>;
56
+ finishProps?: Record<string, unknown>;
57
+ };
@@ -0,0 +1,5 @@
1
+ export const applyArgsInjections = (base, injections) => injections.reduce((acc, inj) => ({
2
+ ...acc,
3
+ ...inj,
4
+ parts: inj.parts ? { ...acc.parts, ...inj.parts } : acc.parts,
5
+ }), base);
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@vnejs/plugins.canvas.child.bg",
3
+ "version": "0.1.30",
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": "npx @vnejs/monorepo test",
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
+ "dependencies": {
33
+ "@vnejs/contracts.canvas.child.bg": "~0.1.0"
34
+ },
35
+ "peerDependencies": {
36
+ "@vnejs/helpers": "~0.1.0",
37
+ "@vnejs/module.core": "~0.1.0",
38
+ "@vnejs/contracts.canvas.layer": "~0.1.0",
39
+ "@vnejs/shared": "~0.1.0"
40
+ },
41
+ "devDependencies": {
42
+ "@vnejs/configs.ts-common": "~0.1.0",
43
+ "@vnejs/configs.vitest": "~0.1.0",
44
+ "@vnejs/test-utils": "~0.1.0",
45
+ "@vnejs/contracts.canvas.layer": "~0.1.0",
46
+ "@vnejs/contracts.canvas.child.bg": "~0.1.0",
47
+ "@vnejs/helpers": "~0.1.0",
48
+ "@vnejs/sources-set": "~0.1.0",
49
+ "@vnejs/module.core": "~0.1.0",
50
+ "@vnejs/shared": "~0.1.0"
51
+ }
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1,20 @@
1
+ import "@vnejs/contracts.canvas.child.bg";
2
+
3
+ import { regPlugin } from "@vnejs/shared";
4
+ import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.canvas.child.bg";
5
+
6
+ import { LayerBgChange } from "./modules/change.js";
7
+ import { LayerBgHide } from "./modules/hide.js";
8
+ import { LayerBgPreload } from "./modules/preload.js";
9
+ import { LayerBgShow } from "./modules/show.js";
10
+ import { LayerBgStaff } from "./modules/staff.js";
11
+ import { LayerBgState } from "./modules/state.js";
12
+
13
+ regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS }, [
14
+ LayerBgShow,
15
+ LayerBgHide,
16
+ LayerBgStaff,
17
+ LayerBgState,
18
+ LayerBgChange,
19
+ LayerBgPreload,
20
+ ]);
@@ -0,0 +1,50 @@
1
+ import { createPromiseResolve } from "@vnejs/helpers";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+
4
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
5
+ import type { BgArgs, BgChangePayload, BgRecord } from "../utils/bg.js";
6
+
7
+ export class LayerBgChange extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
8
+ name = "layer.bg.change";
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.LAYER_BG.CHANGE, this.onBgChange);
12
+ };
13
+
14
+ onBgChange = async ({ id, name, layer, duration, args = {} }: BgChangePayload = {}) => {
15
+ if (!name || !layer) return;
16
+
17
+ const realId = await this.getBgId(id, layer);
18
+
19
+ if (!realId) return;
20
+
21
+ const clonedArgs = await this.emitClone<BgArgs>(args);
22
+
23
+ const src = await this.emitOne(this.EVENTS.LAYER_BG.SRC_GET, { name, layer, args: clonedArgs });
24
+
25
+ const changeArgs = { src, isAllSrcRendered: Boolean(clonedArgs.parts), name, args: clonedArgs };
26
+
27
+ await this.emit(this.EVENTS.LAYER_BG.STATE_UPDATE, { id: realId, name, args: clonedArgs });
28
+
29
+ if (!duration || this.shared.viewForceAnimationSources.isNotEmpty()) return this.emitChildChange(layer, realId, 0, { ...changeArgs, transition: 0 });
30
+
31
+ const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
32
+
33
+ return this.emit(this.EVENTS.INTERACT.PUSH_POP, {
34
+ key: `${this.name}_${layer}_${name}`,
35
+ skipHandler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve),
36
+ handler: () => Promise.race([this.emitChildChange(layer, realId, Number(duration), { ...changeArgs, transition: duration }), skipPromise]),
37
+ });
38
+ };
39
+
40
+ getBgId = async (id: string | undefined, layer: string) => {
41
+ if (id) return id;
42
+
43
+ const oldChilds = (await this.emitOne(this.EVENTS.LAYER_BG.STATE_GET, { layer })) as BgRecord[];
44
+
45
+ return oldChilds.length === 1 ? oldChilds[0].id : "";
46
+ };
47
+
48
+ emitChildChange = (layer: string, childId: string, duration: number, props: Record<string, unknown>) =>
49
+ this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration, props });
50
+ }
@@ -0,0 +1,42 @@
1
+ import { createPromiseResolve } from "@vnejs/helpers";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+
4
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
5
+ import type { BgHidePayload, BgStateIdPayload, TransitionProps } from "../utils/bg.js";
6
+
7
+ export class LayerBgHide extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
8
+ name = "layer.bg.hide";
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.LAYER_BG.HIDE, this.onBgHide);
12
+ };
13
+
14
+ onBgHide = async ({ layer, transition, duration }: BgHidePayload = {}) => {
15
+ const oldChildsId = ((await this.emitOne(this.EVENTS.LAYER_BG.STATE_GET, { layer })) as BgStateIdPayload[]).map((item) => item.id!);
16
+
17
+ if (transition && duration && this.shared.viewForceAnimationSources.isEmpty()) {
18
+ const { initialProps = {}, finishProps = {} } = this.getTransitionProps(transition, duration);
19
+ const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
20
+ const key = `${this.name}_${layer}`;
21
+
22
+ await Promise.all(oldChildsId.map((childId) => this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration: 0, props: initialProps })));
23
+ await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
24
+ const promises = oldChildsId.map((childId) => this.emit(this.EVENTS.LAYER.CHILD_CHANGE, { layer, childId, duration, props: finishProps }));
25
+ await Promise.race([Promise.all(promises), skipPromise]);
26
+ await this.emit(this.EVENTS.INTERACT.POP, { key });
27
+ }
28
+
29
+ await Promise.all(oldChildsId.map((childId) => this.emitStateRm(childId)));
30
+ await this.emit(this.EVENTS.LAYER.CHILDS_RM, { layer, childsIds: oldChildsId });
31
+ };
32
+
33
+ getTransitionProps = (transition: string, duration: number): TransitionProps => {
34
+ const transitions = this.PARAMS.LAYER_BG.TRANSITIONS_HIDE;
35
+
36
+ return transitions[transition as keyof typeof transitions]
37
+ ? transitions[transition as keyof typeof transitions](duration)
38
+ : transitions.opacity(duration);
39
+ };
40
+
41
+ emitStateRm = (id: string) => this.emit(this.EVENTS.LAYER_BG.STATE_RM, { id });
42
+ }
@@ -0,0 +1,28 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
4
+ import type { BgPreloadPayload } from "../utils/bg.js";
5
+
6
+ export class LayerBgPreload extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
7
+ name = "layer.bg.preload";
8
+
9
+ subscribe = () => {
10
+ this.on(this.EVENTS.LAYER_BG.PRELOAD, this.onPreload);
11
+ };
12
+
13
+ onPreload = async ({ name, mediaType, priority = this.CONST.MEDIA.PRIORITIES.BACKGROUND, parts }: BgPreloadPayload = {}) => {
14
+ if (!name) return;
15
+
16
+ const [type, quality] = [this.CONST.MEDIA.TYPES.IMAGE, this.shared.settings[this.SETTINGS.LAYER.QUALITY]!];
17
+ const args = { type, mediaType, priority, quality };
18
+
19
+ if (!parts) return this.loadMedia(name, args);
20
+
21
+ const promises = [this.loadMedia(`${name}/index`, args)];
22
+ Object.entries(parts).forEach(([key, value]) => promises.push(this.loadMedia(`${name}/${key}/${value}`, args)));
23
+
24
+ return Promise.all(promises);
25
+ };
26
+
27
+ loadMedia = (name: string, args: Record<string, unknown>) => this.emit(this.EVENTS.MEDIA.LOAD, { name, ...args });
28
+ }
@@ -0,0 +1,55 @@
1
+ import { createPromiseResolve } from "@vnejs/helpers";
2
+ import { ModuleCore } from "@vnejs/module.core";
3
+
4
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
5
+ import type { BgArgs, BgShowPayload, BgStateIdPayload, TransitionProps } from "../utils/bg.js";
6
+
7
+ export class LayerBgShow extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
8
+ name = "layer.bg.show";
9
+
10
+ subscribe = () => {
11
+ this.on(this.EVENTS.LAYER_BG.SHOW, this.onBgShow);
12
+ };
13
+
14
+ onBgShow = async ({ name, layer, transition, duration, args = {}, hideOldBgsOnShow = true }: BgShowPayload = {}) => {
15
+ const clonedArgs = await this.emitClone<BgArgs>(args);
16
+
17
+ const shouldHideOldImages = hideOldBgsOnShow || clonedArgs.hideOldBgsOnShow;
18
+
19
+ const id = clonedArgs.id || (await this.emitOne(this.EVENTS.VENDORS.UID));
20
+ const oldChildsId = shouldHideOldImages
21
+ ? ((await this.emitOne(this.EVENTS.LAYER_BG.STATE_GET, { layer })) as BgStateIdPayload[]).map((item) => item.id!)
22
+ : [];
23
+ const src = await this.emitOne(this.EVENTS.LAYER_BG.SRC_GET, { name, layer, args: clonedArgs });
24
+
25
+ const childCommon = { type: this.CONST.LAYER_BG.LAYER_CHILD_TYPE, id, src, isAllSrcRendered: Boolean(clonedArgs.parts), name, args };
26
+
27
+ await this.emit(this.EVENTS.LAYER_BG.STATE_ADD, { id, name, layer, args: clonedArgs });
28
+
29
+ if (!duration || !transition || this.shared.viewForceAnimationSources.isNotEmpty()) {
30
+ await this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: childCommon });
31
+ } else {
32
+ const { initialProps = {}, finishProps = {} } = this.getTransitionProps(transition, duration);
33
+ const { resolve: skipResolve, promise: skipPromise } = createPromiseResolve();
34
+ const [key, changeArgs] = [`${this.name}_${layer}_${name}`, { layer, childId: id, duration, props: finishProps }];
35
+
36
+ await this.emit(this.EVENTS.LAYER.CHILD_ADD, { layer, child: { ...childCommon, ...initialProps } });
37
+ await this.emit(this.EVENTS.INTERACT.PUSH, { key, handler: () => void this.emit(this.EVENTS.LAYER.UPDATE_FORCE, { layer })?.then(skipResolve) });
38
+ await Promise.race([this.emit(this.EVENTS.LAYER.CHILD_CHANGE, changeArgs), skipPromise]);
39
+ await this.emit(this.EVENTS.INTERACT.POP, { key });
40
+ }
41
+
42
+ if (shouldHideOldImages)
43
+ return Promise.all([...oldChildsId.map((childId) => this.emitStateRm(childId)), this.emit(this.EVENTS.LAYER.CHILDS_RM, { layer, childsIds: oldChildsId })]);
44
+ };
45
+
46
+ getTransitionProps = (transition: string, duration: number): TransitionProps => {
47
+ const transitions = this.PARAMS.LAYER_BG.TRANSITIONS_SHOW;
48
+
49
+ return transitions[transition as keyof typeof transitions]
50
+ ? transitions[transition as keyof typeof transitions](duration)
51
+ : transitions.opacity(duration);
52
+ };
53
+
54
+ emitStateRm = (id: string) => this.emit(this.EVENTS.LAYER_BG.STATE_RM, { id });
55
+ }
@@ -0,0 +1,38 @@
1
+ import { ModuleCore } from "@vnejs/module.core";
2
+
3
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
4
+ import type { BgArgs, BgArgsInject, BgSrcGetPayload } from "../utils/bg.js";
5
+ import { applyArgsInjections } from "../utils/bg.js";
6
+
7
+ const mediaToSrcs = (q: { src: string }) => ({ url: q.src });
8
+
9
+ export class LayerBgStaff extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams> {
10
+ name = "layer.bg.staff";
11
+
12
+ subscribe = () => {
13
+ this.on(this.EVENTS.LAYER_BG.SRC_GET, this.onBgGetSrc);
14
+ };
15
+
16
+ onBgGetSrc = async ({ name = "", args = {} }: BgSrcGetPayload = {}) => {
17
+ const clonedArgs = await this.emitClone<BgArgs>(args);
18
+ const injections = (await this.emitReals(this.EVENTS.LAYER_BG.ARGS_INJECT, { args: clonedArgs })) as BgArgsInject[];
19
+ const { parts } = applyArgsInjections(clonedArgs, injections);
20
+
21
+ return parts ? this.getBgSrcParts(name, parts) : this.getBgSrcOne(name);
22
+ };
23
+
24
+ getBgSrcParts = async (name: string, parts: Record<string, string>) => {
25
+ const partsPromises = Object.entries(parts).map(([partName, partValue]) => this.getBgMedia(`${name}/${partName}/${partValue}`));
26
+
27
+ return (await Promise.all([this.getBgMedia(`${name}/index`), ...partsPromises])).map(mediaToSrcs);
28
+ };
29
+
30
+ getBgSrcOne = async (name: string) => (await this.getBgMedia(name)).src;
31
+
32
+ getBgMedia = (name: string) => {
33
+ const [type, quality, mediaType] = [this.CONST.MEDIA.TYPES.IMAGE, this.shared.settings[this.SETTINGS.LAYER.QUALITY]!, "canvas"];
34
+ const mediaName = `${this.CONST.LAYER_BG.LAYER_CHILD_TYPE}/${name}`;
35
+
36
+ return this.emitOne(this.EVENTS.MEDIA.GET, { name: mediaName, mediaType, type, quality }) as Promise<{ src: string }>;
37
+ };
38
+ }
@@ -0,0 +1,114 @@
1
+ import { ModuleCore, type ModuleGlobalStateRegistry } from "@vnejs/module.core";
2
+
3
+ import type { BgPluginConstants, BgPluginEvents, BgPluginParams, BgPluginSettings } from "../types.js";
4
+ import type { BgRecord, BgState, BgStateIdPayload, BgStateLayerPayload } from "../utils/bg.js";
5
+
6
+ export class LayerBgState extends ModuleCore<BgPluginEvents, BgPluginConstants, BgPluginSettings, BgPluginParams, BgState> {
7
+ name = "layer.bg.state";
8
+
9
+ subscribe = () => {
10
+ this.on(this.EVENTS.LAYER_BG.STATE_UPDATE, this.onBgStateUpdate);
11
+ this.on(this.EVENTS.LAYER_BG.STATE_GET, this.onBgStateGet);
12
+ this.on(this.EVENTS.LAYER_BG.STATE_GET_ALL, this.onBgStateGetAll);
13
+ this.on(this.EVENTS.LAYER_BG.STATE_ADD, this.onBgStateAdd);
14
+ this.on(this.EVENTS.LAYER_BG.STATE_RM, this.onBgStateRm);
15
+
16
+ this.on(this.EVENTS.STATE.SET, this.onStateSet);
17
+ this.on(this.EVENTS.STATE.CLEAR, this.onStateClear);
18
+ this.on(this.EVENTS.STATE.PRELOAD, this.onStatePreload);
19
+
20
+ this.on(this.EVENTS.MEMORY.CLEARED, this.onMemoryCleared);
21
+ };
22
+
23
+ onBgStateUpdate = ({ id, ...image }: BgStateIdPayload = {}) => {
24
+ this.state.bgs = this.state.bgs.map((e) => (e.id === id ? { ...e, ...image } : e));
25
+ };
26
+
27
+ onBgStateGet = ({ layer }: BgStateLayerPayload = {}) => this.state.bgs.filter((e) => e.layer === layer);
28
+
29
+ onBgStateGetAll = () => this.state.bgs;
30
+
31
+ onBgStateAdd = ({ id, name, layer, args }: BgStateIdPayload = {}) => {
32
+ this.state.bgs.push({ id: id!, name, layer, args });
33
+ };
34
+
35
+ onBgStateRm = ({ id }: BgStateIdPayload = {}) => {
36
+ this.state.bgs = this.state.bgs.filter((e) => e.id !== id);
37
+ };
38
+
39
+ onStateSet = async (payload: ModuleGlobalStateRegistry) => {
40
+ const clonedStateOld = await this.emitClone<BgState>(this.state);
41
+ const clonedStateNew = await this.emitClone<BgState>(this.getStateSlice(payload));
42
+
43
+ await this.preloadState(clonedStateNew);
44
+
45
+ const oldBgsByLayer = clonedStateOld.bgs.reduce(this.getBgsByLayerFromState, {} as Record<string, BgRecord[]>);
46
+ const newBgsByLayer = clonedStateNew.bgs.reduce(this.getBgsByLayerFromState, {} as Record<string, BgRecord[]>);
47
+
48
+ const [promises, uniqLayers, oldLayers, newLayers] = [[] as Promise<unknown>[], [] as string[], Object.keys(oldBgsByLayer), Object.keys(newBgsByLayer)];
49
+
50
+ oldLayers.forEach((layer) => uniqLayers.push(layer));
51
+ newLayers.forEach((layer) => !uniqLayers.includes(layer) && uniqLayers.push(layer));
52
+
53
+ uniqLayers.forEach((layer) => {
54
+ const oldBgs = oldBgsByLayer[layer] || [];
55
+ const newBgs = newBgsByLayer[layer] || [];
56
+
57
+ const bgsToChange = oldBgs.slice(0, newBgs.length);
58
+ const bgsToShow = newBgs.slice(oldBgs.length);
59
+ const bgsToHide = oldBgs.slice(newBgs.length);
60
+
61
+ promises.push(...bgsToChange.map((img, i) => this.cloneBgAndChangeWithNewId(newBgs[i], img.id)));
62
+ promises.push(...bgsToShow.map(this.cloneBgAndShow));
63
+ promises.push(...bgsToHide.map(this.cloneBgAndHide));
64
+ });
65
+
66
+ this.shared.viewForceAnimationSources.add(`${this.name}.set`);
67
+ await Promise.all(promises);
68
+ this.shared.viewForceAnimationSources.rm(`${this.name}.set`);
69
+ };
70
+
71
+ onStateClear = () => this.clearState();
72
+
73
+ onStatePreload = ({ [this.name]: state }: Record<string, BgState>) => this.preloadState(state);
74
+
75
+ onMemoryCleared = async () => {
76
+ await this.preloadState(this.state);
77
+ this.shared.viewForceAnimationSources.add(`${this.name}.memory`);
78
+ await Promise.all(this.state.bgs.map(this.cloneBgAndChange));
79
+ this.shared.viewForceAnimationSources.rm(`${this.name}.memory`);
80
+ };
81
+
82
+ clearState = async () => {
83
+ this.shared.viewForceAnimationSources.add(`${this.name}.clear`);
84
+ await Promise.all(this.state.bgs.map(this.cloneBgAndHide));
85
+ this.shared.viewForceAnimationSources.rm(`${this.name}.clear`);
86
+ };
87
+
88
+ preloadState = (state: BgState) => Promise.all(state.bgs.map(this.emitBgSrcGet));
89
+
90
+ cloneBgAndChangeWithNewId = async (bg: BgRecord, id: string) =>
91
+ this.emitEventWithBg({ ...(await this.emitClone<BgRecord>(bg)), id }, this.emitBgChange);
92
+
93
+ cloneBgAndChange = async (bg: BgRecord) => this.emitEventWithBg(await this.emitClone<BgRecord>(bg), this.emitBgChange);
94
+
95
+ cloneBgAndHide = async (bg: BgRecord) => this.emitEventWithBg(await this.emitClone<BgRecord>(bg), this.emitBgHide);
96
+
97
+ cloneBgAndShow = async (bg: BgRecord) => this.emitEventWithBg(await this.emitClone<BgRecord>(bg), this.emitBgShow);
98
+
99
+ emitEventWithBg = (bg: BgRecord, cb: (bg: BgRecord) => unknown) => cb(bg);
100
+
101
+ emitBgSrcGet = (bg: BgRecord) => this.emit(this.EVENTS.LAYER_BG.SRC_GET, bg);
102
+ emitBgChange = (bg: BgRecord) => this.emit(this.EVENTS.LAYER_BG.CHANGE, bg);
103
+ emitBgHide = (bg: BgRecord) => this.emit(this.EVENTS.LAYER_BG.HIDE, bg);
104
+ emitBgShow = (bg: BgRecord) => this.emit(this.EVENTS.LAYER_BG.SHOW, bg);
105
+
106
+ getBgsByLayerFromState = (acc: Record<string, BgRecord[]>, img: BgRecord) => {
107
+ if (!acc[img.layer!]) acc[img.layer!] = [];
108
+ acc[img.layer!].push(img);
109
+
110
+ return acc;
111
+ };
112
+
113
+ getDefaultState = (): BgState => ({ bgs: [] });
114
+ }
@@ -0,0 +1,31 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { spyEvent } from "@vnejs/test-utils";
4
+
5
+ import { LayerBgPreload } from "../modules/preload.js";
6
+ import { createBgTestModule, MEDIA_EVENTS, registerBgPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
7
+
8
+ describe("LayerBgPreload", () => {
9
+ beforeEach(() => {
10
+ registerBgPluginVne();
11
+ });
12
+
13
+ it("PRELOAD emits MEDIA.LOAD for image", async () => {
14
+ const { observer } = createBgTestModule(LayerBgPreload);
15
+ const mediaLoad = spyEvent(observer, MEDIA_EVENTS.LOAD);
16
+
17
+ await observer.emit(SUBSCRIBE_EVENTS.PRELOAD, { name: "hero" });
18
+
19
+ expect(mediaLoad).toHaveBeenCalledOnce();
20
+ expect(mediaLoad.mock.calls[0][0]).toMatchObject({ name: "hero" });
21
+ });
22
+
23
+ it("PRELOAD with parts loads index and part assets", async () => {
24
+ const { observer } = createBgTestModule(LayerBgPreload);
25
+ const mediaLoad = spyEvent(observer, MEDIA_EVENTS.LOAD);
26
+
27
+ await observer.emit(SUBSCRIBE_EVENTS.PRELOAD, { name: "hero", parts: { body: "default" } });
28
+
29
+ expect(mediaLoad).toHaveBeenCalledTimes(2);
30
+ });
31
+ });
@@ -0,0 +1,55 @@
1
+ import { SUBSCRIBE_EVENTS as MEDIA_EVENTS } from "@vnejs/contracts.core.media";
2
+ import {
3
+ CONSTANTS as LAYER_CONST,
4
+ PARAMS as LAYER_PARAMS,
5
+ PLUGIN_NAME as LAYER,
6
+ SETTINGS_KEYS as LAYER_SETTINGS,
7
+ SUBSCRIBE_EVENTS as LAYER_EVENTS,
8
+ } from "@vnejs/contracts.canvas.layer";
9
+ import { CONSTANTS, PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/contracts.canvas.child.bg";
10
+ import { SourcesSet } from "@vnejs/sources-set";
11
+ import {
12
+ createTestModule as baseCreateTestModule,
13
+ registerCoreVne,
14
+ registerVnePlugin,
15
+ stubEvent,
16
+ stubSilentEvent,
17
+ stubVendorEvents,
18
+ } from "@vnejs/test-utils";
19
+
20
+ export const registerBgPluginVne = () => {
21
+ registerCoreVne();
22
+ registerVnePlugin(LAYER, { constants: LAYER_CONST, events: LAYER_EVENTS, params: LAYER_PARAMS, settings: LAYER_SETTINGS });
23
+ registerVnePlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS, params: PARAMS });
24
+ };
25
+
26
+ export const createBgTestModule = <T extends Parameters<typeof baseCreateTestModule>[0]>(
27
+ ModuleClass: T,
28
+ options: Parameters<typeof baseCreateTestModule>[1] = {},
29
+ ) => {
30
+ const result = baseCreateTestModule(ModuleClass, {
31
+ state: {
32
+ "layer.bg.state": { bgs: [] },
33
+ ...(options.state ?? {}),
34
+ },
35
+ shared: {
36
+ settings: { [LAYER_SETTINGS.QUALITY]: LAYER_CONST.QUALITIES.FHD },
37
+ viewForceAnimationSources: new SourcesSet(),
38
+ ...(options.shared ?? {}),
39
+ },
40
+ ...options,
41
+ });
42
+
43
+ stubVendorEvents(result.observer);
44
+ stubEvent(result.observer, SUBSCRIBE_EVENTS.SRC_GET, () => "hero.png");
45
+ stubEvent(result.observer, SUBSCRIBE_EVENTS.STATE_GET, () => []);
46
+ stubSilentEvent(result.observer, LAYER_EVENTS.CHILD_ADD);
47
+ stubSilentEvent(result.observer, LAYER_EVENTS.CHILD_CHANGE);
48
+ stubSilentEvent(result.observer, LAYER_EVENTS.CHILDS_RM);
49
+ stubSilentEvent(result.observer, LAYER_EVENTS.UPDATE_FORCE);
50
+ stubSilentEvent(result.observer, MEDIA_EVENTS.LOAD);
51
+
52
+ return result;
53
+ };
54
+
55
+ export { SUBSCRIBE_EVENTS, CONSTANTS, PARAMS, PLUGIN_NAME, LAYER_EVENTS, MEDIA_EVENTS };
@@ -0,0 +1,24 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { spyEvent } from "@vnejs/test-utils";
4
+
5
+ import { LayerBgShow } from "../modules/show.js";
6
+ import { createBgTestModule, LAYER_EVENTS, registerBgPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
7
+
8
+ describe("LayerBgShow", () => {
9
+ beforeEach(() => {
10
+ registerBgPluginVne();
11
+ });
12
+
13
+ it("SHOW emits STATE_ADD and CHILD_ADD", async () => {
14
+ const { observer } = createBgTestModule(LayerBgShow);
15
+ const stateAdd = spyEvent(observer, SUBSCRIBE_EVENTS.STATE_ADD);
16
+ const childAdd = spyEvent(observer, LAYER_EVENTS.CHILD_ADD);
17
+
18
+ await observer.emit(SUBSCRIBE_EVENTS.SHOW, { name: "hero", layer: "bl" });
19
+
20
+ expect(stateAdd).toHaveBeenCalledOnce();
21
+ expect(childAdd).toHaveBeenCalledOnce();
22
+ expect(stateAdd.mock.calls[0][0]).toMatchObject({ name: "hero", layer: "bl", id: "test-uid" });
23
+ });
24
+ });
@@ -0,0 +1,39 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+
3
+ import { LayerBgState } from "../modules/state.js";
4
+ import { createBgTestModule, registerBgPluginVne, SUBSCRIBE_EVENTS } from "./setup.js";
5
+
6
+ describe("LayerBgState", () => {
7
+ beforeEach(() => {
8
+ registerBgPluginVne();
9
+ });
10
+
11
+ it("STATE_ADD stores image record", async () => {
12
+ const { module, observer } = createBgTestModule(LayerBgState);
13
+
14
+ await observer.emit(SUBSCRIBE_EVENTS.STATE_ADD, { id: "1", name: "hero", layer: "bl", args: {} });
15
+
16
+ expect((module as LayerBgState).state.bgs).toEqual([{ id: "1", name: "hero", layer: "bl", args: {} }]);
17
+ });
18
+
19
+ it("STATE_GET returns bgs by layer", async () => {
20
+ const { observer } = createBgTestModule(LayerBgState);
21
+
22
+ await observer.emit(SUBSCRIBE_EVENTS.STATE_ADD, { id: "1", name: "hero", layer: "bl" });
23
+ await observer.emit(SUBSCRIBE_EVENTS.STATE_ADD, { id: "2", name: "sky", layer: "fl" });
24
+
25
+ const [bgImages] = await observer.emit(SUBSCRIBE_EVENTS.STATE_GET, { layer: "bl" });
26
+
27
+ expect(bgImages).toHaveLength(1);
28
+ expect(bgImages[0].id).toBe("1");
29
+ });
30
+
31
+ it("STATE_RM removes image by id", async () => {
32
+ const { module, observer } = createBgTestModule(LayerBgState);
33
+
34
+ await observer.emit(SUBSCRIBE_EVENTS.STATE_ADD, { id: "1", name: "hero", layer: "bl" });
35
+ await observer.emit(SUBSCRIBE_EVENTS.STATE_RM, { id: "1" });
36
+
37
+ expect((module as LayerBgState).state.bgs).toEqual([]);
38
+ });
39
+ });
package/src/types.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { ModuleCoreConstants, ModuleCoreEvents, ModuleCoreParams, ModuleCoreSettings } from "@vnejs/module.core";
2
+ import type { Constants, Params, PluginName, SubscribeEvents } from "@vnejs/contracts.canvas.child.bg";
3
+ import type { PluginName as LayerPluginName, SettingsKeys as LayerSettingsKeys, SubscribeEvents as LayerSubscribeEvents } from "@vnejs/contracts.canvas.layer";
4
+
5
+ export type BgPluginEvents = ModuleCoreEvents & Record<PluginName, SubscribeEvents> & Record<LayerPluginName, LayerSubscribeEvents>;
6
+
7
+ export type BgPluginConstants = ModuleCoreConstants & Record<PluginName, Constants>;
8
+
9
+ export type BgPluginSettings = ModuleCoreSettings & Record<LayerPluginName, LayerSettingsKeys>;
10
+
11
+ export type BgPluginParams = ModuleCoreParams & Record<PluginName, Params>;
@@ -0,0 +1,78 @@
1
+ import type { BgArgs, BgPreloadPayload } from "@vnejs/contracts.canvas.child.bg";
2
+
3
+ export type { BgArgs, BgPreloadPayload };
4
+
5
+ export type BgArgsInject = Partial<BgArgs>;
6
+
7
+ export type BgArgsInjectPayload = {
8
+ args?: BgArgs;
9
+ };
10
+
11
+ export const applyArgsInjections = (base: BgArgs, injections: BgArgsInject[]): BgArgs =>
12
+ injections.reduce(
13
+ (acc, inj) => ({
14
+ ...acc,
15
+ ...inj,
16
+ parts: inj.parts ? { ...acc.parts, ...inj.parts } : acc.parts,
17
+ }),
18
+ base,
19
+ );
20
+
21
+ export type BgRecord = {
22
+ id: string;
23
+ name?: string;
24
+ layer?: string;
25
+ args?: BgArgs;
26
+ transition?: string;
27
+ duration?: number;
28
+ hideOldBgsOnShow?: boolean;
29
+ };
30
+
31
+ export type BgState = {
32
+ bgs: BgRecord[];
33
+ };
34
+
35
+ export type BgShowPayload = {
36
+ name?: string;
37
+ layer?: string;
38
+ transition?: string;
39
+ duration?: number;
40
+ args?: BgArgs;
41
+ hideOldBgsOnShow?: boolean;
42
+ };
43
+
44
+ export type BgHidePayload = {
45
+ layer?: string;
46
+ transition?: string;
47
+ duration?: number;
48
+ };
49
+
50
+ export type BgChangePayload = {
51
+ id?: string;
52
+ name?: string;
53
+ layer?: string;
54
+ duration?: number;
55
+ args?: BgArgs;
56
+ };
57
+
58
+ export type BgSrcGetPayload = {
59
+ name?: string;
60
+ layer?: string;
61
+ args?: BgArgs;
62
+ };
63
+
64
+ export type BgStateLayerPayload = {
65
+ layer?: string;
66
+ };
67
+
68
+ export type BgStateIdPayload = {
69
+ id?: string;
70
+ name?: string;
71
+ layer?: string;
72
+ args?: BgArgs;
73
+ };
74
+
75
+ export type TransitionProps = {
76
+ initialProps?: Record<string, unknown>;
77
+ finishProps?: Record<string, unknown>;
78
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": [
8
+ "src/**/*.ts"
9
+ ],
10
+ "exclude": [
11
+ "dist",
12
+ "node_modules",
13
+ "src/tests"
14
+ ]
15
+ }