monoidentity 0.11.0 → 0.11.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.
@@ -23,17 +23,14 @@ export const wrapWithReplay = (storage) => {
23
23
  const paths = Object.keys(modifications);
24
24
  const tasks = paths.map((path) => (async () => {
25
25
  const mod = modifications[path];
26
+ delete modifications[path];
26
27
  if (mod.type == "set" && mod.old == mod.new) {
27
- delete modifications[path];
28
28
  return;
29
29
  }
30
30
  await tx(path, mod);
31
31
  })()
32
32
  .catch((err) => {
33
33
  console.warn(`[monoidentity] transmitting "${path}" failed`, err);
34
- })
35
- .finally(() => {
36
- delete modifications[path];
37
34
  }));
38
35
  await Promise.all(tasks);
39
36
  }
@@ -16,18 +16,15 @@ export const createLocalStorage = () => createStore({
16
16
  return localStorage[prefixed(key)];
17
17
  },
18
18
  set(key, value) {
19
- if (typeof key == "string") {
20
- localStorage[prefixed(key)] = value;
21
- return true;
22
- }
23
- return false;
19
+ if (typeof key != "string")
20
+ return false;
21
+ localStorage[prefixed(key)] = value;
22
+ return true;
24
23
  },
25
24
  deleteProperty(key) {
26
- if (typeof key == "string") {
27
- delete localStorage[prefixed(key)];
28
- return true;
29
- }
30
- return false;
25
+ if (typeof key != "string")
26
+ return false;
27
+ return delete localStorage[prefixed(key)];
31
28
  },
32
29
  ownKeys() {
33
30
  const keys = [];
@@ -1,3 +1,3 @@
1
1
  import type { Dict } from "./createstore.js";
2
2
  import type { Bucket } from "../utils-bucket.js";
3
- export declare const wrapCloud: (storage: Dict, bucket: Bucket) => Dict;
3
+ export declare const wrapCloud: (storage: Dict, bucket: Bucket) => Promise<Dict>;
@@ -2,7 +2,7 @@ import { wrapWithReplay } from "./_replay.js";
2
2
  import { AwsClient } from "aws4fetch";
3
3
  import { CLOUD_CACHE_KEY, loadCloud } from "./_cloud.js";
4
4
  import { md5 } from "./_md5.js";
5
- export const wrapCloud = (storage, bucket) => {
5
+ export const wrapCloud = async (storage, bucket) => {
6
6
  const client = new AwsClient({
7
7
  accessKeyId: bucket.accessKeyId,
8
8
  secretAccessKey: bucket.secretAccessKey,
@@ -10,43 +10,41 @@ export const wrapCloud = (storage, bucket) => {
10
10
  const { proxy, setTransmit, flush, load } = wrapWithReplay(storage);
11
11
  const isFirstLoad = !localStorage[CLOUD_CACHE_KEY];
12
12
  if (isFirstLoad) {
13
- loadCloud(bucket.base, client).then((data) => {
14
- load(data);
15
- location.reload();
16
- });
13
+ const data = await loadCloud(bucket.base, client);
14
+ load(data);
17
15
  }
18
- else {
19
- setTransmit(async (path, mod) => {
20
- if (mod.type == "set") {
21
- const url = `${bucket.base}/${path}`;
22
- const headers = new Headers();
23
- if (mod.old) {
24
- // Update only if current matches our known prior content
25
- const etagFromOld = md5(mod.old);
26
- headers.set("If-Match", `"${etagFromOld}"`);
27
- }
28
- else {
29
- // Create only if it does not already exist
30
- headers.set("If-None-Match", "*");
31
- }
32
- const r = await client.fetch(url, {
33
- method: "PUT",
34
- headers,
35
- body: mod.new,
36
- });
37
- if (!r.ok) {
38
- throw new Error(`Cloud is ${r.status}ing`);
39
- }
16
+ setTransmit(async (path, mod) => {
17
+ if (mod.type == "set") {
18
+ const url = `${bucket.base}/${path}`;
19
+ const headers = new Headers();
20
+ if (mod.old) {
21
+ // Update only if current matches our known prior content
22
+ const etagFromOld = md5(mod.old);
23
+ headers.set("If-Match", `"${etagFromOld}"`);
40
24
  }
41
- else if (mod.type == "delete") {
42
- const url = `${bucket.base}/${path}`;
43
- const r = await client.fetch(url, {
44
- method: "DELETE",
45
- });
46
- if (!r.ok)
47
- throw new Error(`Cloud is ${r.status}ing`);
25
+ else {
26
+ // Create only if it does not already exist
27
+ headers.set("If-None-Match", "*");
48
28
  }
49
- });
29
+ const r = await client.fetch(url, {
30
+ method: "PUT",
31
+ headers,
32
+ body: mod.new,
33
+ });
34
+ if (!r.ok) {
35
+ throw new Error(`Cloud is ${r.status}ing`);
36
+ }
37
+ }
38
+ else if (mod.type == "delete") {
39
+ const url = `${bucket.base}/${path}`;
40
+ const r = await client.fetch(url, {
41
+ method: "DELETE",
42
+ });
43
+ if (!r.ok)
44
+ throw new Error(`Cloud is ${r.status}ing`);
45
+ }
46
+ });
47
+ if (!isFirstLoad) {
50
48
  flush().then(async () => {
51
49
  const data = await loadCloud(bucket.base, client);
52
50
  load(data);
@@ -27,12 +27,15 @@ export const trackReady = async (app, intents, requestBackup) => {
27
27
  redirectURI: location.origin,
28
28
  });
29
29
  location.href = target.toString();
30
- return;
30
+ await new Promise(() => {
31
+ /* never resolves */
32
+ });
33
+ throw new Error("unreachable");
31
34
  }
32
35
  let storage;
33
36
  if (setup.method == "cloud") {
34
37
  storage = createLocalStorage();
35
- storage = wrapCloud(storage, setup);
38
+ storage = await wrapCloud(storage, setup);
36
39
  }
37
40
  else if (setup.method == "localStorage") {
38
41
  storage = createLocalStorage();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monoidentity",
3
- "version": "0.11.0",
3
+ "version": "0.11.2",
4
4
  "repository": "KTibow/monoidentity",
5
5
  "author": {
6
6
  "name": "KTibow"