@novely/core 0.15.1 → 0.16.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -381,9 +381,9 @@ interface NovelyInit<Languages extends string, Characters extends Record<string,
381
381
  }
382
382
  declare const novely: <Languages extends string, Characters extends Record<string, Character<Languages>>, StateScheme extends State, DataScheme extends Data>({ characters, storage, storageDelay, renderer: createRenderer, initialScreen, translation, languages, state: defaultState, data: defaultData, autosaves, migrations, throttleTimeout, getLanguage, overrideLanguage, askBeforeExit, preloadAssets, }: NovelyInit<Languages, Characters, StateScheme, DataScheme>) => {
383
383
  /**
384
- * Function to set story
384
+ * Function to set game script
385
385
  */
386
- withStory: (story: Story) => Promise<void>;
386
+ script: (part: Story) => Promise<void>;
387
387
  /**
388
388
  * Function to get actions
389
389
  */
@@ -1,9 +1,14 @@
1
1
  "use strict";
2
2
  var Novely = (() => {
3
+ var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
6
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __commonJS = (cb, mod) => function __require() {
10
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
+ };
7
12
  var __export = (target, all) => {
8
13
  for (var name in all)
9
14
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,8 +21,134 @@ var Novely = (() => {
16
21
  }
17
22
  return to;
18
23
  };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
19
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
33
 
34
+ // ../../node_modules/.pnpm/yocto-queue@0.1.0/node_modules/yocto-queue/index.js
35
+ var require_yocto_queue = __commonJS({
36
+ "../../node_modules/.pnpm/yocto-queue@0.1.0/node_modules/yocto-queue/index.js"(exports, module) {
37
+ "use strict";
38
+ var Node = class {
39
+ /// value;
40
+ /// next;
41
+ constructor(value) {
42
+ this.value = value;
43
+ this.next = void 0;
44
+ }
45
+ };
46
+ var Queue = class {
47
+ // TODO: Use private class fields when targeting Node.js 12.
48
+ // #_head;
49
+ // #_tail;
50
+ // #_size;
51
+ constructor() {
52
+ this.clear();
53
+ }
54
+ enqueue(value) {
55
+ const node = new Node(value);
56
+ if (this._head) {
57
+ this._tail.next = node;
58
+ this._tail = node;
59
+ } else {
60
+ this._head = node;
61
+ this._tail = node;
62
+ }
63
+ this._size++;
64
+ }
65
+ dequeue() {
66
+ const current = this._head;
67
+ if (!current) {
68
+ return;
69
+ }
70
+ this._head = this._head.next;
71
+ this._size--;
72
+ return current.value;
73
+ }
74
+ clear() {
75
+ this._head = void 0;
76
+ this._tail = void 0;
77
+ this._size = 0;
78
+ }
79
+ get size() {
80
+ return this._size;
81
+ }
82
+ *[Symbol.iterator]() {
83
+ let current = this._head;
84
+ while (current) {
85
+ yield current.value;
86
+ current = current.next;
87
+ }
88
+ }
89
+ };
90
+ module.exports = Queue;
91
+ }
92
+ });
93
+
94
+ // ../../node_modules/.pnpm/p-limit@3.1.0/node_modules/p-limit/index.js
95
+ var require_p_limit = __commonJS({
96
+ "../../node_modules/.pnpm/p-limit@3.1.0/node_modules/p-limit/index.js"(exports, module) {
97
+ "use strict";
98
+ var Queue = require_yocto_queue();
99
+ var pLimit2 = (concurrency) => {
100
+ if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) {
101
+ throw new TypeError("Expected `concurrency` to be a number from 1 and up");
102
+ }
103
+ const queue = new Queue();
104
+ let activeCount = 0;
105
+ const next = () => {
106
+ activeCount--;
107
+ if (queue.size > 0) {
108
+ queue.dequeue()();
109
+ }
110
+ };
111
+ const run = async (fn, resolve, ...args) => {
112
+ activeCount++;
113
+ const result = (async () => fn(...args))();
114
+ resolve(result);
115
+ try {
116
+ await result;
117
+ } catch {
118
+ }
119
+ next();
120
+ };
121
+ const enqueue = (fn, resolve, ...args) => {
122
+ queue.enqueue(run.bind(null, fn, resolve, ...args));
123
+ (async () => {
124
+ await Promise.resolve();
125
+ if (activeCount < concurrency && queue.size > 0) {
126
+ queue.dequeue()();
127
+ }
128
+ })();
129
+ };
130
+ const generator = (fn, ...args) => new Promise((resolve) => {
131
+ enqueue(fn, resolve, ...args);
132
+ });
133
+ Object.defineProperties(generator, {
134
+ activeCount: {
135
+ get: () => activeCount
136
+ },
137
+ pendingCount: {
138
+ get: () => queue.size
139
+ },
140
+ clearQueue: {
141
+ value: () => {
142
+ queue.clear();
143
+ }
144
+ }
145
+ });
146
+ return generator;
147
+ };
148
+ module.exports = pLimit2;
149
+ }
150
+ });
151
+
21
152
  // src/index.ts
22
153
  var src_exports = {};
23
154
  __export(src_exports, {
@@ -353,6 +484,9 @@ var Novely = (() => {
353
484
  };
354
485
  };
355
486
 
487
+ // src/novely.ts
488
+ var import_p_limit = __toESM(require_p_limit(), 1);
489
+
356
490
  // ../../node_modules/.pnpm/esm-env@1.0.0/node_modules/esm-env/prod-ssr.js
357
491
  var DEV = false;
358
492
 
@@ -375,28 +509,41 @@ var Novely = (() => {
375
509
  askBeforeExit = true,
376
510
  preloadAssets = "lazy"
377
511
  }) => {
512
+ const limitScript = (0, import_p_limit.default)(1);
378
513
  const story = {};
379
514
  const times = /* @__PURE__ */ new Set();
380
515
  const ASSETS_TO_PRELOAD = /* @__PURE__ */ new Set();
381
516
  const assetsLoaded = createControlledPromise();
517
+ const dataLoaded = createControlledPromise();
518
+ let scriptCalled = false;
382
519
  defaultData ||= {};
383
520
  defaultState ||= {};
384
521
  const intime = (value) => {
385
522
  return times.add(value), value;
386
523
  };
387
- const script = (part) => {
524
+ const scriptBase = async (part) => {
388
525
  Object.assign(story, flattenStory(part));
389
- };
390
- const withStory = async (story2) => {
391
- script(story2);
392
526
  if (preloadAssets === "blocking" && ASSETS_TO_PRELOAD.size > 0) {
393
527
  renderer.ui.showScreen("loading");
394
528
  await renderer.misc.preloadImagesBlocking(ASSETS_TO_PRELOAD);
395
529
  }
530
+ const screen = renderer.ui.getScreen();
531
+ const nextScreen = scriptCalled ? screen : initialScreen;
396
532
  ASSETS_TO_PRELOAD.clear();
397
533
  assetsLoaded.resolve();
398
- if (initialScreen !== "game")
399
- renderer.ui.showScreen(initialScreen);
534
+ if (nextScreen === "game") {
535
+ await assetsLoaded.promise;
536
+ await dataLoaded.promise;
537
+ if (!scriptCalled) {
538
+ restore();
539
+ }
540
+ } else {
541
+ renderer.ui.showScreen(nextScreen);
542
+ }
543
+ scriptCalled = true;
544
+ };
545
+ const script = (part) => {
546
+ return limitScript(() => scriptBase(part));
400
547
  };
401
548
  const action = new Proxy({}, {
402
549
  get(_, prop) {
@@ -494,11 +641,8 @@ var Novely = (() => {
494
641
  stored.data = defaultData;
495
642
  }
496
643
  $$.update((prev) => (prev.dataLoaded = true, prev));
644
+ dataLoaded.resolve();
497
645
  $.update(() => stored);
498
- if (initialScreen === "game") {
499
- await assetsLoaded.promise;
500
- restore();
501
- }
502
646
  };
503
647
  storageDelay.then(getStoredData);
504
648
  const initial = getDefaultSave(klona(defaultState));
@@ -880,9 +1024,18 @@ var Novely = (() => {
880
1024
  push();
881
1025
  },
882
1026
  animateCharacter([character, timeout, ...classes]) {
1027
+ if (DEV && classes.length === 0) {
1028
+ throw new Error("Attempt to use AnimateCharacter without classes. Classes should be provided [https://novely.pages.dev/guide/actions/animateCharacter.html]");
1029
+ }
1030
+ if (DEV && (timeout <= 0 || !Number.isFinite(timeout) || Number.isNaN(timeout))) {
1031
+ throw new Error("Attempt to use AnimateCharacter with unacceptable timeout. It should be finite and greater than zero");
1032
+ }
883
1033
  const handler = (get) => {
884
1034
  const { clear } = get("@@internal-animate-character", false);
885
1035
  const char = renderer.store.characters[character];
1036
+ if (DEV && !char) {
1037
+ throw new Error(`Attempt to call AnimateCharacter with character "${character}" which is not currently exists. Maybe AnimateCharacter was called before ShowCharacter?`);
1038
+ }
886
1039
  if (!char)
887
1040
  return;
888
1041
  const target = char.canvas;
@@ -1026,9 +1179,9 @@ var Novely = (() => {
1026
1179
  }
1027
1180
  return {
1028
1181
  /**
1029
- * Function to set story
1182
+ * Function to set game script
1030
1183
  */
1031
- withStory,
1184
+ script,
1032
1185
  /**
1033
1186
  * Function to get actions
1034
1187
  */