@pie-element/shared-player-events 0.1.2-next.20261012213240 → 0.2.0

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.
@@ -0,0 +1,12 @@
1
+ /** How long, in milliseconds, the learner pauses before an editor commits, when the player sets none. */
2
+ export declare const DEFAULT_IDLE_SAVE_DELAY_MS = 1500;
3
+ /** A finite, non-negative delay, or the default for anything else; `0` commits on blur only. */
4
+ export declare function resolveIdleSaveDelay(idleSaveDelayMs: unknown): number;
5
+ /**
6
+ * Calls `onDelay` with the delay in effect for `host` whenever its player answers,
7
+ * including a change the host makes later. With no provider it is never called,
8
+ * and the caller keeps `DEFAULT_IDLE_SAVE_DELAY_MS`. Call it from
9
+ * `connectedCallback`, where the request can reach the player; returns the
10
+ * cleanup for `disconnectedCallback`.
11
+ */
12
+ export declare function subscribeIdleSaveDelay(host: Element, onDelay: (delayMs: number) => void): () => void;
package/dist/index.d.ts CHANGED
@@ -37,3 +37,4 @@ export declare class SessionChangedEvent extends CustomEvent<SessionChangedDetai
37
37
  }
38
38
  export { SESSION_COMMIT_METHOD, createSessionNotifier, flushSessionNotifiers, } from './session-notifier.js';
39
39
  export type { SessionNotifier, SessionNotifierOptions } from './session-notifier.js';
40
+ export { DEFAULT_IDLE_SAVE_DELAY_MS, resolveIdleSaveDelay, subscribeIdleSaveDelay, } from './idle-save-delay.js';
package/dist/index.js CHANGED
@@ -50,8 +50,141 @@ function o(e) {
50
50
  if (n) for (let e of [...n]) e.flush();
51
51
  }
52
52
  //#endregion
53
+ //#region ../../../node_modules/.bun/@pie-players+pie-context@1.0.0-beta.3/node_modules/@pie-players/pie-context/dist/events.js
54
+ function s(e, t, n) {
55
+ let r = t.ownerDocument?.defaultView?.Event ?? Event;
56
+ return Object.assign(new r(e, {
57
+ bubbles: !0,
58
+ composed: !0
59
+ }), n);
60
+ }
61
+ var c = /* @__PURE__ */ new WeakSet(), l = (e, t) => typeof t == "object" && !!t && e.has(t), u = class {
62
+ constructor(e, t, n, r) {
63
+ let i = s("context-request", t, {
64
+ context: e,
65
+ contextTarget: t,
66
+ callback: n,
67
+ subscribe: r ?? !1
68
+ });
69
+ return c.add(i), i;
70
+ }
71
+ static [Symbol.hasInstance](e) {
72
+ return l(c, e);
73
+ }
74
+ }, d = (e) => {
75
+ let t = [];
76
+ for (let n of e) n.requestorRef.deref() && n.callbackRef.deref() && t.push(n);
77
+ return t;
78
+ }, f = class {
79
+ host;
80
+ pendingByContext = /* @__PURE__ */ new Map();
81
+ attached = !1;
82
+ constructor(e) {
83
+ this.host = e;
84
+ }
85
+ attach() {
86
+ this.attached || (this.attached = !0, this.host.addEventListener("context-request", this.onContextRequest), this.host.addEventListener("context-provider", this.onContextProvider));
87
+ }
88
+ detach() {
89
+ this.attached && (this.attached = !1, this.host.removeEventListener("context-request", this.onContextRequest), this.host.removeEventListener("context-provider", this.onContextProvider), this.pendingByContext.clear());
90
+ }
91
+ onContextRequest = (e) => {
92
+ if (e.cancelBubble) return;
93
+ let t = e;
94
+ if (t.subscribe !== !0 || typeof t.callback != "function" || t.context === void 0) return;
95
+ let n = t.contextTarget ?? e.composedPath?.()[0];
96
+ if (!n) return;
97
+ let r = this.pendingByContext.get(t.context);
98
+ r || (r = {
99
+ callbacks: /* @__PURE__ */ new WeakMap(),
100
+ requests: []
101
+ }, this.pendingByContext.set(t.context, r));
102
+ let i = t.callback, a = r.callbacks.get(n);
103
+ a || (a = /* @__PURE__ */ new WeakSet(), r.callbacks.set(n, a)), !a.has(i) && (a.add(i), r.requests.push({
104
+ requestorRef: new WeakRef(n),
105
+ callbackRef: new WeakRef(i)
106
+ }), r.requests = d(r.requests));
107
+ };
108
+ onContextProvider = (e) => {
109
+ let t = e.context;
110
+ if (t === void 0) return;
111
+ let n = this.pendingByContext.get(t);
112
+ if (!(!n || n.requests.length === 0)) {
113
+ this.pendingByContext.delete(t);
114
+ for (let e of n.requests) {
115
+ let n = e.requestorRef.deref(), r = e.callbackRef.deref();
116
+ !n || !r || n.dispatchEvent(new u(t, n, r, !0));
117
+ }
118
+ }
119
+ };
120
+ }, p = Symbol.for("pie.context.documentRoot");
121
+ function m(e) {
122
+ if (!e) return;
123
+ let t = e;
124
+ if (t[p]) return;
125
+ let n = e.documentElement;
126
+ if (!n) return;
127
+ let r = new f(n);
128
+ r.attach(), t[p] = r;
129
+ }
130
+ //#endregion
131
+ //#region ../../../node_modules/.bun/@pie-players+pie-context@1.0.0-beta.3/node_modules/@pie-players/pie-context/dist/consumer.js
132
+ var h = class {
133
+ host;
134
+ context;
135
+ subscribe;
136
+ onValue;
137
+ isConnected = !1;
138
+ isDisconnected = !1;
139
+ unsubscribe;
140
+ currentValue;
141
+ constructor(e, t) {
142
+ this.host = e, this.context = t.context, this.subscribe = t.subscribe ?? !0, this.onValue = t.onValue;
143
+ }
144
+ connect() {
145
+ this.isConnected || (this.isConnected = !0, this.isDisconnected = !1, this.subscribe && m(this.host.ownerDocument), this.requestValue());
146
+ }
147
+ disconnect() {
148
+ this.isConnected && (this.isConnected = !1, this.isDisconnected = !0, this.unsubscribe?.(), this.unsubscribe = void 0);
149
+ }
150
+ get value() {
151
+ return this.currentValue;
152
+ }
153
+ handleValue = (e, t) => {
154
+ if (this.isDisconnected) {
155
+ t?.();
156
+ return;
157
+ }
158
+ t !== this.unsubscribe && (this.unsubscribe?.(), this.unsubscribe = t), !this.subscribe && this.unsubscribe && (this.unsubscribe(), this.unsubscribe = void 0), this.currentValue = e, this.onValue?.(e);
159
+ };
160
+ requestValue() {
161
+ this.host.dispatchEvent(new u(this.context, this.host, this.handleValue, this.subscribe));
162
+ }
163
+ }, g = (e, t, n) => {
164
+ let r = new h(e, {
165
+ context: t,
166
+ subscribe: !0,
167
+ onValue: n
168
+ });
169
+ r.connect();
170
+ let i = (e) => {
171
+ e.context === t && r.requestValue();
172
+ };
173
+ return e.addEventListener("context-provider", i), () => {
174
+ e.removeEventListener("context-provider", i), r.disconnect();
175
+ };
176
+ }, _ = ((e) => e)(Symbol.for("pie.sessionCommit")), v = 1500;
177
+ function y(e) {
178
+ return typeof e == "number" && Number.isFinite(e) && e >= 0 ? e : v;
179
+ }
180
+ function b(e, t) {
181
+ return g(e, _, (e) => {
182
+ t(y(e?.idleSaveDelayMs));
183
+ });
184
+ }
185
+ //#endregion
53
186
  //#region src/index.ts
54
- var s = class e extends CustomEvent {
187
+ var x = class e extends CustomEvent {
55
188
  static {
56
189
  this.TYPE = "model-set";
57
190
  }
@@ -66,7 +199,7 @@ var s = class e extends CustomEvent {
66
199
  }
67
200
  }), this.component = t, this.complete = n;
68
201
  }
69
- }, c = class e extends CustomEvent {
202
+ }, S = class e extends CustomEvent {
70
203
  static {
71
204
  this.TYPE = "session-changed";
72
205
  }
@@ -82,4 +215,4 @@ var s = class e extends CustomEvent {
82
215
  }
83
216
  };
84
217
  //#endregion
85
- export { s as ModelSetEvent, e as SESSION_COMMIT_METHOD, c as SessionChangedEvent, a as createSessionNotifier, o as flushSessionNotifiers };
218
+ export { v as DEFAULT_IDLE_SAVE_DELAY_MS, x as ModelSetEvent, e as SESSION_COMMIT_METHOD, S as SessionChangedEvent, a as createSessionNotifier, o as flushSessionNotifiers, y as resolveIdleSaveDelay, b as subscribeIdleSaveDelay };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/shared-player-events",
3
- "version": "0.1.2-next.20261012213240",
3
+ "version": "0.2.0",
4
4
  "description": "PIE Player Events (ESM) - internalized from @pie-framework/pie-player-events",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",