@civet/core 0.6.9 → 1.0.0-rc1

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/src/DataStore.js DELETED
@@ -1,141 +0,0 @@
1
- import deepEquals from 'fast-deep-equal';
2
- import PropTypes from 'prop-types';
3
-
4
- import AbortSignal from './AbortSignal';
5
- import ChannelNotifier from './ChannelNotifier';
6
- import Meta from './Meta';
7
-
8
- const getMeta = (meta) => (meta instanceof Meta ? meta : new Meta(meta));
9
-
10
- class DataStore {
11
- notifier = new ChannelNotifier();
12
-
13
- constructor() {
14
- this.resourcePlugins = [];
15
- this.extend({
16
- resource: (plugin) => {
17
- if (!this.resourcePlugins.includes(plugin)) {
18
- this.resourcePlugins.push(plugin);
19
- }
20
- },
21
- });
22
- }
23
-
24
- extend() {}
25
-
26
- subscribe(resource, handler) {
27
- if (resource == null) throw new Error('No resource name specified');
28
- return this.notifier.subscribe(resource, handler);
29
- }
30
-
31
- notify(resource) {
32
- this.notifier.trigger(resource);
33
- }
34
-
35
- get(resource, ids, query, options, meta) {
36
- return new Promise((resolve, reject) =>
37
- this.continuousGet(resource, ids, query, options, meta, (error, done, result) => {
38
- if (error != null) {
39
- reject(error);
40
- return;
41
- }
42
- if (done) resolve(result);
43
- }),
44
- );
45
- }
46
-
47
- continuousGet(resource, ids, query, options, meta, callback, abortSignal) {
48
- new Promise((resolve) => {
49
- if (resource == null) throw new Error('No resource name specified');
50
- if (ids != null && !Array.isArray(ids)) throw new Error('IDs must be an array');
51
-
52
- let complete = false;
53
- const signal = abortSignal == null ? new AbortSignal() : abortSignal;
54
-
55
- // result transformation
56
- const cb = (error, done, result) => {
57
- // prevent updates after completion
58
- if (complete) return;
59
- if (error != null || done) {
60
- complete = true;
61
- signal.lock();
62
- }
63
- if (error != null) callback(error, true, []);
64
- else if (result == null) callback(undefined, done, []);
65
- else if (Array.isArray(result)) callback(undefined, done, result);
66
- else callback(undefined, done, [result]);
67
- };
68
-
69
- resolve(
70
- Promise.resolve(this.handleGet(resource, ids, query, options, getMeta(meta))).then(
71
- (result) => {
72
- if (typeof result === 'function') {
73
- result(cb, signal);
74
- } else {
75
- cb(undefined, true, result);
76
- }
77
- },
78
- ),
79
- );
80
- }).catch((e) => {
81
- callback(e, true, []);
82
- });
83
- }
84
-
85
- create(resource, data, options, meta) {
86
- return new Promise((resolve) => {
87
- if (resource == null) throw new Error('No resource name specified');
88
- if (data == null) throw new Error('No data specified');
89
- resolve(Promise.resolve(this.handleCreate(resource, data, options, getMeta(meta))));
90
- });
91
- }
92
-
93
- update(resource, ids, query, data, options, meta) {
94
- return new Promise((resolve) => {
95
- if (resource == null) throw new Error('No resource name specified');
96
- if (ids != null && !Array.isArray(ids)) throw new Error('IDs must be an array');
97
- if (data == null) throw new Error('No data specified');
98
- resolve(
99
- Promise.resolve(this.handleUpdate(resource, ids, query, data, options, getMeta(meta))),
100
- );
101
- });
102
- }
103
-
104
- patch(resource, ids, query, data, options, meta) {
105
- return new Promise((resolve) => {
106
- if (resource == null) throw new Error('No resource name specified');
107
- if (ids != null && !Array.isArray(ids)) throw new Error('IDs must be an array');
108
- if (data == null) throw new Error('No data specified');
109
- resolve(
110
- Promise.resolve(this.handlePatch(resource, ids, query, data, options, getMeta(meta))),
111
- );
112
- });
113
- }
114
-
115
- remove(resource, ids, query, options, meta) {
116
- return new Promise((resolve) => {
117
- if (resource == null) throw new Error('No resource name specified');
118
- if (ids != null && !Array.isArray(ids)) throw new Error('IDs must be an array');
119
- resolve(Promise.resolve(this.handleRemove(resource, ids, query, options, getMeta(meta))));
120
- });
121
- }
122
-
123
- transition(nextData) {
124
- return nextData;
125
- }
126
-
127
- recycleItems(nextData) {
128
- return nextData;
129
- }
130
-
131
- compareRequests(prev, next) {
132
- return deepEquals(prev, next);
133
- }
134
- }
135
-
136
- const isDataStore = (dataStore) => dataStore instanceof DataStore;
137
-
138
- const dataStorePropType = PropTypes.instanceOf(DataStore);
139
-
140
- export default DataStore;
141
- export { isDataStore, dataStorePropType };
@@ -1,38 +0,0 @@
1
- import objectHash from 'object-hash';
2
-
3
- import BaseDataStore from './DataStore';
4
-
5
- class DefaultDataStore extends BaseDataStore {
6
- recycleItemsUniqueIdentifier(item) {
7
- return objectHash(item);
8
- }
9
-
10
- recycleItemsIsUnchanged() {
11
- return true;
12
- }
13
-
14
- recycleItems(nextData, prevData) {
15
- const prevMapping = {};
16
- prevData.forEach((item) => {
17
- const id = this.recycleItemsUniqueIdentifier(item);
18
- if (id != null) prevMapping[id] = item;
19
- });
20
- const result = nextData.map((nextItem) => {
21
- const id = this.recycleItemsUniqueIdentifier(nextItem);
22
- if (id != null && Object.prototype.hasOwnProperty.call(prevMapping, id)) {
23
- const prevItem = prevMapping[id];
24
- if (this.recycleItemsIsUnchanged(nextItem, prevItem)) return prevItem;
25
- }
26
- return nextItem;
27
- });
28
- if (
29
- prevData.length === result.length &&
30
- result.reduce((sum, item, i) => sum && Object.is(prevData[i], item), true)
31
- ) {
32
- return prevData;
33
- }
34
- return result;
35
- }
36
- }
37
-
38
- export default DefaultDataStore;