@unseenco/theatre-core 0.1.8 → 0.1.10

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.mjs CHANGED
@@ -6510,6 +6510,12 @@ var TheatreSheet = class {
6510
6510
  }
6511
6511
  internal2.deleteObject(sanitizedPath);
6512
6512
  }
6513
+ getObjects() {
6514
+ return privateAPI(this).getObjects().map((obj) => obj.publicApi);
6515
+ }
6516
+ unload() {
6517
+ privateAPI(this).unload();
6518
+ }
6513
6519
  declareOutlineNamespace(namespacePath, opts) {
6514
6520
  const internal2 = privateAPI(this);
6515
6521
  const parsedPath = parseOutlineNamespacePath(
@@ -6597,12 +6603,36 @@ var Sheet = class {
6597
6603
  getObject(key) {
6598
6604
  return this._objects.get()[key];
6599
6605
  }
6606
+ getObjects() {
6607
+ return Object.values(this._objects.get()).filter(
6608
+ (obj) => !!obj
6609
+ );
6610
+ }
6600
6611
  deleteObject(objectKey) {
6612
+ const obj = this._objects.get()[objectKey];
6601
6613
  this._objects.reduce((state) => {
6602
6614
  const newState = { ...state };
6603
6615
  delete newState[objectKey];
6604
6616
  return newState;
6605
6617
  });
6618
+ if (obj) {
6619
+ this.project._remoteSync.unregisterObject(obj);
6620
+ }
6621
+ }
6622
+ /**
6623
+ * Runtime-only teardown: pause sequences, detach all objects, and remove
6624
+ * this sheet instance from the project. Persisted state is kept.
6625
+ */
6626
+ unload() {
6627
+ for (const sequence of Object.values(this._sequences)) {
6628
+ sequence.pause();
6629
+ }
6630
+ for (const objectKey of Object.keys(
6631
+ this._objects.get()
6632
+ )) {
6633
+ this.deleteObject(objectKey);
6634
+ }
6635
+ this.project._unloadSheetInstance(this);
6606
6636
  }
6607
6637
  getSequence(variant) {
6608
6638
  const variantId = variant ?? val7(this._activeSequenceVariant.pointer);
@@ -6697,6 +6727,21 @@ var SheetTemplate = class {
6697
6727
  }
6698
6728
  return inst;
6699
6729
  }
6730
+ getInstances() {
6731
+ return Object.values(this._instances.get()).filter(
6732
+ (inst) => !!inst
6733
+ );
6734
+ }
6735
+ hasInstance(instanceId) {
6736
+ return !!this._instances.get()[instanceId];
6737
+ }
6738
+ removeInstance(instanceId) {
6739
+ this._instances.reduce((state) => {
6740
+ const next = { ...state };
6741
+ delete next[instanceId];
6742
+ return next;
6743
+ });
6744
+ }
6700
6745
  getObjectTemplate(objectKey, nativeObject, config, actions, transient, staticPropPaths) {
6701
6746
  let template = this._objectTemplates.get()[objectKey];
6702
6747
  if (!template) {
@@ -7230,6 +7275,7 @@ var RemoteSync = class {
7230
7275
  __publicField(this, "channel");
7231
7276
  __publicField(this, "sheets", /* @__PURE__ */ new Map());
7232
7277
  __publicField(this, "objects", /* @__PURE__ */ new Map());
7278
+ __publicField(this, "objectUnsubs", /* @__PURE__ */ new Map());
7233
7279
  __publicField(this, "studio");
7234
7280
  __publicField(this, "activeSheet");
7235
7281
  if (typeof BroadcastChannel === "undefined")
@@ -7267,12 +7313,26 @@ var RemoteSync = class {
7267
7313
  registerSheet(sheet) {
7268
7314
  this.sheets.set(sheet.address.sheetId, sheet);
7269
7315
  }
7316
+ /**
7317
+ * Removes `sheet` from the sync map if it is the currently registered
7318
+ * instance for its `sheetId`. Returns whether it was removed.
7319
+ */
7320
+ unregisterSheet(sheet) {
7321
+ if (this.sheets.get(sheet.address.sheetId) !== sheet) {
7322
+ return false;
7323
+ }
7324
+ this.sheets.delete(sheet.address.sheetId);
7325
+ if (this.activeSheet === sheet) {
7326
+ this.activeSheet = void 0;
7327
+ }
7328
+ return true;
7329
+ }
7270
7330
  registerObject(obj) {
7271
7331
  const id = "".concat(obj.address.sheetId, "_").concat(obj.address.objectKey);
7272
7332
  this.objects.set(id, obj);
7273
7333
  if (this.isEditor && this.channel) {
7274
7334
  const channel = this.channel;
7275
- obj.onFinalValueChange((values) => {
7335
+ const unsubscribe = obj.onFinalValueChange((values) => {
7276
7336
  const message = {
7277
7337
  event: "updateSheetObject",
7278
7338
  // Some prop values (e.g. rgba colors) are class instances with
@@ -7283,6 +7343,16 @@ var RemoteSync = class {
7283
7343
  };
7284
7344
  channel.postMessage(message);
7285
7345
  });
7346
+ this.objectUnsubs.set(id, unsubscribe);
7347
+ }
7348
+ }
7349
+ unregisterObject(obj) {
7350
+ const id = "".concat(obj.address.sheetId, "_").concat(obj.address.objectKey);
7351
+ this.objects.delete(id);
7352
+ const unsubscribe = this.objectUnsubs.get(id);
7353
+ if (unsubscribe) {
7354
+ unsubscribe();
7355
+ this.objectUnsubs.delete(id);
7286
7356
  }
7287
7357
  }
7288
7358
  attachStudio(studio) {
@@ -7620,6 +7690,61 @@ var Project = class {
7620
7690
  this._remoteSync.registerSheet(sheet);
7621
7691
  return sheet;
7622
7692
  }
7693
+ getSheets() {
7694
+ const sheets = [];
7695
+ for (const template of Object.values(this._sheetTemplates.get())) {
7696
+ if (!template)
7697
+ continue;
7698
+ sheets.push(...template.getInstances());
7699
+ }
7700
+ return sheets;
7701
+ }
7702
+ /**
7703
+ * Runtime-only: unload one or all instances of a sheet. Persisted state is kept.
7704
+ * When `instanceId` is omitted, all loaded instances of `sheetId` are unloaded.
7705
+ */
7706
+ unloadSheet(sheetId, instanceId) {
7707
+ const template = this._sheetTemplates.get()[sheetId];
7708
+ if (!template) {
7709
+ return false;
7710
+ }
7711
+ const instances = instanceId ? template.hasInstance(instanceId) ? [template.getInstance(instanceId)] : [] : template.getInstances();
7712
+ if (instances.length === 0) {
7713
+ return false;
7714
+ }
7715
+ for (const sheet of instances) {
7716
+ sheet.unload();
7717
+ }
7718
+ return true;
7719
+ }
7720
+ unloadSheets() {
7721
+ for (const sheet of this.getSheets()) {
7722
+ sheet.unload();
7723
+ }
7724
+ }
7725
+ /**
7726
+ * Called by {@link Sheet.unload} after objects are detached. Removes the
7727
+ * instance from its template and drops the template when no instances remain.
7728
+ */
7729
+ _unloadSheetInstance(sheet) {
7730
+ const sheetId = sheet.address.sheetId;
7731
+ const template = this._sheetTemplates.get()[sheetId];
7732
+ if (!template || !template.hasInstance(sheet.instanceId)) {
7733
+ return;
7734
+ }
7735
+ template.removeInstance(sheet.instanceId);
7736
+ const wasRegistered = this._remoteSync.unregisterSheet(sheet);
7737
+ const remaining = template.getInstances();
7738
+ if (remaining.length === 0) {
7739
+ this._sheetTemplates.reduce((state) => {
7740
+ const next = { ...state };
7741
+ delete next[sheetId];
7742
+ return next;
7743
+ });
7744
+ } else if (wasRegistered) {
7745
+ this._remoteSync.registerSheet(remaining[0]);
7746
+ }
7747
+ }
7623
7748
  _commitOutlineNamespaceConfig(sheetId, namespacePathKey, config) {
7624
7749
  this._mutateCoreAhistoric((ahistoric) => {
7625
7750
  var _a;
@@ -7880,6 +8005,43 @@ var TheatreProject = class {
7880
8005
  sheetOpts
7881
8006
  ).publicApi;
7882
8007
  }
8008
+ getSheets() {
8009
+ return privateAPI(this).getSheets().map((sheet) => sheet.publicApi);
8010
+ }
8011
+ unloadSheet(sheetId, instanceId) {
8012
+ const sanitizedPath = validateAndSanitiseSlashedPathOrThrow(
8013
+ sheetId,
8014
+ "project.unloadSheet"
8015
+ );
8016
+ let sanitizedInstanceId;
8017
+ if (instanceId !== void 0) {
8018
+ if (process.env.NODE_ENV !== "production") {
8019
+ validateInstanceId(
8020
+ instanceId,
8021
+ "instanceId in project.unloadSheet(sheetId, instanceId)",
8022
+ true
8023
+ );
8024
+ }
8025
+ sanitizedInstanceId = instanceId;
8026
+ }
8027
+ const unloaded = privateAPI(this).unloadSheet(
8028
+ sanitizedPath,
8029
+ sanitizedInstanceId
8030
+ );
8031
+ if (!unloaded) {
8032
+ const instanceHint = instanceId !== void 0 ? ' (instance "'.concat(instanceId, '")') : "";
8033
+ notify.warning(
8034
+ "Couldn't unload sheet \"".concat(sanitizedPath, '"').concat(instanceHint),
8035
+ 'There is no loaded sheet with id "'.concat(sanitizedPath, '"').concat(instanceHint, ".\n\nTo fix this, make sure you are calling `project.unloadSheet` with a sheet that was previously created via `project.sheet(...)`.")
8036
+ );
8037
+ console.warn(
8038
+ 'Sheet "'.concat(sanitizedPath, '"').concat(instanceHint, " is not currently loaded.")
8039
+ );
8040
+ }
8041
+ }
8042
+ unloadSheets() {
8043
+ privateAPI(this).unloadSheets();
8044
+ }
7883
8045
  };
7884
8046
 
7885
8047
  // core/src/coreExports.ts
@@ -7980,7 +8142,7 @@ var CoreBundle = class {
7980
8142
  return "Theatre_CoreBundle";
7981
8143
  }
7982
8144
  get version() {
7983
- return "0.1.8";
8145
+ return "0.1.10";
7984
8146
  }
7985
8147
  getBitsForStudio(studio, callback) {
7986
8148
  if (this._studio) {