blubblub-the-machine 0.1.0 → 0.1.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 CHANGED
@@ -6,8 +6,8 @@ up to three extra questions; the app shows them in its own screens and hands the
6
6
 
7
7
  - **Onboarding never waits.** `start` resolves within 1.5 s, and any error, timeout or refusal is
8
8
  "no questions". Nothing in the package throws.
9
- - **Answers are never lost to a bad connection.** They are queued in storage and retried on every
10
- `flush` for 7 days.
9
+ - **Answers are never lost to a bad connection.** They, and the reports of which questions were shown,
10
+ are queued in storage and retried on every `flush` for 7 days.
11
11
  - **The app key identifies the app; it is not a secret.** It ships inside the app.
12
12
 
13
13
  ## Use (Capacitor)
@@ -33,7 +33,9 @@ const { sessionId, questions } = await machine.start({
33
33
  ownQuestions: ["babyBirthDate", "olderSiblings"], // the app's own questions, as it names their answers
34
34
  });
35
35
 
36
- // Show each question in the app's screens; then, without waiting:
36
+ // Show each question in the app's screens. As each one appears, without waiting:
37
+ if (sessionId) void machine.shown(sessionId, [questions[0].id]);
38
+ // Then the answers, also without waiting:
37
39
  if (sessionId) void machine.answer(sessionId, [{ questionId: questions[0].id, value: "a" }, { questionId: questions[1].id, skipped: true }]);
38
40
  ```
39
41
 
@@ -41,6 +43,8 @@ if (sessionId) void machine.answer(sessionId, [{ questionId: questions[0].id, va
41
43
  tracking, and `email` only when the app already has it.
42
44
  - A question's `type` is `single` or `multi` (answer with option `value`s), `number`, `date`
43
45
  (`YYYY-MM-DD`) or `text`. `text` and option labels are already in the requested locale.
46
+ - Call `shown` when a question is actually on screen, not when it arrives: The Machine counts how many
47
+ of the questions it gives are seen, answered and skipped (from 0.1.1).
44
48
  - `onError` (optional) is called with every failure, for the app's own logging.
45
49
 
46
50
  The API contract is `docs/standards/SDK_API.md` in The Machine's repository.
package/dist/index.d.ts CHANGED
@@ -3,11 +3,12 @@
3
3
  * answers back (contract: docs/standards/SDK_API.md in the-machine2).
4
4
  *
5
5
  * Two promises hold for the app: `start` never throws and never takes longer than `timeoutMs`
6
- * (any error, timeout or non-200 answer is "no questions"), and `answer` never throws either: answers
7
- * are queued in `storage` and sent in the background, retried on the next `flush` while their
8
- * session is open (7 days). An app calls `flush` when it starts and when it returns to the foreground.
6
+ * (any error, timeout or non-200 answer is "no questions"), and `answer` and `shown` never throw
7
+ * either: they are queued in `storage` and sent in the background, retried on the next `flush` while
8
+ * their session is open (7 days). An app calls `shown` as each question appears on screen, and
9
+ * `flush` when it starts and when it returns to the foreground.
9
10
  */
10
- export declare const SDK_VERSION = "0.1.0";
11
+ export declare const SDK_VERSION = "0.1.1";
11
12
  export declare const DEFAULT_BASE_URL = "https://the-machine-cy38u.ondigitalocean.app";
12
13
  export declare const START_TIMEOUT_MS = 1500;
13
14
  /** How long the server accepts answers for a session; older queued answers are dropped. */
@@ -79,6 +80,8 @@ export interface OnboardingOptions {
79
80
  export interface Onboarding {
80
81
  start(input: StartInput): Promise<OnboardingSession>;
81
82
  answer(sessionId: string, answers: Answer[]): Promise<void>;
83
+ /** Report questions the app has put on screen, by id; call it as each one appears. */
84
+ shown(sessionId: string, questionIds: string[]): Promise<void>;
82
85
  flush(): Promise<void>;
83
86
  }
84
87
  /** Empty values and an all-zero ad id (tracking not allowed yet) are left out, as the server would. */
package/dist/index.js CHANGED
@@ -3,11 +3,12 @@
3
3
  * answers back (contract: docs/standards/SDK_API.md in the-machine2).
4
4
  *
5
5
  * Two promises hold for the app: `start` never throws and never takes longer than `timeoutMs`
6
- * (any error, timeout or non-200 answer is "no questions"), and `answer` never throws either: answers
7
- * are queued in `storage` and sent in the background, retried on the next `flush` while their
8
- * session is open (7 days). An app calls `flush` when it starts and when it returns to the foreground.
6
+ * (any error, timeout or non-200 answer is "no questions"), and `answer` and `shown` never throw
7
+ * either: they are queued in `storage` and sent in the background, retried on the next `flush` while
8
+ * their session is open (7 days). An app calls `shown` as each question appears on screen, and
9
+ * `flush` when it starts and when it returns to the foreground.
9
10
  */
10
- export const SDK_VERSION = "0.1.0";
11
+ export const SDK_VERSION = "0.1.1";
11
12
  export const DEFAULT_BASE_URL = "https://the-machine-cy38u.ondigitalocean.app";
12
13
  export const START_TIMEOUT_MS = 1500;
13
14
  /** How long the server accepts answers for a session; older queued answers are dropped. */
@@ -135,23 +136,33 @@ export function createOnboarding(options) {
135
136
  return NO_QUESTIONS;
136
137
  }
137
138
  }
138
- async function answer(sessionId, answers) {
139
- if (!sessionId || !answers.length)
140
- return;
139
+ async function enqueue(item) {
141
140
  await locked(async () => {
142
141
  const q = await readQueue();
143
- q.push({ sessionId, answers, queuedAt: now() });
142
+ q.push({ ...item, queuedAt: now() });
144
143
  await writeQueue(q);
145
144
  });
146
145
  await flush();
147
146
  }
147
+ async function answer(sessionId, answers) {
148
+ if (!sessionId || !answers.length)
149
+ return;
150
+ await enqueue({ sessionId, answers });
151
+ }
152
+ async function shown(sessionId, questionIds) {
153
+ const ids = [...new Set(questionIds.filter((id) => typeof id === "string" && id))];
154
+ if (!sessionId || !ids.length)
155
+ return;
156
+ await enqueue({ sessionId, answers: [], shown: ids });
157
+ }
148
158
  async function send() {
149
159
  const keep = [];
150
160
  for (const item of await readQueue()) {
151
161
  if (now() - item.queuedAt > SESSION_MS)
152
162
  continue; // the session is closed on the server
153
163
  try {
154
- const res = await post("/api/sdk/v1/onboarding/answers", { sessionId: item.sessionId, answers: item.answers }, ANSWERS_TIMEOUT_MS);
164
+ const body = { sessionId: item.sessionId, answers: item.answers ?? [], ...(item.shown?.length ? { shown: item.shown } : {}) };
165
+ const res = await post("/api/sdk/v1/onboarding/answers", body, ANSWERS_TIMEOUT_MS);
155
166
  if (res.status === 200)
156
167
  continue;
157
168
  report("answers", new Error(`HTTP ${res.status}`));
@@ -185,5 +196,5 @@ export function createOnboarding(options) {
185
196
  });
186
197
  return flushing;
187
198
  }
188
- return { start, answer, flush };
199
+ return { start, answer, shown, flush };
189
200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blubblub-the-machine",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "The Machine's client for Blub Blub apps: extra onboarding questions now, cross-promo next.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -15,6 +15,13 @@
15
15
  "import": "./dist/capacitor.js"
16
16
  }
17
17
  },
18
+ "typesVersions": {
19
+ "*": {
20
+ "capacitor": [
21
+ "./dist/capacitor.d.ts"
22
+ ]
23
+ }
24
+ },
18
25
  "files": [
19
26
  "dist",
20
27
  "README.md"