@defold-typescript/types 0.46.0 → 0.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/hsm/index.d.ts +107 -0
  2. package/package.json +1 -1
package/hsm/index.d.ts CHANGED
@@ -304,4 +304,111 @@ export type MachineConfig<Ctx, E extends EventObject> = CompoundState<Ctx, E, un
304
304
  * sees the edit; without one, each call builds a separate machine.
305
305
  */
306
306
  export declare function defineMachine<Ctx, E extends EventObject>(key?: string): <T>(config: RootState<Ctx, E, T>) => Machine<Ctx, E, TreePath<T>>;
307
+ /**
308
+ * The key a `definePrivateMachine` config adds to the root, beside `initial` and `states`.
309
+ * @noSelf
310
+ */
311
+ export interface PrivateCtxConfig<Ctx, Priv> {
312
+ /**
313
+ * Builds the fields only the machine uses. It runs once per `start`, before the root's `enter`
314
+ * hook, so every instance gets fresh fields of its own; a value written straight into the
315
+ * config would be one array or table shared by every instance. It takes no arguments: to set
316
+ * a private field from a field of `Ctx`, do it in the root's `enter` hook.
317
+ *
318
+ * The fields are added to the object passed to `start`, which stays the instance's `ctx`, so
319
+ * restarting with `start(old.ctx)` keeps the public fields and builds the private ones fresh.
320
+ * A hot reload under the machine's key adds the fields a new factory introduces and leaves
321
+ * every field that already holds a value alone. A private field may not share a name with a
322
+ * field of `Ctx`.
323
+ *
324
+ * The fields are hidden from callers by type only. At run time `ctx` is one table, and hooks,
325
+ * `hsm-view` and a debugger see every field.
326
+ */
327
+ readonly privateCtx: () => Priv & {
328
+ readonly [K in keyof Ctx & keyof Priv]: never;
329
+ };
330
+ }
331
+ /**
332
+ * Like `defineMachine`, for a machine that keeps fields of its own, such as a counter, a heat
333
+ * level or a list it builds up. The config's `privateCtx` returns those fields. Every hook,
334
+ * `when`, `run`, `task` and `update` sees them beside `Ctx`, while `start` takes only `Ctx` and
335
+ * the instance it returns shows only `Ctx` in its `ctx`. A machine without fields of its own
336
+ * uses `defineMachine`.
337
+ *
338
+ * There are two ways to type the fields.
339
+ *
340
+ * @example
341
+ * **Inferred.** For a few fields, write them inline and let TypeScript infer their types. Add
342
+ * `as` where the inferred type is wrong: an empty array is `never[]` until it reads
343
+ * `[] as Hash[]`, and a field holding one of several strings is any `string` until it reads
344
+ * `"cool" as "cool" | "hot"`.
345
+ *
346
+ * ```ts
347
+ * type TurretEvent = { type: "SPOTTED" } | { type: "LOST" };
348
+ *
349
+ * export const turret = definePrivateMachine<{ readonly fireRate: number }, TurretEvent>("turret")({
350
+ * privateCtx: () => ({ heat: 0, targets: [] as Hash[] }),
351
+ * initial: "/idle",
352
+ * states: {
353
+ * idle: { on: { SPOTTED: "/firing" } },
354
+ * firing: {
355
+ * enter: (ctx) => {
356
+ * ctx.heat += 1 / ctx.fireRate;
357
+ * },
358
+ * on: { LOST: "/idle" },
359
+ * },
360
+ * },
361
+ * });
362
+ *
363
+ * const gun = turret.start({ fireRate: 2 }); // gun.ctx shows fireRate only
364
+ * ```
365
+ *
366
+ * @example
367
+ * **A named type.** For more fields, or when a helper outside the config needs the whole
368
+ * context, declare the fields as an interface and make it the factory's return type. The fields
369
+ * need no `as`, a misspelled or unknown field is a compile error, and the whole context is
370
+ * `TurretOptions & TurretPrivate`. `satisfies TurretPrivate` does not do this: it checks the
371
+ * object but keeps its narrow types, so an empty array stays `never[]`.
372
+ *
373
+ * ```ts
374
+ * interface TurretOptions {
375
+ * readonly fireRate: number;
376
+ * }
377
+ *
378
+ * interface TurretPrivate {
379
+ * heat: number;
380
+ * mode: "cool" | "hot";
381
+ * }
382
+ *
383
+ * type TurretEvent = { type: "SPOTTED" } | { type: "LOST" };
384
+ *
385
+ * const overheated = (ctx: TurretOptions & TurretPrivate) => ctx.heat > ctx.fireRate;
386
+ *
387
+ * export const turret = definePrivateMachine<TurretOptions, TurretEvent>("turret")({
388
+ * privateCtx: (): TurretPrivate => ({ heat: 0, mode: "cool" }),
389
+ * initial: "/idle",
390
+ * states: {
391
+ * idle: { on: { SPOTTED: "/firing" } },
392
+ * firing: {
393
+ * enter: (ctx) => {
394
+ * ctx.heat += 1 / ctx.fireRate;
395
+ * },
396
+ * always: { to: "/cooling", when: overheated },
397
+ * on: { LOST: "/idle" },
398
+ * },
399
+ * cooling: {
400
+ * enter: (ctx) => {
401
+ * ctx.mode = "hot";
402
+ * },
403
+ * exit: (ctx) => {
404
+ * ctx.heat = 0;
405
+ * ctx.mode = "cool";
406
+ * },
407
+ * after: { 2: "/idle" },
408
+ * },
409
+ * },
410
+ * });
411
+ * ```
412
+ */
413
+ export declare const definePrivateMachine: <Ctx, E extends EventObject>(key?: string) => <T, Priv extends object>(config: RootState<Ctx & Priv, E, T> & PrivateCtxConfig<Ctx, Priv>) => Machine<Ctx, E, TreePath<T>>;
307
414
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defold-typescript/types",
3
- "version": "0.46.0",
3
+ "version": "0.47.0",
4
4
  "description": "TypeScript types for the Defold engine's Lua APIs.",
5
5
  "license": "MIT",
6
6
  "repository": {