@legendapp/state 2.2.0-next.74 → 2.2.0-next.76
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/helpers/time.d.ts +2 -2
- package/index.d.ts +1 -1
- package/index.js +82 -31
- package/index.js.map +1 -1
- package/index.mjs +81 -32
- package/index.mjs.map +1 -1
- package/package.json +16 -1
- package/persist.js +122 -129
- package/persist.js.map +1 -1
- package/persist.mjs +122 -129
- package/persist.mjs.map +1 -1
- package/react.js +5 -5
- package/react.js.map +1 -1
- package/react.mjs +6 -6
- package/react.mjs.map +1 -1
- package/src/ObservableObject.ts +34 -15
- package/src/batching.ts +9 -3
- package/src/computed.ts +4 -2
- package/src/globals.ts +17 -7
- package/src/helpers.ts +3 -3
- package/src/history/undoRedo.ts +111 -0
- package/src/is.ts +7 -0
- package/src/observableInterfaces.ts +6 -5
- package/src/observableTypes.ts +5 -0
- package/src/observe.ts +1 -1
- package/src/react/For.tsx +6 -6
- package/src/sync/activateSyncedNode.ts +9 -25
- package/src/sync/syncHelpers.ts +53 -12
- package/src/sync/syncObservable.ts +117 -101
- package/src/sync-plugins/crud.ts +384 -0
- package/src/sync-plugins/fetch.ts +57 -27
- package/src/sync-plugins/keel.ts +447 -0
- package/src/sync-plugins/supabase.ts +225 -0
- package/src/syncTypes.ts +12 -6
- package/src/when.ts +6 -1
- package/sync-plugins/crud.d.ts +40 -0
- package/sync-plugins/crud.js +275 -0
- package/sync-plugins/crud.js.map +1 -0
- package/sync-plugins/crud.mjs +271 -0
- package/sync-plugins/crud.mjs.map +1 -0
- package/sync-plugins/fetch.d.ts +8 -7
- package/sync-plugins/fetch.js +34 -12
- package/sync-plugins/fetch.js.map +1 -1
- package/sync-plugins/fetch.mjs +35 -13
- package/sync-plugins/fetch.mjs.map +1 -1
- package/sync-plugins/keel.d.ts +91 -0
- package/sync-plugins/keel.js +278 -0
- package/sync-plugins/keel.js.map +1 -0
- package/sync-plugins/keel.mjs +274 -0
- package/sync-plugins/keel.mjs.map +1 -0
- package/sync-plugins/supabase.d.ts +32 -0
- package/sync-plugins/supabase.js +134 -0
- package/sync-plugins/supabase.js.map +1 -0
- package/sync-plugins/supabase.mjs +131 -0
- package/sync-plugins/supabase.mjs.map +1 -0
- package/sync.d.ts +1 -0
- package/sync.js +157 -127
- package/sync.js.map +1 -1
- package/sync.mjs +156 -129
- package/sync.mjs.map +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var state = require('@legendapp/state');
|
|
4
|
+
var crud = require('@legendapp/state/sync-plugins/crud');
|
|
5
|
+
|
|
6
|
+
let channelNum = 1;
|
|
7
|
+
const supabaseConfig = {};
|
|
8
|
+
const isEnabled$ = state.observable(true);
|
|
9
|
+
function configureSyncedSupabase(config) {
|
|
10
|
+
const { enabled, ...rest } = config;
|
|
11
|
+
if (enabled !== undefined) {
|
|
12
|
+
isEnabled$.set(enabled);
|
|
13
|
+
}
|
|
14
|
+
Object.assign(supabaseConfig, rest);
|
|
15
|
+
}
|
|
16
|
+
function syncedSupabase(props) {
|
|
17
|
+
state.mergeIntoObservable(props, supabaseConfig);
|
|
18
|
+
const { supabase: client, collection, filter, actions, fieldUpdatedAt, realtime, changesSince, waitFor, waitForSet, ...rest } = props;
|
|
19
|
+
const list = !actions || actions.includes('read')
|
|
20
|
+
? async (params) => {
|
|
21
|
+
const { lastSync } = params;
|
|
22
|
+
let select = client.from(collection).select();
|
|
23
|
+
if (changesSince === 'last-sync') {
|
|
24
|
+
select = select.neq('deleted', true);
|
|
25
|
+
if (lastSync) {
|
|
26
|
+
const date = new Date(lastSync).toISOString();
|
|
27
|
+
select = select.or(`created_at.gt.${date}${fieldUpdatedAt ? `,${fieldUpdatedAt}.gt.${date}` : ''}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (filter) {
|
|
31
|
+
select = filter(select, params);
|
|
32
|
+
}
|
|
33
|
+
const { data, error } = await select;
|
|
34
|
+
if (error) {
|
|
35
|
+
throw new Error(error === null || error === void 0 ? void 0 : error.message);
|
|
36
|
+
}
|
|
37
|
+
return (data || []);
|
|
38
|
+
}
|
|
39
|
+
: undefined;
|
|
40
|
+
const upsert = async (input) => {
|
|
41
|
+
const res = await client.from(collection).upsert(input).select();
|
|
42
|
+
const { data, error } = res;
|
|
43
|
+
if (data) {
|
|
44
|
+
const created = data[0];
|
|
45
|
+
return created;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
throw new Error(error === null || error === void 0 ? void 0 : error.message);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const create = !actions || actions.includes('create') ? upsert : undefined;
|
|
52
|
+
const update = !actions || actions.includes('update') ? upsert : undefined;
|
|
53
|
+
const deleteFn = !actions || actions.includes('delete')
|
|
54
|
+
? async (input) => {
|
|
55
|
+
const id = input.id;
|
|
56
|
+
const from = client.from(collection);
|
|
57
|
+
const res = await (changesSince === 'last-sync' ? from.update({ deleted: true }) : from.delete())
|
|
58
|
+
.eq('id', id)
|
|
59
|
+
.select();
|
|
60
|
+
const { data, error } = res;
|
|
61
|
+
if (data) {
|
|
62
|
+
const created = data[0];
|
|
63
|
+
return created;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
throw new Error(error === null || error === void 0 ? void 0 : error.message);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
: undefined;
|
|
70
|
+
const subscribe = realtime
|
|
71
|
+
? ({ node, update }) => {
|
|
72
|
+
const { filter, schema } = realtime;
|
|
73
|
+
const channel = client
|
|
74
|
+
.channel(`LS_${node.key || ''}${channelNum++}`)
|
|
75
|
+
.on('postgres_changes', {
|
|
76
|
+
event: '*',
|
|
77
|
+
table: collection,
|
|
78
|
+
schema: schema || 'public',
|
|
79
|
+
filter: filter || undefined,
|
|
80
|
+
}, (payload) => {
|
|
81
|
+
var _a;
|
|
82
|
+
const { eventType, new: value, old } = payload;
|
|
83
|
+
if (eventType === 'INSERT' || eventType === 'UPDATE') {
|
|
84
|
+
const cur = (_a = state.getNodeValue(node)) === null || _a === void 0 ? void 0 : _a[value.id];
|
|
85
|
+
const curDateStr = cur && (cur.updated_at || cur.created_at);
|
|
86
|
+
const valueDateStr = value.updated_at || value.created_at;
|
|
87
|
+
const valueDate = +new Date(valueDateStr);
|
|
88
|
+
// Check if new or newer than last seen locally
|
|
89
|
+
if (valueDateStr && (!curDateStr || valueDate > +new Date(curDateStr))) {
|
|
90
|
+
// Update local with the new value
|
|
91
|
+
update({
|
|
92
|
+
value: { [value.id]: value },
|
|
93
|
+
lastSync: valueDate,
|
|
94
|
+
mode: 'merge',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (eventType === 'DELETE') {
|
|
99
|
+
const { id } = old;
|
|
100
|
+
update({
|
|
101
|
+
value: { [id]: state.symbolDelete },
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
.subscribe();
|
|
106
|
+
return channel.unsubscribe;
|
|
107
|
+
}
|
|
108
|
+
: undefined;
|
|
109
|
+
return crud.syncedCrud({
|
|
110
|
+
...rest,
|
|
111
|
+
list,
|
|
112
|
+
create,
|
|
113
|
+
update,
|
|
114
|
+
delete: deleteFn,
|
|
115
|
+
onSaved: (saved) => {
|
|
116
|
+
// Update the local timestamps with server response
|
|
117
|
+
return {
|
|
118
|
+
id: saved.id,
|
|
119
|
+
created_at: saved.created_at,
|
|
120
|
+
updated_at: saved.updated_at,
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
subscribe,
|
|
124
|
+
fieldCreatedAt: 'created_at',
|
|
125
|
+
fieldUpdatedAt,
|
|
126
|
+
updatePartial: true,
|
|
127
|
+
waitFor: () => isEnabled$.get() && (waitFor ? state.computeSelector(waitFor) : true),
|
|
128
|
+
waitForSet: () => isEnabled$.get() && (waitForSet ? state.computeSelector(waitForSet) : true),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
exports.configureSyncedSupabase = configureSyncedSupabase;
|
|
133
|
+
exports.syncedSupabase = syncedSupabase;
|
|
134
|
+
//# sourceMappingURL=supabase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"supabase.js","sources":["../src/sync-plugins/supabase.ts"],"sourcesContent":[null],"names":["observable","mergeIntoObservable","getNodeValue","symbolDelete","syncedCrud","computeSelector"],"mappings":";;;;;AA4EA,IAAI,UAAU,GAAG,CAAC,CAAC;AACnB,MAAM,cAAc,GAA+B,EAAE,CAAC;AACtD,MAAM,UAAU,GAAGA,gBAAU,CAAC,IAAI,CAAC,CAAC;AAE9B,SAAU,uBAAuB,CAAC,MAAkC,EAAA;IACtE,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;AACpC,IAAA,IAAI,OAAO,KAAK,SAAS,EAAE;AACvB,QAAA,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;KAC3B;AACD,IAAA,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE,IAAI,CAAC,CAAC;AACxC,CAAC;AAEK,SAAU,cAAc,CAI5B,KAAwD,EAAA;AACtD,IAAAC,yBAAmB,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;IAC3C,MAAM,EACF,QAAQ,EAAE,MAAM,EAChB,UAAU,EACV,MAAM,EACN,OAAO,EACP,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,OAAO,EACP,UAAU,EACV,GAAG,IAAI,EACV,GAAG,KAAK,CAAC;IACV,MAAM,IAAI,GACN,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;AAChC,UAAE,OAAO,MAAuB,KAAI;AAC9B,YAAA,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;YAC5B,IAAI,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,EAAE,CAAC;AAC9C,YAAA,IAAI,YAAY,K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AAO;AAChB,yBAAA,CAAC,CAAC;qBACN;iBACJ;AAAM,qBAAA,IAAI,SAAS,KAAK,QAAQ,EAAE;AAC/B,oBAAA,MAAM,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;AACnB,oBAAA,MAAM,CAAC;AACH,wBAAA,KAAK,EAAE,EAAE,CAAC,EAAE,GAAGC,kBAAY,EAAE;AAChC,qBAAA,CAAC,CAAC;iBACN;AACL,aAAC,CACJ;AACA,iBAAA,SAAS,EAAE,CAAC;YAEjB,OAAO,OAAO,CAAC,WAAW,CAAC;SAC9B;UACD,SAAS,CAAC;AAEhB,IAAA,OAAOC,eAAU,CAAiE;AAC9E,QAAA,GAAG,IAAI;QACP,IAAI;QACJ,MAAM;QACN,MAAM;AACN,QAAA,MAAM,EAAE,QAAQ;AAChB,QAAA,OAAO,EAAE,CAAC,KAAK,KAAI;;YAEf,OAAO;gBACH,EAAE,EAAE,KAAK,CAAC,EAAE;gBACZ,UAAU,EAAE,KAAK,CAAC,UAAU;gBAC5B,UAAU,EAAE,KAAK,CAAC,UAAU;aAC/B,CAAC;SACL;QACD,SAAS;AACT,QAAA,cAAc,EAAE,YAAY;QAC5B,cAAc;AACd,QAAA,aAAa,EAAE,IAAI;QACnB,OAAO,EAAE,MAAM,UAAU,CAAC,GAAG,EAAE,KAAK,OAAO,GAAGC,qBAAe,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;QAC9E,UAAU,EAAE,MAAM,UAAU,CAAC,GAAG,EAAE,KAAK,UAAU,GAAGA,qBAAe,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;AAC1F,KAAA,CAAC,CAAC;AACP;;;;;"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { observable, mergeIntoObservable, computeSelector, getNodeValue, symbolDelete } from '@legendapp/state';
|
|
2
|
+
import { syncedCrud } from '@legendapp/state/sync-plugins/crud';
|
|
3
|
+
|
|
4
|
+
let channelNum = 1;
|
|
5
|
+
const supabaseConfig = {};
|
|
6
|
+
const isEnabled$ = observable(true);
|
|
7
|
+
function configureSyncedSupabase(config) {
|
|
8
|
+
const { enabled, ...rest } = config;
|
|
9
|
+
if (enabled !== undefined) {
|
|
10
|
+
isEnabled$.set(enabled);
|
|
11
|
+
}
|
|
12
|
+
Object.assign(supabaseConfig, rest);
|
|
13
|
+
}
|
|
14
|
+
function syncedSupabase(props) {
|
|
15
|
+
mergeIntoObservable(props, supabaseConfig);
|
|
16
|
+
const { supabase: client, collection, filter, actions, fieldUpdatedAt, realtime, changesSince, waitFor, waitForSet, ...rest } = props;
|
|
17
|
+
const list = !actions || actions.includes('read')
|
|
18
|
+
? async (params) => {
|
|
19
|
+
const { lastSync } = params;
|
|
20
|
+
let select = client.from(collection).select();
|
|
21
|
+
if (changesSince === 'last-sync') {
|
|
22
|
+
select = select.neq('deleted', true);
|
|
23
|
+
if (lastSync) {
|
|
24
|
+
const date = new Date(lastSync).toISOString();
|
|
25
|
+
select = select.or(`created_at.gt.${date}${fieldUpdatedAt ? `,${fieldUpdatedAt}.gt.${date}` : ''}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (filter) {
|
|
29
|
+
select = filter(select, params);
|
|
30
|
+
}
|
|
31
|
+
const { data, error } = await select;
|
|
32
|
+
if (error) {
|
|
33
|
+
throw new Error(error === null || error === void 0 ? void 0 : error.message);
|
|
34
|
+
}
|
|
35
|
+
return (data || []);
|
|
36
|
+
}
|
|
37
|
+
: undefined;
|
|
38
|
+
const upsert = async (input) => {
|
|
39
|
+
const res = await client.from(collection).upsert(input).select();
|
|
40
|
+
const { data, error } = res;
|
|
41
|
+
if (data) {
|
|
42
|
+
const created = data[0];
|
|
43
|
+
return created;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
throw new Error(error === null || error === void 0 ? void 0 : error.message);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const create = !actions || actions.includes('create') ? upsert : undefined;
|
|
50
|
+
const update = !actions || actions.includes('update') ? upsert : undefined;
|
|
51
|
+
const deleteFn = !actions || actions.includes('delete')
|
|
52
|
+
? async (input) => {
|
|
53
|
+
const id = input.id;
|
|
54
|
+
const from = client.from(collection);
|
|
55
|
+
const res = await (changesSince === 'last-sync' ? from.update({ deleted: true }) : from.delete())
|
|
56
|
+
.eq('id', id)
|
|
57
|
+
.select();
|
|
58
|
+
const { data, error } = res;
|
|
59
|
+
if (data) {
|
|
60
|
+
const created = data[0];
|
|
61
|
+
return created;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw new Error(error === null || error === void 0 ? void 0 : error.message);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
: undefined;
|
|
68
|
+
const subscribe = realtime
|
|
69
|
+
? ({ node, update }) => {
|
|
70
|
+
const { filter, schema } = realtime;
|
|
71
|
+
const channel = client
|
|
72
|
+
.channel(`LS_${node.key || ''}${channelNum++}`)
|
|
73
|
+
.on('postgres_changes', {
|
|
74
|
+
event: '*',
|
|
75
|
+
table: collection,
|
|
76
|
+
schema: schema || 'public',
|
|
77
|
+
filter: filter || undefined,
|
|
78
|
+
}, (payload) => {
|
|
79
|
+
var _a;
|
|
80
|
+
const { eventType, new: value, old } = payload;
|
|
81
|
+
if (eventType === 'INSERT' || eventType === 'UPDATE') {
|
|
82
|
+
const cur = (_a = getNodeValue(node)) === null || _a === void 0 ? void 0 : _a[value.id];
|
|
83
|
+
const curDateStr = cur && (cur.updated_at || cur.created_at);
|
|
84
|
+
const valueDateStr = value.updated_at || value.created_at;
|
|
85
|
+
const valueDate = +new Date(valueDateStr);
|
|
86
|
+
// Check if new or newer than last seen locally
|
|
87
|
+
if (valueDateStr && (!curDateStr || valueDate > +new Date(curDateStr))) {
|
|
88
|
+
// Update local with the new value
|
|
89
|
+
update({
|
|
90
|
+
value: { [value.id]: value },
|
|
91
|
+
lastSync: valueDate,
|
|
92
|
+
mode: 'merge',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else if (eventType === 'DELETE') {
|
|
97
|
+
const { id } = old;
|
|
98
|
+
update({
|
|
99
|
+
value: { [id]: symbolDelete },
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
.subscribe();
|
|
104
|
+
return channel.unsubscribe;
|
|
105
|
+
}
|
|
106
|
+
: undefined;
|
|
107
|
+
return syncedCrud({
|
|
108
|
+
...rest,
|
|
109
|
+
list,
|
|
110
|
+
create,
|
|
111
|
+
update,
|
|
112
|
+
delete: deleteFn,
|
|
113
|
+
onSaved: (saved) => {
|
|
114
|
+
// Update the local timestamps with server response
|
|
115
|
+
return {
|
|
116
|
+
id: saved.id,
|
|
117
|
+
created_at: saved.created_at,
|
|
118
|
+
updated_at: saved.updated_at,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
subscribe,
|
|
122
|
+
fieldCreatedAt: 'created_at',
|
|
123
|
+
fieldUpdatedAt,
|
|
124
|
+
updatePartial: true,
|
|
125
|
+
waitFor: () => isEnabled$.get() && (waitFor ? computeSelector(waitFor) : true),
|
|
126
|
+
waitForSet: () => isEnabled$.get() && (waitForSet ? computeSelector(waitForSet) : true),
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export { configureSyncedSupabase, syncedSupabase };
|
|
131
|
+
//# sourceMappingURL=supabase.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"supabase.mjs","sources":["../src/sync-plugins/supabase.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AA4EA,IAAI,UAAU,GAAG,CAAC,CAAC;AACnB,MAAM,cAAc,GAA+B,EAAE,CAAC;AACtD,MAAM,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;AAE9B,SAAU,uBAAuB,CAAC,MAAkC,EAAA;IACtE,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;AACpC,IAAA,IAAI,OAAO,KAAK,SAAS,EAAE;AACvB,Q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KAAK,CAAC,EAAE;gBACZ,UAAU,EAAE,KAAK,CAAC,UAAU;gBAC5B,UAAU,EAAE,KAAK,CAAC,UAAU;aAC/B,CAAC;SACL;QACD,SAAS;AACT,QAAA,cAAc,EAAE,YAAY;QAC5B,cAAc;AACd,QAAA,aAAa,EAAE,IAAI;QACnB,OAAO,EAAE,MAAM,UAAU,CAAC,GAAG,EAAE,KAAK,OAAO,GAAG,eAAe,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;QAC9E,UAAU,EAAE,MAAM,UAAU,CAAC,GAAG,EAAE,KAAK,UAAU,GAAG,eAAe,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;AAC1F,KAAA,CAAC,CAAC;AACP;;;;"}
|
package/sync.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { SyncedOptionsGlobal } from '@legendapp/state';
|
|
|
2
2
|
export { configureObservableSync } from './src/sync/configureObservableSync';
|
|
3
3
|
export { mapSyncPlugins, syncObservable } from './src/sync/syncObservable';
|
|
4
4
|
export { synced } from './src/sync/synced';
|
|
5
|
+
export { removeNullUndefined, deepEqual, diffObjects } from './src/sync/syncHelpers';
|
|
5
6
|
export declare function isInRemoteChange(): boolean;
|
|
6
7
|
export declare const internal: {
|
|
7
8
|
observableSyncConfiguration: SyncedOptionsGlobal;
|