@istar-ts/actions 0.15.0-beta.2 → 0.15.0-beta.3

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.cjs CHANGED
@@ -316,17 +316,25 @@ function createActionDispatcher(store, options = {}) {
316
316
  const listeners = /* @__PURE__ */ new Set();
317
317
  let saved = s.getModel();
318
318
  let dirty = false;
319
+ /**
320
+ * The origin of the dispatch currently applying synchronously (set by `dispatch`, around the
321
+ * synchronous part of `run`): what the store's subscription below echoes on
322
+ * `sourceModelChanged`/`setDirtyState`. `undefined` for a change not caused by a dispatch (a
323
+ * host's direct `store.load`) or once `run`'s synchronous part has returned.
324
+ */
325
+ let currentOrigin;
319
326
  const emit = (action) => {
320
327
  for (const listener of listeners) listener(action);
321
328
  };
322
- const updateDirty = (reason) => {
329
+ const updateDirty = (reason, origin) => {
323
330
  const next = s.getModel() !== saved;
324
331
  if (next === dirty) return;
325
332
  dirty = next;
326
333
  emit({
327
334
  kind: "setDirtyState",
328
335
  isDirty: next,
329
- reason
336
+ reason,
337
+ ...origin === void 0 ? {} : { origin }
330
338
  });
331
339
  };
332
340
  const REASONS = {
@@ -346,9 +354,10 @@ function createActionDispatcher(store, options = {}) {
346
354
  ...options.sendModel ? {
347
355
  canUndo: s.canUndo(),
348
356
  canRedo: s.canRedo()
349
- } : {}
357
+ } : {},
358
+ ...currentOrigin === void 0 ? {} : { origin: currentOrigin }
350
359
  });
351
- updateDirty(REASONS[event.source]);
360
+ updateDirty(REASONS[event.source], currentOrigin);
352
361
  });
353
362
  const requireElement = (id) => s.getModel().elements.get(id) ?? reject(`unknown element "${id}"`, id, "unknown-element");
354
363
  const runGuards = (operation, warnings) => {
@@ -745,7 +754,7 @@ function createActionDispatcher(store, options = {}) {
745
754
  default: return;
746
755
  }
747
756
  };
748
- const run = async (action) => {
757
+ const run = async (action, origin) => {
749
758
  if (isOperation(action)) return runOperation(action);
750
759
  if (isRequestAction(action)) return {
751
760
  ok: true,
@@ -770,7 +779,7 @@ function createActionDispatcher(store, options = {}) {
770
779
  const model = s.getModel();
771
780
  await options.save((0, _istar_ts_core.toPistarObject)(model, { saveDate: /* @__PURE__ */ new Date() }), action);
772
781
  saved = model;
773
- updateDirty("save");
782
+ updateDirty("save", origin);
774
783
  return {
775
784
  ok: true,
776
785
  createdIds: []
@@ -790,9 +799,17 @@ function createActionDispatcher(store, options = {}) {
790
799
  }
791
800
  };
792
801
  return {
793
- async dispatch(action) {
802
+ async dispatch(action, dispatchOptions) {
803
+ const origin = dispatchOptions?.origin;
804
+ currentOrigin = origin;
805
+ let pending;
806
+ try {
807
+ pending = run(action, origin);
808
+ } finally {
809
+ currentOrigin = void 0;
810
+ }
794
811
  try {
795
- return await run(action);
812
+ return await pending;
796
813
  } catch (error) {
797
814
  if (error instanceof Rejection) return {
798
815
  ok: false,
@@ -1016,8 +1033,8 @@ function createMirrorStore(remote, options = {}) {
1016
1033
  return {
1017
1034
  ready,
1018
1035
  dispatcher: {
1019
- async dispatch(action) {
1020
- const result = await r.dispatch(action);
1036
+ async dispatch(action, options) {
1037
+ const result = await r.dispatch(action, options);
1021
1038
  await tail;
1022
1039
  return result;
1023
1040
  },
@@ -1103,8 +1120,9 @@ function connectDispatcher(port, dispatcher) {
1103
1120
  port.onmessage = (ev) => {
1104
1121
  const message = ev.data;
1105
1122
  if (!isRecord(message) || message.jsonrpc !== JSONRPC_VERSION) return;
1106
- const { id, method, params } = message;
1123
+ const { id, method, params, origin } = message;
1107
1124
  const hasId = isJsonRpcId(id);
1125
+ const dispatchOptions = typeof origin === "string" ? { origin } : void 0;
1108
1126
  if (typeof method !== "string" || method.length === 0) {
1109
1127
  if (hasId) port.postMessage(errorMessage(id, -32601, "method not found"));
1110
1128
  return;
@@ -1115,10 +1133,10 @@ function connectDispatcher(port, dispatcher) {
1115
1133
  }
1116
1134
  const action = params;
1117
1135
  if (!hasId) {
1118
- dispatcher.dispatch(action);
1136
+ dispatcher.dispatch(action, dispatchOptions);
1119
1137
  return;
1120
1138
  }
1121
- dispatcher.dispatch(action).then((result) => {
1139
+ dispatcher.dispatch(action, dispatchOptions).then((result) => {
1122
1140
  port.postMessage({
1123
1141
  jsonrpc: JSONRPC_VERSION,
1124
1142
  id,
@@ -1162,7 +1180,7 @@ function createRemoteDispatcher(port) {
1162
1180
  }
1163
1181
  };
1164
1182
  return {
1165
- dispatch(action) {
1183
+ dispatch(action, dispatchOptions) {
1166
1184
  const id = nextId++;
1167
1185
  return new Promise((resolve, reject) => {
1168
1186
  pending.set(id, {
@@ -1173,7 +1191,8 @@ function createRemoteDispatcher(port) {
1173
1191
  jsonrpc: JSONRPC_VERSION,
1174
1192
  id,
1175
1193
  method: action.kind,
1176
- params: action
1194
+ params: action,
1195
+ ...dispatchOptions?.origin === void 0 ? {} : { origin: dispatchOptions.origin }
1177
1196
  });
1178
1197
  });
1179
1198
  },