@axium/client 0.39.1 → 0.39.3

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/dist/cli/cache.js CHANGED
@@ -6,7 +6,7 @@ import { dirname, join, resolve } from 'node:path/posix';
6
6
  import * as z from 'zod';
7
7
  import { CacheData } from '../cache.js';
8
8
  import { fetchAPI } from '../requests.js';
9
- import { applyDiff } from '../sync.js';
9
+ import { applyDiff, onSync } from '../sync.js';
10
10
  import { apiUserCache, getCurrentSession } from '../user.js';
11
11
  export const dir = join(process.env.XDG_CACHE_HOME || join(homedir(), '.cache'), 'axium');
12
12
  mkdirSync(dir, { recursive: true });
@@ -84,21 +84,34 @@ export const meta = useAt({
84
84
  },
85
85
  isValid: meta => meta.fetched + _dayMs > Date.now(),
86
86
  });
87
+ function applySyncDiff(state, diff) {
88
+ applyDiff(state.objects, diff);
89
+ if (diff.index > state.index)
90
+ state.index = diff.index;
91
+ return state;
92
+ }
87
93
  export const sync = useAt({
88
94
  path: 'sync.json',
89
95
  schema: SyncState,
90
- async update(sync) {
91
- if (!sync)
96
+ async update(state) {
97
+ if (!state)
98
+ return await fetchAPI('GET', 'sync/init');
99
+ const diff = await fetchAPI('GET', 'sync', { since: state.index });
100
+ if (diff.index < state.index)
92
101
  return await fetchAPI('GET', 'sync/init');
93
- const diff = await fetchAPI('GET', 'sync', { since: sync.index });
94
- applyDiff(sync.objects, diff);
95
- return { objects: sync.objects, index: diff.index };
102
+ return applySyncDiff(state, diff);
96
103
  },
97
104
  async isValid({ index }) {
98
105
  const md = await fetchAPI('GET', 'sync/metadata');
99
- return md.index >= index;
106
+ return md.index == index;
100
107
  },
101
108
  });
109
+ onSync(diff => {
110
+ if (!sync.data)
111
+ return;
112
+ applySyncDiff(sync.data, diff);
113
+ sync.save();
114
+ });
102
115
  const persistedAPICaches = [];
103
116
  function persistAPI(cache, path) {
104
117
  path = resolve(dir, path);
package/dist/cli/index.js CHANGED
@@ -80,9 +80,13 @@ program
80
80
  .action(async (opt) => {
81
81
  for (const plugin of plugins.values())
82
82
  await plugin._client?.run?.();
83
+ if (!opt.socket || !config.token)
84
+ return;
83
85
  // Hold a socket connection to the server for the lifetime of the daemon.
84
- if (opt.socket && config.token)
85
- await connectSocket({ rejectUnauthorized: !opt.insecure });
86
+ const socket = await connectSocket({ rejectUnauthorized: !opt.insecure });
87
+ const catchUp = () => cache.sync.update().catch(e => io.error('Failed to update sync cache:', io.errorText(e)));
88
+ await catchUp();
89
+ socket.io.on('reconnect', catchUp);
86
90
  });
87
91
  const axcPlugin = createPluginCommand('client', program, {
88
92
  safe,
@@ -11,4 +11,5 @@ type ObjectValues = keyof $Objects extends never ? Record<string, {
11
11
  })[];
12
12
  };
13
13
  export declare function get<Type extends ObjectType>(type: Type): ObjectValues[Type];
14
- export declare function save<Type extends ObjectType>(type: Type, objects: ObjectValues[Type]): void;
14
+ /** Pull changes from the server into the local cache. */
15
+ export declare function pull(): Promise<void>;
package/dist/cli/sync.js CHANGED
@@ -1,22 +1,14 @@
1
1
  import { schemas } from '../sync.js';
2
2
  import { sync as syncCache } from './cache.js';
3
3
  export { schemas };
4
- let _byType;
5
- function byType() {
6
- _byType ||= Object.groupBy(syncCache.data.objects, o => o.$type);
7
- return _byType;
8
- }
9
4
  export function get(type) {
10
- const value = byType()[type] || [];
5
+ const value = syncCache.data.objects.filter(o => o.$type == type);
11
6
  const schema = schemas.get(type);
12
7
  if (!schema)
13
8
  return value;
14
9
  return value.map(obj => schema.parse(obj));
15
10
  }
16
- export function save(type, objects) {
17
- _byType ||= {};
18
- _byType[type] = objects;
19
- syncCache.data.objects = syncCache.data.objects.filter(o => o.$type !== type);
20
- for (const object of objects)
21
- syncCache.data.objects.push(Object.assign(object, { $type: type }));
11
+ /** Pull changes from the server into the local cache. */
12
+ export async function pull() {
13
+ await syncCache.update();
22
14
  }
@@ -21,7 +21,7 @@
21
21
  <UserPFP {user} --size="2.5em" />
22
22
  <div>
23
23
  <strong>{user.name}</strong>
24
- {#if user.email}<span class="subtle">{user.email}</span>{/if}
24
+ <span class="subtle">{user.username}</span>
25
25
  </div>
26
26
  </div>
27
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axium/client",
3
- "version": "0.39.1",
3
+ "version": "0.39.3",
4
4
  "author": "James Prevett <jp@jamespre.dev>",
5
5
  "funding": {
6
6
  "type": "individual",