@pie-lib/delivery-events-svelte 0.1.1-next.0 → 0.2.0-next.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.
@@ -0,0 +1,47 @@
1
+ export interface DeliveryElementOptions<Model, Session> {
2
+ /**
3
+ * Whether `session` is a complete response to `model`. It is the `complete`
4
+ * flag of every `model-set` and `session-changed` the element dispatches, which
5
+ * is what a player gates the item's answered state on.
6
+ */
7
+ isComplete(model: Model | undefined, session: Session | undefined): boolean;
8
+ }
9
+ /**
10
+ * The delivery custom element a player mounts: it owns the session the player
11
+ * hands it and announces every change to it.
12
+ */
13
+ export interface DeliveryElement<Model = unknown, Session = unknown> extends HTMLElement {
14
+ model: Model | undefined;
15
+ /** The session object the player set, with every update written into it. */
16
+ session: Session | undefined;
17
+ /**
18
+ * The component's `onSessionChange` prop, which it calls with each update:
19
+ * writes the update and dispatches `session-changed`.
20
+ */
21
+ onSessionChange: (session: Session) => void;
22
+ /** `isComplete` for the current model and session. A subclass adds its own conditions here. */
23
+ isComplete(): boolean;
24
+ /**
25
+ * Writes `next` into the player's session and hands the component `next`,
26
+ * a fresh reference its `$derived` reads re-run on. Dispatches nothing.
27
+ */
28
+ writeSession(next: Session): void;
29
+ dispatchSessionChanged(): void;
30
+ }
31
+ export type DeliveryElementConstructor<Model = unknown, Session = unknown> = new () => DeliveryElement<Model, Session>;
32
+ /**
33
+ * The delivery custom element for `Component`, a Svelte component compiled
34
+ * with `customElement` that declares `model`, `session` and an
35
+ * `onSessionChange` callback prop.
36
+ *
37
+ * The session contract lives here, once, for every Svelte element: the update
38
+ * is written into the object the player handed the element (`writeSessionInPlace`),
39
+ * and `session-changed` and `model-set` are dispatched under the tag the player
40
+ * registered the element with, carrying `complete` from `options.isComplete`.
41
+ *
42
+ * `onSessionChange` is an ordinary prop of the component, which the element
43
+ * assigns in its constructor through the accessor Svelte generates for it; a
44
+ * component mounted with `mount()` takes it like any other prop. A subclass
45
+ * assigns its own callback props the same way.
46
+ */
47
+ export declare function defineDeliveryElement<Model = unknown, Session = unknown>(Component: object, options: DeliveryElementOptions<Model, Session>): DeliveryElementConstructor<Model, Session>;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * @pie-lib/delivery-events-svelte
3
3
  *
4
- * Shared host/session bridge utilities for Svelte delivery custom elements.
4
+ * The delivery custom element shell and session write-through for Svelte elements.
5
5
  */
6
- export * from './session-bridge.js';
6
+ export * from './delivery-element.js';
7
+ export * from './write-session-in-place.js';
7
8
  export { ModelSetEvent, SessionChangedEvent } from '@pie-element/shared-player-events';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
- import { ModelSetEvent as e, SessionChangedEvent as t } from "./index2.js";
2
- import { forwardSessionChange as n, resolveDeliveryHost as r, writeSessionInPlace as i } from "./index3.js";
3
- export { e as ModelSetEvent, t as SessionChangedEvent, n as forwardSessionChange, r as resolveDeliveryHost, i as writeSessionInPlace };
1
+ import { writeSessionInPlace as e } from "./index2.js";
2
+ import { defineDeliveryElement as t } from "./index3.js";
3
+ import { ModelSetEvent as n, SessionChangedEvent as r } from "@pie-element/shared-player-events";
4
+ export { n as ModelSetEvent, r as SessionChangedEvent, t as defineDeliveryElement, e as writeSessionInPlace };
package/dist/index2.js CHANGED
@@ -1,33 +1,17 @@
1
- //#region ../../shared/player-events/dist/index.js
2
- var e = class e extends CustomEvent {
3
- static {
4
- this.TYPE = "model-set";
1
+ //#region src/write-session-in-place.ts
2
+ function e(e) {
3
+ return !!e && typeof e == "object" && !Array.isArray(e);
4
+ }
5
+ function t(t, n) {
6
+ if (t === n) return t;
7
+ if (!e(t) || !e(n) || Object.isFrozen(t) || !Object.isExtensible(t)) return n;
8
+ try {
9
+ for (let e of Object.keys(t)) e in n || delete t[e];
10
+ Object.assign(t, n);
11
+ } catch {
12
+ return n;
5
13
  }
6
- constructor(t, n, r) {
7
- super(e.TYPE, {
8
- bubbles: !0,
9
- composed: !0,
10
- detail: {
11
- complete: n,
12
- component: t,
13
- hasModel: r
14
- }
15
- }), this.component = t, this.complete = n;
16
- }
17
- }, t = class e extends CustomEvent {
18
- static {
19
- this.TYPE = "session-changed";
20
- }
21
- constructor(t, n) {
22
- super(e.TYPE, {
23
- bubbles: !0,
24
- composed: !0,
25
- detail: {
26
- complete: n,
27
- component: t
28
- }
29
- }), this.component = t, this.complete = n;
30
- }
31
- };
14
+ return t;
15
+ }
32
16
  //#endregion
33
- export { e as ModelSetEvent, t as SessionChangedEvent };
17
+ export { t as writeSessionInPlace };
package/dist/index3.js CHANGED
@@ -1,38 +1,42 @@
1
- import { SessionChangedEvent as e } from "./index2.js";
2
- //#region src/session-bridge.ts
3
- var t = (e) => {
4
- let t = e;
5
- return t ? typeof t.onSessionChange == "function" || typeof t.onAudioStarted == "function" || typeof t.onAudioEnded == "function" : !1;
6
- };
7
- function n(e, n = {}) {
8
- let r = n.hostPredicate ?? t, i = e;
9
- for (; i;) {
10
- if (r(i)) return i;
11
- i = i.parentElement;
1
+ import { writeSessionInPlace as e } from "./index2.js";
2
+ import { ModelSetEvent as t, SessionChangedEvent as n } from "@pie-element/shared-player-events";
3
+ //#region src/delivery-element.ts
4
+ function r(r, i) {
5
+ let a = r.element;
6
+ if (!a) throw TypeError("defineDeliveryElement: the component was not compiled with `customElement`");
7
+ class o extends a {
8
+ #e;
9
+ #t;
10
+ constructor() {
11
+ super(), this.onSessionChange = (e) => {
12
+ this.writeSession(e), this.dispatchSessionChanged();
13
+ };
14
+ }
15
+ set model(e) {
16
+ this.#e = e, super.model = e, queueMicrotask(() => {
17
+ this.dispatchEvent(new t(this.tagName.toLowerCase(), this.isComplete(), this.#e !== void 0));
18
+ });
19
+ }
20
+ get model() {
21
+ return this.#e;
22
+ }
23
+ set session(e) {
24
+ this.#t = e, super.session = e, this.dispatchSessionChanged();
25
+ }
26
+ get session() {
27
+ return this.#t;
28
+ }
29
+ isComplete() {
30
+ return i.isComplete(this.#e, this.#t);
31
+ }
32
+ writeSession(t) {
33
+ this.#t = e(this.#t, t), super.session = t;
34
+ }
35
+ dispatchSessionChanged() {
36
+ this.dispatchEvent(new n(this.tagName.toLowerCase(), this.isComplete()));
37
+ }
12
38
  }
13
- if (typeof document < "u" && n.fallbackSelector) {
14
- let e = document.querySelector(n.fallbackSelector);
15
- if (r(e)) return e;
16
- }
17
- return null;
18
- }
19
- function r(e) {
20
- return !!e && typeof e == "object" && !Array.isArray(e);
21
- }
22
- function i(e, t) {
23
- if (e === t) return e;
24
- if (!r(e) || !r(t) || Object.isFrozen(e) || !Object.isExtensible(e)) return t;
25
- try {
26
- for (let n of Object.keys(e)) n in t || delete e[n];
27
- Object.assign(e, t);
28
- } catch {
29
- return t;
30
- }
31
- return e;
32
- }
33
- function a({ sourceEl: t, fallbackSelector: r, component: i, complete: a, session: o }) {
34
- let s = n(t, { fallbackSelector: r });
35
- return s ? typeof s.onSessionChange == "function" ? (s.onSessionChange(o), s) : (s.dispatchEvent(new e(i, a)), s) : null;
39
+ return o;
36
40
  }
37
41
  //#endregion
38
- export { a as forwardSessionChange, n as resolveDeliveryHost, i as writeSessionInPlace };
42
+ export { r as defineDeliveryElement };
@@ -1,19 +1,3 @@
1
- export type DeliveryHostElement = HTMLElement & {
2
- session?: unknown;
3
- onSessionChange?: (session: unknown) => void;
4
- onAudioStarted?: () => void;
5
- onAudioEnded?: () => void;
6
- };
7
- type HostPredicate = (node: unknown) => boolean;
8
- export interface ResolveDeliveryHostOptions {
9
- fallbackSelector?: string;
10
- hostPredicate?: HostPredicate;
11
- }
12
- /**
13
- * Walk up the DOM from a source element and find the nearest delivery host wrapper.
14
- * Falls back to selector lookup to support detached nested render roots.
15
- */
16
- export declare function resolveDeliveryHost(sourceEl?: HTMLElement | null, options?: ResolveDeliveryHostOptions): DeliveryHostElement | null;
17
1
  /**
18
2
  * Assign a delivery update into the session object the player owns, keeping the
19
3
  * reference.
@@ -37,16 +21,3 @@ export declare function resolveDeliveryHost(sourceEl?: HTMLElement | null, optio
37
21
  * learner's update with it before the element ever stored it.
38
22
  */
39
23
  export declare function writeSessionInPlace(target: unknown, next: unknown): unknown;
40
- export interface ForwardSessionChangeOptions {
41
- sourceEl?: HTMLElement | null;
42
- fallbackSelector?: string;
43
- component: string;
44
- complete: boolean;
45
- session: unknown;
46
- }
47
- /**
48
- * Forward a delivery session update using the host callback when available.
49
- * If no callback is exposed, dispatch the canonical session-changed metadata event.
50
- */
51
- export declare function forwardSessionChange({ sourceEl, fallbackSelector, component, complete, session, }: ForwardSessionChangeOptions): DeliveryHostElement | null;
52
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/delivery-events-svelte",
3
- "version": "0.1.1-next.0",
3
+ "version": "0.2.0-next.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/pie-framework/pie-elements-ng.git",