@novely/core 0.32.0 → 0.33.0-beta.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.js CHANGED
@@ -478,6 +478,90 @@ var getIntlLanguageDisplayName = (lang) => {
478
478
  return lang;
479
479
  }
480
480
  };
481
+ var createReferFunction = (story) => {
482
+ const refer = (path) => {
483
+ let current = story;
484
+ let precurrent = story;
485
+ const blocks = [];
486
+ for (const [type, val] of path) {
487
+ if (type === "jump") {
488
+ precurrent = story;
489
+ current = current[val];
490
+ } else if (type === null) {
491
+ precurrent = current;
492
+ current = current[val];
493
+ } else if (type === "choice") {
494
+ blocks.push(precurrent);
495
+ current = current[val + 1][1];
496
+ } else if (type === "condition") {
497
+ blocks.push(precurrent);
498
+ current = current[2][val];
499
+ } else if (type === "block") {
500
+ blocks.push(precurrent);
501
+ current = story[val];
502
+ } else if (type === "block:exit" || type === "choice:exit" || type === "condition:exit") {
503
+ current = blocks.pop();
504
+ }
505
+ }
506
+ return current;
507
+ };
508
+ return refer;
509
+ };
510
+ var exitPath = ({ path, refer, onExitImpossible }) => {
511
+ const last = path.at(-1);
512
+ const ignore = [];
513
+ let wasExitImpossible = false;
514
+ if (!isAction(refer(path))) {
515
+ if (last && isNull(last[0]) && isNumber(last[1])) {
516
+ last[1]--;
517
+ } else {
518
+ path.pop();
519
+ }
520
+ }
521
+ if (isExitImpossible(path)) {
522
+ const referred = refer(path);
523
+ if (isAction(referred) && isSkippedDuringRestore(referred[0])) {
524
+ onExitImpossible();
525
+ }
526
+ wasExitImpossible = true;
527
+ return {
528
+ exitImpossible: wasExitImpossible
529
+ };
530
+ }
531
+ for (let i = path.length - 1; i > 0; i--) {
532
+ const [name] = path[i];
533
+ if (isBlockExitStatement(name)) {
534
+ ignore.push(name);
535
+ }
536
+ if (!isBlockStatement(name))
537
+ continue;
538
+ if (ignore.at(-1)?.startsWith(name)) {
539
+ ignore.pop();
540
+ continue;
541
+ }
542
+ path.push([`${name}:exit`]);
543
+ const prev = findLastPathItemBeforeItemOfType(path.slice(0, i + 1), name);
544
+ if (prev)
545
+ path.push([null, prev[1] + 1]);
546
+ if (!isAction(refer(path))) {
547
+ path.pop();
548
+ continue;
549
+ }
550
+ break;
551
+ }
552
+ return {
553
+ exitImpossible: wasExitImpossible
554
+ };
555
+ };
556
+ var nextPath = (path) => {
557
+ const last = path.at(-1);
558
+ if (last && (isNull(last[0]) || last[0] === "jump") && isNumber(last[1])) {
559
+ last[1]++;
560
+ } else {
561
+ path.push([null, 0]);
562
+ }
563
+ return path;
564
+ };
481
565
 
482
566
  // src/novely.ts
483
567
  import { dequal } from "dequal/lite";
@@ -977,10 +1061,12 @@ var novely = ({
977
1061
  }
978
1062
  }
979
1063
  }
980
- match("clear", [keep, characters2, audio], {
981
- ctx: context,
982
- data: latest[1]
983
- });
1064
+ if (context.meta.goingBack) {
1065
+ match("clear", [keep, characters2, audio], {
1066
+ ctx: context,
1067
+ data: latest[1]
1068
+ });
1069
+ }
984
1070
  const lastQueueItem = queue.at(-1) || [];
985
1071
  const lastQueueItemRequiresUserAction = isSkippedDuringRestore(lastQueueItem[0]) || isUserRequiredAction(lastQueueItem);
986
1072
  await run((item) => {
@@ -998,32 +1084,7 @@ var novely = ({
998
1084
  context.meta.restoring = context.meta.goingBack = false;
999
1085
  render(context);
1000
1086
  };
1001
- const refer = (path) => {
1002
- let current = story;
1003
- let precurrent = story;
1004
- const blocks = [];
1005
- for (const [type, val] of path) {
1006
- if (type === "jump") {
1007
- precurrent = story;
1008
- current = current[val];
1009
- } else if (type === null) {
1010
- precurrent = current;
1011
- current = current[val];
1012
- } else if (type === "choice") {
1013
- blocks.push(precurrent);
1014
- current = current[val + 1][1];
1015
- } else if (type === "condition") {
1016
- blocks.push(precurrent);
1017
- current = current[2][val];
1018
- } else if (type === "block") {
1019
- blocks.push(precurrent);
1020
- current = story[val];
1021
- } else if (type === "block:exit" || type === "choice:exit" || type === "condition:exit") {
1022
- current = blocks.pop();
1023
- }
1024
- }
1025
- return current;
1026
- };
1087
+ const refer = createReferFunction(story);
1027
1088
  const exit = (force = false, saving = true) => {
1028
1089
  const ctx = renderer.getContext(MAIN_CONTEXT_KEY);
1029
1090
  const stack = useStack(ctx);
@@ -1148,15 +1209,6 @@ var novely = ({
1148
1209
  stack.push(current);
1149
1210
  save("auto");
1150
1211
  };
1151
- const nextPath = (path) => {
1152
- const last = path.at(-1);
1153
- if (last && (isNull(last[0]) || last[0] === "jump") && isNumber(last[1])) {
1154
- last[1]++;
1155
- } else {
1156
- path.push([null, 0]);
1157
- }
1158
- return path;
1159
- };
1160
1212
  const next = (ctx) => {
1161
1213
  const stack = useStack(ctx);
1162
1214
  const path = stack.value[0];
@@ -1421,49 +1473,19 @@ var novely = ({
1421
1473
  exit({ ctx, data: data2 }) {
1422
1474
  if (ctx.meta.restoring)
1423
1475
  return;
1424
- const stack = useStack(ctx);
1425
- const path = stack.value[0];
1426
- const last = path.at(-1);
1427
- const ignore = [];
1428
- if (!isAction(refer(path))) {
1429
- if (last && isNull(last[0]) && isNumber(last[1])) {
1430
- last[1]--;
1431
- } else {
1432
- path.pop();
1433
- }
1434
- }
1435
- if (isExitImpossible(path)) {
1436
- const referred = refer(path);
1437
- if (isAction(referred) && isSkippedDuringRestore(referred[0])) {
1476
+ const { exitImpossible } = exitPath({
1477
+ path: useStack(ctx).value[0],
1478
+ refer,
1479
+ onExitImpossible: () => {
1438
1480
  match("end", [], {
1439
1481
  ctx,
1440
1482
  data: data2
1441
1483
  });
1442
1484
  }
1443
- return;
1444
- }
1445
- for (let i = path.length - 1; i > 0; i--) {
1446
- const [name] = path[i];
1447
- if (isBlockExitStatement(name)) {
1448
- ignore.push(name);
1449
- }
1450
- if (!isBlockStatement(name))
1451
- continue;
1452
- if (ignore.at(-1)?.startsWith(name)) {
1453
- ignore.pop();
1454
- continue;
1455
- }
1456
- path.push([`${name}:exit`]);
1457
- const prev = findLastPathItemBeforeItemOfType(path.slice(0, i + 1), name);
1458
- if (prev)
1459
- path.push([null, prev[1] + 1]);
1460
- if (!isAction(refer(path))) {
1461
- path.pop();
1462
- continue;
1463
- }
1464
- break;
1485
+ });
1486
+ if (!exitImpossible) {
1487
+ render(ctx);
1465
1488
  }
1466
- render(ctx);
1467
1489
  },
1468
1490
  preload({ ctx, push }, [source]) {
1469
1491
  if (!ctx.meta.goingBack && !ctx.meta.restoring && !PRELOADED_ASSETS.has(source)) {