@schematichq/schematic-react 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.tsx
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ Schematic: () => Schematic,
23
24
  SchematicProvider: () => SchematicProvider,
24
25
  useSchematic: () => useSchematic,
25
26
  useSchematicContext: () => useSchematicContext,
@@ -101,13 +102,13 @@ var Schematic = class {
101
102
  if (options?.webSocketUrl !== void 0) {
102
103
  this.webSocketUrl = options.webSocketUrl;
103
104
  }
104
- if (typeof window !== "undefined") {
105
+ if (window?.addEventListener) {
105
106
  window.addEventListener("beforeunload", () => {
106
107
  this.flushEventQueue();
107
108
  });
108
109
  }
109
110
  }
110
- checkFlag = async (options) => {
111
+ async checkFlag(options) {
111
112
  const { fallback = false, key } = options;
112
113
  const context = options.context || this.context;
113
114
  if (this.useWebSocket) {
@@ -133,7 +134,7 @@ var Schematic = class {
133
134
  console.error("There was a problem with the fetch operation:", error);
134
135
  return fallback;
135
136
  });
136
- };
137
+ }
137
138
  // Make a REST API call to fetch all flag values for a given context
138
139
  checkFlags = async (context) => {
139
140
  context = context || this.context;
@@ -222,7 +223,7 @@ var Schematic = class {
222
223
  tracker_user_id: this.getAnonymousId(),
223
224
  type: eventType
224
225
  };
225
- if (typeof document !== "undefined" && document.hidden) {
226
+ if (document?.hidden) {
226
227
  this.storeEvent(event);
227
228
  } else {
228
229
  this.sendEvent(event);
@@ -255,7 +256,7 @@ var Schematic = class {
255
256
  if (this.conn) {
256
257
  resolve();
257
258
  }
258
- const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
259
+ const wsUrl = `${this.webSocketUrl}/flags/subscribe`;
259
260
  const webSocket = new WebSocket(wsUrl);
260
261
  this.conn = webSocket;
261
262
  webSocket.onopen = () => {
@@ -281,13 +282,9 @@ var Schematic = class {
281
282
  let resolved = false;
282
283
  this.conn.onmessage = (event) => {
283
284
  const message = JSON.parse(event.data);
284
- this.values[contextString(context)] = (message.flags ?? []).reduce(
285
- (accum, flag) => {
286
- accum[flag.flag] = flag.value;
287
- return accum;
288
- },
289
- {}
290
- );
285
+ (message.flags ?? []).forEach((flag) => {
286
+ this.values[contextString(context)][flag.flag] = flag.value;
287
+ });
291
288
  if (this.flagListener) {
292
289
  this.flagListener(this.values[contextString(context)]);
293
290
  }
@@ -337,28 +334,29 @@ var SchematicContext = (0, import_react.createContext)({
337
334
  flagValues: {}
338
335
  });
339
336
  var SchematicProvider = ({
340
- apiUrl,
341
337
  children,
342
- eventUrl,
343
- publishableKey,
344
- webSocketUrl
338
+ client: providedClient,
339
+ clientOpts,
340
+ publishableKey
345
341
  }) => {
346
342
  const [client, setClient] = (0, import_react.useState)();
347
343
  const [flagValues, setFlagValues] = (0, import_react.useState)({});
348
344
  (0, import_react.useEffect)(() => {
345
+ if (providedClient) {
346
+ setClient(providedClient);
347
+ return providedClient.cleanup;
348
+ }
349
349
  if (publishableKey === void 0) {
350
350
  return;
351
351
  }
352
- const client2 = new Schematic(publishableKey, {
353
- apiUrl,
354
- eventUrl,
352
+ const newClient = new Schematic(publishableKey, {
353
+ ...clientOpts,
355
354
  flagListener: setFlagValues,
356
- useWebSocket: true,
357
- webSocketUrl
355
+ useWebSocket: true
358
356
  });
359
- setClient(client2);
360
- return client2.cleanup;
361
- }, [apiUrl, eventUrl, publishableKey, webSocketUrl]);
357
+ setClient(newClient);
358
+ return newClient.cleanup;
359
+ }, [clientOpts, providedClient, publishableKey]);
362
360
  const contextValue = {
363
361
  client,
364
362
  flagValues
@@ -366,23 +364,40 @@ var SchematicProvider = ({
366
364
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SchematicContext.Provider, { value: contextValue, children });
367
365
  };
368
366
  var useSchematic = () => (0, import_react.useContext)(SchematicContext);
369
- var useSchematicContext = () => {
370
- const { client } = useSchematic();
367
+ var useSchematicClient = (opts) => {
368
+ const schematic = useSchematic();
369
+ const { client } = opts ?? {};
370
+ if (client) {
371
+ return client;
372
+ }
373
+ return schematic.client;
374
+ };
375
+ var useSchematicContext = (opts) => {
376
+ const client = useSchematicClient(opts);
371
377
  const { setContext } = client ?? {};
372
378
  return { setContext };
373
379
  };
374
- var useSchematicEvents = () => {
375
- const { client } = useSchematic();
380
+ var useSchematicEvents = (opts) => {
381
+ const client = useSchematicClient(opts);
376
382
  const { track, identify } = client ?? {};
377
383
  return { track, identify };
378
384
  };
379
- var useSchematicFlag = (key, fallback) => {
385
+ var useSchematicFlag = (key, opts) => {
380
386
  const { flagValues } = useSchematic();
387
+ const { client } = opts ?? {};
388
+ const { fallback = false } = opts ?? {};
381
389
  const [value, setValue] = (0, import_react.useState)(fallback ?? false);
382
390
  const flagValue = flagValues[key];
383
391
  (0, import_react.useEffect)(() => {
384
- typeof flagValue === "undefined" ? setValue(fallback ?? false) : setValue(flagValue);
392
+ typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
385
393
  }, [key, fallback, flagValue]);
394
+ (0, import_react.useEffect)(() => {
395
+ if (!client)
396
+ return;
397
+ client.checkFlag({ key, fallback }).then((value2) => {
398
+ setValue(value2);
399
+ });
400
+ }, [client, key, fallback]);
386
401
  return value;
387
402
  };
388
403
  /*! Bundled license information:
@@ -9,8 +9,10 @@ import { FlagCheckWithKeyResponseBody } from '@schematichq/schematic-js';
9
9
  import { Keys } from '@schematichq/schematic-js';
10
10
  import { default as React_2 } from 'react';
11
11
  import { ReactNode } from 'react';
12
+ import { Schematic } from '@schematichq/schematic-js';
12
13
  import { SchematicContext } from '@schematichq/schematic-js';
13
14
  import * as SchematicJS from '@schematichq/schematic-js';
15
+ import { SchematicOptions } from '@schematichq/schematic-js';
14
16
  import { Traits } from '@schematichq/schematic-js';
15
17
 
16
18
  export { Event_2 as Event }
@@ -31,6 +33,8 @@ export { FlagCheckWithKeyResponseBody }
31
33
 
32
34
  export { Keys }
33
35
 
36
+ export { Schematic }
37
+
34
38
  export { SchematicContext }
35
39
 
36
40
  declare interface SchematicContextProps {
@@ -42,29 +46,38 @@ export declare interface SchematicFlags {
42
46
  [key: string]: boolean;
43
47
  }
44
48
 
49
+ export declare interface SchematicHookOpts {
50
+ client?: SchematicJS.Schematic;
51
+ }
52
+
53
+ export { SchematicOptions }
54
+
45
55
  export declare const SchematicProvider: React_2.FC<SchematicProviderProps>;
46
56
 
47
- declare interface SchematicProviderProps {
57
+ export declare interface SchematicProviderProps {
48
58
  children: ReactNode;
59
+ client?: SchematicJS.Schematic;
60
+ clientOpts?: SchematicJS.SchematicOptions;
49
61
  publishableKey?: string;
50
- apiUrl?: string;
51
- eventUrl?: string;
52
- webSocketUrl?: string;
53
62
  }
54
63
 
55
64
  export { Traits }
56
65
 
57
66
  export declare const useSchematic: () => SchematicContextProps;
58
67
 
59
- export declare const useSchematicContext: () => {
68
+ export declare const useSchematicContext: (opts?: SchematicHookOpts) => {
60
69
  setContext: ((context: SchematicJS.SchematicContext) => Promise<void>) | undefined;
61
70
  };
62
71
 
63
- export declare const useSchematicEvents: () => {
72
+ export declare const useSchematicEvents: (opts?: SchematicHookOpts) => {
64
73
  track: ((body: SchematicJS.EventBodyTrack) => void) | undefined;
65
74
  identify: ((body: SchematicJS.EventBodyIdentify) => void) | undefined;
66
75
  };
67
76
 
68
- export declare const useSchematicFlag: (key: string, fallback?: boolean) => boolean;
77
+ export declare const useSchematicFlag: (key: string, opts?: UseSchematicFlagOpts) => boolean;
78
+
79
+ export declare type UseSchematicFlagOpts = SchematicHookOpts & {
80
+ fallback?: boolean;
81
+ };
69
82
 
70
83
  export { }
@@ -71,13 +71,13 @@ var Schematic = class {
71
71
  if (options?.webSocketUrl !== void 0) {
72
72
  this.webSocketUrl = options.webSocketUrl;
73
73
  }
74
- if (typeof window !== "undefined") {
74
+ if (window?.addEventListener) {
75
75
  window.addEventListener("beforeunload", () => {
76
76
  this.flushEventQueue();
77
77
  });
78
78
  }
79
79
  }
80
- checkFlag = async (options) => {
80
+ async checkFlag(options) {
81
81
  const { fallback = false, key } = options;
82
82
  const context = options.context || this.context;
83
83
  if (this.useWebSocket) {
@@ -103,7 +103,7 @@ var Schematic = class {
103
103
  console.error("There was a problem with the fetch operation:", error);
104
104
  return fallback;
105
105
  });
106
- };
106
+ }
107
107
  // Make a REST API call to fetch all flag values for a given context
108
108
  checkFlags = async (context) => {
109
109
  context = context || this.context;
@@ -192,7 +192,7 @@ var Schematic = class {
192
192
  tracker_user_id: this.getAnonymousId(),
193
193
  type: eventType
194
194
  };
195
- if (typeof document !== "undefined" && document.hidden) {
195
+ if (document?.hidden) {
196
196
  this.storeEvent(event);
197
197
  } else {
198
198
  this.sendEvent(event);
@@ -225,7 +225,7 @@ var Schematic = class {
225
225
  if (this.conn) {
226
226
  resolve();
227
227
  }
228
- const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
228
+ const wsUrl = `${this.webSocketUrl}/flags/subscribe`;
229
229
  const webSocket = new WebSocket(wsUrl);
230
230
  this.conn = webSocket;
231
231
  webSocket.onopen = () => {
@@ -251,13 +251,9 @@ var Schematic = class {
251
251
  let resolved = false;
252
252
  this.conn.onmessage = (event) => {
253
253
  const message = JSON.parse(event.data);
254
- this.values[contextString(context)] = (message.flags ?? []).reduce(
255
- (accum, flag) => {
256
- accum[flag.flag] = flag.value;
257
- return accum;
258
- },
259
- {}
260
- );
254
+ (message.flags ?? []).forEach((flag) => {
255
+ this.values[contextString(context)][flag.flag] = flag.value;
256
+ });
261
257
  if (this.flagListener) {
262
258
  this.flagListener(this.values[contextString(context)]);
263
259
  }
@@ -312,28 +308,29 @@ var SchematicContext = createContext({
312
308
  flagValues: {}
313
309
  });
314
310
  var SchematicProvider = ({
315
- apiUrl,
316
311
  children,
317
- eventUrl,
318
- publishableKey,
319
- webSocketUrl
312
+ client: providedClient,
313
+ clientOpts,
314
+ publishableKey
320
315
  }) => {
321
316
  const [client, setClient] = useState();
322
317
  const [flagValues, setFlagValues] = useState({});
323
318
  useEffect(() => {
319
+ if (providedClient) {
320
+ setClient(providedClient);
321
+ return providedClient.cleanup;
322
+ }
324
323
  if (publishableKey === void 0) {
325
324
  return;
326
325
  }
327
- const client2 = new Schematic(publishableKey, {
328
- apiUrl,
329
- eventUrl,
326
+ const newClient = new Schematic(publishableKey, {
327
+ ...clientOpts,
330
328
  flagListener: setFlagValues,
331
- useWebSocket: true,
332
- webSocketUrl
329
+ useWebSocket: true
333
330
  });
334
- setClient(client2);
335
- return client2.cleanup;
336
- }, [apiUrl, eventUrl, publishableKey, webSocketUrl]);
331
+ setClient(newClient);
332
+ return newClient.cleanup;
333
+ }, [clientOpts, providedClient, publishableKey]);
337
334
  const contextValue = {
338
335
  client,
339
336
  flagValues
@@ -341,26 +338,44 @@ var SchematicProvider = ({
341
338
  return /* @__PURE__ */ jsx(SchematicContext.Provider, { value: contextValue, children });
342
339
  };
343
340
  var useSchematic = () => useContext(SchematicContext);
344
- var useSchematicContext = () => {
345
- const { client } = useSchematic();
341
+ var useSchematicClient = (opts) => {
342
+ const schematic = useSchematic();
343
+ const { client } = opts ?? {};
344
+ if (client) {
345
+ return client;
346
+ }
347
+ return schematic.client;
348
+ };
349
+ var useSchematicContext = (opts) => {
350
+ const client = useSchematicClient(opts);
346
351
  const { setContext } = client ?? {};
347
352
  return { setContext };
348
353
  };
349
- var useSchematicEvents = () => {
350
- const { client } = useSchematic();
354
+ var useSchematicEvents = (opts) => {
355
+ const client = useSchematicClient(opts);
351
356
  const { track, identify } = client ?? {};
352
357
  return { track, identify };
353
358
  };
354
- var useSchematicFlag = (key, fallback) => {
359
+ var useSchematicFlag = (key, opts) => {
355
360
  const { flagValues } = useSchematic();
361
+ const { client } = opts ?? {};
362
+ const { fallback = false } = opts ?? {};
356
363
  const [value, setValue] = useState(fallback ?? false);
357
364
  const flagValue = flagValues[key];
358
365
  useEffect(() => {
359
- typeof flagValue === "undefined" ? setValue(fallback ?? false) : setValue(flagValue);
366
+ typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
360
367
  }, [key, fallback, flagValue]);
368
+ useEffect(() => {
369
+ if (!client)
370
+ return;
371
+ client.checkFlag({ key, fallback }).then((value2) => {
372
+ setValue(value2);
373
+ });
374
+ }, [client, key, fallback]);
361
375
  return value;
362
376
  };
363
377
  export {
378
+ Schematic,
364
379
  SchematicProvider,
365
380
  useSchematic,
366
381
  useSchematicContext,
package/package.json CHANGED
@@ -42,9 +42,9 @@
42
42
  "test": "jest --config jest.config.js"
43
43
  },
44
44
  "types": "dist/schematic-react.d.ts",
45
- "version": "0.1.5",
45
+ "version": "0.1.7",
46
46
  "dependencies": {
47
- "@schematichq/schematic-js": "^0.1.5"
47
+ "@schematichq/schematic-js": "^0.1.8"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "react": ">=18"