@legendapp/state 3.0.0-alpha.33 → 3.0.0-alpha.35
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/config.d.mts +1 -1
- package/config.d.ts +1 -1
- package/config.js +18 -12
- package/config.mjs +18 -12
- package/index.d.mts +10 -3
- package/index.d.ts +10 -3
- package/index.js +79 -42
- package/index.mjs +76 -42
- package/{observableInterfaces-CZR3_8mM.d.mts → observableInterfaces-CgBddSU6.d.mts} +6 -2
- package/{observableInterfaces-CZR3_8mM.d.ts → observableInterfaces-CgBddSU6.d.ts} +6 -2
- package/package.json +1 -1
- package/persist-plugins/async-storage.d.mts +2 -2
- package/persist-plugins/async-storage.d.ts +2 -2
- package/persist-plugins/async-storage.js +3 -7
- package/persist-plugins/async-storage.mjs +3 -7
- package/persist-plugins/indexeddb.d.mts +2 -2
- package/persist-plugins/indexeddb.d.ts +2 -2
- package/persist-plugins/indexeddb.js +3 -7
- package/persist-plugins/indexeddb.mjs +3 -7
- package/persist-plugins/mmkv.d.mts +2 -2
- package/persist-plugins/mmkv.d.ts +2 -2
- package/persist-plugins/mmkv.js +3 -7
- package/persist-plugins/mmkv.mjs +3 -7
- package/sync-plugins/crud.js +6 -7
- package/sync-plugins/crud.mjs +7 -8
- package/sync-plugins/keel.d.mts +28 -28
- package/sync-plugins/keel.d.ts +28 -28
- package/sync-plugins/keel.js +106 -82
- package/sync-plugins/keel.mjs +108 -82
- package/sync-plugins/supabase.d.mts +2 -2
- package/sync-plugins/supabase.d.ts +2 -2
- package/sync.d.mts +11 -13
- package/sync.d.ts +11 -13
- package/sync.js +149 -130
- package/sync.mjs +150 -131
package/sync-plugins/keel.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SyncedGetSetSubscribeBaseParams,
|
|
2
|
-
import { SyncedCrudPropsBase,
|
|
1
|
+
import { SyncedGetSetSubscribeBaseParams, OnErrorRetryParams, SyncedSetParams } from '@legendapp/state/sync';
|
|
2
|
+
import { SyncedCrudPropsBase, SyncedCrudReturnType, CrudAsOption, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
|
|
3
3
|
|
|
4
4
|
interface KeelObjectBase {
|
|
5
5
|
id: string;
|
|
@@ -39,30 +39,17 @@ interface KeelListParams<Where = {}> {
|
|
|
39
39
|
before?: string;
|
|
40
40
|
}
|
|
41
41
|
interface KeelRealtimePlugin {
|
|
42
|
-
subscribe: (realtimeKey: string, params: SyncedGetSetSubscribeBaseParams) => void;
|
|
43
|
-
|
|
42
|
+
subscribe: (realtimeKey: string, params: SyncedGetSetSubscribeBaseParams) => () => void;
|
|
43
|
+
setSaved: (realtimeKey: string) => void;
|
|
44
44
|
}
|
|
45
|
-
interface
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
queries: Record<string, (i: any) => Promise<any>>;
|
|
53
|
-
};
|
|
45
|
+
interface KeelClient {
|
|
46
|
+
auth: {
|
|
47
|
+
refresh: () => Promise<APIResult<boolean>>;
|
|
48
|
+
isAuthenticated: () => Promise<APIResult<boolean>>;
|
|
49
|
+
};
|
|
50
|
+
api: {
|
|
51
|
+
queries: Record<string, (i: any) => Promise<any>>;
|
|
54
52
|
};
|
|
55
|
-
realtimePlugin?: KeelRealtimePlugin;
|
|
56
|
-
as?: Exclude<CrudAsOption, 'value'>;
|
|
57
|
-
enabled?: boolean;
|
|
58
|
-
onError?: (params: {
|
|
59
|
-
type: 'create' | 'update' | 'delete';
|
|
60
|
-
params: SyncedSetParams<any>;
|
|
61
|
-
input: any;
|
|
62
|
-
action: string;
|
|
63
|
-
error: APIResult<any>['error'];
|
|
64
|
-
}) => void;
|
|
65
|
-
refreshAuth?: () => void | Promise<void>;
|
|
66
53
|
}
|
|
67
54
|
interface SyncedKeelPropsManyBase<TRemote extends {
|
|
68
55
|
id: string;
|
|
@@ -101,9 +88,17 @@ interface SyncedKeelPropsSingle<TRemote extends {
|
|
|
101
88
|
list?: never;
|
|
102
89
|
as?: never;
|
|
103
90
|
}
|
|
91
|
+
interface ErrorDetails {
|
|
92
|
+
type: 'create' | 'update' | 'delete';
|
|
93
|
+
params: SyncedSetParams<any>;
|
|
94
|
+
input: any;
|
|
95
|
+
action: string;
|
|
96
|
+
error: APIResult<any>['error'];
|
|
97
|
+
}
|
|
104
98
|
interface SyncedKeelPropsBase<TRemote extends {
|
|
105
99
|
id: string;
|
|
106
|
-
}, TLocal = TRemote> extends Omit<SyncedCrudPropsBase<TRemote, TLocal>, 'create' | 'update' | 'delete' | 'updatePartial' | 'fieldUpdatedAt' | 'fieldCreatedAt'> {
|
|
100
|
+
}, TLocal = TRemote> extends Omit<SyncedCrudPropsBase<TRemote, TLocal>, 'create' | 'update' | 'delete' | 'updatePartial' | 'fieldUpdatedAt' | 'fieldCreatedAt' | 'onError'> {
|
|
101
|
+
client?: KeelClient;
|
|
107
102
|
create?: (i: NoInfer<Partial<TRemote>>) => Promise<APIResult<NoInfer<TRemote>>>;
|
|
108
103
|
update?: (params: {
|
|
109
104
|
where: any;
|
|
@@ -112,9 +107,14 @@ interface SyncedKeelPropsBase<TRemote extends {
|
|
|
112
107
|
delete?: (params: {
|
|
113
108
|
id: string;
|
|
114
109
|
}) => Promise<APIResult<string>>;
|
|
110
|
+
realtime?: {
|
|
111
|
+
path?: (action: string, inputs: any) => string | Promise<string>;
|
|
112
|
+
plugin?: KeelRealtimePlugin;
|
|
113
|
+
};
|
|
114
|
+
refreshAuth?: () => void | Promise<void>;
|
|
115
|
+
requireAuth?: boolean;
|
|
116
|
+
onError?: (error: Error, retryParams: OnErrorRetryParams, details: ErrorDetails) => void;
|
|
115
117
|
}
|
|
116
|
-
declare function getSyncedKeelConfiguration(): SyncedKeelConfiguration;
|
|
117
|
-
declare function configureSyncedKeel(config: SyncedKeelConfiguration): void;
|
|
118
118
|
declare function syncedKeel<TRemote extends {
|
|
119
119
|
id: string;
|
|
120
120
|
}, TLocal = TRemote>(props: SyncedKeelPropsBase<TRemote, TLocal> & SyncedKeelPropsSingle<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
|
|
@@ -122,4 +122,4 @@ declare function syncedKeel<TRemote extends {
|
|
|
122
122
|
id: string;
|
|
123
123
|
}, TLocal = TRemote, TOption extends CrudAsOption = 'object', Where extends Record<string, any> = {}>(props: SyncedKeelPropsBase<TRemote, TLocal> & SyncedKeelPropsMany<TRemote, TLocal, TOption, Where>): SyncedCrudReturnType<TLocal, Exclude<TOption, 'value'>>;
|
|
124
124
|
|
|
125
|
-
export { type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type
|
|
125
|
+
export { type KeelClient, type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type SyncedKeelPropsBase, generateKeelId, syncedKeel };
|
package/sync-plugins/keel.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SyncedGetSetSubscribeBaseParams,
|
|
2
|
-
import { SyncedCrudPropsBase,
|
|
1
|
+
import { SyncedGetSetSubscribeBaseParams, OnErrorRetryParams, SyncedSetParams } from '@legendapp/state/sync';
|
|
2
|
+
import { SyncedCrudPropsBase, SyncedCrudReturnType, CrudAsOption, SyncedCrudPropsSingle, CrudResult, SyncedCrudPropsMany } from '@legendapp/state/sync-plugins/crud';
|
|
3
3
|
|
|
4
4
|
interface KeelObjectBase {
|
|
5
5
|
id: string;
|
|
@@ -39,30 +39,17 @@ interface KeelListParams<Where = {}> {
|
|
|
39
39
|
before?: string;
|
|
40
40
|
}
|
|
41
41
|
interface KeelRealtimePlugin {
|
|
42
|
-
subscribe: (realtimeKey: string, params: SyncedGetSetSubscribeBaseParams) => void;
|
|
43
|
-
|
|
42
|
+
subscribe: (realtimeKey: string, params: SyncedGetSetSubscribeBaseParams) => () => void;
|
|
43
|
+
setSaved: (realtimeKey: string) => void;
|
|
44
44
|
}
|
|
45
|
-
interface
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
queries: Record<string, (i: any) => Promise<any>>;
|
|
53
|
-
};
|
|
45
|
+
interface KeelClient {
|
|
46
|
+
auth: {
|
|
47
|
+
refresh: () => Promise<APIResult<boolean>>;
|
|
48
|
+
isAuthenticated: () => Promise<APIResult<boolean>>;
|
|
49
|
+
};
|
|
50
|
+
api: {
|
|
51
|
+
queries: Record<string, (i: any) => Promise<any>>;
|
|
54
52
|
};
|
|
55
|
-
realtimePlugin?: KeelRealtimePlugin;
|
|
56
|
-
as?: Exclude<CrudAsOption, 'value'>;
|
|
57
|
-
enabled?: boolean;
|
|
58
|
-
onError?: (params: {
|
|
59
|
-
type: 'create' | 'update' | 'delete';
|
|
60
|
-
params: SyncedSetParams<any>;
|
|
61
|
-
input: any;
|
|
62
|
-
action: string;
|
|
63
|
-
error: APIResult<any>['error'];
|
|
64
|
-
}) => void;
|
|
65
|
-
refreshAuth?: () => void | Promise<void>;
|
|
66
53
|
}
|
|
67
54
|
interface SyncedKeelPropsManyBase<TRemote extends {
|
|
68
55
|
id: string;
|
|
@@ -101,9 +88,17 @@ interface SyncedKeelPropsSingle<TRemote extends {
|
|
|
101
88
|
list?: never;
|
|
102
89
|
as?: never;
|
|
103
90
|
}
|
|
91
|
+
interface ErrorDetails {
|
|
92
|
+
type: 'create' | 'update' | 'delete';
|
|
93
|
+
params: SyncedSetParams<any>;
|
|
94
|
+
input: any;
|
|
95
|
+
action: string;
|
|
96
|
+
error: APIResult<any>['error'];
|
|
97
|
+
}
|
|
104
98
|
interface SyncedKeelPropsBase<TRemote extends {
|
|
105
99
|
id: string;
|
|
106
|
-
}, TLocal = TRemote> extends Omit<SyncedCrudPropsBase<TRemote, TLocal>, 'create' | 'update' | 'delete' | 'updatePartial' | 'fieldUpdatedAt' | 'fieldCreatedAt'> {
|
|
100
|
+
}, TLocal = TRemote> extends Omit<SyncedCrudPropsBase<TRemote, TLocal>, 'create' | 'update' | 'delete' | 'updatePartial' | 'fieldUpdatedAt' | 'fieldCreatedAt' | 'onError'> {
|
|
101
|
+
client?: KeelClient;
|
|
107
102
|
create?: (i: NoInfer<Partial<TRemote>>) => Promise<APIResult<NoInfer<TRemote>>>;
|
|
108
103
|
update?: (params: {
|
|
109
104
|
where: any;
|
|
@@ -112,9 +107,14 @@ interface SyncedKeelPropsBase<TRemote extends {
|
|
|
112
107
|
delete?: (params: {
|
|
113
108
|
id: string;
|
|
114
109
|
}) => Promise<APIResult<string>>;
|
|
110
|
+
realtime?: {
|
|
111
|
+
path?: (action: string, inputs: any) => string | Promise<string>;
|
|
112
|
+
plugin?: KeelRealtimePlugin;
|
|
113
|
+
};
|
|
114
|
+
refreshAuth?: () => void | Promise<void>;
|
|
115
|
+
requireAuth?: boolean;
|
|
116
|
+
onError?: (error: Error, retryParams: OnErrorRetryParams, details: ErrorDetails) => void;
|
|
115
117
|
}
|
|
116
|
-
declare function getSyncedKeelConfiguration(): SyncedKeelConfiguration;
|
|
117
|
-
declare function configureSyncedKeel(config: SyncedKeelConfiguration): void;
|
|
118
118
|
declare function syncedKeel<TRemote extends {
|
|
119
119
|
id: string;
|
|
120
120
|
}, TLocal = TRemote>(props: SyncedKeelPropsBase<TRemote, TLocal> & SyncedKeelPropsSingle<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
|
|
@@ -122,4 +122,4 @@ declare function syncedKeel<TRemote extends {
|
|
|
122
122
|
id: string;
|
|
123
123
|
}, TLocal = TRemote, TOption extends CrudAsOption = 'object', Where extends Record<string, any> = {}>(props: SyncedKeelPropsBase<TRemote, TLocal> & SyncedKeelPropsMany<TRemote, TLocal, TOption, Where>): SyncedCrudReturnType<TLocal, Exclude<TOption, 'value'>>;
|
|
124
124
|
|
|
125
|
-
export { type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type
|
|
125
|
+
export { type KeelClient, type KeelGetParams, type KeelKey, KeelKeys, type KeelListParams, type KeelObjectBase, type KeelRealtimePlugin, type OmitKeelBuiltins, type SyncedKeelPropsBase, generateKeelId, syncedKeel };
|
package/sync-plugins/keel.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var state = require('@legendapp/state');
|
|
4
|
-
var sync = require('@legendapp/state/sync');
|
|
5
4
|
var crud = require('@legendapp/state/sync-plugins/crud');
|
|
6
5
|
var ksuid = require('ksuid');
|
|
7
6
|
|
|
@@ -14,24 +13,50 @@ var KeelKeys = ["createdAt", "updatedAt"];
|
|
|
14
13
|
function generateKeelId() {
|
|
15
14
|
return ksuid__default.default.randomSync().string;
|
|
16
15
|
}
|
|
17
|
-
var keelConfig = {};
|
|
18
16
|
var modifiedClients = /* @__PURE__ */ new WeakSet();
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
|
|
17
|
+
var isAuthed$ = state.observable(false);
|
|
18
|
+
var isAuthing$ = state.observable(false);
|
|
19
|
+
async function ensureAuthToken(props, force) {
|
|
20
|
+
if (!force && isAuthed$.get()) {
|
|
21
|
+
return true;
|
|
24
22
|
}
|
|
25
|
-
|
|
23
|
+
const { client, refreshAuth } = props;
|
|
24
|
+
let isAuthed = await client.auth.isAuthenticated().then(({ data }) => data);
|
|
26
25
|
if (!isAuthed) {
|
|
27
|
-
|
|
26
|
+
if (!force && isAuthing$.get()) {
|
|
27
|
+
return state.when(
|
|
28
|
+
() => !isAuthing$.get(),
|
|
29
|
+
() => isAuthed$.get()
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
isAuthing$.set(true);
|
|
33
|
+
if (refreshAuth) {
|
|
34
|
+
await refreshAuth();
|
|
35
|
+
}
|
|
36
|
+
isAuthed = await client.auth.isAuthenticated().then(({ data }) => data);
|
|
37
|
+
if (!isAuthed) {
|
|
38
|
+
isAuthed = await client.auth.refresh().then(({ data }) => data);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (isAuthed) {
|
|
42
|
+
state.batch(() => {
|
|
43
|
+
isAuthed$.set(true);
|
|
44
|
+
isAuthing$.set(false);
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
setTimeout(() => ensureAuthToken(
|
|
48
|
+
props,
|
|
49
|
+
/*force*/
|
|
50
|
+
true
|
|
51
|
+
), 1e3);
|
|
28
52
|
}
|
|
29
53
|
return isAuthed;
|
|
30
54
|
}
|
|
31
|
-
async function handleApiError(error, retry) {
|
|
55
|
+
async function handleApiError(props, error, retry) {
|
|
32
56
|
if (error.type === "unauthorized" || error.type === "forbidden") {
|
|
33
57
|
console.warn("Keel token expired, refreshing...");
|
|
34
|
-
|
|
58
|
+
isAuthed$.set(false);
|
|
59
|
+
await ensureAuthToken(props);
|
|
35
60
|
}
|
|
36
61
|
}
|
|
37
62
|
function convertObjectToCreate(item) {
|
|
@@ -51,49 +76,30 @@ function convertObjectToCreate(item) {
|
|
|
51
76
|
});
|
|
52
77
|
return cloned;
|
|
53
78
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const queries = client.api.queries;
|
|
68
|
-
Object.keys(queries).forEach((key) => {
|
|
69
|
-
if (key.startsWith("list")) {
|
|
70
|
-
const oldFn = queries[key];
|
|
71
|
-
queries[key] = (i) => {
|
|
72
|
-
const realtimeKey = [key, ...Object.values(i.where || {})].filter((value) => value && typeof value !== "object").join("/");
|
|
73
|
-
const subscribe = (params) => {
|
|
74
|
-
if (realtimeKey) {
|
|
75
|
-
return realtimePlugin.subscribe(realtimeKey, params);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
return oldFn(i).then((ret) => {
|
|
79
|
-
if (subscribe) {
|
|
80
|
-
ret.subscribe = subscribe;
|
|
81
|
-
ret.subscribeKey = realtimeKey;
|
|
82
|
-
}
|
|
83
|
-
return ret;
|
|
84
|
-
});
|
|
79
|
+
var realtimeState = { current: {} };
|
|
80
|
+
function setupRealtime(props) {
|
|
81
|
+
const { client } = props;
|
|
82
|
+
if (client && !modifiedClients.has(client)) {
|
|
83
|
+
modifiedClients.add(client);
|
|
84
|
+
const queries = client.api.queries;
|
|
85
|
+
Object.keys(queries).forEach((key) => {
|
|
86
|
+
if (key.startsWith("list")) {
|
|
87
|
+
const origFn = queries[key];
|
|
88
|
+
queries[key] = (i) => {
|
|
89
|
+
realtimeState.current = {
|
|
90
|
+
lastAction: key,
|
|
91
|
+
lastParams: i
|
|
85
92
|
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
93
|
+
return origFn(i);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
});
|
|
89
97
|
}
|
|
90
98
|
}
|
|
91
99
|
var NumPerPage = 200;
|
|
92
|
-
async function getAllPages(listFn, params) {
|
|
100
|
+
async function getAllPages(props, listFn, params) {
|
|
93
101
|
const allData = [];
|
|
94
102
|
let pageInfo = void 0;
|
|
95
|
-
let subscribe_;
|
|
96
|
-
let subscribeKey_;
|
|
97
103
|
const { first: firstParam } = params;
|
|
98
104
|
do {
|
|
99
105
|
const first = firstParam ? Math.min(firstParam - allData.length, NumPerPage) : NumPerPage;
|
|
@@ -105,13 +111,9 @@ async function getAllPages(listFn, params) {
|
|
|
105
111
|
pageInfo = void 0;
|
|
106
112
|
const ret = await listFn(paramsWithCursor);
|
|
107
113
|
if (ret) {
|
|
108
|
-
const { data, error
|
|
109
|
-
if (subscribe) {
|
|
110
|
-
subscribe_ = subscribe;
|
|
111
|
-
subscribeKey_ = subscribeKey;
|
|
112
|
-
}
|
|
114
|
+
const { data, error } = ret;
|
|
113
115
|
if (error) {
|
|
114
|
-
await handleApiError(error);
|
|
116
|
+
await handleApiError(props, error);
|
|
115
117
|
throw new Error(error.message);
|
|
116
118
|
} else if (data) {
|
|
117
119
|
pageInfo = data.pageInfo;
|
|
@@ -119,23 +121,25 @@ async function getAllPages(listFn, params) {
|
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
} while (pageInfo == null ? void 0 : pageInfo.hasNextPage);
|
|
122
|
-
return
|
|
124
|
+
return allData;
|
|
123
125
|
}
|
|
124
126
|
function syncedKeel(props) {
|
|
125
|
-
const { realtimePlugin } = keelConfig;
|
|
126
|
-
props = { ...keelConfig, ...props };
|
|
127
127
|
const {
|
|
128
128
|
get: getParam,
|
|
129
129
|
list: listParam,
|
|
130
130
|
create: createParam,
|
|
131
131
|
update: updateParam,
|
|
132
132
|
delete: deleteParam,
|
|
133
|
+
subscribe: subscribeParam,
|
|
133
134
|
first,
|
|
134
135
|
where: whereParam,
|
|
135
136
|
waitFor,
|
|
136
137
|
waitForSet,
|
|
137
138
|
fieldDeleted,
|
|
139
|
+
realtime,
|
|
138
140
|
mode,
|
|
141
|
+
onError,
|
|
142
|
+
requireAuth = true,
|
|
139
143
|
...rest
|
|
140
144
|
} = props;
|
|
141
145
|
const { changesSince } = props;
|
|
@@ -144,13 +148,15 @@ function syncedKeel(props) {
|
|
|
144
148
|
const subscribeFnKey$ = state.observable("");
|
|
145
149
|
const fieldCreatedAt = "createdAt";
|
|
146
150
|
const fieldUpdatedAt = "updatedAt";
|
|
147
|
-
const setupSubscribe =
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (
|
|
151
|
-
|
|
151
|
+
const setupSubscribe = realtime ? async (getParams) => {
|
|
152
|
+
const { lastAction, lastParams } = realtimeState.current;
|
|
153
|
+
const { path, plugin } = realtime;
|
|
154
|
+
if (lastAction && path && plugin) {
|
|
155
|
+
const key = await path(lastAction, lastParams);
|
|
156
|
+
subscribeFn = () => realtime.plugin.subscribe(key, getParams);
|
|
157
|
+
subscribeFnKey$.set(key);
|
|
152
158
|
}
|
|
153
|
-
};
|
|
159
|
+
} : void 0;
|
|
154
160
|
const list = listParam ? async (listParams) => {
|
|
155
161
|
const { lastSync } = listParams;
|
|
156
162
|
const queryBySync = !!lastSync && changesSince === "last-sync";
|
|
@@ -159,18 +165,21 @@ function syncedKeel(props) {
|
|
|
159
165
|
state.isFunction(whereParam) ? whereParam() : whereParam
|
|
160
166
|
);
|
|
161
167
|
const params = { where, first };
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
168
|
+
realtimeState.current = {};
|
|
169
|
+
const promise = getAllPages(props, listParam, params);
|
|
170
|
+
if (realtime) {
|
|
171
|
+
setupSubscribe(listParams);
|
|
165
172
|
}
|
|
166
|
-
return
|
|
173
|
+
return promise;
|
|
167
174
|
} : void 0;
|
|
168
175
|
const get = getParam ? async (getParams) => {
|
|
169
176
|
const { refresh } = getParams;
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
177
|
+
realtimeState.current = {};
|
|
178
|
+
const promise = getParam({ refresh });
|
|
179
|
+
if (realtime) {
|
|
180
|
+
setupSubscribe(getParams);
|
|
173
181
|
}
|
|
182
|
+
const { data, error } = await promise;
|
|
174
183
|
if (error) {
|
|
175
184
|
throw new Error(error.message);
|
|
176
185
|
} else {
|
|
@@ -179,17 +188,16 @@ function syncedKeel(props) {
|
|
|
179
188
|
} : void 0;
|
|
180
189
|
const onSaved = ({ saved }) => {
|
|
181
190
|
if (saved) {
|
|
182
|
-
|
|
183
|
-
if (updatedAt && realtimePlugin) {
|
|
191
|
+
if (realtime == null ? void 0 : realtime.plugin) {
|
|
184
192
|
const subscribeFnKey = subscribeFnKey$.get();
|
|
185
193
|
if (subscribeFnKey) {
|
|
186
|
-
|
|
194
|
+
realtime == null ? void 0 : realtime.plugin.setSaved(subscribeFnKey);
|
|
187
195
|
}
|
|
188
196
|
}
|
|
189
197
|
}
|
|
190
198
|
};
|
|
191
199
|
const handleSetError = async (error, params, input, fn, from) => {
|
|
192
|
-
var _a, _b
|
|
200
|
+
var _a, _b;
|
|
193
201
|
const { retryNum, update: update2 } = params;
|
|
194
202
|
if (from === "create" && ((_a = error.message) == null ? void 0 : _a.includes("for the unique")) && ((_b = error.message) == null ? void 0 : _b.includes("must be unique"))) {
|
|
195
203
|
if (__DEV__) {
|
|
@@ -208,13 +216,19 @@ function syncedKeel(props) {
|
|
|
208
216
|
params.cancelRetry = true;
|
|
209
217
|
}
|
|
210
218
|
} else if (error.type === "bad_request") {
|
|
211
|
-
|
|
219
|
+
onError == null ? void 0 : onError(new Error(error.message), params, {
|
|
220
|
+
error,
|
|
221
|
+
params,
|
|
222
|
+
input,
|
|
223
|
+
type: from,
|
|
224
|
+
action: fn.name || fn.toString()
|
|
225
|
+
});
|
|
212
226
|
if (retryNum > 4) {
|
|
213
227
|
params.cancelRetry = true;
|
|
214
228
|
}
|
|
215
229
|
throw new Error(error.message, { cause: { input } });
|
|
216
230
|
} else {
|
|
217
|
-
await handleApiError(error);
|
|
231
|
+
await handleApiError(props, error);
|
|
218
232
|
throw new Error(error.message, { cause: { input } });
|
|
219
233
|
}
|
|
220
234
|
};
|
|
@@ -244,7 +258,10 @@ function syncedKeel(props) {
|
|
|
244
258
|
}
|
|
245
259
|
return data;
|
|
246
260
|
} : void 0;
|
|
247
|
-
|
|
261
|
+
if (realtime) {
|
|
262
|
+
setupRealtime(props);
|
|
263
|
+
}
|
|
264
|
+
const subscribe = subscribeParam || (realtime ? (params) => {
|
|
248
265
|
let unsubscribe = void 0;
|
|
249
266
|
state.when(subscribeFnKey$, () => {
|
|
250
267
|
unsubscribe = subscribeFn(params);
|
|
@@ -252,7 +269,7 @@ function syncedKeel(props) {
|
|
|
252
269
|
return () => {
|
|
253
270
|
unsubscribe == null ? void 0 : unsubscribe();
|
|
254
271
|
};
|
|
255
|
-
};
|
|
272
|
+
} : void 0);
|
|
256
273
|
return crud.syncedCrud({
|
|
257
274
|
...rest,
|
|
258
275
|
as: asType,
|
|
@@ -261,8 +278,17 @@ function syncedKeel(props) {
|
|
|
261
278
|
create,
|
|
262
279
|
update,
|
|
263
280
|
delete: deleteFn,
|
|
264
|
-
waitFor: () =>
|
|
265
|
-
|
|
281
|
+
waitFor: () => {
|
|
282
|
+
ensureAuthToken(props);
|
|
283
|
+
return [requireAuth ? isAuthed$ : true, waitFor || true];
|
|
284
|
+
},
|
|
285
|
+
waitForSet: (params) => {
|
|
286
|
+
ensureAuthToken(props);
|
|
287
|
+
return [
|
|
288
|
+
requireAuth ? isAuthed$ : true,
|
|
289
|
+
() => waitForSet ? state.isFunction(waitForSet) ? waitForSet(params) : waitForSet : true
|
|
290
|
+
];
|
|
291
|
+
},
|
|
266
292
|
onSaved,
|
|
267
293
|
fieldCreatedAt,
|
|
268
294
|
fieldUpdatedAt,
|
|
@@ -277,7 +303,5 @@ function syncedKeel(props) {
|
|
|
277
303
|
}
|
|
278
304
|
|
|
279
305
|
exports.KeelKeys = KeelKeys;
|
|
280
|
-
exports.configureSyncedKeel = configureSyncedKeel;
|
|
281
306
|
exports.generateKeelId = generateKeelId;
|
|
282
|
-
exports.getSyncedKeelConfiguration = getSyncedKeelConfiguration;
|
|
283
307
|
exports.syncedKeel = syncedKeel;
|