@playcademy/sdk 0.9.0 → 0.9.1-beta.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 CHANGED
@@ -1953,17 +1953,21 @@ function createTimebackActivityTracker(client) {
1953
1953
  }
1954
1954
  }
1955
1955
  return {
1956
+ currentRunId() {
1957
+ return currentActivity?.runId;
1958
+ },
1956
1959
  startActivity(metadata, options) {
1957
1960
  if (options?.runId !== undefined && !isValidUUID(options.runId)) {
1958
1961
  throw new Error(`startActivity: \`runId\` must be a UUID (received \`${JSON.stringify(options.runId)}\`). Use crypto.randomUUID() or persist a previously-generated UUID.`);
1959
1962
  }
1960
1963
  cleanupListeners();
1961
1964
  const now = Date.now();
1965
+ const runId = options?.runId ?? crypto.randomUUID();
1962
1966
  const heartbeatIntervalMs = normalizeDelayMs(options?.heartbeatIntervalMs, DEFAULT_HEARTBEAT_INTERVAL_MS, false);
1963
1967
  const pausedHeartbeatTimeoutMs = normalizeDelayMs(options?.pausedHeartbeatTimeoutMs ?? options?.hiddenTimeoutMs, DEFAULT_PAUSED_HEARTBEAT_TIMEOUT_MS, false);
1964
1968
  const inactivityTimeoutMs = normalizeDelayMs(options?.inactivityTimeoutMs, DEFAULT_INACTIVITY_TIMEOUT_MS, false);
1965
1969
  currentActivity = {
1966
- runId: options?.runId ?? crypto.randomUUID(),
1970
+ runId,
1967
1971
  resumeId: crypto.randomUUID(),
1968
1972
  startTime: now,
1969
1973
  metadata,
@@ -2006,6 +2010,7 @@ function createTimebackActivityTracker(client) {
2006
2010
  messaging.listen("PLAYCADEMY_PAUSE" /* PAUSE */, boundShellPauseHandler);
2007
2011
  messaging.listen("PLAYCADEMY_RESUME" /* RESUME */, boundShellResumeHandler);
2008
2012
  syncInactivityTracking();
2013
+ return { runId };
2009
2014
  },
2010
2015
  pauseActivity() {
2011
2016
  if (!currentActivity) {
@@ -2095,6 +2100,15 @@ function createTimebackUserStore(client) {
2095
2100
  enrollments: response.enrollments,
2096
2101
  organizations: response.organizations
2097
2102
  };
2103
+ },
2104
+ setEnrollments(enrollments) {
2105
+ const base = override ?? client["initPayload"]?.timeback;
2106
+ override = base ? { ...base, enrollments } : {
2107
+ id: undefined,
2108
+ role: undefined,
2109
+ enrollments,
2110
+ organizations: []
2111
+ };
2098
2112
  }
2099
2113
  };
2100
2114
  }
@@ -2110,20 +2124,50 @@ function createTimebackEngine(client) {
2110
2124
  ttl: 5000,
2111
2125
  keyPrefix: "game.timeback.xp"
2112
2126
  });
2127
+ const enrollmentsCache = createTTLCache({
2128
+ ttl: 5 * 60 * 1000,
2129
+ keyPrefix: "game.timeback.enrollments"
2130
+ });
2113
2131
  async function applyPromotion(promotion) {
2114
2132
  if (promotion.status !== "promoted" && promotion.status !== "already-promoted") {
2115
2133
  return;
2116
2134
  }
2117
2135
  userCache.clear("current");
2136
+ enrollmentsCache.clear("current");
2118
2137
  try {
2119
2138
  await userStore.refresh();
2120
2139
  } catch {}
2121
2140
  }
2122
2141
  const activityTracker = createTimebackActivityTracker(client);
2142
+ async function refreshUserContext() {
2143
+ const context = await userStore.refresh();
2144
+ enrollmentsCache.clear("current");
2145
+ return context;
2146
+ }
2147
+ async function refreshEnrollmentSlice(options) {
2148
+ const enrollments = await enrollmentsCache.get("current", async () => {
2149
+ const response = await client["request"]("/timeback/user/enrollments", "GET");
2150
+ userStore.setEnrollments(response);
2151
+ userCache.clear("current");
2152
+ return response;
2153
+ }, options);
2154
+ userStore.setEnrollments(enrollments);
2155
+ return userStore.snapshot();
2156
+ }
2123
2157
  return {
2124
2158
  user: {
2125
2159
  snapshot: () => userStore.snapshot(),
2126
- fetch: (options) => userCache.get("current", () => userStore.refresh(), options),
2160
+ fetch: (options) => userCache.get("current", refreshUserContext, options),
2161
+ refresh: (options) => {
2162
+ if (!options?.only.includes("enrollments")) {
2163
+ throw new Error("At least one TimeBack user refresh field must be selected");
2164
+ }
2165
+ return refreshEnrollmentSlice({
2166
+ force: options.force,
2167
+ skipCache: options.skipCache,
2168
+ ttl: options.ttl
2169
+ });
2170
+ },
2127
2171
  xp: {
2128
2172
  fetch: (options) => {
2129
2173
  const cacheKey = [
@@ -2150,6 +2194,7 @@ function createTimebackEngine(client) {
2150
2194
  }
2151
2195
  },
2152
2196
  activity: {
2197
+ currentRunId: activityTracker.currentRunId,
2153
2198
  start: activityTracker.startActivity,
2154
2199
  pause: activityTracker.pauseActivity,
2155
2200
  resume: activityTracker.resumeActivity,
@@ -2184,6 +2229,7 @@ function createTimebackNamespace(client) {
2184
2229
  return engine.user.snapshot()?.organizations ?? [];
2185
2230
  },
2186
2231
  fetch: (options) => engine.user.fetch(options),
2232
+ refresh: (options) => engine.user.refresh(options),
2187
2233
  xp: {
2188
2234
  fetch: async (options = {}) => {
2189
2235
  const hasGrade = options.grade !== undefined;
@@ -2209,9 +2255,13 @@ function createTimebackNamespace(client) {
2209
2255
  }
2210
2256
  };
2211
2257
  },
2258
+ get currentRunId() {
2259
+ assertPlatformMode(client, "timeback.currentRunId");
2260
+ return engine.activity.currentRunId();
2261
+ },
2212
2262
  startActivity: (metadata, options) => {
2213
2263
  assertPlatformMode(client, "timeback.startActivity()");
2214
- engine.activity.start(metadata, options);
2264
+ return engine.activity.start(metadata, options);
2215
2265
  },
2216
2266
  pauseActivity: () => {
2217
2267
  assertPlatformMode(client, "timeback.pauseActivity()");