@graffiti-garden/wrapper-synchronize 0.0.3 → 0.1.0
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/browser/ajv-65NGXB2O.js +9 -0
- package/dist/browser/{ajv-LKZCCH33.js.map → ajv-65NGXB2O.js.map} +2 -2
- package/dist/browser/chunk-POQWWD2G.js +7 -0
- package/dist/browser/chunk-POQWWD2G.js.map +7 -0
- package/dist/browser/fast-json-patch-RVBYS6DB.js +19 -0
- package/dist/browser/fast-json-patch-RVBYS6DB.js.map +7 -0
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +4 -4
- package/dist/cjs/index.js +240 -1
- package/dist/cjs/index.js.map +3 -3
- package/dist/esm/index.js +216 -1
- package/dist/esm/index.js.map +3 -3
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/index.spec.ts +6 -2
- package/src/index.ts +5 -11
- package/dist/browser/ajv-LKZCCH33.js +0 -9
- package/dist/browser/chunk-JSDGZ4NY.js +0 -2
- package/dist/browser/chunk-JSDGZ4NY.js.map +0 -7
- package/dist/browser/fast-json-patch-HPEBNS53.js +0 -19
- package/dist/browser/fast-json-patch-HPEBNS53.js.map +0 -7
package/dist/cjs/index.js
CHANGED
|
@@ -1,2 +1,241 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var index_exports = {};
|
|
30
|
+
__export(index_exports, {
|
|
31
|
+
GraffitiSynchronize: () => GraffitiSynchronize
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
var import_api = require("@graffiti-garden/api");
|
|
35
|
+
var import_repeater = require("@repeaterjs/repeater");
|
|
36
|
+
var import_utilities = require("@graffiti-garden/implementation-local/utilities");
|
|
37
|
+
class GraffitiSynchronize extends import_api.Graffiti {
|
|
38
|
+
ajv_;
|
|
39
|
+
applyPatch_;
|
|
40
|
+
graffiti;
|
|
41
|
+
callbacks = /* @__PURE__ */ new Set();
|
|
42
|
+
options;
|
|
43
|
+
channelStats;
|
|
44
|
+
login;
|
|
45
|
+
logout;
|
|
46
|
+
sessionEvents;
|
|
47
|
+
get ajv() {
|
|
48
|
+
if (!this.ajv_) {
|
|
49
|
+
this.ajv_ = (async () => {
|
|
50
|
+
const { default: Ajv } = await import("ajv");
|
|
51
|
+
return new Ajv({ strict: false });
|
|
52
|
+
})();
|
|
53
|
+
}
|
|
54
|
+
return this.ajv_;
|
|
55
|
+
}
|
|
56
|
+
get applyPatch() {
|
|
57
|
+
if (!this.applyPatch_) {
|
|
58
|
+
this.applyPatch_ = (async () => {
|
|
59
|
+
const { applyPatch } = await import("fast-json-patch");
|
|
60
|
+
return applyPatch;
|
|
61
|
+
})();
|
|
62
|
+
}
|
|
63
|
+
return this.applyPatch_;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Wraps a Graffiti API instance to provide the synchronize methods.
|
|
67
|
+
* The GraffitiSyncrhonize class rather than the Graffiti class
|
|
68
|
+
* must be used for all functions for the synchronize methods to work.
|
|
69
|
+
*/
|
|
70
|
+
constructor(graffiti, options) {
|
|
71
|
+
super();
|
|
72
|
+
this.options = options ?? {};
|
|
73
|
+
this.graffiti = graffiti;
|
|
74
|
+
this.channelStats = graffiti.channelStats.bind(graffiti);
|
|
75
|
+
this.login = graffiti.login.bind(graffiti);
|
|
76
|
+
this.logout = graffiti.logout.bind(graffiti);
|
|
77
|
+
this.sessionEvents = graffiti.sessionEvents;
|
|
78
|
+
}
|
|
79
|
+
synchronize(matchObject, channels, schema, session) {
|
|
80
|
+
const repeater = new import_repeater.Repeater(
|
|
81
|
+
async (push, stop) => {
|
|
82
|
+
const validate = (0, import_utilities.compileGraffitiObjectSchema)(await this.ajv, schema);
|
|
83
|
+
const callback = (oldObjectRaw, newObjectRaw) => {
|
|
84
|
+
for (const objectRaw of [newObjectRaw, oldObjectRaw]) {
|
|
85
|
+
if (objectRaw && matchObject(objectRaw) && (this.options.omniscient || (0, import_utilities.isActorAllowedGraffitiObject)(objectRaw, session))) {
|
|
86
|
+
const object = { ...objectRaw };
|
|
87
|
+
if (!this.options.omniscient) {
|
|
88
|
+
(0, import_utilities.maskGraffitiObject)(object, channels, session);
|
|
89
|
+
}
|
|
90
|
+
if (validate(object)) {
|
|
91
|
+
push({ value: object });
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
this.callbacks.add(callback);
|
|
98
|
+
await stop;
|
|
99
|
+
this.callbacks.delete(callback);
|
|
100
|
+
}
|
|
101
|
+
);
|
|
102
|
+
return repeater;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* This method has the same signature as {@link discover} but listens for
|
|
106
|
+
* changes made via {@link put}, {@link patch}, and {@link delete} or
|
|
107
|
+
* fetched from {@link get}, {@link discover}, and {@link recoverOrphans}
|
|
108
|
+
* and then streams appropriate changes to provide a responsive and
|
|
109
|
+
* consistent user experience.
|
|
110
|
+
*
|
|
111
|
+
* Unlike {@link discover}, this method continuously listens for changes
|
|
112
|
+
* and will not terminate unless the user calls the `return` method on the iterator
|
|
113
|
+
* or `break`s out of the loop.
|
|
114
|
+
*
|
|
115
|
+
* @group Synchronize Methods
|
|
116
|
+
*/
|
|
117
|
+
synchronizeDiscover(...args) {
|
|
118
|
+
const [channels, schema, session] = args;
|
|
119
|
+
function matchObject(object) {
|
|
120
|
+
return object.channels.some((channel) => channels.includes(channel));
|
|
121
|
+
}
|
|
122
|
+
return this.synchronize(matchObject, channels, schema, session);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* This method has the same signature as {@link get} but
|
|
126
|
+
* listens for changes made via {@link put}, {@link patch}, and {@link delete} or
|
|
127
|
+
* fetched from {@link get}, {@link discover}, and {@link recoverOrphans} and then
|
|
128
|
+
* streams appropriate changes to provide a responsive and consistent user experience.
|
|
129
|
+
*
|
|
130
|
+
* Unlike {@link get}, which returns a single result, this method continuously
|
|
131
|
+
* listens for changes which are output as an asynchronous {@link GraffitiStream}.
|
|
132
|
+
*
|
|
133
|
+
* @group Synchronize Methods
|
|
134
|
+
*/
|
|
135
|
+
synchronizeGet(...args) {
|
|
136
|
+
const [locationOrUri, schema, session] = args;
|
|
137
|
+
const uri = (0, import_utilities.unpackLocationOrUri)(locationOrUri);
|
|
138
|
+
function matchObject(object) {
|
|
139
|
+
return object.uri === uri;
|
|
140
|
+
}
|
|
141
|
+
return this.synchronize(matchObject, [], schema, session);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* This method has the same signature as {@link recoverOrphans} but
|
|
145
|
+
* listens for changes made via
|
|
146
|
+
* {@link put}, {@link patch}, and {@link delete} or fetched from
|
|
147
|
+
* {@link get}, {@link discover}, and {@link recoverOrphans} and then
|
|
148
|
+
* streams appropriate changes to provide a responsive and consistent user experience.
|
|
149
|
+
*
|
|
150
|
+
* Unlike {@link recoverOrphans}, this method continuously listens for changes
|
|
151
|
+
* and will not terminate unless the user calls the `return` method on the iterator
|
|
152
|
+
* or `break`s out of the loop.
|
|
153
|
+
*
|
|
154
|
+
* @group Synchronize Methods
|
|
155
|
+
*/
|
|
156
|
+
synchronizeRecoverOrphans(...args) {
|
|
157
|
+
const [schema, session] = args;
|
|
158
|
+
function matchObject(object) {
|
|
159
|
+
return object.actor === session.actor && object.channels.length === 0;
|
|
160
|
+
}
|
|
161
|
+
return this.synchronize(matchObject, [], schema, session);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Streams changes made to *any* object in *any* channel
|
|
165
|
+
* and made by *any* user. You may want to use it in conjuction with
|
|
166
|
+
* {@link GraffitiSynchronizeOptions.omniscient} to get a global view
|
|
167
|
+
* of all Graffiti objects passing through the system. This is useful
|
|
168
|
+
* for building a client-side cache, for example.
|
|
169
|
+
*
|
|
170
|
+
* Be careful using this method. Without additional filters it can
|
|
171
|
+
* expose the user to content out of context.
|
|
172
|
+
*/
|
|
173
|
+
synchronizeAll(schema, session) {
|
|
174
|
+
return this.synchronize(() => true, [], schema ?? {}, session);
|
|
175
|
+
}
|
|
176
|
+
async synchronizeDispatch(oldObject, newObject, waitForListeners = false) {
|
|
177
|
+
for (const callback of this.callbacks) {
|
|
178
|
+
callback(oldObject, newObject);
|
|
179
|
+
}
|
|
180
|
+
if (waitForListeners) {
|
|
181
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
get = async (...args) => {
|
|
185
|
+
const object = await this.graffiti.get(...args);
|
|
186
|
+
this.synchronizeDispatch(object);
|
|
187
|
+
return object;
|
|
188
|
+
};
|
|
189
|
+
put = async (...args) => {
|
|
190
|
+
const oldObject = await this.graffiti.put(...args);
|
|
191
|
+
const partialObject = args[0];
|
|
192
|
+
const newObject = {
|
|
193
|
+
...oldObject,
|
|
194
|
+
value: partialObject.value,
|
|
195
|
+
channels: partialObject.channels,
|
|
196
|
+
allowed: partialObject.allowed,
|
|
197
|
+
tombstone: false
|
|
198
|
+
};
|
|
199
|
+
await this.synchronizeDispatch(oldObject, newObject, true);
|
|
200
|
+
return oldObject;
|
|
201
|
+
};
|
|
202
|
+
patch = async (...args) => {
|
|
203
|
+
const oldObject = await this.graffiti.patch(...args);
|
|
204
|
+
const newObject = { ...oldObject };
|
|
205
|
+
newObject.tombstone = false;
|
|
206
|
+
for (const prop of ["value", "channels", "allowed"]) {
|
|
207
|
+
(0, import_utilities.applyGraffitiPatch)(await this.applyPatch, prop, args[0], newObject);
|
|
208
|
+
}
|
|
209
|
+
await this.synchronizeDispatch(oldObject, newObject, true);
|
|
210
|
+
return oldObject;
|
|
211
|
+
};
|
|
212
|
+
delete = async (...args) => {
|
|
213
|
+
const oldObject = await this.graffiti.delete(...args);
|
|
214
|
+
await this.synchronizeDispatch(oldObject, void 0, true);
|
|
215
|
+
return oldObject;
|
|
216
|
+
};
|
|
217
|
+
objectStream(iterator) {
|
|
218
|
+
const dispatch = this.synchronizeDispatch.bind(this);
|
|
219
|
+
const wrapper = async function* () {
|
|
220
|
+
let result = await iterator.next();
|
|
221
|
+
while (!result.done) {
|
|
222
|
+
if (!result.value.error) {
|
|
223
|
+
dispatch(result.value.value);
|
|
224
|
+
}
|
|
225
|
+
yield result.value;
|
|
226
|
+
result = await iterator.next();
|
|
227
|
+
}
|
|
228
|
+
return result.value;
|
|
229
|
+
};
|
|
230
|
+
return wrapper();
|
|
231
|
+
}
|
|
232
|
+
discover = (...args) => {
|
|
233
|
+
const iterator = this.graffiti.discover(...args);
|
|
234
|
+
return this.objectStream(iterator);
|
|
235
|
+
};
|
|
236
|
+
recoverOrphans = (...args) => {
|
|
237
|
+
const iterator = this.graffiti.recoverOrphans(...args);
|
|
238
|
+
return this.objectStream(iterator);
|
|
239
|
+
};
|
|
240
|
+
}
|
|
2
241
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import type Ajv from \"ajv\";\nimport { Graffiti } from \"@graffiti-garden/api\";\nimport type {\n GraffitiSession,\n GraffitiObject,\n JSONSchema,\n GraffitiStream,\n} from \"@graffiti-garden/api\";\nimport type { GraffitiObjectBase } from \"@graffiti-garden/api\";\nimport { Repeater } from \"@repeaterjs/repeater\";\nimport type { applyPatch } from \"fast-json-patch\";\nimport {\n applyGraffitiPatch,\n compileGraffitiObjectSchema,\n isActorAllowedGraffitiObject,\n locationToUri,\n maskGraffitiObject,\n unpackLocationOrUri,\n} from \"@graffiti-garden/implementation-local/utilities\";\nexport type * from \"@graffiti-garden/api\";\n\nexport type GraffitiSynchronizeCallback = (\n oldObject: GraffitiObjectBase,\n newObject?: GraffitiObjectBase,\n) => void;\n\nexport interface GraffitiSynchronizeOptions {\n /**\n * Allows synchronize to listen to all objects, not just those\n * that the session is allowed to see. This is useful to get a\n * global view of all Graffiti objects passsing through the system,\n * for example to build a client-side cache. Additional mechanisms\n * should be in place to ensure that users do not see objects or\n * properties they are not allowed to see.\n *\n * Default: `false`\n */\n omniscient?: boolean;\n}\n\n/**\n * Wraps the [Graffiti API](https://api.graffiti.garden/classes/Graffiti.html)\n * so that changes made or received in one part of an application\n * are automatically routed to other parts of the application.\n * This is an important tool for building responsive\n * and consistent user interfaces, and is built upon to make\n * the [Graffiti Vue Plugin](https://vue.graffiti.garden/variables/GraffitiPlugin.html)\n * and possibly other front-end libraries in the future.\n *\n * Specifically, it provides the following *synchronize*\n * methods for each of the following API methods:\n *\n * | API Method | Synchronize Method |\n * |------------|--------------------|\n * | {@link get} | {@link synchronizeGet} |\n * | {@link discover} | {@link synchronizeDiscover} |\n * | {@link recoverOrphans} | {@link synchronizeRecoverOrphans} |\n *\n * Whenever a change is made via {@link put}, {@link patch}, and {@link delete} or\n * received from {@link get}, {@link discover}, and {@link recoverOrphans},\n * those changes are forwarded to the appropriate synchronize method.\n * Each synchronize method returns an iterator that streams these changes\n * continually until the user calls `return` on the iterator or `break`s out of the loop,\n * allowing for live updates without additional polling.\n *\n * Example 1: Suppose a user publishes a post using {@link put}. If the feed\n * displaying that user's posts is using {@link synchronizeDiscover} to listen for changes,\n * then the user's new post will instantly appear in their feed, giving the UI a\n * responsive feel.\n *\n * Example 2: Suppose one of a user's friends changes their name. As soon as the\n * user's application receives one notice of that change (using {@link get}\n * or {@link discover}), then {@link synchronizeDiscover} listeners can be used to update\n * all instance's of that friend's name in the user's application instantly,\n * providing a consistent user experience.\n *\n * @groupDescription Synchronize Methods\n * This group contains methods that listen for changes made via\n * {@link put}, {@link patch}, and {@link delete} or fetched from\n * {@link get}, {@link discover}, and {@link recoverOrphans} and then\n * streams appropriate changes to provide a responsive and consistent user experience.\n */\nexport class GraffitiSynchronize extends Graffiti {\n protected ajv_: Promise<Ajv> | undefined;\n protected applyPatch_: Promise<typeof applyPatch> | undefined;\n protected readonly graffiti: Graffiti;\n protected readonly callbacks = new Set<GraffitiSynchronizeCallback>();\n protected readonly options: GraffitiSynchronizeOptions;\n\n channelStats: Graffiti[\"channelStats\"];\n locationToUri: Graffiti[\"locationToUri\"];\n uriToLocation: Graffiti[\"uriToLocation\"];\n login: Graffiti[\"login\"];\n logout: Graffiti[\"logout\"];\n sessionEvents: Graffiti[\"sessionEvents\"];\n\n get ajv() {\n if (!this.ajv_) {\n this.ajv_ = (async () => {\n const { default: Ajv } = await import(\"ajv\");\n return new Ajv({ strict: false });\n })();\n }\n return this.ajv_;\n }\n\n get applyPatch() {\n if (!this.applyPatch_) {\n this.applyPatch_ = (async () => {\n const { applyPatch } = await import(\"fast-json-patch\");\n return applyPatch;\n })();\n }\n return this.applyPatch_;\n }\n\n /**\n * Wraps a Graffiti API instance to provide the synchronize methods.\n * The GraffitiSyncrhonize class rather than the Graffiti class\n * must be used for all functions for the synchronize methods to work.\n */\n constructor(\n /**\n * The [Graffiti API](https://api.graffiti.garden/classes/Graffiti.html)\n * instance to wrap.\n */\n graffiti: Graffiti,\n options?: GraffitiSynchronizeOptions,\n ) {\n super();\n this.options = options ?? {};\n this.graffiti = graffiti;\n this.channelStats = graffiti.channelStats.bind(graffiti);\n this.locationToUri = graffiti.locationToUri.bind(graffiti);\n this.uriToLocation = graffiti.uriToLocation.bind(graffiti);\n this.login = graffiti.login.bind(graffiti);\n this.logout = graffiti.logout.bind(graffiti);\n this.sessionEvents = graffiti.sessionEvents;\n }\n\n protected synchronize<Schema extends JSONSchema>(\n matchObject: (object: GraffitiObjectBase) => boolean,\n channels: string[],\n schema: Schema,\n session?: GraffitiSession | null,\n ) {\n const repeater: GraffitiStream<GraffitiObject<Schema>> = new Repeater(\n async (push, stop) => {\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n const callback: GraffitiSynchronizeCallback = (\n oldObjectRaw,\n newObjectRaw,\n ) => {\n for (const objectRaw of [newObjectRaw, oldObjectRaw]) {\n if (\n objectRaw &&\n matchObject(objectRaw) &&\n (this.options.omniscient ||\n isActorAllowedGraffitiObject(objectRaw, session))\n ) {\n const object = { ...objectRaw };\n if (!this.options.omniscient) {\n maskGraffitiObject(object, channels, session);\n }\n if (validate(object)) {\n push({ value: object });\n break;\n }\n }\n }\n };\n\n this.callbacks.add(callback);\n await stop;\n this.callbacks.delete(callback);\n },\n );\n\n return repeater;\n }\n\n /**\n * This method has the same signature as {@link discover} but listens for\n * changes made via {@link put}, {@link patch}, and {@link delete} or\n * fetched from {@link get}, {@link discover}, and {@link recoverOrphans}\n * and then streams appropriate changes to provide a responsive and\n * consistent user experience.\n *\n * Unlike {@link discover}, this method continuously listens for changes\n * and will not terminate unless the user calls the `return` method on the iterator\n * or `break`s out of the loop.\n *\n * @group Synchronize Methods\n */\n synchronizeDiscover<Schema extends JSONSchema>(\n ...args: Parameters<typeof Graffiti.prototype.discover<Schema>>\n ): GraffitiStream<GraffitiObject<Schema>> {\n const [channels, schema, session] = args;\n function matchObject(object: GraffitiObjectBase) {\n return object.channels.some((channel) => channels.includes(channel));\n }\n return this.synchronize<Schema>(matchObject, channels, schema, session);\n }\n\n /**\n * This method has the same signature as {@link get} but\n * listens for changes made via {@link put}, {@link patch}, and {@link delete} or\n * fetched from {@link get}, {@link discover}, and {@link recoverOrphans} and then\n * streams appropriate changes to provide a responsive and consistent user experience.\n *\n * Unlike {@link get}, which returns a single result, this method continuously\n * listens for changes which are output as an asynchronous {@link GraffitiStream}.\n *\n * @group Synchronize Methods\n */\n synchronizeGet<Schema extends JSONSchema>(\n ...args: Parameters<typeof Graffiti.prototype.get<Schema>>\n ): GraffitiStream<GraffitiObject<Schema>> {\n const [locationOrUri, schema, session] = args;\n function matchObject(object: GraffitiObjectBase) {\n const objectUri = locationToUri(object);\n const { uri } = unpackLocationOrUri(locationOrUri);\n return objectUri === uri;\n }\n return this.synchronize<Schema>(matchObject, [], schema, session);\n }\n\n /**\n * This method has the same signature as {@link recoverOrphans} but\n * listens for changes made via\n * {@link put}, {@link patch}, and {@link delete} or fetched from\n * {@link get}, {@link discover}, and {@link recoverOrphans} and then\n * streams appropriate changes to provide a responsive and consistent user experience.\n *\n * Unlike {@link recoverOrphans}, this method continuously listens for changes\n * and will not terminate unless the user calls the `return` method on the iterator\n * or `break`s out of the loop.\n *\n * @group Synchronize Methods\n */\n synchronizeRecoverOrphans<Schema extends JSONSchema>(\n ...args: Parameters<typeof Graffiti.prototype.recoverOrphans<Schema>>\n ): GraffitiStream<GraffitiObject<Schema>> {\n const [schema, session] = args;\n function matchObject(object: GraffitiObjectBase) {\n return object.actor === session.actor && object.channels.length === 0;\n }\n return this.synchronize<Schema>(matchObject, [], schema, session);\n }\n\n /**\n * Streams changes made to *any* object in *any* channel\n * and made by *any* user. You may want to use it in conjuction with\n * {@link GraffitiSyncrhonizeOptions.omniscient} to get a global view\n * of all Graffiti objects passing through the system. This is useful\n * for building a client-side cache, for example.\n *\n * Be careful using this method. Without additional filters it can\n * expose the user to content out of context.\n */\n synchronizeAll(\n schema?: JSONSchema,\n session?: GraffitiSession | null,\n ): GraffitiStream<GraffitiObjectBase> {\n return this.synchronize(() => true, [], schema ?? {}, session);\n }\n\n protected async synchronizeDispatch(\n oldObject: GraffitiObjectBase,\n newObject?: GraffitiObjectBase,\n waitForListeners = false,\n ) {\n for (const callback of this.callbacks) {\n callback(oldObject, newObject);\n }\n if (waitForListeners) {\n // Wait for the listeners to receive\n // their objects, before returning the operation\n // that triggered them.\n //\n // This is important for mutators (put, patch, delete)\n // to ensure the application state has been updated\n // everywhere before returning, giving consistent\n // feedback to the user that the operation has completed.\n //\n // The opposite is true for accessors (get, discover, recoverOrphans),\n // where it is a weird user experience to call `get`\n // in one place and have the application update\n // somewhere else first. It is also less efficient.\n //\n // The hack is simply to await one \"macro task cycle\".\n // We need to wait for this cycle rather than using\n // `await push` in the callback, because it turns out\n // that `await push` won't resolve until the following\n // .next() call of the iterator, so if only\n // one .next() is called, this dispatch will hang.\n await new Promise((resolve) => setTimeout(resolve, 0));\n }\n }\n\n get: Graffiti[\"get\"] = async (...args) => {\n const object = await this.graffiti.get(...args);\n this.synchronizeDispatch(object);\n return object;\n };\n\n put: Graffiti[\"put\"] = async (...args) => {\n const oldObject = await this.graffiti.put<{}>(...args);\n const partialObject = args[0];\n const newObject: GraffitiObjectBase = {\n ...oldObject,\n value: partialObject.value,\n channels: partialObject.channels,\n allowed: partialObject.allowed,\n tombstone: false,\n };\n await this.synchronizeDispatch(oldObject, newObject, true);\n return oldObject;\n };\n\n patch: Graffiti[\"patch\"] = async (...args) => {\n const oldObject = await this.graffiti.patch(...args);\n const newObject: GraffitiObjectBase = { ...oldObject };\n newObject.tombstone = false;\n for (const prop of [\"value\", \"channels\", \"allowed\"] as const) {\n applyGraffitiPatch(await this.applyPatch, prop, args[0], newObject);\n }\n await this.synchronizeDispatch(oldObject, newObject, true);\n return oldObject;\n };\n\n delete: Graffiti[\"delete\"] = async (...args) => {\n const oldObject = await this.graffiti.delete(...args);\n await this.synchronizeDispatch(oldObject, undefined, true);\n return oldObject;\n };\n\n protected objectStream<Schema extends JSONSchema>(\n iterator: ReturnType<typeof Graffiti.prototype.discover<Schema>>,\n ) {\n const dispatch = this.synchronizeDispatch.bind(this);\n const wrapper = async function* () {\n let result = await iterator.next();\n while (!result.done) {\n if (!result.value.error) {\n dispatch(result.value.value);\n }\n yield result.value;\n result = await iterator.next();\n }\n return result.value;\n };\n return wrapper();\n }\n\n discover: Graffiti[\"discover\"] = (...args) => {\n const iterator = this.graffiti.discover(...args);\n return this.objectStream<(typeof args)[1]>(iterator);\n };\n\n recoverOrphans: Graffiti[\"recoverOrphans\"] = (...args) => {\n const iterator = this.graffiti.recoverOrphans(...args);\n return this.objectStream<(typeof args)[0]>(iterator);\n };\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["import type Ajv from \"ajv\";\nimport { Graffiti } from \"@graffiti-garden/api\";\nimport type {\n GraffitiSession,\n GraffitiObject,\n JSONSchema,\n GraffitiStream,\n} from \"@graffiti-garden/api\";\nimport type { GraffitiObjectBase } from \"@graffiti-garden/api\";\nimport { Repeater } from \"@repeaterjs/repeater\";\nimport type { applyPatch } from \"fast-json-patch\";\nimport {\n applyGraffitiPatch,\n compileGraffitiObjectSchema,\n isActorAllowedGraffitiObject,\n maskGraffitiObject,\n unpackLocationOrUri,\n} from \"@graffiti-garden/implementation-local/utilities\";\nexport type * from \"@graffiti-garden/api\";\n\nexport type GraffitiSynchronizeCallback = (\n oldObject: GraffitiObjectBase,\n newObject?: GraffitiObjectBase,\n) => void;\n\nexport interface GraffitiSynchronizeOptions {\n /**\n * Allows synchronize to listen to all objects, not just those\n * that the session is allowed to see. This is useful to get a\n * global view of all Graffiti objects passsing through the system,\n * for example to build a client-side cache. Additional mechanisms\n * should be in place to ensure that users do not see objects or\n * properties they are not allowed to see.\n *\n * Default: `false`\n */\n omniscient?: boolean;\n}\n\n/**\n * Wraps the [Graffiti API](https://api.graffiti.garden/classes/Graffiti.html)\n * so that changes made or received in one part of an application\n * are automatically routed to other parts of the application.\n * This is an important tool for building responsive\n * and consistent user interfaces, and is built upon to make\n * the [Graffiti Vue Plugin](https://vue.graffiti.garden/variables/GraffitiPlugin.html)\n * and possibly other front-end libraries in the future.\n *\n * Specifically, it provides the following *synchronize*\n * methods for each of the following API methods:\n *\n * | API Method | Synchronize Method |\n * |------------|--------------------|\n * | {@link get} | {@link synchronizeGet} |\n * | {@link discover} | {@link synchronizeDiscover} |\n * | {@link recoverOrphans} | {@link synchronizeRecoverOrphans} |\n *\n * Whenever a change is made via {@link put}, {@link patch}, and {@link delete} or\n * received from {@link get}, {@link discover}, and {@link recoverOrphans},\n * those changes are forwarded to the appropriate synchronize method.\n * Each synchronize method returns an iterator that streams these changes\n * continually until the user calls `return` on the iterator or `break`s out of the loop,\n * allowing for live updates without additional polling.\n *\n * Example 1: Suppose a user publishes a post using {@link put}. If the feed\n * displaying that user's posts is using {@link synchronizeDiscover} to listen for changes,\n * then the user's new post will instantly appear in their feed, giving the UI a\n * responsive feel.\n *\n * Example 2: Suppose one of a user's friends changes their name. As soon as the\n * user's application receives one notice of that change (using {@link get}\n * or {@link discover}), then {@link synchronizeDiscover} listeners can be used to update\n * all instance's of that friend's name in the user's application instantly,\n * providing a consistent user experience.\n *\n * @groupDescription Synchronize Methods\n * This group contains methods that listen for changes made via\n * {@link put}, {@link patch}, and {@link delete} or fetched from\n * {@link get}, {@link discover}, and {@link recoverOrphans} and then\n * streams appropriate changes to provide a responsive and consistent user experience.\n */\nexport class GraffitiSynchronize extends Graffiti {\n protected ajv_: Promise<Ajv> | undefined;\n protected applyPatch_: Promise<typeof applyPatch> | undefined;\n protected readonly graffiti: Graffiti;\n protected readonly callbacks = new Set<GraffitiSynchronizeCallback>();\n protected readonly options: GraffitiSynchronizeOptions;\n\n channelStats: Graffiti[\"channelStats\"];\n login: Graffiti[\"login\"];\n logout: Graffiti[\"logout\"];\n sessionEvents: Graffiti[\"sessionEvents\"];\n\n get ajv() {\n if (!this.ajv_) {\n this.ajv_ = (async () => {\n const { default: Ajv } = await import(\"ajv\");\n return new Ajv({ strict: false });\n })();\n }\n return this.ajv_;\n }\n\n get applyPatch() {\n if (!this.applyPatch_) {\n this.applyPatch_ = (async () => {\n const { applyPatch } = await import(\"fast-json-patch\");\n return applyPatch;\n })();\n }\n return this.applyPatch_;\n }\n\n /**\n * Wraps a Graffiti API instance to provide the synchronize methods.\n * The GraffitiSyncrhonize class rather than the Graffiti class\n * must be used for all functions for the synchronize methods to work.\n */\n constructor(\n /**\n * The [Graffiti API](https://api.graffiti.garden/classes/Graffiti.html)\n * instance to wrap.\n */\n graffiti: Graffiti,\n options?: GraffitiSynchronizeOptions,\n ) {\n super();\n this.options = options ?? {};\n this.graffiti = graffiti;\n this.channelStats = graffiti.channelStats.bind(graffiti);\n this.login = graffiti.login.bind(graffiti);\n this.logout = graffiti.logout.bind(graffiti);\n this.sessionEvents = graffiti.sessionEvents;\n }\n\n protected synchronize<Schema extends JSONSchema>(\n matchObject: (object: GraffitiObjectBase) => boolean,\n channels: string[],\n schema: Schema,\n session?: GraffitiSession | null,\n ) {\n const repeater: GraffitiStream<GraffitiObject<Schema>> = new Repeater(\n async (push, stop) => {\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n const callback: GraffitiSynchronizeCallback = (\n oldObjectRaw,\n newObjectRaw,\n ) => {\n for (const objectRaw of [newObjectRaw, oldObjectRaw]) {\n if (\n objectRaw &&\n matchObject(objectRaw) &&\n (this.options.omniscient ||\n isActorAllowedGraffitiObject(objectRaw, session))\n ) {\n const object = { ...objectRaw };\n if (!this.options.omniscient) {\n maskGraffitiObject(object, channels, session);\n }\n if (validate(object)) {\n push({ value: object });\n break;\n }\n }\n }\n };\n\n this.callbacks.add(callback);\n await stop;\n this.callbacks.delete(callback);\n },\n );\n\n return repeater;\n }\n\n /**\n * This method has the same signature as {@link discover} but listens for\n * changes made via {@link put}, {@link patch}, and {@link delete} or\n * fetched from {@link get}, {@link discover}, and {@link recoverOrphans}\n * and then streams appropriate changes to provide a responsive and\n * consistent user experience.\n *\n * Unlike {@link discover}, this method continuously listens for changes\n * and will not terminate unless the user calls the `return` method on the iterator\n * or `break`s out of the loop.\n *\n * @group Synchronize Methods\n */\n synchronizeDiscover<Schema extends JSONSchema>(\n ...args: Parameters<typeof Graffiti.prototype.discover<Schema>>\n ): GraffitiStream<GraffitiObject<Schema>> {\n const [channels, schema, session] = args;\n function matchObject(object: GraffitiObjectBase) {\n return object.channels.some((channel) => channels.includes(channel));\n }\n return this.synchronize<Schema>(matchObject, channels, schema, session);\n }\n\n /**\n * This method has the same signature as {@link get} but\n * listens for changes made via {@link put}, {@link patch}, and {@link delete} or\n * fetched from {@link get}, {@link discover}, and {@link recoverOrphans} and then\n * streams appropriate changes to provide a responsive and consistent user experience.\n *\n * Unlike {@link get}, which returns a single result, this method continuously\n * listens for changes which are output as an asynchronous {@link GraffitiStream}.\n *\n * @group Synchronize Methods\n */\n synchronizeGet<Schema extends JSONSchema>(\n ...args: Parameters<typeof Graffiti.prototype.get<Schema>>\n ): GraffitiStream<GraffitiObject<Schema>> {\n const [locationOrUri, schema, session] = args;\n const uri = unpackLocationOrUri(locationOrUri);\n function matchObject(object: GraffitiObjectBase) {\n return object.uri === uri;\n }\n return this.synchronize<Schema>(matchObject, [], schema, session);\n }\n\n /**\n * This method has the same signature as {@link recoverOrphans} but\n * listens for changes made via\n * {@link put}, {@link patch}, and {@link delete} or fetched from\n * {@link get}, {@link discover}, and {@link recoverOrphans} and then\n * streams appropriate changes to provide a responsive and consistent user experience.\n *\n * Unlike {@link recoverOrphans}, this method continuously listens for changes\n * and will not terminate unless the user calls the `return` method on the iterator\n * or `break`s out of the loop.\n *\n * @group Synchronize Methods\n */\n synchronizeRecoverOrphans<Schema extends JSONSchema>(\n ...args: Parameters<typeof Graffiti.prototype.recoverOrphans<Schema>>\n ): GraffitiStream<GraffitiObject<Schema>> {\n const [schema, session] = args;\n function matchObject(object: GraffitiObjectBase) {\n return object.actor === session.actor && object.channels.length === 0;\n }\n return this.synchronize<Schema>(matchObject, [], schema, session);\n }\n\n /**\n * Streams changes made to *any* object in *any* channel\n * and made by *any* user. You may want to use it in conjuction with\n * {@link GraffitiSynchronizeOptions.omniscient} to get a global view\n * of all Graffiti objects passing through the system. This is useful\n * for building a client-side cache, for example.\n *\n * Be careful using this method. Without additional filters it can\n * expose the user to content out of context.\n */\n synchronizeAll<Schema extends JSONSchema>(\n schema?: Schema,\n session?: GraffitiSession | null,\n ): GraffitiStream<GraffitiObjectBase> {\n return this.synchronize(() => true, [], schema ?? {}, session);\n }\n\n protected async synchronizeDispatch(\n oldObject: GraffitiObjectBase,\n newObject?: GraffitiObjectBase,\n waitForListeners = false,\n ) {\n for (const callback of this.callbacks) {\n callback(oldObject, newObject);\n }\n if (waitForListeners) {\n // Wait for the listeners to receive\n // their objects, before returning the operation\n // that triggered them.\n //\n // This is important for mutators (put, patch, delete)\n // to ensure the application state has been updated\n // everywhere before returning, giving consistent\n // feedback to the user that the operation has completed.\n //\n // The opposite is true for accessors (get, discover, recoverOrphans),\n // where it is a weird user experience to call `get`\n // in one place and have the application update\n // somewhere else first. It is also less efficient.\n //\n // The hack is simply to await one \"macro task cycle\".\n // We need to wait for this cycle rather than using\n // `await push` in the callback, because it turns out\n // that `await push` won't resolve until the following\n // .next() call of the iterator, so if only\n // one .next() is called, this dispatch will hang.\n await new Promise((resolve) => setTimeout(resolve, 0));\n }\n }\n\n get: Graffiti[\"get\"] = async (...args) => {\n const object = await this.graffiti.get(...args);\n this.synchronizeDispatch(object);\n return object;\n };\n\n put: Graffiti[\"put\"] = async (...args) => {\n const oldObject = await this.graffiti.put<{}>(...args);\n const partialObject = args[0];\n const newObject: GraffitiObjectBase = {\n ...oldObject,\n value: partialObject.value,\n channels: partialObject.channels,\n allowed: partialObject.allowed,\n tombstone: false,\n };\n await this.synchronizeDispatch(oldObject, newObject, true);\n return oldObject;\n };\n\n patch: Graffiti[\"patch\"] = async (...args) => {\n const oldObject = await this.graffiti.patch(...args);\n const newObject: GraffitiObjectBase = { ...oldObject };\n newObject.tombstone = false;\n for (const prop of [\"value\", \"channels\", \"allowed\"] as const) {\n applyGraffitiPatch(await this.applyPatch, prop, args[0], newObject);\n }\n await this.synchronizeDispatch(oldObject, newObject, true);\n return oldObject;\n };\n\n delete: Graffiti[\"delete\"] = async (...args) => {\n const oldObject = await this.graffiti.delete(...args);\n await this.synchronizeDispatch(oldObject, undefined, true);\n return oldObject;\n };\n\n protected objectStream<Schema extends JSONSchema>(\n iterator: ReturnType<typeof Graffiti.prototype.discover<Schema>>,\n ) {\n const dispatch = this.synchronizeDispatch.bind(this);\n const wrapper = async function* () {\n let result = await iterator.next();\n while (!result.done) {\n if (!result.value.error) {\n dispatch(result.value.value);\n }\n yield result.value;\n result = await iterator.next();\n }\n return result.value;\n };\n return wrapper();\n }\n\n discover: Graffiti[\"discover\"] = (...args) => {\n const iterator = this.graffiti.discover(...args);\n return this.objectStream<(typeof args)[1]>(iterator);\n };\n\n recoverOrphans: Graffiti[\"recoverOrphans\"] = (...args) => {\n const iterator = this.graffiti.recoverOrphans(...args);\n return this.objectStream<(typeof args)[0]>(iterator);\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AA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,SAA6B,UAAU,SAAS;AAC9C,UAAM,YAAY,MAAM,KAAK,SAAS,OAAO,GAAG,IAAI;AACpD,UAAM,KAAK,oBAAoB,WAAW,QAAW,IAAI;AACzD,WAAO;AAAA,EACT;AAAA,EAEU,aACR,UACA;AACA,UAAM,WAAW,KAAK,oBAAoB,KAAK,IAAI;AACnD,UAAM,UAAU,mBAAmB;AACjC,UAAI,SAAS,MAAM,SAAS,KAAK;AACjC,aAAO,CAAC,OAAO,MAAM;AACnB,YAAI,CAAC,OAAO,MAAM,OAAO;AACvB,mBAAS,OAAO,MAAM,KAAK;AAAA,QAC7B;AACA,cAAM,OAAO;AACb,iBAAS,MAAM,SAAS,KAAK;AAAA,MAC/B;AACA,aAAO,OAAO;AAAA,IAChB;AACA,WAAO,QAAQ;AAAA,EACjB;AAAA,EAEA,WAAiC,IAAI,SAAS;AAC5C,UAAM,WAAW,KAAK,SAAS,SAAS,GAAG,IAAI;AAC/C,WAAO,KAAK,aAA+B,QAAQ;AAAA,EACrD;AAAA,EAEA,iBAA6C,IAAI,SAAS;AACxD,UAAM,WAAW,KAAK,SAAS,eAAe,GAAG,IAAI;AACrD,WAAO,KAAK,aAA+B,QAAQ;AAAA,EACrD;AACF;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,217 @@
|
|
|
1
|
-
import{Graffiti
|
|
1
|
+
import { Graffiti } from "@graffiti-garden/api";
|
|
2
|
+
import { Repeater } from "@repeaterjs/repeater";
|
|
3
|
+
import {
|
|
4
|
+
applyGraffitiPatch,
|
|
5
|
+
compileGraffitiObjectSchema,
|
|
6
|
+
isActorAllowedGraffitiObject,
|
|
7
|
+
maskGraffitiObject,
|
|
8
|
+
unpackLocationOrUri
|
|
9
|
+
} from "@graffiti-garden/implementation-local/utilities";
|
|
10
|
+
class GraffitiSynchronize extends Graffiti {
|
|
11
|
+
ajv_;
|
|
12
|
+
applyPatch_;
|
|
13
|
+
graffiti;
|
|
14
|
+
callbacks = /* @__PURE__ */ new Set();
|
|
15
|
+
options;
|
|
16
|
+
channelStats;
|
|
17
|
+
login;
|
|
18
|
+
logout;
|
|
19
|
+
sessionEvents;
|
|
20
|
+
get ajv() {
|
|
21
|
+
if (!this.ajv_) {
|
|
22
|
+
this.ajv_ = (async () => {
|
|
23
|
+
const { default: Ajv } = await import("ajv");
|
|
24
|
+
return new Ajv({ strict: false });
|
|
25
|
+
})();
|
|
26
|
+
}
|
|
27
|
+
return this.ajv_;
|
|
28
|
+
}
|
|
29
|
+
get applyPatch() {
|
|
30
|
+
if (!this.applyPatch_) {
|
|
31
|
+
this.applyPatch_ = (async () => {
|
|
32
|
+
const { applyPatch } = await import("fast-json-patch");
|
|
33
|
+
return applyPatch;
|
|
34
|
+
})();
|
|
35
|
+
}
|
|
36
|
+
return this.applyPatch_;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Wraps a Graffiti API instance to provide the synchronize methods.
|
|
40
|
+
* The GraffitiSyncrhonize class rather than the Graffiti class
|
|
41
|
+
* must be used for all functions for the synchronize methods to work.
|
|
42
|
+
*/
|
|
43
|
+
constructor(graffiti, options) {
|
|
44
|
+
super();
|
|
45
|
+
this.options = options ?? {};
|
|
46
|
+
this.graffiti = graffiti;
|
|
47
|
+
this.channelStats = graffiti.channelStats.bind(graffiti);
|
|
48
|
+
this.login = graffiti.login.bind(graffiti);
|
|
49
|
+
this.logout = graffiti.logout.bind(graffiti);
|
|
50
|
+
this.sessionEvents = graffiti.sessionEvents;
|
|
51
|
+
}
|
|
52
|
+
synchronize(matchObject, channels, schema, session) {
|
|
53
|
+
const repeater = new Repeater(
|
|
54
|
+
async (push, stop) => {
|
|
55
|
+
const validate = compileGraffitiObjectSchema(await this.ajv, schema);
|
|
56
|
+
const callback = (oldObjectRaw, newObjectRaw) => {
|
|
57
|
+
for (const objectRaw of [newObjectRaw, oldObjectRaw]) {
|
|
58
|
+
if (objectRaw && matchObject(objectRaw) && (this.options.omniscient || isActorAllowedGraffitiObject(objectRaw, session))) {
|
|
59
|
+
const object = { ...objectRaw };
|
|
60
|
+
if (!this.options.omniscient) {
|
|
61
|
+
maskGraffitiObject(object, channels, session);
|
|
62
|
+
}
|
|
63
|
+
if (validate(object)) {
|
|
64
|
+
push({ value: object });
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
this.callbacks.add(callback);
|
|
71
|
+
await stop;
|
|
72
|
+
this.callbacks.delete(callback);
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
return repeater;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* This method has the same signature as {@link discover} but listens for
|
|
79
|
+
* changes made via {@link put}, {@link patch}, and {@link delete} or
|
|
80
|
+
* fetched from {@link get}, {@link discover}, and {@link recoverOrphans}
|
|
81
|
+
* and then streams appropriate changes to provide a responsive and
|
|
82
|
+
* consistent user experience.
|
|
83
|
+
*
|
|
84
|
+
* Unlike {@link discover}, this method continuously listens for changes
|
|
85
|
+
* and will not terminate unless the user calls the `return` method on the iterator
|
|
86
|
+
* or `break`s out of the loop.
|
|
87
|
+
*
|
|
88
|
+
* @group Synchronize Methods
|
|
89
|
+
*/
|
|
90
|
+
synchronizeDiscover(...args) {
|
|
91
|
+
const [channels, schema, session] = args;
|
|
92
|
+
function matchObject(object) {
|
|
93
|
+
return object.channels.some((channel) => channels.includes(channel));
|
|
94
|
+
}
|
|
95
|
+
return this.synchronize(matchObject, channels, schema, session);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* This method has the same signature as {@link get} but
|
|
99
|
+
* listens for changes made via {@link put}, {@link patch}, and {@link delete} or
|
|
100
|
+
* fetched from {@link get}, {@link discover}, and {@link recoverOrphans} and then
|
|
101
|
+
* streams appropriate changes to provide a responsive and consistent user experience.
|
|
102
|
+
*
|
|
103
|
+
* Unlike {@link get}, which returns a single result, this method continuously
|
|
104
|
+
* listens for changes which are output as an asynchronous {@link GraffitiStream}.
|
|
105
|
+
*
|
|
106
|
+
* @group Synchronize Methods
|
|
107
|
+
*/
|
|
108
|
+
synchronizeGet(...args) {
|
|
109
|
+
const [locationOrUri, schema, session] = args;
|
|
110
|
+
const uri = unpackLocationOrUri(locationOrUri);
|
|
111
|
+
function matchObject(object) {
|
|
112
|
+
return object.uri === uri;
|
|
113
|
+
}
|
|
114
|
+
return this.synchronize(matchObject, [], schema, session);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* This method has the same signature as {@link recoverOrphans} but
|
|
118
|
+
* listens for changes made via
|
|
119
|
+
* {@link put}, {@link patch}, and {@link delete} or fetched from
|
|
120
|
+
* {@link get}, {@link discover}, and {@link recoverOrphans} and then
|
|
121
|
+
* streams appropriate changes to provide a responsive and consistent user experience.
|
|
122
|
+
*
|
|
123
|
+
* Unlike {@link recoverOrphans}, this method continuously listens for changes
|
|
124
|
+
* and will not terminate unless the user calls the `return` method on the iterator
|
|
125
|
+
* or `break`s out of the loop.
|
|
126
|
+
*
|
|
127
|
+
* @group Synchronize Methods
|
|
128
|
+
*/
|
|
129
|
+
synchronizeRecoverOrphans(...args) {
|
|
130
|
+
const [schema, session] = args;
|
|
131
|
+
function matchObject(object) {
|
|
132
|
+
return object.actor === session.actor && object.channels.length === 0;
|
|
133
|
+
}
|
|
134
|
+
return this.synchronize(matchObject, [], schema, session);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Streams changes made to *any* object in *any* channel
|
|
138
|
+
* and made by *any* user. You may want to use it in conjuction with
|
|
139
|
+
* {@link GraffitiSynchronizeOptions.omniscient} to get a global view
|
|
140
|
+
* of all Graffiti objects passing through the system. This is useful
|
|
141
|
+
* for building a client-side cache, for example.
|
|
142
|
+
*
|
|
143
|
+
* Be careful using this method. Without additional filters it can
|
|
144
|
+
* expose the user to content out of context.
|
|
145
|
+
*/
|
|
146
|
+
synchronizeAll(schema, session) {
|
|
147
|
+
return this.synchronize(() => true, [], schema ?? {}, session);
|
|
148
|
+
}
|
|
149
|
+
async synchronizeDispatch(oldObject, newObject, waitForListeners = false) {
|
|
150
|
+
for (const callback of this.callbacks) {
|
|
151
|
+
callback(oldObject, newObject);
|
|
152
|
+
}
|
|
153
|
+
if (waitForListeners) {
|
|
154
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
get = async (...args) => {
|
|
158
|
+
const object = await this.graffiti.get(...args);
|
|
159
|
+
this.synchronizeDispatch(object);
|
|
160
|
+
return object;
|
|
161
|
+
};
|
|
162
|
+
put = async (...args) => {
|
|
163
|
+
const oldObject = await this.graffiti.put(...args);
|
|
164
|
+
const partialObject = args[0];
|
|
165
|
+
const newObject = {
|
|
166
|
+
...oldObject,
|
|
167
|
+
value: partialObject.value,
|
|
168
|
+
channels: partialObject.channels,
|
|
169
|
+
allowed: partialObject.allowed,
|
|
170
|
+
tombstone: false
|
|
171
|
+
};
|
|
172
|
+
await this.synchronizeDispatch(oldObject, newObject, true);
|
|
173
|
+
return oldObject;
|
|
174
|
+
};
|
|
175
|
+
patch = async (...args) => {
|
|
176
|
+
const oldObject = await this.graffiti.patch(...args);
|
|
177
|
+
const newObject = { ...oldObject };
|
|
178
|
+
newObject.tombstone = false;
|
|
179
|
+
for (const prop of ["value", "channels", "allowed"]) {
|
|
180
|
+
applyGraffitiPatch(await this.applyPatch, prop, args[0], newObject);
|
|
181
|
+
}
|
|
182
|
+
await this.synchronizeDispatch(oldObject, newObject, true);
|
|
183
|
+
return oldObject;
|
|
184
|
+
};
|
|
185
|
+
delete = async (...args) => {
|
|
186
|
+
const oldObject = await this.graffiti.delete(...args);
|
|
187
|
+
await this.synchronizeDispatch(oldObject, void 0, true);
|
|
188
|
+
return oldObject;
|
|
189
|
+
};
|
|
190
|
+
objectStream(iterator) {
|
|
191
|
+
const dispatch = this.synchronizeDispatch.bind(this);
|
|
192
|
+
const wrapper = async function* () {
|
|
193
|
+
let result = await iterator.next();
|
|
194
|
+
while (!result.done) {
|
|
195
|
+
if (!result.value.error) {
|
|
196
|
+
dispatch(result.value.value);
|
|
197
|
+
}
|
|
198
|
+
yield result.value;
|
|
199
|
+
result = await iterator.next();
|
|
200
|
+
}
|
|
201
|
+
return result.value;
|
|
202
|
+
};
|
|
203
|
+
return wrapper();
|
|
204
|
+
}
|
|
205
|
+
discover = (...args) => {
|
|
206
|
+
const iterator = this.graffiti.discover(...args);
|
|
207
|
+
return this.objectStream(iterator);
|
|
208
|
+
};
|
|
209
|
+
recoverOrphans = (...args) => {
|
|
210
|
+
const iterator = this.graffiti.recoverOrphans(...args);
|
|
211
|
+
return this.objectStream(iterator);
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
export {
|
|
215
|
+
GraffitiSynchronize
|
|
216
|
+
};
|
|
2
217
|
//# sourceMappingURL=index.js.map
|