anear-js-api 0.6.2 → 0.6.4

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.
@@ -346,18 +346,12 @@ class DisplayEventProcessor {
346
346
  const participantStruct = this.participantsIndex.get(participantId)
347
347
  const appCtx = templateRenderContext.app || {}
348
348
 
349
- // Determine which participant (if any) is currently timed according to
350
- // the AppM context (e.g., Tic-Tac-Toe currentPlayerToken).
351
- const currentToken = appCtx.currentPlayerToken
352
- const currentPlayerId = currentToken && appCtx.playerIds ? appCtx.playerIds[currentToken] : null
353
- const isCurrentPlayer = currentPlayerId && participantId === currentPlayerId
354
-
355
349
  if (timeoutFn) {
356
- // Only the current player should receive a real timeout value that
357
- // starts an APM timer. For other participants we keep `timeout` null
358
- // so their APMs never start per-participant timers.
359
- if (isCurrentPlayer) {
360
- timeout = timeoutFn(appCtx, participantStruct)
350
+ // For participant displays, any non-null / positive timeout value should
351
+ // start a real per-participant timer on that participant's APM.
352
+ const msecs = timeoutFn(appCtx, participantStruct)
353
+ if (typeof msecs === 'number' && msecs > 0) {
354
+ timeout = msecs
361
355
  }
362
356
  }
363
357
  const privateRenderContext = {
@@ -366,41 +360,36 @@ class DisplayEventProcessor {
366
360
  }
367
361
 
368
362
  // Build visual timeout (meta.timeout) used by the countdown bar.
369
- // For the current player, derive from their own APM state.
370
- // For opponents / other participants, mirror the current player's timer
371
- // so everyone sees the same countdown without starting extra APM timers.
363
+ // For participant displays we support two patterns:
364
+ // 1) Global participants timeout (allParticipants flow) mirror the
365
+ // shared participantsActionTimeout so EVERYONE sees the same bar,
366
+ // regardless of whether their own APM is timed.
367
+ // 2) Per-participant timeout (eachParticipant flow) – no global timeout;
368
+ // only the timed participant gets a bar, derived from their own APM.
372
369
  let visualTimeout = null
373
370
  try {
374
- // Helper to compute remaining from a given participant machine
375
- const computeRemainingFromMachine = (pm) => {
376
- const state = pm?.state
377
- const ctx = state?.context
378
- if (!ctx || !ctx.actionTimeoutStart || ctx.actionTimeoutMsecs == null) return null
371
+ // First, prefer a shared participants timeout if one exists (e.g., move timer).
372
+ const pat = this.precomputedParticipantsTimeout || this.participantsActionTimeout
373
+ if (pat && typeof pat.msecs === 'number') {
379
374
  const now = Date.now()
380
- const elapsed = now - ctx.actionTimeoutStart
381
- const base = ctx.actionTimeoutMsecs
382
- const remaining = base - elapsed
383
- return remaining > 0 ? remaining : 0
384
- }
385
-
386
- if (isCurrentPlayer) {
387
- if (timeout !== null) {
388
- const remainingMsecs = computeRemainingFromMachine(participantMachine) ?? timeout
389
- visualTimeout = { msecs: timeout, remainingMsecs }
375
+ const start = pat.startedAt || Date.now()
376
+ const remainingMsecs = Math.max(0, pat.msecs - (now - start))
377
+ visualTimeout = { msecs: pat.msecs, remainingMsecs }
378
+ } else if (timeout !== null) {
379
+ // Fallback: no global timeout; if this participant has a real timer,
380
+ // show a bar based on their own APM state.
381
+ const state = participantMachine?.state
382
+ const ctx = state?.context
383
+ let remainingMsecs = null
384
+ if (ctx && ctx.actionTimeoutStart && ctx.actionTimeoutMsecs != null) {
385
+ const now = Date.now()
386
+ const elapsed = now - ctx.actionTimeoutStart
387
+ const remaining = ctx.actionTimeoutMsecs - elapsed
388
+ remainingMsecs = remaining > 0 ? remaining : 0
390
389
  }
391
- } else if (currentPlayerId) {
392
- // Mirror the current player's timer for non-current participants.
393
- const currentPm = this.participantMachines[currentPlayerId]
394
- const remainingMsecs = computeRemainingFromMachine(currentPm)
395
-
396
- if (remainingMsecs != null) {
397
- // Prefer the app-configured move timeout if available; otherwise
398
- // fall back to the remaining time as the total for visuals.
399
- const moveTimeout =
400
- (appCtx.C && typeof appCtx.C.MOVE_TIMEOUT_MSECS === 'number' && appCtx.C.MOVE_TIMEOUT_MSECS > 0)
401
- ? appCtx.C.MOVE_TIMEOUT_MSECS
402
- : remainingMsecs
403
- visualTimeout = { msecs: moveTimeout, remainingMsecs }
390
+ visualTimeout = {
391
+ msecs: timeout,
392
+ remainingMsecs: remainingMsecs ?? timeout
404
393
  }
405
394
  }
406
395
  } catch (_e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anear-js-api",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "description": "Javascript Developer API for Anear Apps",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {