@pie-element/shared-player-events 0.1.0 → 0.1.1-next.1
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/README.md +98 -0
- package/dist/index.d.ts +13 -3
- package/dist/index.js +84 -25
- package/dist/session-notifier.d.ts +46 -0
- package/package.json +11 -8
- package/dist/index.d.ts.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# @pie-element/shared-player-events
|
|
2
|
+
|
|
3
|
+
Canonical runtime event contract for PIE element/player communication.
|
|
4
|
+
|
|
5
|
+
## Delivery events
|
|
6
|
+
|
|
7
|
+
- `session-changed`
|
|
8
|
+
- `detail.complete: boolean`
|
|
9
|
+
- `detail.component: string`
|
|
10
|
+
- Session data should be read from the element instance `.session`.
|
|
11
|
+
Host players may enrich forwarded events with `detail.session`.
|
|
12
|
+
- `model-set`
|
|
13
|
+
- `detail.complete: boolean`
|
|
14
|
+
- `detail.component: string`
|
|
15
|
+
- `detail.hasModel: boolean`
|
|
16
|
+
- Used for model lifecycle readiness metadata, not model mutation payloads.
|
|
17
|
+
|
|
18
|
+
## Deferred notification
|
|
19
|
+
|
|
20
|
+
An element coalesces repeated session writes so a host backend is not driven at
|
|
21
|
+
input rate. A raw `debounce` drops the pending notification when the element is
|
|
22
|
+
torn down inside its own window, which loses the learner's last response with no
|
|
23
|
+
event a host could have detected.
|
|
24
|
+
|
|
25
|
+
`createSessionNotifier` registers each deferred notification against its host, so
|
|
26
|
+
one call commits everything the element owns whatever its internal structure:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import {
|
|
30
|
+
SessionChangedEvent,
|
|
31
|
+
createSessionNotifier,
|
|
32
|
+
flushSessionNotifiers,
|
|
33
|
+
} from '@pie-element/shared-player-events';
|
|
34
|
+
|
|
35
|
+
class MyElement extends HTMLElement {
|
|
36
|
+
constructor() {
|
|
37
|
+
super();
|
|
38
|
+
this._notifier = createSessionNotifier(
|
|
39
|
+
this,
|
|
40
|
+
() => this.dispatchEvent(new SessionChangedEvent(this.tagName.toLowerCase(), isComplete(this._session))),
|
|
41
|
+
{ delayMs: 200 },
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
valueChange(value) {
|
|
46
|
+
this._session.value = value; // synchronous
|
|
47
|
+
this._notifier.notify(); // deferred
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
disconnectedCallback() {
|
|
51
|
+
flushSessionNotifiers(this);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
The contract this supports: **session state is written synchronously on commit,
|
|
57
|
+
and only the dispatch is deferred.** Deferring the session write instead leaves
|
|
58
|
+
`.session` stale until the timer fires, so no other layer can read the committed
|
|
59
|
+
response.
|
|
60
|
+
|
|
61
|
+
`delayMs` accepts a function, for an element whose delay depends on its model.
|
|
62
|
+
`maxWaitMs` bounds how long repeated calls may postpone a dispatch. `flush()` is
|
|
63
|
+
a no-op when nothing is pending, so a teardown adds no event in the normal path.
|
|
64
|
+
A dispatch that throws during a flush is contained, since a commit runs
|
|
65
|
+
mid-unmount, and reported through `console.warn` so a lost response is not
|
|
66
|
+
silent.
|
|
67
|
+
|
|
68
|
+
The helper also installs `commitPendingSession()` on the host. A player calls it
|
|
69
|
+
on each mounted element before discarding it, so the element dispatches its own
|
|
70
|
+
event — with its own `complete` semantics — while still attached and therefore
|
|
71
|
+
still able to reach a `document`-level listener. An element's own
|
|
72
|
+
`disconnectedCallback` runs after removal, where the event never gets that far.
|
|
73
|
+
When the call dispatches nothing, the player synthesizes the event from
|
|
74
|
+
`element.session`.
|
|
75
|
+
|
|
76
|
+
The installed method is an own property of the host. A `commitPendingSession`
|
|
77
|
+
the element class declares still runs: the installed method calls it, then
|
|
78
|
+
flushes the notifiers, so a player's call never skips a pending dispatch.
|
|
79
|
+
|
|
80
|
+
### Elements that do not defer
|
|
81
|
+
|
|
82
|
+
The `elements-svelte` packages (`mc-populated-blank`, `simple-cloze`,
|
|
83
|
+
`venn-classification`) dispatch `session-changed` synchronously on every change,
|
|
84
|
+
so nothing is ever pending and they install no `commitPendingSession()`. A
|
|
85
|
+
player's commit sweep synthesizes their event from `element.session` whenever
|
|
86
|
+
the host has not heard the response it holds.
|
|
87
|
+
|
|
88
|
+
What those elements owe a player instead is that `element.session` carries the
|
|
89
|
+
response by the time the event is dispatched, written into the object the player
|
|
90
|
+
handed the element rather than a replacement reference — `writeSessionInPlace` in
|
|
91
|
+
`@pie-lib/delivery-events-svelte`.
|
|
92
|
+
|
|
93
|
+
## Notes
|
|
94
|
+
|
|
95
|
+
- Events bubble and are composed by default.
|
|
96
|
+
- The legacy `pie.*` events (`pie.model_set`, `pie.session_changed`,
|
|
97
|
+
`pie.model_updated`, declared in `@pie-element/shared-types`) are
|
|
98
|
+
compatibility-only and not the primary runtime interop contract.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* PIE Player Events
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Runtime contract for communication between delivery/author elements and host players.
|
|
5
|
+
*
|
|
6
|
+
* Delivery contract:
|
|
7
|
+
* - `session-changed` carries metadata (`complete`, `component`).
|
|
8
|
+
* - Session state is sourced from the element instance `.session`; host players may enrich
|
|
9
|
+
* forwarded events with `detail.session` snapshots for convenience.
|
|
10
|
+
* - `model-set` communicates model lifecycle readiness metadata only.
|
|
11
|
+
*
|
|
12
|
+
* This package is the canonical source for runtime event names used by element delivery/player
|
|
13
|
+
* interop (`session-changed`, `model-set`), internalized from
|
|
14
|
+
* @pie-framework/pie-player-events for ESM compatibility.
|
|
6
15
|
*/
|
|
7
16
|
export type ModelSetDetail = {
|
|
8
17
|
complete: boolean;
|
|
@@ -26,4 +35,5 @@ export declare class SessionChangedEvent extends CustomEvent<SessionChangedDetai
|
|
|
26
35
|
static TYPE: string;
|
|
27
36
|
constructor(component: string, complete: boolean);
|
|
28
37
|
}
|
|
29
|
-
|
|
38
|
+
export { SESSION_COMMIT_METHOD, createSessionNotifier, flushSessionNotifiers, } from './session-notifier.js';
|
|
39
|
+
export type { SessionNotifier, SessionNotifierOptions } from './session-notifier.js';
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
//#region src/session-notifier.ts
|
|
2
|
+
var e = "commitPendingSession", t = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function n(e, n) {
|
|
4
|
+
let r = t.get(e);
|
|
5
|
+
r || (r = /* @__PURE__ */ new Set(), t.set(e, r)), r.add(n);
|
|
6
|
+
}
|
|
7
|
+
function r(t) {
|
|
8
|
+
return Object.getOwnPropertyDescriptor(t, e) !== void 0;
|
|
9
|
+
}
|
|
10
|
+
function i(t) {
|
|
11
|
+
if (r(t)) return;
|
|
12
|
+
let n = t[e];
|
|
13
|
+
Object.defineProperty(t, e, {
|
|
14
|
+
value: () => {
|
|
15
|
+
if (typeof n == "function") try {
|
|
16
|
+
n.call(t);
|
|
17
|
+
} catch {}
|
|
18
|
+
o(t);
|
|
19
|
+
},
|
|
20
|
+
configurable: !0,
|
|
21
|
+
enumerable: !1,
|
|
22
|
+
writable: !0
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function a(e, t, r = {}) {
|
|
26
|
+
let a = r.delayMs, o = typeof a == "function" ? a : () => a ?? 0, s = r.maxWaitMs, c = null, l = null, u = !1, d = () => {
|
|
27
|
+
c !== null && (clearTimeout(c), c = null), l !== null && (clearTimeout(l), l = null);
|
|
28
|
+
}, f = () => {
|
|
29
|
+
d(), u && (u = !1, t());
|
|
30
|
+
}, p = {
|
|
31
|
+
notify() {
|
|
32
|
+
u = !0, c !== null && clearTimeout(c), c = setTimeout(f, Math.max(0, o())), s !== void 0 && l === null && (l = setTimeout(f, Math.max(0, s)));
|
|
33
|
+
},
|
|
34
|
+
flush() {
|
|
35
|
+
if (!u) {
|
|
36
|
+
d();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
f();
|
|
41
|
+
} catch (e) {
|
|
42
|
+
console.warn("[session-notifier] a committed session-changed dispatch threw", e);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
return n(e, p), i(e), p;
|
|
47
|
+
}
|
|
48
|
+
function o(e) {
|
|
49
|
+
let n = t.get(e);
|
|
50
|
+
if (n) for (let e of [...n]) e.flush();
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/index.ts
|
|
54
|
+
var s = class e extends CustomEvent {
|
|
55
|
+
static {
|
|
56
|
+
this.TYPE = "model-set";
|
|
57
|
+
}
|
|
58
|
+
constructor(t, n, r) {
|
|
59
|
+
super(e.TYPE, {
|
|
60
|
+
bubbles: !0,
|
|
61
|
+
composed: !0,
|
|
62
|
+
detail: {
|
|
63
|
+
complete: n,
|
|
64
|
+
component: t,
|
|
65
|
+
hasModel: r
|
|
66
|
+
}
|
|
67
|
+
}), this.component = t, this.complete = n;
|
|
68
|
+
}
|
|
69
|
+
}, c = class e extends CustomEvent {
|
|
70
|
+
static {
|
|
71
|
+
this.TYPE = "session-changed";
|
|
72
|
+
}
|
|
73
|
+
constructor(t, n) {
|
|
74
|
+
super(e.TYPE, {
|
|
75
|
+
bubbles: !0,
|
|
76
|
+
composed: !0,
|
|
77
|
+
detail: {
|
|
78
|
+
complete: n,
|
|
79
|
+
component: t
|
|
80
|
+
}
|
|
81
|
+
}), this.component = t, this.complete = n;
|
|
82
|
+
}
|
|
26
83
|
};
|
|
84
|
+
//#endregion
|
|
85
|
+
export { s as ModelSetEvent, e as SESSION_COMMIT_METHOD, c as SessionChangedEvent, a as createSessionNotifier, o as flushSessionNotifiers };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deferred `session-changed` notification with a guaranteed commit.
|
|
3
|
+
*
|
|
4
|
+
* Delivery elements coalesce repeated session writes so a host backend is not
|
|
5
|
+
* driven at input rate. A raw `debounce` drops the pending notification when the
|
|
6
|
+
* element is torn down inside its own window, which loses the learner's last
|
|
7
|
+
* response with no event the host could have detected. A notifier created here
|
|
8
|
+
* is registered against its host element, so one `flushSessionNotifiers(host)`
|
|
9
|
+
* call in `disconnectedCallback` commits every pending notification the element
|
|
10
|
+
* owns, whatever its internal structure.
|
|
11
|
+
*
|
|
12
|
+
* The element contract this supports: session state is written synchronously on
|
|
13
|
+
* commit, and only the `session-changed` dispatch is deferred. A host or player
|
|
14
|
+
* reading `element.session` then always sees the committed response.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Method installed on a host that owns at least one notifier. A player calls it
|
|
18
|
+
* on each mounted element before discarding it, so the element dispatches its
|
|
19
|
+
* own event — with its own `complete` semantics — while still attached and
|
|
20
|
+
* therefore still able to reach a `document`-level listener.
|
|
21
|
+
*/
|
|
22
|
+
export declare const SESSION_COMMIT_METHOD = "commitPendingSession";
|
|
23
|
+
export interface SessionNotifier {
|
|
24
|
+
/** Schedule a dispatch, coalescing repeated calls inside the delay window. */
|
|
25
|
+
notify(): void;
|
|
26
|
+
/** Dispatch immediately if one is pending; no-op otherwise. */
|
|
27
|
+
flush(): void;
|
|
28
|
+
}
|
|
29
|
+
export interface SessionNotifierOptions {
|
|
30
|
+
/**
|
|
31
|
+
* Trailing-edge delay in milliseconds. A function is evaluated on every
|
|
32
|
+
* `notify()`, for elements whose delay depends on their model. Default 0,
|
|
33
|
+
* which defers to the next macrotask.
|
|
34
|
+
*/
|
|
35
|
+
delayMs?: number | (() => number);
|
|
36
|
+
/**
|
|
37
|
+
* Upper bound on how long repeated `notify()` calls may postpone a dispatch.
|
|
38
|
+
*/
|
|
39
|
+
maxWaitMs?: number;
|
|
40
|
+
}
|
|
41
|
+
export declare function createSessionNotifier(host: object, dispatch: () => void, options?: SessionNotifierOptions): SessionNotifier;
|
|
42
|
+
/**
|
|
43
|
+
* Dispatch every pending notification registered against `host`. Call this from
|
|
44
|
+
* `disconnectedCallback` before unmounting the element's render tree.
|
|
45
|
+
*/
|
|
46
|
+
export declare function flushSessionNotifiers(host: object): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-element/shared-player-events",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-next.1",
|
|
4
4
|
"description": "PIE Player Events (ESM) - internalized from @pie-framework/pie-player-events",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -21,14 +21,17 @@
|
|
|
21
21
|
"clean": "rm -rf dist"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"@types/bun": "latest",
|
|
26
|
-
"typescript": "^5.9.3",
|
|
27
|
-
"vite": "^7.2.2",
|
|
28
|
-
"vitest": "^4.0.17"
|
|
29
|
-
},
|
|
30
24
|
"publishConfig": {
|
|
31
25
|
"access": "public"
|
|
32
26
|
},
|
|
33
|
-
"license": "MIT"
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/pie-framework/pie-elements-ng.git",
|
|
31
|
+
"directory": "packages/shared/player-events"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/pie-framework/pie-elements-ng#readme",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/pie-framework/pie-elements-ng/issues"
|
|
36
|
+
}
|
|
34
37
|
}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,GAAG,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,qBAAa,aAAc,SAAQ,WAAW,CAAC,cAAc,CAAC;IAI1D,QAAQ,CAAC,SAAS,EAAE,MAAM;IAC1B,QAAQ,CAAC,QAAQ,EAAE,OAAO;IAJ5B,MAAM,CAAC,IAAI,SAAe;gBAGf,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,OAAO,EAC1B,QAAQ,EAAE,OAAO;CAQpB;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC;AAE7C,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,GAAG,CAAC;CAChB,CAAC;AAEF,qBAAa,mBAAoB,SAAQ,WAAW,CAAC,oBAAoB,CAAC;IAG5D,QAAQ,CAAC,SAAS,EAAE,MAAM;IAAE,QAAQ,CAAC,QAAQ,EAAE,OAAO;IAFlE,MAAM,CAAC,IAAI,SAAqB;gBAEX,SAAS,EAAE,MAAM,EAAW,QAAQ,EAAE,OAAO;CAOnE"}
|