@antha/engine 0.0.1
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/LICENSE-CC0 +121 -0
- package/LICENSE-MIT +21 -0
- package/README.md +11 -0
- package/dist/antha-engine.d.ts +312 -0
- package/dist/antha-engine.js +297 -0
- package/dist/antha-ui.element.d.ts +41 -0
- package/dist/antha-ui.element.js +68 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/logger/antha-logger.d.ts +30 -0
- package/dist/logger/antha-logger.js +1 -0
- package/dist/logger/browser-antha-logger.d.ts +8 -0
- package/dist/logger/browser-antha-logger.js +29 -0
- package/dist/logger/empty-antha-logger.d.ts +14 -0
- package/dist/logger/empty-antha-logger.js +22 -0
- package/dist/logger/sentry-antha-logger.d.ts +8 -0
- package/dist/logger/sentry-antha-logger.js +30 -0
- package/package.json +69 -0
package/LICENSE-CC0
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
CC0 1.0 Universal
|
|
2
|
+
|
|
3
|
+
Creative Commons Legal Code
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator
|
|
18
|
+
and subsequent owner(s) (each and all, an "owner") of an original work of
|
|
19
|
+
authorship and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for
|
|
22
|
+
the purpose of contributing to a commons of creative, cultural and
|
|
23
|
+
scientific works ("Commons") that the public can reliably and without fear
|
|
24
|
+
of later claims of infringement build upon, modify, incorporate in other
|
|
25
|
+
works, reuse and redistribute as freely as possible in any form whatsoever
|
|
26
|
+
and for any purposes, including without limitation commercial purposes.
|
|
27
|
+
These owners may contribute to the Commons to promote the ideal of a free
|
|
28
|
+
culture and the further production of creative, cultural and scientific
|
|
29
|
+
works, or to gain reputation or greater distribution for their Work in
|
|
30
|
+
part through the use and efforts of others.
|
|
31
|
+
|
|
32
|
+
For these and/or other purposes and motivations, and without any
|
|
33
|
+
expectation of additional consideration or compensation, the person
|
|
34
|
+
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
|
35
|
+
is an owner of Copyright and Related Rights in the Work, voluntarily
|
|
36
|
+
elects to apply CC0 to the Work and publicly distribute the Work under its
|
|
37
|
+
terms, with knowledge of his or her Copyright and Related Rights in the
|
|
38
|
+
Work and the meaning and intended legal effect of CC0 on those rights.
|
|
39
|
+
|
|
40
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
41
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
42
|
+
Related Rights"). Copyright and Related Rights include, but are not
|
|
43
|
+
limited to, the following:
|
|
44
|
+
|
|
45
|
+
i. the right to reproduce, adapt, distribute, perform, display,
|
|
46
|
+
communicate, and translate a Work;
|
|
47
|
+
ii. moral rights retained by the original author(s) and/or performer(s);
|
|
48
|
+
iii. publicity and privacy rights pertaining to a person's image or
|
|
49
|
+
likeness depicted in a Work;
|
|
50
|
+
iv. rights protecting against unfair competition in regards to a Work,
|
|
51
|
+
subject to the limitations in paragraph 4(a), below;
|
|
52
|
+
v. rights protecting the extraction, dissemination, use and reuse of data
|
|
53
|
+
in a Work;
|
|
54
|
+
vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
55
|
+
European Parliament and of the Council of 11 March 1996 on the legal
|
|
56
|
+
protection of databases, and under any national implementation
|
|
57
|
+
thereof, including any amended or successor version of such
|
|
58
|
+
directive); and
|
|
59
|
+
vii. other similar, equivalent or corresponding rights throughout the
|
|
60
|
+
world based on applicable law or treaty, and any national
|
|
61
|
+
implementations thereof.
|
|
62
|
+
|
|
63
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention
|
|
64
|
+
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
|
65
|
+
irrevocably and unconditionally waives, abandons, and surrenders all of
|
|
66
|
+
Affirmer's Copyright and Related Rights and associated claims and causes
|
|
67
|
+
of action, whether now known or unknown (including existing as well as
|
|
68
|
+
future claims and causes of action), in the Work (i) in all territories
|
|
69
|
+
worldwide, (ii) for the maximum duration provided by applicable law or
|
|
70
|
+
treaty (including future time extensions), (iii) in any current or future
|
|
71
|
+
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
|
72
|
+
including without limitation commercial, advertising or promotional
|
|
73
|
+
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
|
74
|
+
member of the public at large and to the detriment of Affirmer's heirs and
|
|
75
|
+
successors, fully intending that such Waiver shall not be subject to
|
|
76
|
+
revocation, rescission, cancellation, termination, or any other legal or
|
|
77
|
+
equitable action to disrupt the quiet enjoyment of the Work by the public
|
|
78
|
+
as contemplated by Affirmer's express Statement of Purpose.
|
|
79
|
+
|
|
80
|
+
3. Public License Fallback. Should any part of the Waiver for any reason
|
|
81
|
+
be judged legally invalid or ineffective under applicable law, then the
|
|
82
|
+
Waiver shall be preserved to the maximum extent permitted taking into
|
|
83
|
+
account Affirmer's express Statement of Purpose. In addition, to the
|
|
84
|
+
extent the Waiver is so judged Affirmer hereby grants to each affected
|
|
85
|
+
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
|
86
|
+
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
|
87
|
+
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
|
88
|
+
maximum duration provided by applicable law or treaty (including future
|
|
89
|
+
time extensions), (iii) in any current or future medium and for any number
|
|
90
|
+
of copies, and (iv) for any purpose whatsoever, including without
|
|
91
|
+
limitation commercial, advertising or promotional purposes (the
|
|
92
|
+
"License"). The License shall be deemed effective as of the date CC0 was
|
|
93
|
+
applied by Affirmer to the Work. Should any part of the License for any
|
|
94
|
+
reason be judged legally invalid or ineffective under applicable law, such
|
|
95
|
+
partial invalidity or ineffectiveness shall not invalidate the remainder
|
|
96
|
+
of the License, and in such case Affirmer hereby affirms that he or she
|
|
97
|
+
will not (i) exercise any of his or her remaining Copyright and Related
|
|
98
|
+
Rights in the Work or (ii) assert any associated claims and causes of
|
|
99
|
+
action with respect to the Work, in either case contrary to Affirmer's
|
|
100
|
+
express Statement of Purpose.
|
|
101
|
+
|
|
102
|
+
4. Limitations and Disclaimers.
|
|
103
|
+
|
|
104
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
105
|
+
surrendered, licensed or otherwise affected by this document.
|
|
106
|
+
b. Affirmer offers the Work as-is and makes no representations or
|
|
107
|
+
warranties of any kind concerning the Work, express, implied,
|
|
108
|
+
statutory or otherwise, including without limitation warranties of
|
|
109
|
+
title, merchantability, fitness for a particular purpose, non
|
|
110
|
+
infringement, or the absence of latent or other defects, accuracy, or
|
|
111
|
+
the present or absence of errors, whether or not discoverable, all to
|
|
112
|
+
the greatest extent permissible under applicable law.
|
|
113
|
+
c. Affirmer disclaims responsibility for clearing rights of other persons
|
|
114
|
+
that may apply to the Work or any use thereof, including without
|
|
115
|
+
limitation any person's Copyright and Related Rights in the Work.
|
|
116
|
+
Further, Affirmer disclaims responsibility for obtaining any necessary
|
|
117
|
+
consents, permissions or other rights required for any use of the
|
|
118
|
+
Work.
|
|
119
|
+
d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
120
|
+
party to this document and has no duty or obligation with respect to
|
|
121
|
+
this CC0 or use of the Work.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 electrovir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Antha
|
|
2
|
+
|
|
3
|
+
The Antha game engine (for the web), where everything is a mod! The only thing this engine core includes is a framework for flexibly defining and inserting mods with all the tools they need to be successful.
|
|
4
|
+
|
|
5
|
+
- Demo: https://electrovir.github.io/antha/demo
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i antha
|
|
11
|
+
```
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { type AnyObject, type Branded, type MaybePromise, type PartialWithUndefined, type RequiredAndNotNull } from '@augment-vir/common';
|
|
2
|
+
import { type HtmlInterpolation } from 'element-vir';
|
|
3
|
+
import { Observable } from 'observavir';
|
|
4
|
+
import { type RequireExactlyOne } from 'type-fest';
|
|
5
|
+
import { type AnthaLogger } from './logger/antha-logger.js';
|
|
6
|
+
/**
|
|
7
|
+
* A string type used for mod instances. In reality these are just strings, but this branded type
|
|
8
|
+
* helps keep track of them. Under the hood, these are CUID2 ids.
|
|
9
|
+
*
|
|
10
|
+
* @category Internal
|
|
11
|
+
*/
|
|
12
|
+
export type ModInstanceId = Branded<string, 'antha-mod-instance-id'>;
|
|
13
|
+
/**
|
|
14
|
+
* Parameters passed to the execute callback in {@link AnthaMod}.
|
|
15
|
+
*
|
|
16
|
+
* @category Internal
|
|
17
|
+
*/
|
|
18
|
+
export type ModExecuteParams<State extends AnyObject> = {
|
|
19
|
+
state: Partial<State>;
|
|
20
|
+
engine: AnthaEngine;
|
|
21
|
+
ticksSinceLastExecute: number;
|
|
22
|
+
msSinceLastExecute: DOMHighResTimeStamp;
|
|
23
|
+
lastExecution: Readonly<LastExecution> | undefined;
|
|
24
|
+
currentTick: number;
|
|
25
|
+
hostElement: HTMLElement;
|
|
26
|
+
modInstanceId: ModInstanceId;
|
|
27
|
+
} & ModOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Parameters passed to the cleanup callback in {@link AnthaMod}.
|
|
30
|
+
*
|
|
31
|
+
* @category Internal
|
|
32
|
+
*/
|
|
33
|
+
export type ModCleanupParams<State extends AnyObject> = {
|
|
34
|
+
state: Partial<State>;
|
|
35
|
+
engine: AnthaEngine;
|
|
36
|
+
hostElement: HTMLElement;
|
|
37
|
+
modInstanceId: ModInstanceId;
|
|
38
|
+
} & ModOptions;
|
|
39
|
+
/**
|
|
40
|
+
* Return this from a mod's {@link AnthaMod.execute} callback to instruct {@link AnthaEngine} that the
|
|
41
|
+
* mod's current execution should not count toward the mod's frequency schedule. This is useful when
|
|
42
|
+
* a mod needs a dependency (such as a canvas or external resource) that isn't ready yet so that the
|
|
43
|
+
* engine will keep retrying on subsequent ticks, even if the mod has a low execution frequency.
|
|
44
|
+
*
|
|
45
|
+
* @category Mod
|
|
46
|
+
*/
|
|
47
|
+
export declare const SkipExecution: unique symbol;
|
|
48
|
+
/**
|
|
49
|
+
* Type for {@link SkipExecution}.
|
|
50
|
+
*
|
|
51
|
+
* @category Mod
|
|
52
|
+
*/
|
|
53
|
+
export type SkipExecution = typeof SkipExecution;
|
|
54
|
+
/**
|
|
55
|
+
* Allowed output from the execute callback in {@link AnthaMod}.
|
|
56
|
+
*
|
|
57
|
+
* @category Internal
|
|
58
|
+
*/
|
|
59
|
+
export type ModExecuteResult = MaybePromise<HtmlInterpolation | void | SkipExecution>;
|
|
60
|
+
/**
|
|
61
|
+
* Possible options to use when defining {@link AnthaMod}.
|
|
62
|
+
*
|
|
63
|
+
* @category Internal
|
|
64
|
+
*/
|
|
65
|
+
export type ModOptions = {
|
|
66
|
+
/**
|
|
67
|
+
* The frequency at which this mod should execute.
|
|
68
|
+
*
|
|
69
|
+
* - `undefined`: execute on every tick
|
|
70
|
+
* - `duration`: execute on each duration. This will be converted to a tick count based on what
|
|
71
|
+
* tick speed the engine is running at.
|
|
72
|
+
* - `ticks`: execute every `ticks` ticks.
|
|
73
|
+
*
|
|
74
|
+
* @default undefined
|
|
75
|
+
*/
|
|
76
|
+
frequency: RequireExactlyOne<{
|
|
77
|
+
durationMs: number;
|
|
78
|
+
ticks: number;
|
|
79
|
+
}> | undefined;
|
|
80
|
+
/**
|
|
81
|
+
* If `true`, this mod will execute immediately on game engine init instead of waiting for its
|
|
82
|
+
* first tick to hit based on the given frequency configuration. If frequency is omitted or
|
|
83
|
+
* `undefined`, this option is not necessary.
|
|
84
|
+
*
|
|
85
|
+
* @default false
|
|
86
|
+
*/
|
|
87
|
+
executeImmediately: boolean;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* A mod for {@link AnthaEngine}. This is the core of getting anything done in the engine, everything
|
|
91
|
+
* runs in a mod.
|
|
92
|
+
*
|
|
93
|
+
* @category Mod
|
|
94
|
+
* @example
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* import {AnthaMod} from '@antha/engine';
|
|
98
|
+
* import {html} from 'element-vir';
|
|
99
|
+
*
|
|
100
|
+
* const myMod: AnthaMod<{count: number}> = {
|
|
101
|
+
* execute({state}) {
|
|
102
|
+
* state.count = (state.count || 0) + 1;
|
|
103
|
+
* return html`
|
|
104
|
+
* <span class="counter">${String(state.count)}</span>
|
|
105
|
+
* `;
|
|
106
|
+
* },
|
|
107
|
+
* };
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export type AnthaMod<State extends AnyObject = any> = {
|
|
111
|
+
/**
|
|
112
|
+
* Not used by the Antha engine, just a nice string to help debugging so you can which mod is
|
|
113
|
+
* which.
|
|
114
|
+
*/
|
|
115
|
+
modName: string;
|
|
116
|
+
/**
|
|
117
|
+
* The execute callback. This is where the mod's functionality lives. This will be called in
|
|
118
|
+
* each tick based on the provided frequency. Any non-nullish output will be rendered as HTML to
|
|
119
|
+
* the DOM.
|
|
120
|
+
*/
|
|
121
|
+
execute: (this: void, params: Readonly<ModExecuteParams<NoInfer<State>>>) => ModExecuteResult;
|
|
122
|
+
} & PartialWithUndefined<{
|
|
123
|
+
initState: Partial<NoInfer<State>>;
|
|
124
|
+
/**
|
|
125
|
+
* Use this to cleanup resources that the mod has created. This is called when the engine is
|
|
126
|
+
* reset.
|
|
127
|
+
*/
|
|
128
|
+
cleanup: (this: void, params: Readonly<ModCleanupParams<NoInfer<State>>>) => MaybePromise<void>;
|
|
129
|
+
} & ModOptions>;
|
|
130
|
+
/**
|
|
131
|
+
* Extracts state type from a mod.
|
|
132
|
+
*
|
|
133
|
+
* @category Internal
|
|
134
|
+
*/
|
|
135
|
+
export type AnthaModeState<Mod extends AnthaMod> = Mod extends AnthaMod<infer State> ? State : never;
|
|
136
|
+
/**
|
|
137
|
+
* A helper for defining {@link AnthaMod} inline. This is _not_ required in order to define an
|
|
138
|
+
* {@link AnthaMod}. It simply helps with type inference.
|
|
139
|
+
*
|
|
140
|
+
* @category Mod
|
|
141
|
+
* @example
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* import {defineAnthaMod} from '@antha/engine';
|
|
145
|
+
* import {html} from 'element-vir';
|
|
146
|
+
*
|
|
147
|
+
* defineAnthaMod<{count: number}>({
|
|
148
|
+
* execute({state}) {
|
|
149
|
+
* state.count = (state.count || 0) + 1;
|
|
150
|
+
* return html`
|
|
151
|
+
* <span class="counter">${String(state.count)}</span>
|
|
152
|
+
* `;
|
|
153
|
+
* },
|
|
154
|
+
* });
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
export declare function defineAnthaMod<State extends AnyObject = never>(mod: AnthaMod<NoInfer<State>>): AnthaMod<NoInfer<State>>;
|
|
158
|
+
/**
|
|
159
|
+
* Possible options to use when constructing {@link AnthaEngine}. Defaults are contained in
|
|
160
|
+
* {@link defaultAnthaEngineOptions}.
|
|
161
|
+
*
|
|
162
|
+
* @category Engine
|
|
163
|
+
*/
|
|
164
|
+
export type AnthaEngineOptions = PartialWithUndefined<{
|
|
165
|
+
/**
|
|
166
|
+
* A custom logger to handle mod and engine logs. By default, this merely logs to the browser
|
|
167
|
+
* console.
|
|
168
|
+
*/
|
|
169
|
+
logger: AnthaLogger;
|
|
170
|
+
}>;
|
|
171
|
+
/**
|
|
172
|
+
* Default values for {@link AnthaEngineOptions}.
|
|
173
|
+
*
|
|
174
|
+
* @category Internal
|
|
175
|
+
*/
|
|
176
|
+
export declare const defaultAnthaEngineOptions: RequiredAndNotNull<AnthaEngineOptions>;
|
|
177
|
+
/**
|
|
178
|
+
* The last known execution for a given mod. Passed to each mod's execute callback via
|
|
179
|
+
* {@link ModExecuteParams} and used inside {@link AnthaEngine} to keep track of when a mod should run
|
|
180
|
+
* again.
|
|
181
|
+
*
|
|
182
|
+
* @category Internal
|
|
183
|
+
*/
|
|
184
|
+
export type LastExecution = {
|
|
185
|
+
tick: number;
|
|
186
|
+
timeMs: DOMHighResTimeStamp;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Init options for the constructor of {@link AnthaEngine}.
|
|
190
|
+
*
|
|
191
|
+
* @category Internal
|
|
192
|
+
*/
|
|
193
|
+
export type AnthaEngineInit<State extends AnyObject = AnyObject> = PartialWithUndefined<{
|
|
194
|
+
/** The Antha mods to start the Antha engine with. */
|
|
195
|
+
mods: AnthaMod[];
|
|
196
|
+
/**
|
|
197
|
+
* Various options to start the Antha engine with.
|
|
198
|
+
*
|
|
199
|
+
* @default defaultAnthaEngineOptions
|
|
200
|
+
*/
|
|
201
|
+
engineOptions: Readonly<AnthaEngineOptions>;
|
|
202
|
+
/**
|
|
203
|
+
* The host element to start the Antha engine with.
|
|
204
|
+
*
|
|
205
|
+
* @default globalThis.document.documentElement
|
|
206
|
+
*/
|
|
207
|
+
hostElement: HTMLElement;
|
|
208
|
+
initState: Partial<State>;
|
|
209
|
+
}>;
|
|
210
|
+
/**
|
|
211
|
+
* The Antha Engine. This keeps track of when to execute each mod, the mod's template outputs, and
|
|
212
|
+
* the state passed to and mutated by each mod.
|
|
213
|
+
*
|
|
214
|
+
* @category Engine
|
|
215
|
+
* @example
|
|
216
|
+
*
|
|
217
|
+
* ```ts
|
|
218
|
+
* import {AnthaEngine, defineAnthaMod} from '@antha/engine';
|
|
219
|
+
* import {html} from 'element-vir';
|
|
220
|
+
*
|
|
221
|
+
* new AnthaEngine({
|
|
222
|
+
* mods: [
|
|
223
|
+
* defineAnthaMod<{count: number}>({
|
|
224
|
+
* execute({state}) {
|
|
225
|
+
* state.count = (state.count || 0) + 1;
|
|
226
|
+
* return html`
|
|
227
|
+
* <span class="counter">${String(state.count)}</span>
|
|
228
|
+
* `;
|
|
229
|
+
* },
|
|
230
|
+
* }),
|
|
231
|
+
* ],
|
|
232
|
+
* });
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export declare class AnthaEngine<State extends AnyObject = AnyObject> {
|
|
236
|
+
constructor(init?: AnthaEngineInit<State> | undefined);
|
|
237
|
+
/**
|
|
238
|
+
* Send a log through the engine. This will use the user's provided logger or default to browser
|
|
239
|
+
* logs.
|
|
240
|
+
*/
|
|
241
|
+
log: AnthaLogger;
|
|
242
|
+
/** Get this.hostElement but if one doesn't exist yet, create one. */
|
|
243
|
+
protected getEnsuredHostElement(): HTMLElement;
|
|
244
|
+
/** The element that this engine considers itself to be "hosted" in. */
|
|
245
|
+
hostElement: HTMLElement | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* Hook into this observable for updating event-based code, like UI elements. This is used
|
|
248
|
+
* directly in AnthaUi to update the UI.
|
|
249
|
+
*/
|
|
250
|
+
readonly observable: Observable<HtmlInterpolation[]>;
|
|
251
|
+
/** The current engine options. This can be modified at any time. */
|
|
252
|
+
readonly options: RequiredAndNotNull<AnthaEngineOptions>;
|
|
253
|
+
/** The current mods to execute. This can be modified at any time. */
|
|
254
|
+
readonly currentMods: AnthaMod[];
|
|
255
|
+
/**
|
|
256
|
+
* Used to store each template by its mod so that mod-to-template tracking remains stable even
|
|
257
|
+
* if mods are inserted, removed, or moved around in the `currentMods` array.
|
|
258
|
+
*/
|
|
259
|
+
readonly currentTemplateMap: WeakMap<AnthaMod<any>, HtmlInterpolation>;
|
|
260
|
+
readonly modInstanceIdMap: WeakMap<AnthaMod<any>, ModInstanceId>;
|
|
261
|
+
/**
|
|
262
|
+
* The current array of templates. This array is mutated and passed to the UI via the
|
|
263
|
+
* `observable`.
|
|
264
|
+
*/
|
|
265
|
+
readonly currentTemplateArray: HtmlInterpolation[];
|
|
266
|
+
/**
|
|
267
|
+
* Used to store when each mod was last executed in order to know when the next execution should
|
|
268
|
+
* be.
|
|
269
|
+
*/
|
|
270
|
+
readonly lastModExecution: WeakMap<AnthaMod<any>, Readonly<LastExecution>>;
|
|
271
|
+
/** The engine's current tick number. This is incremented after each tick is finished executing. */
|
|
272
|
+
currentTick: number;
|
|
273
|
+
/**
|
|
274
|
+
* The engine's current state. This is intended to be mutated by mods, but it can also be
|
|
275
|
+
* modified at any time externally.
|
|
276
|
+
*/
|
|
277
|
+
readonly state: Partial<State>;
|
|
278
|
+
/** Total milliseconds elapsed since the engine started. Updated each tick. */
|
|
279
|
+
totalMs: DOMHighResTimeStamp;
|
|
280
|
+
/** When the engine started running its loop. */
|
|
281
|
+
engineStartTime: DOMHighResTimeStamp;
|
|
282
|
+
/**
|
|
283
|
+
* Indicates whether the loop is running or not. This can be freely modified at any time to
|
|
284
|
+
* start or stop the next tick.
|
|
285
|
+
*/
|
|
286
|
+
isLoopRunning: boolean;
|
|
287
|
+
/** Indicates whether a tick is currently running or not. This should not be modified externally. */
|
|
288
|
+
readonly isTickRunning: boolean;
|
|
289
|
+
/**
|
|
290
|
+
* Stop the tick loop. If the loop already isn't running, no changes will be made.
|
|
291
|
+
*
|
|
292
|
+
* @returns Whether the loop was stopped or not.
|
|
293
|
+
*/
|
|
294
|
+
stopLoop(): boolean;
|
|
295
|
+
/** Reset the engine back to its initial state and run all mod cleanup callbacks. */
|
|
296
|
+
reset(): Promise<void>;
|
|
297
|
+
/**
|
|
298
|
+
* Start the tick loop. If the loop is already running, no changes will be made.
|
|
299
|
+
*
|
|
300
|
+
* @returns Whether the loop was started or not.
|
|
301
|
+
*/
|
|
302
|
+
startLoop(): boolean;
|
|
303
|
+
/** Execute a single tick on each render frame. */
|
|
304
|
+
protected runTickInLoop(): Promise<void>;
|
|
305
|
+
/**
|
|
306
|
+
* Run a single tick. This happens automatically in a loop if `startLoop` is called or
|
|
307
|
+
* `isLoopRunning` is set to `true`.
|
|
308
|
+
*/
|
|
309
|
+
runSingleTick(): Promise<void>;
|
|
310
|
+
/** Used to determine if a mod should execute right now or not. */
|
|
311
|
+
shouldModExecute(mod: Readonly<AnthaMod>, lastExecution: Readonly<LastExecution> | undefined): boolean;
|
|
312
|
+
}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { waitUntil } from '@augment-vir/assert';
|
|
2
|
+
import { applyBrand, awaitedForEach, getOrSetFromMap, makeWritable, mergeDefinedProperties, } from '@augment-vir/common';
|
|
3
|
+
import { createId } from '@paralleldrive/cuid2';
|
|
4
|
+
import { css, html } from 'element-vir';
|
|
5
|
+
import { Observable } from 'observavir';
|
|
6
|
+
import { AnthaUi } from './antha-ui.element.js';
|
|
7
|
+
import { browserAnthaLogger } from './logger/browser-antha-logger.js';
|
|
8
|
+
/**
|
|
9
|
+
* Return this from a mod's {@link AnthaMod.execute} callback to instruct {@link AnthaEngine} that the
|
|
10
|
+
* mod's current execution should not count toward the mod's frequency schedule. This is useful when
|
|
11
|
+
* a mod needs a dependency (such as a canvas or external resource) that isn't ready yet so that the
|
|
12
|
+
* engine will keep retrying on subsequent ticks, even if the mod has a low execution frequency.
|
|
13
|
+
*
|
|
14
|
+
* @category Mod
|
|
15
|
+
*/
|
|
16
|
+
export const SkipExecution = Symbol.for('antha-engine-mode-skip-execution');
|
|
17
|
+
/**
|
|
18
|
+
* A helper for defining {@link AnthaMod} inline. This is _not_ required in order to define an
|
|
19
|
+
* {@link AnthaMod}. It simply helps with type inference.
|
|
20
|
+
*
|
|
21
|
+
* @category Mod
|
|
22
|
+
* @example
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import {defineAnthaMod} from '@antha/engine';
|
|
26
|
+
* import {html} from 'element-vir';
|
|
27
|
+
*
|
|
28
|
+
* defineAnthaMod<{count: number}>({
|
|
29
|
+
* execute({state}) {
|
|
30
|
+
* state.count = (state.count || 0) + 1;
|
|
31
|
+
* return html`
|
|
32
|
+
* <span class="counter">${String(state.count)}</span>
|
|
33
|
+
* `;
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function defineAnthaMod(mod) {
|
|
39
|
+
return mod;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Default values for {@link AnthaEngineOptions}.
|
|
43
|
+
*
|
|
44
|
+
* @category Internal
|
|
45
|
+
*/
|
|
46
|
+
export const defaultAnthaEngineOptions = {
|
|
47
|
+
logger: browserAnthaLogger,
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* The Antha Engine. This keeps track of when to execute each mod, the mod's template outputs, and
|
|
51
|
+
* the state passed to and mutated by each mod.
|
|
52
|
+
*
|
|
53
|
+
* @category Engine
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* import {AnthaEngine, defineAnthaMod} from '@antha/engine';
|
|
58
|
+
* import {html} from 'element-vir';
|
|
59
|
+
*
|
|
60
|
+
* new AnthaEngine({
|
|
61
|
+
* mods: [
|
|
62
|
+
* defineAnthaMod<{count: number}>({
|
|
63
|
+
* execute({state}) {
|
|
64
|
+
* state.count = (state.count || 0) + 1;
|
|
65
|
+
* return html`
|
|
66
|
+
* <span class="counter">${String(state.count)}</span>
|
|
67
|
+
* `;
|
|
68
|
+
* },
|
|
69
|
+
* }),
|
|
70
|
+
* ],
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export class AnthaEngine {
|
|
75
|
+
constructor(init) {
|
|
76
|
+
this.state = init?.initState || {};
|
|
77
|
+
this.options = mergeDefinedProperties(defaultAnthaEngineOptions, init?.engineOptions);
|
|
78
|
+
this.currentMods = init?.mods || [];
|
|
79
|
+
this.hostElement = init?.hostElement;
|
|
80
|
+
this.log = this.options.logger;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Send a log through the engine. This will use the user's provided logger or default to browser
|
|
84
|
+
* logs.
|
|
85
|
+
*/
|
|
86
|
+
log;
|
|
87
|
+
/** Get this.hostElement but if one doesn't exist yet, create one. */
|
|
88
|
+
getEnsuredHostElement() {
|
|
89
|
+
if (!this.hostElement) {
|
|
90
|
+
const instance = globalThis.document.createElement(AnthaUi.tagName);
|
|
91
|
+
this.hostElement = instance;
|
|
92
|
+
instance.assignInputs({
|
|
93
|
+
engine: this,
|
|
94
|
+
options: {
|
|
95
|
+
disableConnectStart: true,
|
|
96
|
+
disableDisconnectReset: true,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
globalThis.document.body.append(instance);
|
|
100
|
+
}
|
|
101
|
+
return this.hostElement;
|
|
102
|
+
}
|
|
103
|
+
/** The element that this engine considers itself to be "hosted" in. */
|
|
104
|
+
hostElement;
|
|
105
|
+
/**
|
|
106
|
+
* Hook into this observable for updating event-based code, like UI elements. This is used
|
|
107
|
+
* directly in AnthaUi to update the UI.
|
|
108
|
+
*/
|
|
109
|
+
observable = new Observable({
|
|
110
|
+
defaultValue: [],
|
|
111
|
+
equalityCheck: undefined,
|
|
112
|
+
});
|
|
113
|
+
/** The current engine options. This can be modified at any time. */
|
|
114
|
+
options;
|
|
115
|
+
/** The current mods to execute. This can be modified at any time. */
|
|
116
|
+
currentMods;
|
|
117
|
+
/**
|
|
118
|
+
* Used to store each template by its mod so that mod-to-template tracking remains stable even
|
|
119
|
+
* if mods are inserted, removed, or moved around in the `currentMods` array.
|
|
120
|
+
*/
|
|
121
|
+
currentTemplateMap = new WeakMap();
|
|
122
|
+
modInstanceIdMap = new WeakMap();
|
|
123
|
+
/**
|
|
124
|
+
* The current array of templates. This array is mutated and passed to the UI via the
|
|
125
|
+
* `observable`.
|
|
126
|
+
*/
|
|
127
|
+
currentTemplateArray = [];
|
|
128
|
+
/**
|
|
129
|
+
* Used to store when each mod was last executed in order to know when the next execution should
|
|
130
|
+
* be.
|
|
131
|
+
*/
|
|
132
|
+
lastModExecution = new WeakMap();
|
|
133
|
+
/** The engine's current tick number. This is incremented after each tick is finished executing. */
|
|
134
|
+
currentTick = 0;
|
|
135
|
+
/**
|
|
136
|
+
* The engine's current state. This is intended to be mutated by mods, but it can also be
|
|
137
|
+
* modified at any time externally.
|
|
138
|
+
*/
|
|
139
|
+
state;
|
|
140
|
+
/** Total milliseconds elapsed since the engine started. Updated each tick. */
|
|
141
|
+
totalMs = 0;
|
|
142
|
+
/** When the engine started running its loop. */
|
|
143
|
+
engineStartTime = performance.now();
|
|
144
|
+
/**
|
|
145
|
+
* Indicates whether the loop is running or not. This can be freely modified at any time to
|
|
146
|
+
* start or stop the next tick.
|
|
147
|
+
*/
|
|
148
|
+
isLoopRunning = false;
|
|
149
|
+
/** Indicates whether a tick is currently running or not. This should not be modified externally. */
|
|
150
|
+
isTickRunning = false;
|
|
151
|
+
/**
|
|
152
|
+
* Stop the tick loop. If the loop already isn't running, no changes will be made.
|
|
153
|
+
*
|
|
154
|
+
* @returns Whether the loop was stopped or not.
|
|
155
|
+
*/
|
|
156
|
+
stopLoop() {
|
|
157
|
+
if (this.isLoopRunning) {
|
|
158
|
+
this.isLoopRunning = false;
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Reset the engine back to its initial state and run all mod cleanup callbacks. */
|
|
166
|
+
async reset() {
|
|
167
|
+
this.stopLoop();
|
|
168
|
+
await waitUntil.isFalse(() => this.isTickRunning);
|
|
169
|
+
this.currentTick = 0;
|
|
170
|
+
/** Clear all per-mod tracking for current mods. */
|
|
171
|
+
await awaitedForEach(this.currentMods, async (mod) => {
|
|
172
|
+
this.lastModExecution.delete(mod);
|
|
173
|
+
this.currentTemplateMap.delete(mod);
|
|
174
|
+
await mod.cleanup?.({
|
|
175
|
+
engine: this,
|
|
176
|
+
hostElement: this.getEnsuredHostElement(),
|
|
177
|
+
modInstanceId: getOrSetFromMap(this.modInstanceIdMap, mod, () => applyBrand(createId())),
|
|
178
|
+
state: this.state,
|
|
179
|
+
executeImmediately: mod.executeImmediately || false,
|
|
180
|
+
frequency: mod.frequency || undefined,
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
this.currentTemplateArray.length = 0;
|
|
184
|
+
/** Clear all state properties. */
|
|
185
|
+
Object.keys(this.state).forEach((key) => {
|
|
186
|
+
delete this.state[key];
|
|
187
|
+
});
|
|
188
|
+
this.observable.setValue([]);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Start the tick loop. If the loop is already running, no changes will be made.
|
|
192
|
+
*
|
|
193
|
+
* @returns Whether the loop was started or not.
|
|
194
|
+
*/
|
|
195
|
+
startLoop() {
|
|
196
|
+
if (this.isLoopRunning) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
this.engineStartTime = performance.now();
|
|
200
|
+
this.isLoopRunning = true;
|
|
201
|
+
void this.runTickInLoop();
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
/** Execute a single tick on each render frame. */
|
|
205
|
+
async runTickInLoop() {
|
|
206
|
+
if (!this.isLoopRunning) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
await this.runSingleTick();
|
|
210
|
+
requestAnimationFrame(() => this.runTickInLoop());
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Run a single tick. This happens automatically in a loop if `startLoop` is called or
|
|
214
|
+
* `isLoopRunning` is set to `true`.
|
|
215
|
+
*/
|
|
216
|
+
async runSingleTick() {
|
|
217
|
+
makeWritable(this).isTickRunning = true;
|
|
218
|
+
/** Clear the array as we're about to populate it. */
|
|
219
|
+
this.currentTemplateArray.length = 0;
|
|
220
|
+
const executionStart = performance.now();
|
|
221
|
+
this.totalMs = executionStart - this.engineStartTime;
|
|
222
|
+
/**
|
|
223
|
+
* Use a plain `for` loop instead of `awaitedForEach` so that synchronous mods execute
|
|
224
|
+
* without microtask boundaries between them. Only yield when a mod actually returns a
|
|
225
|
+
* `Promise`.
|
|
226
|
+
*/
|
|
227
|
+
for (let index = 0; index < this.currentMods.length; index++) {
|
|
228
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
229
|
+
const mod = this.currentMods[index];
|
|
230
|
+
const lastExecution = this.lastModExecution.get(mod);
|
|
231
|
+
const shouldExecute = this.shouldModExecute(mod, lastExecution);
|
|
232
|
+
let executeResult;
|
|
233
|
+
if (shouldExecute) {
|
|
234
|
+
if (!lastExecution && mod.initState) {
|
|
235
|
+
Object.assign(this.state, mod.initState);
|
|
236
|
+
}
|
|
237
|
+
const rawResult = mod.execute({
|
|
238
|
+
engine: this,
|
|
239
|
+
modInstanceId: getOrSetFromMap(this.modInstanceIdMap, mod, () => applyBrand(createId())),
|
|
240
|
+
currentTick: this.currentTick,
|
|
241
|
+
state: this.state,
|
|
242
|
+
ticksSinceLastExecute: this.currentTick - (lastExecution?.tick ?? 0),
|
|
243
|
+
executeImmediately: mod.executeImmediately || false,
|
|
244
|
+
frequency: mod.frequency || undefined,
|
|
245
|
+
lastExecution,
|
|
246
|
+
hostElement: this.getEnsuredHostElement(),
|
|
247
|
+
msSinceLastExecute: executionStart - (lastExecution?.timeMs ?? this.engineStartTime),
|
|
248
|
+
});
|
|
249
|
+
executeResult = rawResult instanceof Promise ? await rawResult : rawResult;
|
|
250
|
+
}
|
|
251
|
+
const skipped = executeResult === SkipExecution;
|
|
252
|
+
const rawTemplate = shouldExecute && !skipped
|
|
253
|
+
? executeResult || undefined
|
|
254
|
+
: undefined;
|
|
255
|
+
const modTemplate = shouldExecute && !skipped
|
|
256
|
+
? rawTemplate
|
|
257
|
+
? html `
|
|
258
|
+
<div
|
|
259
|
+
data-antha-mod=${mod.modName}
|
|
260
|
+
style=${css `
|
|
261
|
+
display: contents;
|
|
262
|
+
`}
|
|
263
|
+
>
|
|
264
|
+
${rawTemplate}
|
|
265
|
+
</div>
|
|
266
|
+
`
|
|
267
|
+
: undefined
|
|
268
|
+
: this.currentTemplateMap.get(mod);
|
|
269
|
+
this.currentTemplateArray[index] = modTemplate;
|
|
270
|
+
if (shouldExecute && !skipped) {
|
|
271
|
+
this.currentTemplateMap.set(mod, modTemplate);
|
|
272
|
+
this.lastModExecution.set(mod, {
|
|
273
|
+
tick: this.currentTick,
|
|
274
|
+
timeMs: executionStart,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
this.currentTick++;
|
|
279
|
+
this.observable.setValue(this.currentTemplateArray);
|
|
280
|
+
makeWritable(this).isTickRunning = false;
|
|
281
|
+
}
|
|
282
|
+
/** Used to determine if a mod should execute right now or not. */
|
|
283
|
+
shouldModExecute(mod, lastExecution) {
|
|
284
|
+
if (!mod.frequency || (!lastExecution && mod.executeImmediately)) {
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
else if (mod.frequency.ticks) {
|
|
288
|
+
const ticksSinceLastExecution = this.currentTick - (lastExecution?.tick || 0);
|
|
289
|
+
return ticksSinceLastExecution >= mod.frequency.ticks;
|
|
290
|
+
}
|
|
291
|
+
else if (mod.frequency.durationMs) {
|
|
292
|
+
const msSinceLastExecution = performance.now() - (lastExecution?.timeMs || 0);
|
|
293
|
+
return msSinceLastExecution >= mod.frequency.durationMs;
|
|
294
|
+
}
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type PartialWithUndefined } from '@augment-vir/common';
|
|
2
|
+
import { type AnthaEngine } from './antha-engine.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for {@link AnthaUi}.
|
|
5
|
+
*
|
|
6
|
+
* @category UI
|
|
7
|
+
*/
|
|
8
|
+
export type AnthaUiOptions = {
|
|
9
|
+
/**
|
|
10
|
+
* If set to `true`, the engine won't automatically be cleaned up when the {@link AnthaUi}
|
|
11
|
+
* element is disconnected or destroyed.
|
|
12
|
+
*
|
|
13
|
+
* @default false
|
|
14
|
+
*/
|
|
15
|
+
disableDisconnectReset: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* If set to `true`, the engine won't automatically be started up when the {@link AnthaUi}
|
|
18
|
+
* element is connected.
|
|
19
|
+
*
|
|
20
|
+
* @default false
|
|
21
|
+
*/
|
|
22
|
+
disableConnectStart: boolean;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A built-in element for easily rendering {@link AnthaEngine} templates to the browser.
|
|
26
|
+
*
|
|
27
|
+
* @category UI
|
|
28
|
+
* @example
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import {AnthaUi, AnthaEngine} from '@antha/engine';
|
|
32
|
+
*
|
|
33
|
+
* const engine = new AnthaEngine();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare const AnthaUi: import("element-vir").DeclarativeElementDefinition<"antha-ui", PartialWithUndefined<{
|
|
37
|
+
engine: AnthaEngine;
|
|
38
|
+
options: Readonly<PartialWithUndefined<AnthaUiOptions>>;
|
|
39
|
+
}>, {
|
|
40
|
+
engineObservable: undefined | AnthaEngine["observable"];
|
|
41
|
+
}, {}, "antha-ui-", "antha-ui-", readonly [], readonly []>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { css, defineElement, nothing } from 'element-vir';
|
|
2
|
+
/**
|
|
3
|
+
* A built-in element for easily rendering {@link AnthaEngine} templates to the browser.
|
|
4
|
+
*
|
|
5
|
+
* @category UI
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import {AnthaUi, AnthaEngine} from '@antha/engine';
|
|
10
|
+
*
|
|
11
|
+
* const engine = new AnthaEngine();
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export const AnthaUi = defineElement()({
|
|
15
|
+
tagName: 'antha-ui',
|
|
16
|
+
styles: css `
|
|
17
|
+
:host {
|
|
18
|
+
/* Because all of these styles are defined on the host, you can easily override them. */
|
|
19
|
+
display: flex;
|
|
20
|
+
flex-direction: column;
|
|
21
|
+
align-items: flex-start;
|
|
22
|
+
height: 100dvh;
|
|
23
|
+
gap: 16px;
|
|
24
|
+
width: 100dvw;
|
|
25
|
+
box-sizing: border-box;
|
|
26
|
+
padding: 32px;
|
|
27
|
+
font-family: sans-serif;
|
|
28
|
+
}
|
|
29
|
+
`,
|
|
30
|
+
state() {
|
|
31
|
+
return {
|
|
32
|
+
engineObservable: undefined,
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
cleanup({ inputs, updateState }) {
|
|
36
|
+
if (!inputs.options?.disableDisconnectReset) {
|
|
37
|
+
void inputs.engine?.reset();
|
|
38
|
+
}
|
|
39
|
+
updateState({
|
|
40
|
+
/** Remove the observable on cleanup to stop render updates. */
|
|
41
|
+
engineObservable: undefined,
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
render({ state, updateState, inputs, host }) {
|
|
45
|
+
if (!inputs.engine) {
|
|
46
|
+
return nothing;
|
|
47
|
+
}
|
|
48
|
+
if (inputs.engine.hostElement !== host &&
|
|
49
|
+
(!inputs.engine.hostElement ||
|
|
50
|
+
inputs.engine.hostElement === globalThis.document.documentElement)) {
|
|
51
|
+
/**
|
|
52
|
+
* Automatically attach this element's host to the engine's host element if one has not
|
|
53
|
+
* been specifically set already.
|
|
54
|
+
*/
|
|
55
|
+
inputs.engine.hostElement = host;
|
|
56
|
+
}
|
|
57
|
+
if (state.engineObservable !== inputs.engine.observable) {
|
|
58
|
+
/** Automatically swap out the observable if the engine changes. */
|
|
59
|
+
updateState({
|
|
60
|
+
engineObservable: inputs.engine.observable,
|
|
61
|
+
});
|
|
62
|
+
if (!inputs.options?.disableConnectStart) {
|
|
63
|
+
inputs.engine.startLoop();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return state.engineObservable?.value;
|
|
67
|
+
},
|
|
68
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './antha-engine.js';
|
|
2
|
+
export * from './antha-ui.element.js';
|
|
3
|
+
export * from './logger/antha-logger.js';
|
|
4
|
+
export * from './logger/browser-antha-logger.js';
|
|
5
|
+
export * from './logger/empty-antha-logger.js';
|
|
6
|
+
export * from './logger/sentry-antha-logger.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './antha-engine.js';
|
|
2
|
+
export * from './antha-ui.element.js';
|
|
3
|
+
export * from './logger/antha-logger.js';
|
|
4
|
+
export * from './logger/browser-antha-logger.js';
|
|
5
|
+
export * from './logger/empty-antha-logger.js';
|
|
6
|
+
export * from './logger/sentry-antha-logger.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type PartialWithUndefined } from '@augment-vir/common';
|
|
2
|
+
import { type EventExtraContext, type EventTags } from 'sentry-vir';
|
|
3
|
+
/**
|
|
4
|
+
* Base logger methods.
|
|
5
|
+
*
|
|
6
|
+
* @category Internal
|
|
7
|
+
*/
|
|
8
|
+
export type BaseAnthaLogger = {
|
|
9
|
+
error: AnthaLog;
|
|
10
|
+
info: AnthaLog;
|
|
11
|
+
warning: AnthaLog;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The customizable logger used by Antha engine and its pre-built mods.
|
|
15
|
+
*
|
|
16
|
+
* @category Logger
|
|
17
|
+
*/
|
|
18
|
+
export type AnthaLogger = BaseAnthaLogger & {
|
|
19
|
+
/** Only logs if the given condition is true. */
|
|
20
|
+
if: (condition: boolean) => BaseAnthaLogger;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* An individual log method.
|
|
24
|
+
*
|
|
25
|
+
* @category Internal
|
|
26
|
+
*/
|
|
27
|
+
export type AnthaLog = (this: void, message: string | Error, context?: PartialWithUndefined<{
|
|
28
|
+
context: EventExtraContext;
|
|
29
|
+
tags: EventTags;
|
|
30
|
+
}> | undefined) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type AnthaLogger } from './antha-logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* A pre-built {@link AnthaLogger} that simply logs everything to the browser's console. This is the
|
|
4
|
+
* default logger in `AnthaEngine`.
|
|
5
|
+
*
|
|
6
|
+
* @category Logger
|
|
7
|
+
*/
|
|
8
|
+
export declare const browserAnthaLogger: AnthaLogger;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { baseEmptyAnthaLogger } from './empty-antha-logger.js';
|
|
2
|
+
const baseBrowserAnthaLogger = {
|
|
3
|
+
error(message, context) {
|
|
4
|
+
console.error(message, context);
|
|
5
|
+
},
|
|
6
|
+
info(message, context) {
|
|
7
|
+
console.info(message, context);
|
|
8
|
+
},
|
|
9
|
+
warning(message, context) {
|
|
10
|
+
console.warn(message, context);
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A pre-built {@link AnthaLogger} that simply logs everything to the browser's console. This is the
|
|
15
|
+
* default logger in `AnthaEngine`.
|
|
16
|
+
*
|
|
17
|
+
* @category Logger
|
|
18
|
+
*/
|
|
19
|
+
export const browserAnthaLogger = {
|
|
20
|
+
...baseBrowserAnthaLogger,
|
|
21
|
+
if(condition) {
|
|
22
|
+
if (condition) {
|
|
23
|
+
return baseBrowserAnthaLogger;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return baseEmptyAnthaLogger;
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AnthaLogger, type BaseAnthaLogger } from './antha-logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base log methods of {@link emptyAnthaLogger}.
|
|
4
|
+
*
|
|
5
|
+
* @category Internal
|
|
6
|
+
*/
|
|
7
|
+
export declare const baseEmptyAnthaLogger: BaseAnthaLogger;
|
|
8
|
+
/**
|
|
9
|
+
* A pre-built {@link AnthaLogger} that simply logs everything to the browser's console. This is the
|
|
10
|
+
* default logger in `AnthaEngine`.
|
|
11
|
+
*
|
|
12
|
+
* @category Logger
|
|
13
|
+
*/
|
|
14
|
+
export declare const emptyAnthaLogger: AnthaLogger;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base log methods of {@link emptyAnthaLogger}.
|
|
3
|
+
*
|
|
4
|
+
* @category Internal
|
|
5
|
+
*/
|
|
6
|
+
export const baseEmptyAnthaLogger = {
|
|
7
|
+
error() { },
|
|
8
|
+
info() { },
|
|
9
|
+
warning() { },
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A pre-built {@link AnthaLogger} that simply logs everything to the browser's console. This is the
|
|
13
|
+
* default logger in `AnthaEngine`.
|
|
14
|
+
*
|
|
15
|
+
* @category Logger
|
|
16
|
+
*/
|
|
17
|
+
export const emptyAnthaLogger = {
|
|
18
|
+
...baseEmptyAnthaLogger,
|
|
19
|
+
if() {
|
|
20
|
+
return baseEmptyAnthaLogger;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type AnthaLogger } from './antha-logger.js';
|
|
2
|
+
/**
|
|
3
|
+
* A pre-built {@link AnthaLogger} that sends logs to Sentry. Note that `initSentry` from
|
|
4
|
+
* `sentry-vir` must be called elsewhere for these logs to actually make it to sentry.
|
|
5
|
+
*
|
|
6
|
+
* @category Logger
|
|
7
|
+
*/
|
|
8
|
+
export declare const sentryAnthaLogger: AnthaLogger;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { handleError, sendLog } from 'sentry-vir';
|
|
2
|
+
import { baseEmptyAnthaLogger } from './empty-antha-logger.js';
|
|
3
|
+
const baseSentryAnthaLogger = {
|
|
4
|
+
error(message, context) {
|
|
5
|
+
handleError(message, context);
|
|
6
|
+
},
|
|
7
|
+
info(message, context) {
|
|
8
|
+
sendLog.info(message, context);
|
|
9
|
+
},
|
|
10
|
+
warning(message, context) {
|
|
11
|
+
sendLog.warning(message, context);
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A pre-built {@link AnthaLogger} that sends logs to Sentry. Note that `initSentry` from
|
|
16
|
+
* `sentry-vir` must be called elsewhere for these logs to actually make it to sentry.
|
|
17
|
+
*
|
|
18
|
+
* @category Logger
|
|
19
|
+
*/
|
|
20
|
+
export const sentryAnthaLogger = {
|
|
21
|
+
...baseSentryAnthaLogger,
|
|
22
|
+
if(condition) {
|
|
23
|
+
if (condition) {
|
|
24
|
+
return baseSentryAnthaLogger;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return baseEmptyAnthaLogger;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@antha/engine",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "The Antha game engine (for the web).",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"vir",
|
|
7
|
+
"antha"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/electrovir/antha",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/electrovir/antha/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/electrovir/antha.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "(MIT OR CC0-1.0)",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "electrovir",
|
|
20
|
+
"url": "https://github.com/electrovir"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "dist/index.js",
|
|
25
|
+
"module": "dist/index.js",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "virmator frontend build",
|
|
29
|
+
"compile": "virmator compile",
|
|
30
|
+
"docs": "virmator docs",
|
|
31
|
+
"start": "virmator frontend",
|
|
32
|
+
"test": "virmator test web",
|
|
33
|
+
"test:coverage": "npm run test coverage",
|
|
34
|
+
"test:docs": "virmator docs check",
|
|
35
|
+
"test:update": "npm test update"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@augment-vir/assert": "^31.73.1",
|
|
39
|
+
"@augment-vir/common": "^31.73.1",
|
|
40
|
+
"@paralleldrive/cuid2": "^3.3.0",
|
|
41
|
+
"date-vir": "^8.5.0",
|
|
42
|
+
"observavir": "^2.3.2",
|
|
43
|
+
"sentry-vir": "^4.4.0",
|
|
44
|
+
"type-fest": "^5.7.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@augment-vir/test": "^31.73.1",
|
|
48
|
+
"@augment-vir/web": "^31.73.1",
|
|
49
|
+
"@web/dev-server-esbuild": "^1.0.5",
|
|
50
|
+
"@web/test-runner": "^0.20.2",
|
|
51
|
+
"@web/test-runner-commands": "^0.9.0",
|
|
52
|
+
"@web/test-runner-playwright": "^0.11.1",
|
|
53
|
+
"@web/test-runner-visual-regression": "^0.10.0",
|
|
54
|
+
"istanbul-smart-text-reporter": "^1.1.5",
|
|
55
|
+
"markdown-code-example-inserter": "^3.0.5",
|
|
56
|
+
"typedoc": "^0.28.19",
|
|
57
|
+
"typescript": "^6.0.3",
|
|
58
|
+
"vite": "^8.0.16"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"element-vir": ">=26"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=22"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
}
|
|
69
|
+
}
|