@absolutejs/sync 1.7.0 → 1.7.2

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.
@@ -1102,22 +1102,57 @@ var wrap = (source) => `
1102
1102
  );
1103
1103
  }
1104
1104
  const actions = {
1105
- insert: __syncActionInsert,
1106
- update: __syncActionUpdate,
1107
- delete: __syncActionDelete,
1108
- change: __syncActionChange
1105
+ insert: (table, data) => __syncAction('insert', table, data),
1106
+ update: (table, data) => __syncAction('update', table, data),
1107
+ delete: (table, row) => __syncAction('delete', table, row),
1108
+ change: (collection, change) => __syncAction('change', collection, change)
1109
1109
  };
1110
1110
  return await userFn(args, ctx, actions);
1111
1111
  })()
1112
1112
  `;
1113
+ var DEFAULT_RECYCLE_CONTEXT_AFTER = 256;
1114
+ var installRouter = async (context, currentActions, Reference) => {
1115
+ const router = new Reference((op, ...rest) => {
1116
+ const a = currentActions.value;
1117
+ if (a === undefined) {
1118
+ throw new Error("__syncAction invoked outside an active sandboxed call (shared-slot router)");
1119
+ }
1120
+ switch (op) {
1121
+ case "insert":
1122
+ return a.insert(rest[0], rest[1]);
1123
+ case "update":
1124
+ return a.update(rest[0], rest[1]);
1125
+ case "delete":
1126
+ return a.delete(rest[0], rest[1]);
1127
+ case "change":
1128
+ return a.change(rest[0], rest[1]);
1129
+ default:
1130
+ throw new Error(`unknown sandbox action op: ${String(op)}`);
1131
+ }
1132
+ });
1133
+ await context.setGlobal("__syncAction", router);
1134
+ };
1113
1135
  var compile = async (source, config) => {
1114
- const { createIsolate } = await loadIsolatedJsc();
1136
+ const { createIsolate, Reference } = await loadIsolatedJsc();
1115
1137
  const isolate = await createIsolate({
1116
- backend: config.backend ?? "worker",
1138
+ backend: config.backend ?? "auto",
1117
1139
  memoryLimit: config.memoryLimit ?? 32
1118
1140
  });
1119
1141
  const script = await isolate.compileScript(wrap(source));
1120
- return { isolate, script, timeoutMs: config.timeout ?? 5000 };
1142
+ const context = await isolate.createContext();
1143
+ const currentActions = {
1144
+ value: undefined
1145
+ };
1146
+ await installRouter(context, currentActions, Reference);
1147
+ return {
1148
+ context,
1149
+ currentActions,
1150
+ isolate,
1151
+ runQueue: Promise.resolve(undefined),
1152
+ script,
1153
+ servedCalls: 0,
1154
+ timeoutMs: config.timeout ?? 5000
1155
+ };
1121
1156
  };
1122
1157
  var makeSandboxedHandler = (source, config = {}) => {
1123
1158
  let pending;
@@ -1131,23 +1166,37 @@ var makeSandboxedHandler = (source, config = {}) => {
1131
1166
  pending = compile(source, config);
1132
1167
  return pending;
1133
1168
  };
1134
- return async (args, ctx, actions) => {
1169
+ const recycleContextIfNeeded = async (compiled) => {
1170
+ if (compiled.servedCalls < DEFAULT_RECYCLE_CONTEXT_AFTER)
1171
+ return;
1135
1172
  const { Reference } = await loadIsolatedJsc();
1173
+ await compiled.context.dispose().catch(() => {});
1174
+ compiled.context = await compiled.isolate.createContext();
1175
+ await installRouter(compiled.context, compiled.currentActions, Reference);
1176
+ compiled.servedCalls = 0;
1177
+ };
1178
+ return async (args, ctx, actions) => {
1136
1179
  const compiled = await getCompiled();
1137
- const context = await compiled.isolate.createContext();
1138
- try {
1139
- await context.setGlobal("args", args);
1140
- await context.setGlobal("ctx", ctx);
1141
- await context.setGlobal("__syncActionInsert", new Reference((table, data) => actions.insert(table, data)));
1142
- await context.setGlobal("__syncActionUpdate", new Reference((table, data) => actions.update(table, data)));
1143
- await context.setGlobal("__syncActionDelete", new Reference((table, row) => actions.delete(table, row)));
1144
- await context.setGlobal("__syncActionChange", new Reference((collection, change) => actions.change(collection, change)));
1145
- return await compiled.script.run(context, {
1146
- timeout: compiled.timeoutMs
1147
- });
1148
- } finally {
1149
- await context.dispose().catch(() => {});
1150
- }
1180
+ const prev = compiled.runQueue;
1181
+ const turn = prev.then(async () => {
1182
+ await recycleContextIfNeeded(compiled);
1183
+ compiled.currentActions.value = actions;
1184
+ try {
1185
+ await compiled.context.setGlobal("args", args);
1186
+ await compiled.context.setGlobal("ctx", ctx);
1187
+ const result = await compiled.script.run(compiled.context, {
1188
+ timeout: compiled.timeoutMs
1189
+ });
1190
+ return result;
1191
+ } finally {
1192
+ compiled.currentActions.value = undefined;
1193
+ compiled.servedCalls += 1;
1194
+ }
1195
+ });
1196
+ compiled.runQueue = turn.catch(() => {
1197
+ return;
1198
+ });
1199
+ return turn;
1151
1200
  };
1152
1201
  };
1153
1202
 
@@ -2762,5 +2811,5 @@ export {
2762
2811
  CdcConsumerSlowError
2763
2812
  };
2764
2813
 
2765
- //# debugId=EE73D3886B04BDA964756E2164756E21
2814
+ //# debugId=43E0F3CB08EACD1264756E2164756E21
2766
2815
  //# sourceMappingURL=index.js.map