@schematichq/schematic-react 0.1.6 → 0.1.7

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.
@@ -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,
@@ -107,7 +108,7 @@ var Schematic = class {
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;
@@ -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
  }
@@ -394,9 +391,13 @@ var useSchematicFlag = (key, opts) => {
394
391
  (0, import_react.useEffect)(() => {
395
392
  typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
396
393
  }, [key, fallback, flagValue]);
397
- if (client) {
398
- return client.checkFlag({ key, fallback });
399
- }
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]);
400
401
  return value;
401
402
  };
402
403
  /*! Bundled license information:
@@ -74,7 +74,7 @@ export declare const useSchematicEvents: (opts?: SchematicHookOpts) => {
74
74
  identify: ((body: SchematicJS.EventBodyIdentify) => void) | undefined;
75
75
  };
76
76
 
77
- export declare const useSchematicFlag: (key: string, opts?: UseSchematicFlagOpts) => boolean | Promise<boolean>;
77
+ export declare const useSchematicFlag: (key: string, opts?: UseSchematicFlagOpts) => boolean;
78
78
 
79
79
  export declare type UseSchematicFlagOpts = SchematicHookOpts & {
80
80
  fallback?: boolean;
@@ -77,7 +77,7 @@ var Schematic = class {
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;
@@ -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
  }
@@ -369,12 +365,17 @@ var useSchematicFlag = (key, opts) => {
369
365
  useEffect(() => {
370
366
  typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
371
367
  }, [key, fallback, flagValue]);
372
- if (client) {
373
- return client.checkFlag({ key, fallback });
374
- }
368
+ useEffect(() => {
369
+ if (!client)
370
+ return;
371
+ client.checkFlag({ key, fallback }).then((value2) => {
372
+ setValue(value2);
373
+ });
374
+ }, [client, key, fallback]);
375
375
  return value;
376
376
  };
377
377
  export {
378
+ Schematic,
378
379
  SchematicProvider,
379
380
  useSchematic,
380
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.6",
45
+ "version": "0.1.7",
46
46
  "dependencies": {
47
- "@schematichq/schematic-js": "^0.1.7"
47
+ "@schematichq/schematic-js": "^0.1.8"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "react": ">=18"