@schematichq/schematic-react 0.1.6 → 0.1.8

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,
@@ -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;
@@ -271,6 +272,7 @@ var Schematic = class {
271
272
  return new Promise((resolve, reject) => {
272
273
  if (contextString(context) == contextString(this.context)) {
273
274
  resolve();
275
+ return;
274
276
  }
275
277
  this.context = context;
276
278
  if (!this.conn) {
@@ -281,12 +283,13 @@ var Schematic = class {
281
283
  let resolved = false;
282
284
  this.conn.onmessage = (event) => {
283
285
  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
- {}
286
+ if (!(contextString(context) in this.values)) {
287
+ this.values[contextString(context)] = {};
288
+ }
289
+ (message.flags ?? []).forEach(
290
+ (flag) => {
291
+ this.values[contextString(context)][flag.flag] = flag.value;
292
+ }
290
293
  );
291
294
  if (this.flagListener) {
292
295
  this.flagListener(this.values[contextString(context)]);
@@ -394,9 +397,13 @@ var useSchematicFlag = (key, opts) => {
394
397
  (0, import_react.useEffect)(() => {
395
398
  typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
396
399
  }, [key, fallback, flagValue]);
397
- if (client) {
398
- return client.checkFlag({ key, fallback });
399
- }
400
+ (0, import_react.useEffect)(() => {
401
+ if (!client)
402
+ return;
403
+ client.checkFlag({ key, fallback }).then((value2) => {
404
+ setValue(value2);
405
+ });
406
+ }, [client, key, fallback]);
400
407
  return value;
401
408
  };
402
409
  /*! 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;
@@ -241,6 +241,7 @@ var Schematic = class {
241
241
  return new Promise((resolve, reject) => {
242
242
  if (contextString(context) == contextString(this.context)) {
243
243
  resolve();
244
+ return;
244
245
  }
245
246
  this.context = context;
246
247
  if (!this.conn) {
@@ -251,12 +252,13 @@ var Schematic = class {
251
252
  let resolved = false;
252
253
  this.conn.onmessage = (event) => {
253
254
  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
- {}
255
+ if (!(contextString(context) in this.values)) {
256
+ this.values[contextString(context)] = {};
257
+ }
258
+ (message.flags ?? []).forEach(
259
+ (flag) => {
260
+ this.values[contextString(context)][flag.flag] = flag.value;
261
+ }
260
262
  );
261
263
  if (this.flagListener) {
262
264
  this.flagListener(this.values[contextString(context)]);
@@ -369,12 +371,17 @@ var useSchematicFlag = (key, opts) => {
369
371
  useEffect(() => {
370
372
  typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
371
373
  }, [key, fallback, flagValue]);
372
- if (client) {
373
- return client.checkFlag({ key, fallback });
374
- }
374
+ useEffect(() => {
375
+ if (!client)
376
+ return;
377
+ client.checkFlag({ key, fallback }).then((value2) => {
378
+ setValue(value2);
379
+ });
380
+ }, [client, key, fallback]);
375
381
  return value;
376
382
  };
377
383
  export {
384
+ Schematic,
378
385
  SchematicProvider,
379
386
  useSchematic,
380
387
  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.8",
46
46
  "dependencies": {
47
- "@schematichq/schematic-js": "^0.1.7"
47
+ "@schematichq/schematic-js": "^0.1.10"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "react": ">=18"