ilha 0.10.1 → 0.10.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.
- package/dist/index.js +105 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1077,6 +1077,77 @@ function buildDerivedAccessors(envelopes) {
|
|
|
1077
1077
|
return accessors.get(key) ?? defaultDerivedAccessor();
|
|
1078
1078
|
} });
|
|
1079
1079
|
}
|
|
1080
|
+
const ILHA_QUERY = Symbol.for("ilha.store.query");
|
|
1081
|
+
const ILHA_QUERY_RUN = Symbol.for("ilha.store.query.run");
|
|
1082
|
+
const ILHA_QUERY_PENDING = Symbol.for("ilha.store.query.pending");
|
|
1083
|
+
const ILHA_QUERY_STACK = Symbol.for("ilha.store.query.stack");
|
|
1084
|
+
function queryCaptureStack() {
|
|
1085
|
+
const g = globalThis;
|
|
1086
|
+
let stack = g[ILHA_QUERY_STACK];
|
|
1087
|
+
if (!stack) {
|
|
1088
|
+
stack = [];
|
|
1089
|
+
g[ILHA_QUERY_STACK] = stack;
|
|
1090
|
+
}
|
|
1091
|
+
return stack;
|
|
1092
|
+
}
|
|
1093
|
+
function isQueryCall(value) {
|
|
1094
|
+
return value !== null && typeof value === "object" && ILHA_QUERY in value;
|
|
1095
|
+
}
|
|
1096
|
+
function resolveQueryCall(result, captureSlot) {
|
|
1097
|
+
const pending = globalThis[ILHA_QUERY_PENDING];
|
|
1098
|
+
globalThis[ILHA_QUERY_PENDING] = void 0;
|
|
1099
|
+
return captureSlot.queryCall ?? pending ?? (isQueryCall(result) ? result : void 0);
|
|
1100
|
+
}
|
|
1101
|
+
function runBrandedQuery(queryCall, abortSignal, env) {
|
|
1102
|
+
const writeEnv = (next) => {
|
|
1103
|
+
if (abortSignal.aborted) return;
|
|
1104
|
+
const prevSub = setActiveSub(void 0);
|
|
1105
|
+
env(next);
|
|
1106
|
+
setActiveSub(prevSub);
|
|
1107
|
+
};
|
|
1108
|
+
const run = queryCall[ILHA_QUERY_RUN];
|
|
1109
|
+
if (typeof run !== "function") {
|
|
1110
|
+
writeEnv({
|
|
1111
|
+
loading: false,
|
|
1112
|
+
value: void 0,
|
|
1113
|
+
error: /* @__PURE__ */ new Error("[@ilha] branded query is missing a callable runner")
|
|
1114
|
+
});
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
try {
|
|
1118
|
+
run.call(queryCall, abortSignal, {
|
|
1119
|
+
onResult: (value) => {
|
|
1120
|
+
writeEnv({
|
|
1121
|
+
loading: false,
|
|
1122
|
+
value,
|
|
1123
|
+
error: void 0
|
|
1124
|
+
});
|
|
1125
|
+
},
|
|
1126
|
+
onError: (err) => {
|
|
1127
|
+
writeEnv({
|
|
1128
|
+
loading: false,
|
|
1129
|
+
value: void 0,
|
|
1130
|
+
error: err
|
|
1131
|
+
});
|
|
1132
|
+
},
|
|
1133
|
+
onFetchStart: () => {
|
|
1134
|
+
const prevSub = setActiveSub(void 0);
|
|
1135
|
+
env({
|
|
1136
|
+
loading: true,
|
|
1137
|
+
value: env().value,
|
|
1138
|
+
error: void 0
|
|
1139
|
+
});
|
|
1140
|
+
setActiveSub(prevSub);
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
} catch (err) {
|
|
1144
|
+
writeEnv({
|
|
1145
|
+
loading: false,
|
|
1146
|
+
value: void 0,
|
|
1147
|
+
error: err instanceof Error ? err : new Error(String(err))
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1080
1151
|
function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
1081
1152
|
const envelopes = /* @__PURE__ */ new Map();
|
|
1082
1153
|
for (const entry of entries) {
|
|
@@ -1084,13 +1155,22 @@ function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
|
1084
1155
|
if (derivedSnapshot != null && entry.key in derivedSnapshot) initialEnvelope = { ...derivedSnapshot[entry.key] };
|
|
1085
1156
|
else if (!(entry.fn.constructor.name === "AsyncFunction" || entry.fn.constructor.name === "AsyncGeneratorFunction")) {
|
|
1086
1157
|
const ac = new AbortController();
|
|
1158
|
+
const captureSlot = {};
|
|
1159
|
+
const stack = queryCaptureStack();
|
|
1160
|
+
globalThis[ILHA_QUERY_PENDING] = void 0;
|
|
1161
|
+
stack.push(captureSlot);
|
|
1087
1162
|
try {
|
|
1088
1163
|
const result = entry.fn({
|
|
1089
1164
|
state,
|
|
1090
1165
|
input,
|
|
1091
1166
|
signal: ac.signal
|
|
1092
1167
|
});
|
|
1093
|
-
if (
|
|
1168
|
+
if (resolveQueryCall(result, captureSlot)) initialEnvelope = {
|
|
1169
|
+
loading: true,
|
|
1170
|
+
value: void 0,
|
|
1171
|
+
error: void 0
|
|
1172
|
+
};
|
|
1173
|
+
else if (!(result instanceof Promise)) initialEnvelope = {
|
|
1094
1174
|
loading: false,
|
|
1095
1175
|
value: result,
|
|
1096
1176
|
error: void 0
|
|
@@ -1104,12 +1184,14 @@ function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
|
1104
1184
|
result.catch(() => {});
|
|
1105
1185
|
}
|
|
1106
1186
|
} catch (err) {
|
|
1187
|
+
globalThis[ILHA_QUERY_PENDING] = void 0;
|
|
1107
1188
|
initialEnvelope = {
|
|
1108
1189
|
loading: false,
|
|
1109
1190
|
value: void 0,
|
|
1110
1191
|
error: err instanceof Error ? err : new Error(String(err))
|
|
1111
1192
|
};
|
|
1112
1193
|
} finally {
|
|
1194
|
+
stack.pop();
|
|
1113
1195
|
ac.abort();
|
|
1114
1196
|
}
|
|
1115
1197
|
} else initialEnvelope = {
|
|
@@ -1148,6 +1230,10 @@ function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
|
1148
1230
|
ac = new AbortController();
|
|
1149
1231
|
const currentAc = ac;
|
|
1150
1232
|
let result;
|
|
1233
|
+
const captureSlot = {};
|
|
1234
|
+
const stack = queryCaptureStack();
|
|
1235
|
+
globalThis[ILHA_QUERY_PENDING] = void 0;
|
|
1236
|
+
stack.push(captureSlot);
|
|
1151
1237
|
try {
|
|
1152
1238
|
result = entry.fn({
|
|
1153
1239
|
state,
|
|
@@ -1155,6 +1241,7 @@ function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
|
1155
1241
|
signal: currentAc.signal
|
|
1156
1242
|
});
|
|
1157
1243
|
} catch (err) {
|
|
1244
|
+
globalThis[ILHA_QUERY_PENDING] = void 0;
|
|
1158
1245
|
if (skipFirst) {
|
|
1159
1246
|
skipFirst = false;
|
|
1160
1247
|
return;
|
|
@@ -1167,12 +1254,19 @@ function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
|
1167
1254
|
});
|
|
1168
1255
|
setActiveSub(prevSub);
|
|
1169
1256
|
return;
|
|
1257
|
+
} finally {
|
|
1258
|
+
stack.pop();
|
|
1170
1259
|
}
|
|
1260
|
+
const queryCall = resolveQueryCall(result, captureSlot);
|
|
1171
1261
|
if (skipFirst) {
|
|
1172
1262
|
skipFirst = false;
|
|
1173
1263
|
if (result instanceof Promise) result.catch(() => {});
|
|
1174
1264
|
return;
|
|
1175
1265
|
}
|
|
1266
|
+
if (queryCall) {
|
|
1267
|
+
runBrandedQuery(queryCall, currentAc.signal, env);
|
|
1268
|
+
return;
|
|
1269
|
+
}
|
|
1176
1270
|
if (!(result instanceof Promise)) {
|
|
1177
1271
|
const prevSub = setActiveSub(void 0);
|
|
1178
1272
|
env({
|
|
@@ -1193,6 +1287,16 @@ function createDerivedProxy({ entries, state, input, derivedSnapshot }) {
|
|
|
1193
1287
|
setActiveSub(prevSub);
|
|
1194
1288
|
result.then((value) => {
|
|
1195
1289
|
if (currentAc.signal.aborted) return;
|
|
1290
|
+
if (isQueryCall(value)) {
|
|
1291
|
+
runBrandedQuery(value, currentAc.signal, env);
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
const latePending = globalThis[ILHA_QUERY_PENDING];
|
|
1295
|
+
globalThis[ILHA_QUERY_PENDING] = void 0;
|
|
1296
|
+
if (latePending) {
|
|
1297
|
+
runBrandedQuery(latePending, currentAc.signal, env);
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1196
1300
|
env({
|
|
1197
1301
|
loading: false,
|
|
1198
1302
|
value,
|